Skip to content

Instantly share code, notes, and snippets.

@kawakami-o3
Last active May 10, 2018 12:47
Show Gist options
  • Save kawakami-o3/74f2f0256ff881b0b1939575d52e4833 to your computer and use it in GitHub Desktop.
Save kawakami-o3/74f2f0256ff881b0b1939575d52e4833 to your computer and use it in GitHub Desktop.
Detect line break, new line (CR, LF, CRLF)
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