Skip to content
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

Merged
merged 3 commits into from
May 17, 2018

Conversation

wanglei-ok
Copy link
Contributor

@wanglei-ok wanglei-ok commented May 17, 2018

For compatibility with files created on Windows, ReadFileInto skips a single leading UTF8 BOM sequence if it exists.

  • Add ReadFileInto documentation
  • Add test data testdata/notepad.ini
  • Add testcase TestReadFileIntoNotepad

Related pull request#13

@wanglei-ok wanglei-ok mentioned this pull request May 17, 2018
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. > should be >=
  2. make this a helper function, say skipLeadingUtf8Bom([]byte) []byte
  3. 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 {
Copy link
Contributor

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
@wanglei-ok
Copy link
Contributor Author

Thanks for your guidance. Please take another look.

Copy link
Contributor

@speter speter left a 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. create global var utf8Bom = []byte("\ufeff")
  2. use len(utf8Bom) instead of 3
  3. get rid of variable bom, join the two ifs into one, and use bytes.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{}},
Copy link
Contributor

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.

@wanglei-ok
Copy link
Contributor Author

These changes make them better.

@speter speter merged commit 61b2c08 into go-gcfg:v1 May 17, 2018
@speter
Copy link
Contributor

speter commented May 17, 2018

Thanks!

@wanglei-ok wanglei-ok deleted the skip_utf8bom branch May 18, 2018 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants