refactor constants

master
Charles Iliya Krempeaux 2024-02-18 09:27:47 -08:00
parent ae739f1281
commit 43b24cd789
10 changed files with 53 additions and 51 deletions

View File

@ -1,16 +0,0 @@
package eol
import (
"sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel"
)
const (
LF = string(lf.Rune)
CR = string(cr.Rune)
CRLF = CR+LF
NEL = string(nel.Rune)
LS = string(ls.Rune)
)

View File

@ -19,6 +19,6 @@ import (
// size, err := eol.ReadCR(runescanner) // size, err := eol.ReadCR(runescanner)
func ReadCR(runescanner io.RuneScanner) (size int, err error) { func ReadCR(runescanner io.RuneScanner) (size int, err error) {
const characterNumber uint64 = 1 const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(CR), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(cr.String), characterNumber)
return readthisrune(circumstance, runescanner, cr.Rune) return readthisrune(circumstance, runescanner, cr.Rune)
} }

View File

@ -6,6 +6,7 @@ import (
"sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-eol/cr" "sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/crlf"
"sourcecode.social/reiver/go-eol/lf" "sourcecode.social/reiver/go-eol/lf"
) )
@ -26,7 +27,7 @@ func ReadCRLF(runescanner io.RuneScanner) (size int, err error) {
var err error var err error
const characterNumber uint64 = 1 const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(CRLF), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(crlf.String), characterNumber)
size0, err = readthisrune(circumstance, runescanner, cr.Rune) size0, err = readthisrune(circumstance, runescanner, cr.Rune)
if nil != err { if nil != err {
return size0, err return size0, err
@ -38,7 +39,7 @@ func ReadCRLF(runescanner io.RuneScanner) (size int, err error) {
var err error var err error
const characterNumber uint64 = 2 const characterNumber uint64 = 2
var circumstance internalCircumstance = specifyCircumstance(opt.Something(CRLF), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(crlf.String), characterNumber)
size1, err = readthisrune(circumstance, runescanner, lf.Rune) size1, err = readthisrune(circumstance, runescanner, lf.Rune)
if nil != err { if nil != err {
return size1+size0, err return size1+size0, err

View File

@ -6,6 +6,7 @@ import (
"sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-eol/cr" "sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/crlf"
"sourcecode.social/reiver/go-eol/lf" "sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/ls" "sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel" "sourcecode.social/reiver/go-eol/nel"
@ -48,13 +49,13 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
switch r0 { switch r0 {
case lf.Rune: case lf.Rune:
return LF, size0, nil return lf.String, size0, nil
case cr.Rune: case cr.Rune:
// Nothing here. // Nothing here.
case nel.Rune: case nel.Rune:
return NEL, size0, nil return nel.String, size0, nil
case ls.Rune: case ls.Rune:
return LS, size0, nil return ls.String, size0, nil
default: default:
err := runescanner.UnreadRune() err := runescanner.UnreadRune()
if nil != err { if nil != err {
@ -77,7 +78,7 @@ 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, size0, nil return cr.String, size0, nil
} }
if nil != err { if nil != err {
const characterNumber uint64 = 2 const characterNumber uint64 = 2
@ -90,7 +91,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
switch r1 { switch r1 {
case lf.Rune: case lf.Rune:
return CRLF, size1+size0, nil return crlf.String, size1+size0, nil
default: default:
err := runescanner.UnreadRune() err := runescanner.UnreadRune()
if nil != err { if nil != err {
@ -101,6 +102,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, size0, nil return cr.String, size0, nil
} }
} }

View File

@ -9,6 +9,11 @@ import (
"sourcecode.social/reiver/go-utf8" "sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-eol" "sourcecode.social/reiver/go-eol"
"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"
) )
func TestReadEOL(t *testing.T) { func TestReadEOL(t *testing.T) {
@ -20,27 +25,27 @@ func TestReadEOL(t *testing.T) {
}{ }{
{ {
Value: "\n", Value: "\n",
ExpectedEOL: eol.LF, ExpectedEOL: lf.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\r", Value: "\r",
ExpectedEOL: eol.CR, ExpectedEOL: cr.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\r\n", Value: "\r\n",
ExpectedEOL: eol.CRLF, ExpectedEOL: crlf.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u0085", Value: "\u0085",
ExpectedEOL: eol.NEL, ExpectedEOL: nel.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u2028", Value: "\u2028",
ExpectedEOL: eol.LS, ExpectedEOL: ls.String,
ExpectedSize: 3, ExpectedSize: 3,
}, },
@ -48,27 +53,27 @@ func TestReadEOL(t *testing.T) {
{ {
Value: "\napple banana cherry", Value: "\napple banana cherry",
ExpectedEOL: eol.LF, ExpectedEOL: lf.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\rapple banana cherr", Value: "\rapple banana cherr",
ExpectedEOL: eol.CR, ExpectedEOL: cr.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\r\napple banana cherr", Value: "\r\napple banana cherr",
ExpectedEOL: eol.CRLF, ExpectedEOL: crlf.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u0085apple banana cherr", Value: "\u0085apple banana cherr",
ExpectedEOL: eol.NEL, ExpectedEOL: nel.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u2028apple banana cherr", Value: "\u2028apple banana cherr",
ExpectedEOL: eol.LS, ExpectedEOL: ls.String,
ExpectedSize: 3, ExpectedSize: 3,
}, },
} }

View File

@ -19,6 +19,6 @@ import (
// size, err := eol.ReadLF(runescanner) // size, err := eol.ReadLF(runescanner)
func ReadLF(runescanner io.RuneScanner) (size int, err error) { func ReadLF(runescanner io.RuneScanner) (size int, err error) {
const characterNumber uint64 = 1 const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(LF), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(lf.String), characterNumber)
return readthisrune(circumstance, runescanner, lf.Rune) return readthisrune(circumstance, runescanner, lf.Rune)
} }

View File

@ -19,6 +19,6 @@ import (
// size, err := eol.ReadLS(runescanner) // size, err := eol.ReadLS(runescanner)
func ReadLS(runescanner io.RuneScanner) (size int, err error) { func ReadLS(runescanner io.RuneScanner) (size int, err error) {
const characterNumber uint64 = 1 const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(LS), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(ls.String), characterNumber)
return readthisrune(circumstance, runescanner, ls.Rune) return readthisrune(circumstance, runescanner, ls.Rune)
} }

View File

@ -19,6 +19,6 @@ import (
// size, err := eol.ReadNEL(runescanner) // size, err := eol.ReadNEL(runescanner)
func ReadNEL(runescanner io.RuneScanner) (size int, err error) { func ReadNEL(runescanner io.RuneScanner) (size int, err error) {
const characterNumber uint64 = 1 const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(NEL), characterNumber) var circumstance internalCircumstance = specifyCircumstance(opt.Something(nel.String), characterNumber)
return readthisrune(circumstance, runescanner, nel.Rune) return readthisrune(circumstance, runescanner, nel.Rune)
} }

View File

@ -2,6 +2,12 @@ package eol
import ( import (
"io" "io"
"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"
) )
// ReadThisEOL tries to read the specified end-of-line sequence. // ReadThisEOL tries to read the specified end-of-line sequence.
@ -25,15 +31,15 @@ func ReadThisEOL(runescanner io.RuneScanner, endofline string) (size int, err er
} }
switch endofline { switch endofline {
case LF: case lf.String:
return ReadLF(runescanner) return ReadLF(runescanner)
case CR: case cr.String:
return ReadCR(runescanner) return ReadCR(runescanner)
case CRLF: case crlf.String:
return ReadCRLF(runescanner) return ReadCRLF(runescanner)
case NEL: case nel.String:
return ReadNEL(runescanner) return ReadNEL(runescanner)
case LS: case ls.String:
return ReadLS(runescanner) return ReadLS(runescanner)
default: default:
return 0, errUnrecognizedEOL(endofline) return 0, errUnrecognizedEOL(endofline)

View File

@ -8,6 +8,11 @@ import (
"sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-utf8" "sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-eol/cr"
"sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/ls"
"sourcecode.social/reiver/go-eol/nel"
) )
func TestReadThisRune(t *testing.T) { func TestReadThisRune(t *testing.T) {
@ -123,28 +128,28 @@ func TestReadThisRune_fail(t *testing.T) {
{ {
Value: "", Value: "",
Rune: '\n', Rune: '\n',
EOLSequence: opt.Something(LF), EOLSequence: opt.Something(lf.String),
RuneNumber: 7, RuneNumber: 7,
ExpectedError: `eol: problem reading character №7 of end-of-line sequence "\n": EOF`, ExpectedError: `eol: problem reading character №7 of end-of-line sequence "\n": EOF`,
}, },
{ {
Value: "", Value: "",
Rune: '\r', Rune: '\r',
EOLSequence: opt.Something(CR), EOLSequence: opt.Something(cr.String),
RuneNumber: 8, RuneNumber: 8,
ExpectedError: `eol: problem reading character №8 of end-of-line sequence "\r": EOF`, ExpectedError: `eol: problem reading character №8 of end-of-line sequence "\r": EOF`,
}, },
{ {
Value: "", Value: "",
Rune: '\u0085', Rune: '\u0085',
EOLSequence: opt.Something(NEL), EOLSequence: opt.Something(nel.String),
RuneNumber: 9, RuneNumber: 9,
ExpectedError: `eol: problem reading character №9 of end-of-line sequence "\u0085": EOF`, ExpectedError: `eol: problem reading character №9 of end-of-line sequence "\u0085": EOF`,
}, },
{ {
Value: "", Value: "",
Rune: '\u2028', Rune: '\u2028',
EOLSequence: opt.Something(LS), EOLSequence: opt.Something(ls.String),
RuneNumber: 10, RuneNumber: 10,
ExpectedError: `eol: problem reading character №10 of end-of-line sequence "\u2028": EOF`, ExpectedError: `eol: problem reading character №10 of end-of-line sequence "\u2028": EOF`,
}, },
@ -164,28 +169,28 @@ func TestReadThisRune_fail(t *testing.T) {
{ {
Value: " \n", Value: " \n",
Rune: '\n', Rune: '\n',
EOLSequence: opt.Something(LF), EOLSequence: opt.Something(lf.String),
RuneNumber: 12, RuneNumber: 12,
ExpectedError: `eol: line-feed (LF) character ('\n') (U+000A) not found for end-of-line sequence "\n" character №12 — instead found ' ' (U+0020)`, ExpectedError: `eol: line-feed (LF) character ('\n') (U+000A) not found for end-of-line sequence "\n" character №12 — instead found ' ' (U+0020)`,
}, },
{ {
Value: " \r", Value: " \r",
Rune: '\r', Rune: '\r',
EOLSequence: opt.Something(CR), EOLSequence: opt.Something(cr.String),
RuneNumber: 13, RuneNumber: 13,
ExpectedError: `eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence "\r" character №13 — instead found ' ' (U+0020)`, ExpectedError: `eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence "\r" character №13 — instead found ' ' (U+0020)`,
}, },
{ {
Value: " \u0085", Value: " \u0085",
Rune: '\u0085', Rune: '\u0085',
EOLSequence: opt.Something(NEL), EOLSequence: opt.Something(nel.String),
RuneNumber: 14, RuneNumber: 14,
ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line sequence "\u0085" character №14 — instead found ' ' (U+0020)`, ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line sequence "\u0085" character №14 — instead found ' ' (U+0020)`,
}, },
{ {
Value: " \u2028", Value: " \u2028",
Rune: '\u2028', Rune: '\u2028',
EOLSequence: opt.Something(LS), EOLSequence: opt.Something(ls.String),
RuneNumber: 15, RuneNumber: 15,
ExpectedError: `eol: line-separator (LS) character (U+2028) not found for end-of-line sequence "\u2028" character №15 — instead found ' ' (U+0020)`, ExpectedError: `eol: line-separator (LS) character (U+2028) not found for end-of-line sequence "\u2028" character №15 — instead found ' ' (U+0020)`,
}, },