@@ -47,6 +47,10 @@ type ConfigParser struct {
47
47
func (d Dict ) Keys () []string {
48
48
keys := make ([]string , 0 , len (d ))
49
49
50
+ if len (d ) == 0 {
51
+ return keys
52
+ }
53
+
50
54
for key := range d {
51
55
keys = append (keys , key )
52
56
}
@@ -90,9 +94,8 @@ func NewWithOptions(opts ...optFunc) *ConfigParser {
90
94
func NewWithDefaults (defaults Dict ) (* ConfigParser , error ) {
91
95
p := New ()
92
96
for key , value := range defaults {
93
- if err := p .defaults .Add (key , value ); err != nil {
94
- return nil , fmt .Errorf ("failed to add %q to %q: %w" , key , value , err )
95
- }
97
+ // Add never returns an error.
98
+ _ = p .defaults .Add (key , value )
96
99
}
97
100
return p , nil
98
101
}
@@ -191,34 +194,28 @@ func (p *ConfigParser) SaveWithDelimiter(filename, delimiter string) error {
191
194
}
192
195
193
196
// ParseReader parses data into ConfigParser from provided reader.
194
- func (p * ConfigParser ) ParseReader (in io.Reader ) error {
195
- reader := bufio .NewReader (in )
196
- var lineNo int
197
- var curSect * Section
198
- var key , value string
199
-
200
- keyValue := regexp .MustCompile (
201
- fmt .Sprintf (
202
- `([^%[1]s\s][^%[1]s]*)\s*(?P<vi>[%[1]s]+)\s*(.*)$` ,
203
- p .opt .delimiters ,
204
- ),
205
- )
206
- keyWNoValue := regexp .MustCompile (
207
- fmt .Sprintf (
208
- `([^%[1]s\s][^%[1]s]*)\s*((?P<vi>[%[1]s]+)\s*(.*)$)?` ,
209
- p .opt .delimiters ,
210
- ),
197
+ func (p * ConfigParser ) ParseReader (in io.Reader ) (err error ) {
198
+ var (
199
+ reader = bufio .NewReader (in )
200
+
201
+ lineNo int
202
+ key , value string
203
+ curSect * Section
211
204
)
212
205
206
+ keyValue , keyWNoValue , err := p .opt .compileRegex ()
207
+ if err != nil {
208
+ return err
209
+ }
210
+
213
211
for {
214
212
l , _ , err := reader .ReadLine ()
215
213
if err != nil {
216
214
// If error is end of file, then current key should be checked before return.
217
215
if errors .Is (err , io .EOF ) {
218
216
if key != "" {
219
- if err := curSect .Add (key , value ); err != nil {
220
- return fmt .Errorf ("failed to add %q = %q: %w" , key , value , err )
221
- }
217
+ // Add never returns an error.
218
+ _ = curSect .Add (key , value )
222
219
}
223
220
224
221
return nil
@@ -255,9 +252,8 @@ func (p *ConfigParser) ParseReader(in io.Reader) error {
255
252
// multiline prefixes or it is an empty line which is not allowed within values,
256
253
// then it counts as the value parsing is finished and it can be added
257
254
// to the current section.
258
- if err := curSect .Add (key , value ); err != nil {
259
- return fmt .Errorf ("failed to add %q = %q: %w" , key , value , err )
260
- }
255
+ // Add never returns an error.
256
+ _ = curSect .Add (key , value )
261
257
262
258
// Drop key-value pair to empty strings.
263
259
key , value = "" , ""
@@ -298,19 +294,20 @@ func (p *ConfigParser) ParseReader(in io.Reader) error {
298
294
}
299
295
300
296
value = p .opt .inlineCommentPrefixes .Split (match [3 ])
301
- } else if match = keyWNoValue .FindStringSubmatch (line ); len (match ) > 0 &&
302
- p .opt .allowNoValue {
303
- if curSect == nil {
304
- return fmt .Errorf ("missing section header: %d %s" , lineNo , line )
305
- }
306
- key = strings .TrimSpace (match [1 ])
307
- if p .opt .strict {
308
- if err := p .inOptions (key ); err != nil {
309
- return err
297
+ } else if p .opt .allowNoValue {
298
+ if match = keyWNoValue .FindStringSubmatch (line ); len (match ) > 0 {
299
+ if curSect == nil {
300
+ return fmt .Errorf ("missing section header: %d %s" , lineNo , line )
301
+ }
302
+ key = strings .TrimSpace (match [1 ])
303
+ if p .opt .strict {
304
+ if err := p .inOptions (key ); err != nil {
305
+ return err
306
+ }
310
307
}
311
- }
312
308
313
- value = p .opt .inlineCommentPrefixes .Split (match [4 ])
309
+ value = p .opt .inlineCommentPrefixes .Split (match [4 ])
310
+ }
314
311
}
315
312
}
316
313
}
0 commit comments