From 882d471c90784a3a6577823e37a7d5cf4dff3da0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Nov 2023 10:01:45 +0530 Subject: [PATCH] Make config line parsing in Go use same algorithm as in python --- tools/config/api.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/config/api.go b/tools/config/api.go index c45e0f830..8637103db 100644 --- a/tools/config/api.go +++ b/tools/config/api.go @@ -11,7 +11,9 @@ import ( "io/fs" "os" "path/filepath" + "regexp" "strings" + "sync" "kitty/tools/utils" ) @@ -49,6 +51,10 @@ func (self *ConfigParser) BadLines() []ConfigLine { return self.bad_lines } +var key_pat = sync.OnceValue(func() *regexp.Regexp { + return regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$`) +}) + func (self *ConfigParser) parse(scanner Scanner, name, base_path_for_includes string, depth int) error { if self.seen_includes[name] { // avoid include loops return nil @@ -119,7 +125,12 @@ func (self *ConfigParser) parse(scanner Scanner, name, base_path_for_includes st } continue } - key, val := line, "" + m := key_pat().FindStringSubmatch(line) + if len(m) < 3 { + self.bad_lines = append(self.bad_lines, ConfigLine{Src_file: name, Line: line, Line_number: lnum, Err: fmt.Errorf("Invalid config line: %#v", line)}) + continue + } + key, val := m[1], m[2] for i, ch := range line { if ch == ' ' || ch == '\t' { key = line[:i]