go-eol/readthiseol.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-11-29 18:18:09 +00:00
package eol
import (
"io"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/crlf"
"sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel"
2023-11-29 18:18:09 +00:00
)
// ReadThisEOL tries to read the specified end-of-line sequence.
//
// The end-of-line sequences it supports are:
//
// line-feed (LF) (U+000A) ('\n')
// carriage-return (CR) (U+000D) ('\r')
// carriage-return, line-feed ("\r\n")
// next-line (NEL) (U+0085)
// line-separator (LS) (U+2028)
//
// If successful, ReadThisEOL return the number-of-bytes read (to read in the specified end-of-line sequence).
//
// Example usage:
///
// size, err := eol.ReadThisEOL(runescanner, eol.CRLF)
func ReadThisEOL(runescanner io.RuneScanner, endofline string) (size int, err error) {
if nil == runescanner {
return 0, errNilRuneScanner
}
switch endofline {
2024-02-18 17:27:47 +00:00
case lf.String:
2023-11-29 18:18:09 +00:00
return ReadLF(runescanner)
2024-02-18 17:27:47 +00:00
case cr.String:
2023-11-29 18:18:09 +00:00
return ReadCR(runescanner)
2024-02-18 17:27:47 +00:00
case crlf.String:
2023-11-29 18:18:09 +00:00
return ReadCRLF(runescanner)
2024-02-18 17:27:47 +00:00
case nel.String:
2023-11-29 18:18:09 +00:00
return ReadNEL(runescanner)
2024-02-18 17:27:47 +00:00
case ls.String:
2023-11-29 18:18:09 +00:00
return ReadLS(runescanner)
default:
return 0, errUnrecognizedEOL(endofline)
}
}