返回 DeepSeek-Reasonix
toml_assignment.go
根目录 / internal / config / toml_assignment.go
1 package config
2
3 import (
4 "strconv"
5 "strings"
6 )
7
8 func isTOMLKeyAssignment(line, key string) bool {
9 trimmed := strings.TrimSpace(line)
10 if strings.HasPrefix(trimmed, "#") {
11 return false
12 }
13 got, ok := tomlAssignmentKey(trimmed)
14 return ok && got == key
15 }
16
17 // tomlAssignmentKey returns the simple key on a single TOML assignment line.
18 // It accepts bare keys and both TOML quoted-key forms while leaving dotted
19 // keys, malformed lines, and values containing an equals sign to the parser.
20 func tomlAssignmentKey(line string) (string, bool) {
21 line = strings.TrimSpace(line)
22 if line == "" || strings.HasPrefix(line, "#") {
23 return "", false
24 }
25
26 var key string
27 switch line[0] {
28 case '"':
29 end := 1
30 escaped := false
31 for end < len(line) {
32 if escaped {
33 escaped = false
34 end++
35 continue
36 }
37 switch line[end] {
38 case '\\':
39 escaped = true
40 case '"':
41 decoded, err := strconv.Unquote(line[:end+1])
42 if err != nil {
43 return "", false
44 }
45 key = decoded
46 line = line[end+1:]
47 goto assignment
48 }
49 end++
50 }
51 return "", false
52 case '\'':
53 end := strings.IndexByte(line[1:], '\'')
54 if end < 0 {
55 return "", false
56 }
57 end++
58 key = line[1:end]
59 line = line[end+1:]
60 default:
61 end := 0
62 for end < len(line) && line[end] != '=' && line[end] != ' ' && line[end] != '\t' {
63 end++
64 }
65 if end == 0 {
66 return "", false
67 }
68 key = line[:end]
69 line = line[end:]
70 }
71
72 assignment:
73 line = strings.TrimSpace(line)
74 if !strings.HasPrefix(line, "=") || strings.Contains(key, ".") {
75 return "", false
76 }
77 return key, true
78 }
79
79 lines GO