go-eol/readcrlf.go

51 lines
1.4 KiB
Go
Raw Normal View History

2023-11-28 16:09:43 +00:00
package eol
import (
"io"
"sourcecode.social/reiver/go-opt"
2024-02-18 17:09:29 +00:00
"sourcecode.social/reiver/go-eol/cr"
2024-02-18 17:27:47 +00:00
"sourcecode.social/reiver/go-eol/crlf"
2024-02-18 17:09:29 +00:00
"sourcecode.social/reiver/go-eol/lf"
2023-11-28 16:09:43 +00:00
)
2023-11-28 16:29:07 +00:00
// ReadCRLF tries to read the "\r\n" (i.e., carriage-return line-feed) end-of-line sequence.
//
// If successful, it returns the number-of-bytes read (to read in end-of-line sequence "\r\n").
//
// If the first character read is not a '\r', then ReadCRLF will try to unread the character.
// If the second character read is not a '\n', then ReadCRLF will also try to unread the second character, but will not be able to unread the first character (i.e., '\r') it already read.
//
// Example usage:
//
// size, err := eol.ReadCRLF(runescanner)
2023-11-28 16:09:43 +00:00
func ReadCRLF(runescanner io.RuneScanner) (size int, err error) {
var size0 int
{
var err error
const characterNumber uint64 = 1
2024-02-18 17:27:47 +00:00
var circumstance internalCircumstance = specifyCircumstance(opt.Something(crlf.String), characterNumber)
2024-02-18 17:09:29 +00:00
size0, err = readthisrune(circumstance, runescanner, cr.Rune)
2023-11-28 16:09:43 +00:00
if nil != err {
return size0, err
}
}
var size1 int
{
var err error
const characterNumber uint64 = 2
2024-02-18 17:27:47 +00:00
var circumstance internalCircumstance = specifyCircumstance(opt.Something(crlf.String), characterNumber)
2024-02-18 17:09:29 +00:00
size1, err = readthisrune(circumstance, runescanner, lf.Rune)
2023-11-28 16:09:43 +00:00
if nil != err {
return size1+size0, err
}
}
return size1+size0, nil
}