Skip to content

Commit

Permalink
Merge pull request #36 from apesternikov/backslash_encoding
Browse files Browse the repository at this point in the history
backslashes must be escaped on write
  • Loading branch information
magiconair committed Aug 25, 2020
2 parents ff39abe + 5ce16e8 commit 16ab158
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ func escape(r rune, special string) string {
return "\\r"
case '\t':
return "\\t"
case '\\':
return "\\\\"
default:
if strings.ContainsRune(special, r) {
return "\\" + string(r)
Expand Down
4 changes: 4 additions & 0 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ var writeTests = []struct {
{"key = value \\\n continued", "key = value continued\n", "ISO-8859-1"},
{"key⌘ = value", "key\\u2318 = value\n", "ISO-8859-1"},
{"ke\\ \\:y = value", "ke\\ \\:y = value\n", "ISO-8859-1"},
{"ke\\\\y = val\\\\ue", "ke\\\\y = val\\\\ue\n", "ISO-8859-1"},

// UTF-8 tests
{"key = value", "key = value\n", "UTF-8"},
{"key = value \\\n continued", "key = value continued\n", "UTF-8"},
{"key⌘ = value⌘", "key⌘ = value⌘\n", "UTF-8"},
{"ke\\ \\:y = value", "ke\\ \\:y = value\n", "UTF-8"},
{"ke\\\\y = val\\\\ue", "ke\\\\y = val\\\\ue\n", "UTF-8"},
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -330,6 +332,7 @@ var stringTests = []struct {
}{
// valid values
{"key = abc", "key", "def", "abc"},
{"key = ab\\\\c", "key", "def", "ab\\c"},

// non existent key
{"key = abc", "key2", "def", "def"},
Expand All @@ -346,6 +349,7 @@ var keysTests = []struct {
{"key = abc\nkey2=def", []string{"key", "key2"}},
{"key2 = abc\nkey=def", []string{"key2", "key"}},
{"key = abc\nkey=def", []string{"key"}},
{"key\\\\with\\\\backslashes = abc", []string{"key\\with\\backslashes"}},
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 16ab158

Please sign in to comment.