Make encoding explicit.
diff --git a/example_test.go b/example_test.go index d8ee275..18081aa 100644 --- a/example_test.go +++ b/example_test.go
@@ -9,10 +9,10 @@ "log" ) -func ExampleLoad() { +func ExampleLoad_ISO() { buf := []byte("key = ISO-8859-1 value with unicode literal \\u2318 and umlaut ") buf = append(buf, 0xE4) // 0xE4 == ä - p, _ := Load(buf) + p, _ := Load(buf, ISO_8859_1) v, ok := p.Get("key") fmt.Println(ok) fmt.Println(v) @@ -21,8 +21,8 @@ // ISO-8859-1 value with unicode literal ⌘ and umlaut ä } -func ExampleLoadString() { - p, _ := LoadString("key = UTF-8 value with unicode character ⌘ and umlaut ä") +func ExampleLoad_UTF8() { + p, _ := Load([]byte("key = UTF-8 value with unicode character ⌘ and umlaut ä"), UTF8) v, ok := p.Get("key") fmt.Println(ok) fmt.Println(v) @@ -32,7 +32,7 @@ } func Example_Properties_GetDefault() { - p, _ := LoadString("key=value") + p, _ := Load([]byte("key=value"), ISO_8859_1) v := p.GetDefault("another key", "default value") fmt.Println(v) // Output: @@ -41,7 +41,7 @@ func Example() { // Decode some key/value pairs with expressions - p, err := LoadString("key=value\nkey2=${key}") + p, err := Load([]byte("key=value\nkey2=${key}"), ISO_8859_1) if err != nil { log.Fatal(err) }
diff --git a/load.go b/load.go index ecb9439..b77da41 100644 --- a/load.go +++ b/load.go
@@ -10,57 +10,50 @@ "os" ) -// Load reads an ISO-8859-1 encoded buffer into a Properties struct. -func Load(buf []byte) (*Properties, error) { - return loadBuf(buf, enc_iso_8859_1) -} - -// LoadString reads an UTF-8 string into a Properties struct. -func LoadString(input string) (*Properties, error) { - return loadBuf([]byte(input), enc_utf8) +// Load reads a buffer into a Properties struct. +func Load(buf []byte, enc Encoding) (*Properties, error) { + return loadBuf(buf, enc) } // LoadFile reads a file into a Properties struct. -func LoadFile(filename string) (*Properties, error) { - return loadFiles([]string{filename}, false) +func LoadFile(filename string, enc Encoding) (*Properties, error) { + return loadFiles([]string{filename}, enc, false) } -// LoadFiles reads multiple file in the given order into -// a Properties struct. If 'ignoreMissing' is 'true' then +// LoadFiles reads multiple files in the given order into +// a Properties struct. If 'ignoreMissing' is true then // non-existent files will not be reported as error. -func LoadFiles(filenames []string, ignoreMissing bool) (*Properties, error) { - return loadFiles(filenames, ignoreMissing) +func LoadFiles(filenames []string, enc Encoding, ignoreMissing bool) (*Properties, error) { + return loadFiles(filenames, enc, ignoreMissing) } -// MustLoadFile reads a file into a Properties struct and panics on error. -func MustLoadFile(filename string) *Properties { - return MustLoadFiles([]string{filename}, false) +// MustLoadFile reads a file into a Properties struct and +// panics on error. +func MustLoadFile(filename string, enc Encoding) *Properties { + return mustLoadFiles([]string{filename}, enc, false) } -// MustLoadFiles reads multiple file in the given order into -// a Properties struct and panics on error. -// If 'ignoreMissing' is 'true' then non-existent files will not be reported as error. -func MustLoadFiles(filenames []string, ignoreMissing bool) *Properties { - p, err := loadFiles(filenames, ignoreMissing) - if err != nil { - panic(err) - } - return p +// MustLoadFiles reads multiple files in the given order into +// a Properties struct and panics on error. If 'ignoreMissing' +// is true then non-existent files will not be reported as error. +func MustLoadFiles(filenames []string, enc Encoding, ignoreMissing bool) *Properties { + return mustLoadFiles(filenames, enc, ignoreMissing) } -type encoding uint +// ---------------------------------------------------------------------------- + +type Encoding uint const ( - enc_utf8 encoding = 1 << iota - enc_iso_8859_1 + UTF8 Encoding = 1 << iota + ISO_8859_1 ) -// Loads either an ISO-8859-1 or an UTF-8 encoded string into a Properties struct. -func loadBuf(buf []byte, enc encoding) (*Properties, error) { +func loadBuf(buf []byte, enc Encoding) (*Properties, error) { return parse(convert(buf, enc)) } -func loadFiles(filenames []string, ignoreMissing bool) (*Properties, error) { +func loadFiles(filenames []string, enc Encoding, ignoreMissing bool) (*Properties, error) { buff := make([]byte, 0, 4096) for _, filename := range filenames { @@ -78,17 +71,25 @@ buff = append(append(buff, buf...), '\n') } - return loadBuf(buff, enc_iso_8859_1) + return loadBuf(buff, enc) } -// Interprets a byte buffer either as ISO-8859-1 or UTF-8 encoded string. +func mustLoadFiles(filenames []string, enc Encoding, ignoreMissing bool) *Properties { + p, err := loadFiles(filenames, enc, ignoreMissing) + if err != nil { + panic(err) + } + return p +} + +// Interprets a byte buffer either as an ISO-8859-1 or UTF-8 encoded string. // For ISO-8859-1 we can convert each byte straight into a rune since the // first 256 unicode code points cover ISO-8859-1. -func convert(buf []byte, enc encoding) string { +func convert(buf []byte, enc Encoding) string { switch enc { - case enc_utf8: + case UTF8: return string(buf) - case enc_iso_8859_1: + case ISO_8859_1: runes := make([]rune, len(buf)) for i, b := range buf { runes[i] = rune(b)
diff --git a/load_test.go b/load_test.go index 42fe1d7..0d9ef75 100644 --- a/load_test.go +++ b/load_test.go
@@ -23,7 +23,7 @@ // ---------------------------------------------------------------------------- func (s *LoadSuite) TestLoadFailsWithNotExistingFile(c *C) { - _, err := LoadFile("doesnotexist.properties") + _, err := LoadFile("doesnotexist.properties", ISO_8859_1) c.Assert(err, NotNil) c.Assert(err, ErrorMatches, "open.*no such file or directory") } @@ -31,7 +31,7 @@ // ---------------------------------------------------------------------------- func (s *LoadSuite) TestLoadFilesFailsOnNotExistingFile(c *C) { - _, err := LoadFiles([]string{"doesnotexist.properties"}, false) + _, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, false) c.Assert(err, NotNil) c.Assert(err, ErrorMatches, "open.*no such file or directory") } @@ -39,7 +39,7 @@ // ---------------------------------------------------------------------------- func (s *LoadSuite) TestLoadFilesDoesNotFailOnNotExistingFileAndIgnoreMissing(c *C) { - p, err := LoadFiles([]string{"doesnotexist.properties"}, true) + p, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, true) c.Assert(err, IsNil) c.Assert(p.Len(), Equals, 0) } @@ -48,7 +48,7 @@ func (s *LoadSuite) TestLoad(c *C) { filename := s.makeFile(c, "key=value") - p := MustLoadFile(filename) + p := MustLoadFile(filename, ISO_8859_1) c.Assert(p.Len(), Equals, 1) assertKeyValues(c, "", p, "key", "value") @@ -59,7 +59,7 @@ func (s *LoadSuite) TestLoadFiles(c *C) { filename := s.makeFile(c, "key=value") filename2 := s.makeFile(c, "key2=value2") - p := MustLoadFiles([]string{filename, filename2}, false) + p := MustLoadFiles([]string{filename, filename2}, ISO_8859_1, false) assertKeyValues(c, "", p, "key", "value", "key2", "value2") } @@ -68,7 +68,7 @@ func (s *LoadSuite) TestLoadFilesAndIgnoreMissing(c *C) { filename := s.makeFile(c, "key=value") filename2 := s.makeFile(c, "key2=value2") - p := MustLoadFiles([]string{filename, filename + "foo", filename2, filename2 + "foo"}, true) + p := MustLoadFiles([]string{filename, filename + "foo", filename2, filename2 + "foo"}, ISO_8859_1, true) assertKeyValues(c, "", p, "key", "value", "key2", "value2") }
diff --git a/properties_test.go b/properties_test.go index 91b1a7e..0522007 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -120,7 +120,7 @@ } b.ResetTimer() for i := 0; i < b.N; i++ { - Load([]byte(input)) + Load([]byte(input), ISO_8859_1) } } @@ -188,7 +188,7 @@ func testKeyValue(c *C, input string, keyvalues ...string) { printf("%q\n", input) - p, err := Load([]byte(input)) + p, err := Load([]byte(input), ISO_8859_1) c.Assert(err, IsNil) assertKeyValues(c, input, p, keyvalues...) } @@ -197,7 +197,7 @@ func testError(c *C, input, msg string) { printf("%q\n", input) - _, err := Load([]byte(input)) + _, err := Load([]byte(input), ISO_8859_1) c.Assert(err, NotNil) c.Assert(strings.Contains(err.Error(), msg), Equals, true, Commentf("Expected %q got %q", msg, err.Error())) }