Skip to content

Commit 281f515

Browse files
authored
Merge pull request #81 from magiconair/issue-80
escape leading whitespace on value in Write()
2 parents 3dfc3b5 + 6b7aa68 commit 281f515

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
matrix:
99
os: [ubuntu-latest]
10-
go: ["1.23.x", "1.22.x", "1.21.x", "1.20.x", "1.19.x", "1.18.x"]
10+
go: ["1.24.x", "1.23.x", "1.22.x", "1.21.x", "1.20.x", "1.19.x", "1.18.x"]
1111

1212
steps:
1313
- name: Setup Go ${{ matrix.go }}

properties.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strconv"
1919
"strings"
2020
"time"
21+
"unicode"
2122
"unicode/utf8"
2223
)
2324

@@ -901,8 +902,13 @@ func encodeUtf8(s string, special string) string {
901902
v := ""
902903
for pos := 0; pos < len(s); {
903904
r, w := utf8.DecodeRuneInString(s[pos:])
905+
switch {
906+
case pos == 0 && unicode.IsSpace(r): // escape leading whitespace
907+
v += escape(r, " ")
908+
default:
909+
v += escape(r, special) // escape special chars only
910+
}
904911
pos += w
905-
v += escape(r, special)
906912
}
907913
return v
908914
}
@@ -913,6 +919,8 @@ func encodeIso(s string, special string) string {
913919
var v string
914920
for pos := 0; pos < len(s); {
915921
switch r, w = utf8.DecodeRuneInString(s[pos:]); {
922+
case pos == 0 && unicode.IsSpace(r): // escape leading whitespace
923+
v += escape(r, " ")
916924
case r < 1<<8: // single byte rune -> escape special chars only
917925
v += escape(r, special)
918926
case r < 1<<16: // two byte rune -> unicode literal

properties_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ var writeTests = []struct {
147147
}{
148148
// ISO-8859-1 tests
149149
{"key = value", "key = value\n", "ISO-8859-1"},
150+
{"key = \\ value", "key = \\ value\n", "ISO-8859-1"},
150151
{"key = value \\\n continued", "key = value continued\n", "ISO-8859-1"},
151152
{"key⌘ = value", "key\\u2318 = value\n", "ISO-8859-1"},
152153
{"ke\\ \\:y = value", "ke\\ \\:y = value\n", "ISO-8859-1"},
153154
{"ke\\\\y = val\\\\ue", "ke\\\\y = val\\\\ue\n", "ISO-8859-1"},
154155

155156
// UTF-8 tests
156157
{"key = value", "key = value\n", "UTF-8"},
158+
{"key = \\ value", "key = \\ value\n", "UTF-8"},
157159
{"key = value \\\n continued", "key = value continued\n", "UTF-8"},
158160
{"key⌘ = value⌘", "key⌘ = value⌘\n", "UTF-8"},
159161
{"ke\\ \\:y = value", "ke\\ \\:y = value\n", "UTF-8"},

0 commit comments

Comments
 (0)