This commit is contained in:
Unknwon
2015-12-07 23:17:02 -05:00
parent 9cd12f2950
commit 9f4d2712cf
3 changed files with 21 additions and 13 deletions
+4 -4
View File
@@ -34,7 +34,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.8.1"
_VERSION = "1.8.2"
)
func Version() string {
@@ -974,10 +974,10 @@ func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
val := key.value
// In case key value contains "\n", "`", "\"", "#" or ";".
if strings.ContainsAny(val, "#;") {
val = "`" + val + "`"
} else if strings.Contains(val, "\n`") {
if strings.ContainsAny(val, "\n`") {
val = `"""` + val + `"""`
} else if strings.ContainsAny(val, "#;") {
val = "`" + val + "`"
}
if _, err = buf.WriteString(kname + equalSign + val + LineBreak); err != nil {
return 0, err
+1 -1
View File
@@ -90,7 +90,7 @@ key3 = "one", "two", "three"
[advance]
value with quotes = "some value"
value quote2 again = 'some value'
true = """"2+3=5""""
true = 2+3=5
"1+1=2" = true
"""6+1=7""" = true
"""` + "`" + `5+5` + "`" + `""" = 10
+16 -8
View File
@@ -170,6 +170,14 @@ func (p *parser) readContinuationLines(val string) (string, error) {
return val, nil
}
// hasSurroundedQuote check if and only if the first and last characters
// are quotes \" or \'.
// It returns false if any other parts also contain same kind of quotes.
func hasSurroundedQuote(in string, quote byte) bool {
return in[0] == quote && in[len(in)-1] == quote &&
strings.IndexByte(in[1:], quote) == len(in)-2
}
func (p *parser) readValue(in []byte) (string, error) {
line := strings.TrimLeftFunc(string(in), unicode.IsSpace)
if len(line) == 0 {
@@ -202,18 +210,18 @@ func (p *parser) readValue(in []byte) (string, error) {
return p.readContinuationLines(line[:len(line)-1])
}
// Trim single quotes
if (line[0] == '\'' && line[len(line)-1] == '\'') ||
(line[0] == '"' && line[len(line)-1] == '"') {
line = line[1 : len(line)-1]
}
i := strings.IndexAny(line, "#;")
if i > -1 {
p.comment.WriteString(line[i:])
line = line[:i]
line = strings.TrimSpace(line[:i])
}
return strings.TrimSpace(line), nil
// Trim single quotes
if hasSurroundedQuote(line, '\'') ||
hasSurroundedQuote(line, '"') {
line = line[1 : len(line)-1]
}
return line, nil
}
// parse parses data through an io.Reader.