-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadFileInto skips a single leading UTF8 BOM sequence if it exists. #14
Conversation
read.go
Outdated
@@ -231,6 +234,15 @@ func ReadFileInto(config interface{}, filename string) error { | |||
if err != nil { | |||
return err | |||
} | |||
|
|||
//Skips a single leading UTF8 BOM sequence if it exists. | |||
if len(src) > 3 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>
should be>=
- make this a helper function, say skipLeadingUtf8Bom([]byte) []byte
- the end-to-end test is fine, but please add table-driven unit tests for this new helper function:
- 0 bytes input
- 3 bytes input (BOM only)
- 3 bytes input (comment only, without BOM)
- normal input with BOM
- normal input without BOM
read.go
Outdated
@@ -231,6 +234,15 @@ func ReadFileInto(config interface{}, filename string) error { | |||
if err != nil { | |||
return err | |||
} | |||
|
|||
//Skips a single leading UTF8 BOM sequence if it exists. | |||
if len(src) > 3 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting to look good. A couple more requests.
2. Make helper function, say skipLeadingUtf8Bom([]byte) []byte 3. Add table-driven unit tests for skipLeadingUtf8Bom
Thanks for your guidance. Please take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looking very good. Now we are fully working, tested, documented. Just some more minor cosmetic changes.
Also, make sure to run gofmt before commit. You should always do this when contributing Go code.
read.go
Outdated
fset := token.NewFileSet() | ||
file := fset.AddFile(filename, fset.Base(), len(src)) | ||
return readInto(config, fset, file, src) | ||
} | ||
|
||
func skipLeadingUtf8Bom(src []byte) []byte{ | ||
if len(src) >= 3 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- create global
var utf8Bom = []byte("\ufeff")
- use
len(utf8Bom)
instead of3
- get rid of variable
bom
, join the twoif
s into one, and usebytes.Equal
with utf8Bom for comparison
read_test.go
Outdated
out []byte | ||
}{ | ||
{"0 bytes input",[]byte{}, []byte{}}, | ||
{"3 bytes input (BOM only)",[]byte{0xEF,0xBB,0xBF}, []byte{}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use []byte("\ufeff")
for consistency. same 2 lines below.
These changes make them better. |
Thanks! |
For compatibility with files created on Windows, ReadFileInto skips a single leading UTF8 BOM sequence if it exists.
Related pull request#13