Last active
May 10, 2018 12:47
-
-
Save kawakami-o3/74f2f0256ff881b0b1939575d52e4833 to your computer and use it in GitHub Desktop.
Detect line break, new line (CR, LF, CRLF)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ( | |
| lineUnknown = "unkown" | |
| lineLF = "LF" // 0x0A | |
| lineCR = "CR" // 0x0D | |
| lineCRLF = lineLF + lineCR | |
| ) | |
| const ( | |
| byteLF = 0x0A | |
| byteCR = 0x0D | |
| ) | |
| func detectLineBreak(cnt []byte) string { | |
| pos := 0 | |
| for pos < len(cnt) { | |
| a := cnt[pos] | |
| var b byte | |
| if pos+1 < len(cnt) { | |
| b = cnt[pos+1] | |
| } | |
| if a == byteCR && b == byteLF { | |
| return lineCRLF | |
| } else if a == byteCR && b != byteLF { | |
| return lineCR | |
| } else if a == byteLF || b == byteLF { | |
| return lineLF | |
| } | |
| if b == byteCR { | |
| pos++ | |
| } else { | |
| pos += 2 | |
| } | |
| } | |
| return lineUnknown | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment