Hanlde BOM for UTF-16

This commit is contained in:
Unknwon
2016-12-21 05:59:58 -05:00
parent 2ba15ac2dc
commit da14cebb76
7 changed files with 65 additions and 6 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.21.1"
_VERSION = "1.21.2"
)
// Version returns current package version literal.
+19 -4
View File
@@ -48,16 +48,31 @@ func newParser(r io.Reader) *parser {
}
}
// BOM handles header of BOM-UTF8 format.
// BOM handles header of UTF-8, UTF-16 LE and UTF-16 BE's BOM format.
// http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding
func (p *parser) BOM() error {
mask, err := p.buf.Peek(3)
mask, err := p.buf.Peek(2)
if err != nil && err != io.EOF {
return err
} else if len(mask) < 3 {
} else if len(mask) < 2 {
return nil
} else if mask[0] == 239 && mask[1] == 187 && mask[2] == 191 {
}
switch {
case mask[0] == 254 && mask[1] == 255:
fallthrough
case mask[0] == 255 && mask[1] == 254:
p.buf.Read(mask)
case mask[0] == 239 && mask[1] == 187:
mask, err := p.buf.Peek(3)
if err != nil && err != io.EOF {
return err
} else if len(mask) < 3 {
return nil
}
if mask[2] == 191 {
p.buf.Read(mask)
}
}
return nil
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2016 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package ini
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_BOM(t *testing.T) {
Convey("Test handling BOM", t, func() {
Convey("UTF-8-BOM", func() {
cfg, err := Load("testdata/UTF-8-BOM.ini")
So(err, ShouldBeNil)
So(cfg, ShouldNotBeNil)
So(cfg.Section("author").Key("E-MAIL").String(), ShouldEqual, "u@gogs.io")
})
Convey("UTF-16-LE-BOM", func() {
cfg, err := Load("testdata/UTF-16-LE-BOM.ini")
So(err, ShouldBeNil)
So(cfg, ShouldNotBeNil)
})
Convey("UTF-16-BE-BOM", func() {
})
})
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
[author]
E-MAIL = u@gogs.io
+1 -1
View File
@@ -1,2 +1,2 @@
[author]
[author]
E-MAIL = u@gogs.io