Handle quoted hashes "properly"

Read: not prettily or robustly.
pull/1/head
John Barton (joho) 2013-07-31 12:24:03 +10:00
parent aa6e870b57
commit 74ec3a085f
2 changed files with 19 additions and 5 deletions

View File

@ -78,14 +78,27 @@ func parseLine(line string) (key string, value string, err error) {
value = splitString[1]
// ditch the comments
// ditch the comments (but keep quoted hashes)
if strings.Contains(value, "#") {
segmentsBetweenHashes := strings.Split(value, "#")
value = segmentsBetweenHashes[0]
// open quote in leftmost segment
if strings.Count(value, "\"") == 1 || strings.Count(value, "'") == 1 {
value = value + "#" + segmentsBetweenHashes[1]
quotesAreOpen := false
segmentsToKeep := make([]string, 0)
for _, segment := range segmentsBetweenHashes {
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
if quotesAreOpen {
quotesAreOpen = false
segmentsToKeep = append(segmentsToKeep, segment)
} else {
quotesAreOpen = true
}
}
if len(segmentsToKeep) == 0 || quotesAreOpen {
segmentsToKeep = append(segmentsToKeep, segment)
}
}
value = strings.Join(segmentsToKeep, "#")
}
// check if we've got quoted values

View File

@ -90,6 +90,7 @@ func TestParsing(t *testing.T) {
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
parseAndCompare(t, "FOO=\"bar#baz\" # comment", "FOO", "bar#baz")
parseAndCompare(t, "FOO='bar#baz' # comment", "FOO", "bar#baz")
parseAndCompare(t, "FOO=\"bar#baz#bang\" # comment", "FOO", "bar#baz#bang")
// it 'ignores comment lines' do
// expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')