@@ -48,7 +48,6 @@ type UnitFileParser struct {
48
48
49
49
currentGroup * unitGroup
50
50
pendingComments []* unitLine
51
- lineNr int
52
51
}
53
52
54
53
func newUnitLine (key string , value string , isComment bool ) * unitLine {
@@ -347,7 +346,7 @@ func (p *UnitFileParser) parseKeyValuePair(line string) error {
347
346
return nil
348
347
}
349
348
350
- func (p * UnitFileParser ) parseLine (line string ) error {
349
+ func (p * UnitFileParser ) parseLine (line string , lineNr int ) error {
351
350
switch {
352
351
case lineIsComment (line ):
353
352
return p .parseComment (line )
@@ -356,7 +355,7 @@ func (p *UnitFileParser) parseLine(line string) error {
356
355
case lineIsKeyValuePair (line ):
357
356
return p .parseKeyValuePair (line )
358
357
default :
359
- return fmt .Errorf ("file contains line %d: “%s” which is not a key-value pair, group, or comment" , p . lineNr , line )
358
+ return fmt .Errorf ("file contains line %d: “%s” which is not a key-value pair, group, or comment" , lineNr , line )
360
359
}
361
360
}
362
361
@@ -376,53 +375,34 @@ func (p *UnitFileParser) flushPendingComments(toComment bool) {
376
375
}
377
376
}
378
377
379
- func nextLine (data string , afterPos int ) (string , string ) {
380
- rest := data [afterPos :]
381
- if i := strings .Index (rest , "\n " ); i >= 0 {
382
- return strings .TrimSpace (data [:i + afterPos ]), data [i + afterPos + 1 :]
383
- }
384
- return data , ""
385
- }
386
-
387
- func trimSpacesFromLines (data string ) string {
388
- lines := strings .Split (data , "\n " )
389
- for i , line := range lines {
390
- lines [i ] = strings .TrimSpace (line )
391
- }
392
- return strings .Join (lines , "\n " )
393
- }
394
-
395
378
// Parse an already loaded unit file (in the form of a string)
396
379
func (f * UnitFile ) Parse (data string ) error {
397
380
p := & UnitFileParser {
398
- file : f ,
399
- lineNr : 1 ,
381
+ file : f ,
400
382
}
401
383
402
- data = trimSpacesFromLines (data )
403
-
404
- for len (data ) > 0 {
405
- origdata := data
406
- nLines := 1
407
- var line string
408
- line , data = nextLine (data , 0 )
384
+ lines := strings .Split (strings .TrimSuffix (data , "\n " ), "\n " )
385
+ remaining := ""
409
386
387
+ for lineNr , line := range lines {
410
388
if ! lineIsComment (line ) {
411
- // Handle multi-line continuations
412
- // Note: This doesn't support comments in the middle of the continuation, which systemd does
413
- if lineIsKeyValuePair (line ) {
414
- for len (data ) > 0 && line [len (line )- 1 ] == '\\' {
415
- line , data = nextLine (origdata , len (line )+ 1 )
416
- nLines ++
389
+ line = strings .TrimSpace (line )
390
+ if strings .HasSuffix (line , "\\ " ) {
391
+ line = line [:len (line )- 1 ]
392
+ // check whether the line is a continuation of the previous line
393
+ if lineNr != len (lines )- 1 {
394
+ remaining += line
395
+ continue
417
396
}
418
397
}
398
+ if remaining != "" {
399
+ line = remaining + line
400
+ remaining = ""
401
+ }
419
402
}
420
-
421
- if err := p .parseLine (line ); err != nil {
403
+ if err := p .parseLine (line , lineNr + 1 ); err != nil {
422
404
return err
423
405
}
424
-
425
- p .lineNr += nLines
426
406
}
427
407
428
408
if p .currentGroup == nil {
0 commit comments