lf cr
parent
739c545e2f
commit
230937cb2e
20
readeol.go
20
readeol.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"sourcecode.social/reiver/go-eol/cr"
|
"sourcecode.social/reiver/go-eol/cr"
|
||||||
"sourcecode.social/reiver/go-eol/crlf"
|
"sourcecode.social/reiver/go-eol/crlf"
|
||||||
"sourcecode.social/reiver/go-eol/lf"
|
"sourcecode.social/reiver/go-eol/lf"
|
||||||
|
"sourcecode.social/reiver/go-eol/lfcr"
|
||||||
"sourcecode.social/reiver/go-eol/ls"
|
"sourcecode.social/reiver/go-eol/ls"
|
||||||
"sourcecode.social/reiver/go-eol/nel"
|
"sourcecode.social/reiver/go-eol/nel"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
// The end-of-line sequences it supports are:
|
// The end-of-line sequences it supports are:
|
||||||
//
|
//
|
||||||
// line-feed (LF) (U+000A) ('\n')
|
// line-feed (LF) (U+000A) ('\n')
|
||||||
|
// line-feed, carriage-return ("\n\r")
|
||||||
// carriage-return (CR) (U+000D) ('\r')
|
// carriage-return (CR) (U+000D) ('\r')
|
||||||
// carriage-return, line-feed ("\r\n")
|
// carriage-return, line-feed ("\r\n")
|
||||||
// next-line (NEL) (U+0085)
|
// next-line (NEL) (U+0085)
|
||||||
|
@ -38,7 +40,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
r0, size0, err = runescanner.ReadRune()
|
r0, size0, err = runescanner.ReadRune()
|
||||||
if nil != err {
|
if nil != err && size0 <= 0 {
|
||||||
const characterNumber uint64 = 1
|
const characterNumber uint64 = 1
|
||||||
var eolSequence opt.Optional[string] // = opt.Nothing[string]() // i.e., unknown
|
var eolSequence opt.Optional[string] // = opt.Nothing[string]() // i.e., unknown
|
||||||
var circumstance internalCircumstance = specifyCircumstance(eolSequence, characterNumber)
|
var circumstance internalCircumstance = specifyCircumstance(eolSequence, characterNumber)
|
||||||
|
@ -49,7 +51,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
|
|
||||||
switch r0 {
|
switch r0 {
|
||||||
case lf.Rune:
|
case lf.Rune:
|
||||||
return lf.String, size0, nil
|
// Nothing here.
|
||||||
case cr.Rune:
|
case cr.Rune:
|
||||||
// Nothing here.
|
// Nothing here.
|
||||||
case nel.Rune:
|
case nel.Rune:
|
||||||
|
@ -69,7 +71,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
return "", 0, errNotEOL(r0)
|
return "", 0, errNotEOL(r0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we got here, then we had a CR
|
// if we got here, then we had a LR or CR
|
||||||
|
|
||||||
var r1 rune
|
var r1 rune
|
||||||
var size1 int
|
var size1 int
|
||||||
|
@ -78,9 +80,9 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
|
|
||||||
r1, size1, err = runescanner.ReadRune()
|
r1, size1, err = runescanner.ReadRune()
|
||||||
if io.EOF == err {
|
if io.EOF == err {
|
||||||
return cr.String, size0, nil
|
return string(r0), size0, nil
|
||||||
}
|
}
|
||||||
if nil != err {
|
if nil != err && size1 <= 0 {
|
||||||
const characterNumber uint64 = 2
|
const characterNumber uint64 = 2
|
||||||
var eolSequence opt.Optional[string] // = opt.Nothing[string]() // i.e., unknown
|
var eolSequence opt.Optional[string] // = opt.Nothing[string]() // i.e., unknown
|
||||||
var circumstance internalCircumstance = specifyCircumstance(eolSequence, characterNumber)
|
var circumstance internalCircumstance = specifyCircumstance(eolSequence, characterNumber)
|
||||||
|
@ -89,9 +91,11 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch r1 {
|
switch {
|
||||||
case lf.Rune:
|
case cr.Rune == r0 && lf.Rune == r1:
|
||||||
return crlf.String, size1+size0, nil
|
return crlf.String, size1+size0, nil
|
||||||
|
case lf.Rune == r0 && cr.Rune == r1:
|
||||||
|
return lfcr.String, size1+size0, nil
|
||||||
default:
|
default:
|
||||||
err := runescanner.UnreadRune()
|
err := runescanner.UnreadRune()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -102,6 +106,6 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
|
||||||
return "", size1+size0, errProblemUnreadingRune(circumstance, err, r1)
|
return "", size1+size0, errProblemUnreadingRune(circumstance, err, r1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cr.String, size0, nil
|
return string(r0), size0, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"sourcecode.social/reiver/go-eol/cr"
|
"sourcecode.social/reiver/go-eol/cr"
|
||||||
"sourcecode.social/reiver/go-eol/crlf"
|
"sourcecode.social/reiver/go-eol/crlf"
|
||||||
"sourcecode.social/reiver/go-eol/lf"
|
"sourcecode.social/reiver/go-eol/lf"
|
||||||
|
"sourcecode.social/reiver/go-eol/lfcr"
|
||||||
"sourcecode.social/reiver/go-eol/ls"
|
"sourcecode.social/reiver/go-eol/ls"
|
||||||
"sourcecode.social/reiver/go-eol/nel"
|
"sourcecode.social/reiver/go-eol/nel"
|
||||||
)
|
)
|
||||||
|
@ -28,6 +29,11 @@ func TestReadEOL(t *testing.T) {
|
||||||
ExpectedEOL: lf.String,
|
ExpectedEOL: lf.String,
|
||||||
ExpectedSize: 1,
|
ExpectedSize: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Value: "\n\r",
|
||||||
|
ExpectedEOL: lfcr.String,
|
||||||
|
ExpectedSize: 2,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Value: "\r",
|
Value: "\r",
|
||||||
ExpectedEOL: cr.String,
|
ExpectedEOL: cr.String,
|
||||||
|
@ -56,6 +62,11 @@ func TestReadEOL(t *testing.T) {
|
||||||
ExpectedEOL: lf.String,
|
ExpectedEOL: lf.String,
|
||||||
ExpectedSize: 1,
|
ExpectedSize: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Value: "\n\rapple banana cherr",
|
||||||
|
ExpectedEOL: lfcr.String,
|
||||||
|
ExpectedSize: 2,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Value: "\rapple banana cherr",
|
Value: "\rapple banana cherr",
|
||||||
ExpectedEOL: cr.String,
|
ExpectedEOL: cr.String,
|
||||||
|
|
Loading…
Reference in New Issue