Skip to content

Commit 1ac3a1a

Browse files
authored
Merge pull request #1 from filipnavara/escape
Handle escape sequences in unquoted strings
2 parents f187355 + 2864f0e commit 1ac3a1a

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

read.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gopkg.in/warnings.v0"
1313
)
1414

15-
var unescape = map[rune]rune{'\\': '\\', '"': '"', 'n': '\n', 't': '\t'}
15+
var unescape = map[rune]rune{'\\': '\\', '"': '"', 'n': '\n', 't': '\t', 'b': '\b'}
1616

1717
// no error: invalid literals should be caught by scanner
1818
func unquote(s string) string {
@@ -48,7 +48,7 @@ func unquote(s string) string {
4848
return string(u)
4949
}
5050

51-
func read(c *warnings.Collector, callback func(string,string,string,string,bool)error,
51+
func read(c *warnings.Collector, callback func(string, string, string, string, bool) error,
5252
fset *token.FileSet, file *token.File, src []byte) error {
5353
//
5454
var s scanner.Scanner
@@ -223,7 +223,7 @@ func readInto(config interface{}, fset *token.FileSet, file *token.File,
223223
// (as opposed to set to empty string).
224224
//
225225
// If callback returns an error, ReadWithCallback terminates with an error too.
226-
func ReadWithCallback(reader io.Reader, callback func(string,string,string,string,bool)error) error {
226+
func ReadWithCallback(reader io.Reader, callback func(string, string, string, string, bool) error) error {
227227
src, err := ioutil.ReadAll(reader)
228228
if err != nil {
229229
return err

read_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package gcfg
22

33
import (
4+
"bytes"
45
"fmt"
56
"math/big"
67
"os"
78
"reflect"
8-
"testing"
9-
"bytes"
109
"strconv"
10+
"testing"
11+
1112
"github.com/pkg/errors"
1213
)
1314

@@ -127,6 +128,8 @@ var readtests = []struct {
127128
{"[section]\nname=\"va\\\"lue\"", &cBasic{Section: cBasicS1{Name: "va\"lue"}}, true},
128129
{"[section]\nname=\"va\\nlue\"", &cBasic{Section: cBasicS1{Name: "va\nlue"}}, true},
129130
{"[section]\nname=\"va\\tlue\"", &cBasic{Section: cBasicS1{Name: "va\tlue"}}, true},
131+
{"[section]\nname=x:\\\\path\\\\", &cBasic{Section: cBasicS1{Name: "x:\\path\\"}}, true},
132+
{"[section]\nname=\\b", &cBasic{Section: cBasicS1{Name: "\b"}}, true},
130133
{"\n[section]\nname=\\", &cBasic{}, false},
131134
{"\n[section]\nname=\\a", &cBasic{}, false},
132135
{"\n[section]\nname=\"val\\a\"", &cBasic{}, false},

scanner/scanner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (s *Scanner) scanEscape(val bool) {
170170
switch ch {
171171
case '\\', '"':
172172
// ok
173-
case 'n', 't':
173+
case 'n', 't', 'b':
174174
if val {
175175
break // ok
176176
}
@@ -232,10 +232,10 @@ loop:
232232
s.next()
233233
}
234234
if s.ch != '\n' {
235-
s.error(offs, "unquoted '\\' must be followed by new line")
236-
break loop
235+
s.scanEscape(true)
236+
} else {
237+
s.next()
237238
}
238-
s.next()
239239
case ch == '"':
240240
inQuote = !inQuote
241241
case ch == '\r':

scanner/scanner_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ var errors = []struct {
381381
{"\"\n", token.STRING, 0, "string not terminated"},
382382
{`="`, token.STRING, 1, "string not terminated"},
383383
{"=\"\n", token.STRING, 1, "string not terminated"},
384-
{"=\\", token.STRING, 1, "unquoted '\\' must be followed by new line"},
385-
{"=\\\r", token.STRING, 1, "unquoted '\\' must be followed by new line"},
384+
{"=\\", token.STRING, 2, "unknown escape sequence"},
385+
{"=\\\r", token.STRING, 3, "unknown escape sequence"},
386386
{`"\z"`, token.STRING, 2, "unknown escape sequence"},
387387
{`"\a"`, token.STRING, 2, "unknown escape sequence"},
388388
{`"\b"`, token.STRING, 2, "unknown escape sequence"},

0 commit comments

Comments
 (0)