From 43b24cd7893338643e05c6c8d607e4a68251fd4d Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 18 Feb 2024 09:27:47 -0800 Subject: [PATCH] refactor constants --- consts.go | 16 ---------------- readcr.go | 2 +- readcrlf.go | 5 +++-- readeol.go | 13 +++++++------ readeol_test.go | 25 +++++++++++++++---------- readlf.go | 2 +- readls.go | 2 +- readnel.go | 2 +- readthiseol.go | 16 +++++++++++----- readthisrune_test.go | 21 +++++++++++++-------- 10 files changed, 53 insertions(+), 51 deletions(-) delete mode 100644 consts.go diff --git a/consts.go b/consts.go deleted file mode 100644 index 9d8d375..0000000 --- a/consts.go +++ /dev/null @@ -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) -) diff --git a/readcr.go b/readcr.go index e160713..c61cc0c 100644 --- a/readcr.go +++ b/readcr.go @@ -19,6 +19,6 @@ import ( // size, err := eol.ReadCR(runescanner) func ReadCR(runescanner io.RuneScanner) (size int, err error) { 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) } diff --git a/readcrlf.go b/readcrlf.go index 1d09cf7..73f30db 100644 --- a/readcrlf.go +++ b/readcrlf.go @@ -6,6 +6,7 @@ import ( "sourcecode.social/reiver/go-opt" "sourcecode.social/reiver/go-eol/cr" + "sourcecode.social/reiver/go-eol/crlf" "sourcecode.social/reiver/go-eol/lf" ) @@ -26,7 +27,7 @@ func ReadCRLF(runescanner io.RuneScanner) (size int, err error) { var err error 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) if nil != err { return size0, err @@ -38,7 +39,7 @@ func ReadCRLF(runescanner io.RuneScanner) (size int, err error) { var err error 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) if nil != err { return size1+size0, err diff --git a/readeol.go b/readeol.go index a8fbd40..19348cf 100644 --- a/readeol.go +++ b/readeol.go @@ -6,6 +6,7 @@ import ( "sourcecode.social/reiver/go-opt" "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" @@ -48,13 +49,13 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error) switch r0 { case lf.Rune: - return LF, size0, nil + return lf.String, size0, nil case cr.Rune: // Nothing here. case nel.Rune: - return NEL, size0, nil + return nel.String, size0, nil case ls.Rune: - return LS, size0, nil + return ls.String, size0, nil default: err := runescanner.UnreadRune() if nil != err { @@ -77,7 +78,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error) r1, size1, err = runescanner.ReadRune() if io.EOF == err { - return CR, size0, nil + return cr.String, size0, nil } if nil != err { const characterNumber uint64 = 2 @@ -90,7 +91,7 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error) switch r1 { case lf.Rune: - return CRLF, size1+size0, nil + return crlf.String, size1+size0, nil default: err := runescanner.UnreadRune() 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 CR, size0, nil + return cr.String, size0, nil } } diff --git a/readeol_test.go b/readeol_test.go index e874550..3aa680b 100644 --- a/readeol_test.go +++ b/readeol_test.go @@ -9,6 +9,11 @@ import ( "sourcecode.social/reiver/go-utf8" "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) { @@ -20,27 +25,27 @@ func TestReadEOL(t *testing.T) { }{ { Value: "\n", - ExpectedEOL: eol.LF, + ExpectedEOL: lf.String, ExpectedSize: 1, }, { Value: "\r", - ExpectedEOL: eol.CR, + ExpectedEOL: cr.String, ExpectedSize: 1, }, { Value: "\r\n", - ExpectedEOL: eol.CRLF, + ExpectedEOL: crlf.String, ExpectedSize: 2, }, { Value: "\u0085", - ExpectedEOL: eol.NEL, + ExpectedEOL: nel.String, ExpectedSize: 2, }, { Value: "\u2028", - ExpectedEOL: eol.LS, + ExpectedEOL: ls.String, ExpectedSize: 3, }, @@ -48,27 +53,27 @@ func TestReadEOL(t *testing.T) { { Value: "\napple banana cherry", - ExpectedEOL: eol.LF, + ExpectedEOL: lf.String, ExpectedSize: 1, }, { Value: "\rapple banana cherr", - ExpectedEOL: eol.CR, + ExpectedEOL: cr.String, ExpectedSize: 1, }, { Value: "\r\napple banana cherr", - ExpectedEOL: eol.CRLF, + ExpectedEOL: crlf.String, ExpectedSize: 2, }, { Value: "\u0085apple banana cherr", - ExpectedEOL: eol.NEL, + ExpectedEOL: nel.String, ExpectedSize: 2, }, { Value: "\u2028apple banana cherr", - ExpectedEOL: eol.LS, + ExpectedEOL: ls.String, ExpectedSize: 3, }, } diff --git a/readlf.go b/readlf.go index 4cdb3a2..2a0f8ba 100644 --- a/readlf.go +++ b/readlf.go @@ -19,6 +19,6 @@ import ( // size, err := eol.ReadLF(runescanner) func ReadLF(runescanner io.RuneScanner) (size int, err error) { 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) } diff --git a/readls.go b/readls.go index ad6d462..e8a48fe 100644 --- a/readls.go +++ b/readls.go @@ -19,6 +19,6 @@ import ( // size, err := eol.ReadLS(runescanner) func ReadLS(runescanner io.RuneScanner) (size int, err error) { 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) } diff --git a/readnel.go b/readnel.go index 0791a6e..f563848 100644 --- a/readnel.go +++ b/readnel.go @@ -19,6 +19,6 @@ import ( // size, err := eol.ReadNEL(runescanner) func ReadNEL(runescanner io.RuneScanner) (size int, err error) { 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) } diff --git a/readthiseol.go b/readthiseol.go index a271c42..0ad39fd 100644 --- a/readthiseol.go +++ b/readthiseol.go @@ -2,6 +2,12 @@ package eol import ( "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. @@ -25,15 +31,15 @@ func ReadThisEOL(runescanner io.RuneScanner, endofline string) (size int, err er } switch endofline { - case LF: + case lf.String: return ReadLF(runescanner) - case CR: + case cr.String: return ReadCR(runescanner) - case CRLF: + case crlf.String: return ReadCRLF(runescanner) - case NEL: + case nel.String: return ReadNEL(runescanner) - case LS: + case ls.String: return ReadLS(runescanner) default: return 0, errUnrecognizedEOL(endofline) diff --git a/readthisrune_test.go b/readthisrune_test.go index bc5de11..42d4433 100644 --- a/readthisrune_test.go +++ b/readthisrune_test.go @@ -8,6 +8,11 @@ import ( "sourcecode.social/reiver/go-opt" "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) { @@ -123,28 +128,28 @@ func TestReadThisRune_fail(t *testing.T) { { Value: "", Rune: '\n', - EOLSequence: opt.Something(LF), + EOLSequence: opt.Something(lf.String), RuneNumber: 7, ExpectedError: `eol: problem reading character №7 of end-of-line sequence "\n": EOF`, }, { Value: "", Rune: '\r', - EOLSequence: opt.Something(CR), + EOLSequence: opt.Something(cr.String), RuneNumber: 8, ExpectedError: `eol: problem reading character №8 of end-of-line sequence "\r": EOF`, }, { Value: "", Rune: '\u0085', - EOLSequence: opt.Something(NEL), + EOLSequence: opt.Something(nel.String), RuneNumber: 9, ExpectedError: `eol: problem reading character №9 of end-of-line sequence "\u0085": EOF`, }, { Value: "", Rune: '\u2028', - EOLSequence: opt.Something(LS), + EOLSequence: opt.Something(ls.String), RuneNumber: 10, 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", Rune: '\n', - EOLSequence: opt.Something(LF), + EOLSequence: opt.Something(lf.String), 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)`, }, { Value: " \r", Rune: '\r', - EOLSequence: opt.Something(CR), + EOLSequence: opt.Something(cr.String), 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)`, }, { Value: " \u0085", Rune: '\u0085', - EOLSequence: opt.Something(NEL), + EOLSequence: opt.Something(nel.String), RuneNumber: 14, ExpectedError: `eol: next-line (NEL) character (U+0085) not found for end-of-line sequence "\u0085" character №14 — instead found ' ' (U+0020)`, }, { Value: " \u2028", Rune: '\u2028', - EOLSequence: opt.Something(LS), + EOLSequence: opt.Something(ls.String), RuneNumber: 15, ExpectedError: `eol: line-separator (LS) character (U+2028) not found for end-of-line sequence "\u2028" character №15 — instead found ' ' (U+0020)`, },