8000 Escape delimiters in string slice values by asciimoo · Pull Request #116 · go-ini/ini · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ key = "value"
key2 = "value2" ; This is a comment for key2
key3 = "one", "two", "three"

[string escapes]
key1 = value1, value2, value3
key2 = value1\, value2
key3 = val\ue1, value2
key4 = value1\\, value\\\\2
key5 = value1\,, value2
key6 = aaa bbb\ and\ space ccc

[advance]
value with quotes = "some value"
value quote2 again = 'some value'
Expand Down Expand Up @@ -397,6 +405,14 @@ Good man.
key2 = value2
key3 = "one", "two", "three"

[string escapes]
key1 = value1, value2, value3
key2 = value1\, value2
key3 = val\ue1, value2
key4 = value1\\, value\\\\2
key5 = value1\,, value2
key6 = aaa bbb\ and\ space ccc

[advance]
value with quotes = some value
value quote2 again = some value
Expand Down
37 changes: 33 additions & 4 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ini

import (
"bytes"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -444,11 +445,39 @@ func (k *Key) Strings(delim string) []string {
return []string{}
}

vals := strings.Split(str, delim)
for i := range vals {
// vals[i] = k.transformValue(strings.TrimSpace(vals[i]))
vals[i] = strings.TrimSpace(vals[i])
runes := []rune(str)
vals := make([]string, 0)
idx := 0
var buf bytes.Buffer
escape := false
for {
if escape {
escape = false
if runes[idx] != '\\' && !strings.HasPrefix(string(runes[idx:]), delim) {
buf.WriteRune('\\')
}
buf.WriteRune(runes[idx])
} else {
if runes[idx] == '\\' {
escape = true
} else if strings.HasPrefix(string(runes[idx:]), delim) {
idx += len(delim) - 1
vals = append(vals, strings.TrimSpace(buf.String()))
buf.Reset()
} else {
buf.WriteRune(runes[idx])
}
}
idx += 1
if idx == len(runes) {
break
}
}

if buf.Len() > 0 {
vals = append(vals, strings.TrimSpace(buf.String()))
}

return vals
}

Expand Down
12 changes: 11 additions & 1 deletion key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Test_Key(t *testing.T) {

Convey("Get sections", func() {
sections := cfg.Sections()
for i, name := range []string{DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "advance"} {
for i, name := range []string{DEFAULT_SECTION, "author", "package", "package.sub", "features", "types", "array", "note", "comments", "string escapes", "advance"} {
So(sections[i].Name(), ShouldEqual, name)
}
})
Expand Down Expand Up @@ -243,6 +243,16 @@ func Test_Key(t *testing.T) {
timesEqual(vals6, t, t, t)
})

Convey("Test string slice escapes", func() {
sec := cfg.Section("string escapes")
So(sec.Key("key1").Strings(","), ShouldResemble, []string{"value1", "value2", "value3"})
So(sec.Key("key2").Strings(","), ShouldResemble, []string{"value1, value2"})
So(sec.Key("key3").Strings(","), ShouldResemble, []string{`val\ue1`, "value2"})
So(sec.Key("key4").Strings(","), ShouldResemble, []string{`value1\`, `value\\2`})
So(sec.Key("key5").Strings(",,"), ShouldResemble, []string{"value1,, value2"})
So(sec.Key("key6").Strings(" "), ShouldResemble, []string{"aaa", "bbb and space", "ccc"})
})

Convey("Get valid values into slice", func() {
sec := cfg.Section("array")
vals1 := sec.Key("FLOAT64S").ValidFloat64s(",")
Expand Down
6 changes: 3 additions & 3 deletions section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_Section(t *testing.T) {
So(cfg, ShouldNotBeNil)

Convey("Get section strings", func() {
So(strings.Join(cfg.SectionStrings(), ","), ShouldEqual, "DEFAULT,author,package,package.sub,features,types,array,note,comments,advance")
So(strings.Join(cfg.SectionStrings(), ","), ShouldEqual, "DEFAULT,author,package,package.sub,features,types,array,note,comments,string escapes,advance")
})

Convey("Delete a section", func() {
Expand All @@ -50,7 +50,7 @@ func Test_SectionRaw(t *testing.T) {
Convey("Test section raw string", t, func() {
cfg, err := LoadSources(
LoadOptions{
Insensitive: true,
Insensitive: true,
UnparseableSections: []string{"core_lesson", "comments"},
},
"testdata/aicc.ini")
Expand All @@ -72,4 +72,4 @@ func Test_SectionRaw(t *testing.T) {
111111111111111111100000000000111000000000 – end my lesson state data`)
})
})
}
}
0