diff --git a/config.go b/config.go index 706221e..6cd9eb5 100644 --- a/config.go +++ b/config.go @@ -70,13 +70,19 @@ func parseVal(rawVal string) (*string, error) { val = val[:groups[2*2]] } - // Remove all '"', and replace '\"' with '"' - nonEscapedQuotes := regexp.MustCompile(`[^\\]"`) - val = nonEscapedQuotes.ReplaceAllStringFunc(val, func(s string) string { return s[:len(s)-1] }) - if val[0] == '"' { - val = val[1:] + var sb strings.Builder + for i, r := range val { + if r == '"' { + continue + } + + if val[i] == '\\' && val[i+1] == '"' { + sb.WriteRune('"') + } else { + sb.WriteRune(r) + } } - val = strings.ReplaceAll(val, `\"`, `"`) + val = sb.String() return &val, nil } diff --git a/config_test.go b/config_test.go index fb2d538..dcea584 100644 --- a/config_test.go +++ b/config_test.go @@ -438,3 +438,27 @@ Could not parse config correctly with quotes inside correctly. got: %#v`, want, config) } } + +func TestTwoQuote(t *testing.T) { + type Config struct { + Foo string + } + + config := Config{ + Foo: "", + } + err := LoadConfig("test_configs/twoquotes.cfg", &config) + if err != nil { + t.Fatalf("Could not parse config with escaped quote: %s", err.Error()) + } + + want := Config{ + Foo: "testing", + } + if want != config { + t.Fatalf(` +Could not parse config correctly with quotes inside correctly. + expected: %#v + got: %#v`, want, config) + } +} diff --git a/test_configs/twoquotes.cfg b/test_configs/twoquotes.cfg new file mode 100644 index 0000000..138bc81 --- /dev/null +++ b/test_configs/twoquotes.cfg @@ -0,0 +1 @@ +Foo = test""ing \ No newline at end of file