Skip to content

Commit fc70991

Browse files
authored
Merge pull request #26 from arduino/non-utf-8
Allow ISO8859-1 encoding in properties
2 parents f92e2f6 + 1a443e4 commit fc70991

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

properties.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import (
7777
"regexp"
7878
"runtime"
7979
"strings"
80+
"unicode/utf8"
8081

8182
"github.com/arduino/go-paths-helper"
8283
)
@@ -129,9 +130,23 @@ func NewFromHashmap(orig map[string]string) *Map {
129130
return res
130131
}
131132

133+
func toUtf8(iso8859_1_buf []byte) string {
134+
buf := make([]rune, len(iso8859_1_buf))
135+
for i, b := range iso8859_1_buf {
136+
buf[i] = rune(b)
137+
}
138+
return string(buf)
139+
}
140+
132141
// LoadFromBytes reads properties data and makes a Map out of it.
133142
func LoadFromBytes(bytes []byte) (*Map, error) {
134-
text := string(bytes)
143+
var text string
144+
if utf8.Valid(bytes) {
145+
text = string(bytes)
146+
} else {
147+
// Assume ISO8859-1 encoding and convert to UTF-8
148+
text = toUtf8(bytes)
149+
}
135150
text = strings.Replace(text, "\r\n", "\n", -1)
136151
text = strings.Replace(text, "\r", "\n", -1)
137152

properties_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,9 @@ func TestExtractSubIndexLists(t *testing.T) {
384384
s5 := m.ExtractSubIndexLists("cinque.discovery.required")
385385
require.Len(t, s5, 0)
386386
}
387+
388+
func TestLoadingNonUTF8Properties(t *testing.T) {
389+
m, err := LoadFromPath(paths.New("testdata/non-utf8.properties"))
390+
require.NoError(t, err)
391+
require.Equal(t, "Aáa", m.Get("maintainer"))
392+
}

testdata/non-utf8.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
maintainer=A�a

0 commit comments

Comments
 (0)