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

fix the line handling of UTF-16le #790

Merged
merged 1 commit into from
Oct 19, 2023
Merged

Conversation

kmuto
Copy link
Member

@kmuto kmuto commented Oct 18, 2023

Hi, I noticed check-log doesn't correctly determnine UTF-16le (Little Endian).

The following problems occur with UTF-16le because log files are separated by "\n" based on bytes rather than based on encoding.

  • The newline character 0x0a 0x00 in UTF-16le is delimited in the middle, so the second and subsequent lines contain 0x00 at the beginning.
  • Similarly, line endings are broken when converted to UTF-8 because only 0x0a is used instead of 0x0a 0x00

As this does not happen with other encodings (unless you consider UTF-32), it seemed reasonable to deal only with UTF-16le.
I propose a patch here.

Test:

[Before patch]
$ go run main.go -p ☺️  -f testcode/t1-u16le.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 1 warnings, 1 criticals for pattern /☺️/.
[testcode/t1-u16le.txt]
☺️�

exit status 2

$ go run main.go -p ☺️  -f testcode/t1-u16le-bom.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 1 warnings, 1 criticals for pattern /☺️/.
[testcode/t1-u16le-bom.txt]
☺️�

exit status 2

$ go run main.go -p ☺️  -f testcode/t1-u16le-bom-crlf.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 1 warnings, 1 criticals for pattern /☺️/.
[testcode/t1-u16le-bom-crlf.txt]
�

exit status 2

[after patch]
$ go run main.go -p ☺️  -f testcode/t1-u16le.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 3 warnings, 3 criticals for pattern /☺️/.
[testcode/t1-u16le.txt]
☺️
☺️
☺️

exit status 2

$ go run main.go -p ☺️  -f testcode/t1-u16le-bom.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 3 warnings, 3 criticals for pattern /☺️/.
[testcode/t1-u16le-bom.txt]
☺️
☺️
☺️

exit status 2

$ go run main.go -p ☺️  -f testcode/t1-u16le-bom-crlf.txt --encoding=UTF-16le --check-first -s . --return
LOG CRITICAL: 3 warnings, 3 criticals for pattern /☺️/.
[testcode/t1-u16le-bom-crlf.txt]
☺️
☺️
☺️

exit status 2

testcodes.zip

Copy link
Contributor

@yseto yseto left a comment

Choose a reason for hiding this comment

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

seems good.

@kmuto kmuto merged commit ea0178a into mackerelio:master Oct 19, 2023
16 checks passed
@kmuto kmuto deleted the utf16le-fix branch October 19, 2023 06:14
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