Compare commits

...

2 Commits

Author SHA1 Message Date
Charles Iliya Krempeaux 4bdb41c618 form-feed, etc 2024-03-25 23:24:08 -07:00
Charles Iliya Krempeaux 7cae592170 vertical-tab 2024-03-25 23:07:21 -07:00
10 changed files with 280 additions and 15 deletions

View File

@ -5,8 +5,9 @@ Package **eol** implements tools for working with end-of-line, for the Go progra
The end-of-line sequences it supports is: The end-of-line sequences it supports is:
* `"\n" // line-feed (LF)` * `"\n" // line-feed (LF)`
* `"\v" // vertical-tab (VT)`
* `"\n\r" // line-feed (LF), carriage-return (CR)` * `"\n\r" // line-feed (LF), carriage-return (CR)`
* `"\v" // vertical-tab (VT)`
* `"\f" // form-feed (FF)`
* `"\r" // carriage-return (CR)` * `"\r" // carriage-return (CR)`
* `"\r\n" // carriage-return (CR), line-feed (LF)` * `"\r\n" // carriage-return (CR), line-feed (LF)`
* `"\u0085" // next-line (NEL)` * `"\u0085" // next-line (NEL)`

3
ff/byte.go 100644
View File

@ -0,0 +1,3 @@
package ff
const Byte byte = '\u000C'

3
ff/rune.go 100644
View File

@ -0,0 +1,3 @@
package ff
const Rune rune = '\u000C'

3
ff/string.go 100644
View File

@ -0,0 +1,3 @@
package ff
const String string = string(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/ff"
"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"
@ -53,6 +54,12 @@ func (receiver internalNotFoundError) Error() string {
s = fmt.Sprintf(`eol: vertical-tab (VT) character ('\v') (U+000B) not found for end-of-line sequence %q character №%d — instead found %q (%U)`, sequence, characterNumber, actual, actual) s = fmt.Sprintf(`eol: vertical-tab (VT) character ('\v') (U+000B) not found for end-of-line sequence %q character №%d — instead found %q (%U)`, sequence, characterNumber, actual, actual)
}) })
p = append(p, s...) p = append(p, s...)
case ff.Rune:
var s string = fmt.Sprintf(`eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence character №%d — instead found %q (%U)`, characterNumber, actual, actual)
eolSequence.WhenSomething(func(sequence string){
s = fmt.Sprintf(`eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence %q character №%d — instead found %q (%U)`, sequence, characterNumber, actual, actual)
})
p = append(p, s...)
case cr.Rune: case cr.Rune:
var s string = fmt.Sprintf(`eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence character №%d — instead found %q (%U)`, characterNumber, actual, actual) var s string = fmt.Sprintf(`eol: carriage-return (CR) character ('\r') (U+000D) not found for end-of-line sequence character №%d — instead found %q (%U)`, characterNumber, actual, actual)
eolSequence.WhenSomething(func(sequence string){ eolSequence.WhenSomething(func(sequence string){

View File

@ -7,6 +7,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/ff"
"sourcecode.social/reiver/go-eol/lf" "sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/lfcr" "sourcecode.social/reiver/go-eol/lfcr"
"sourcecode.social/reiver/go-eol/ls" "sourcecode.social/reiver/go-eol/ls"
@ -19,8 +20,9 @@ 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')
// vertical-tab (VT) (U+000B) ('\v')
// line-feed, carriage-return ("\n\r") // line-feed, carriage-return ("\n\r")
// vertical-tab (VT) (U+000B) ('\v')
// vertical-tab (VT) (U+000B) ('\f')
// 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)
@ -56,6 +58,8 @@ func ReadEOL(runescanner io.RuneScanner) (endofline string, size int, err error)
// Nothing here. // Nothing here.
case vt.Rune: case vt.Rune:
return vt.String, size0, nil return vt.String, size0, nil
case ff.Rune:
return ff.String, size0, nil
case cr.Rune: case cr.Rune:
// Nothing here. // Nothing here.
case nel.Rune: case nel.Rune:

View File

@ -11,6 +11,7 @@ import (
"sourcecode.social/reiver/go-eol" "sourcecode.social/reiver/go-eol"
"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/ff"
"sourcecode.social/reiver/go-eol/lf" "sourcecode.social/reiver/go-eol/lf"
"sourcecode.social/reiver/go-eol/lfcr" "sourcecode.social/reiver/go-eol/lfcr"
"sourcecode.social/reiver/go-eol/ls" "sourcecode.social/reiver/go-eol/ls"
@ -30,15 +31,20 @@ func TestReadEOL(t *testing.T) {
ExpectedEOL: lf.String, ExpectedEOL: lf.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{
Value: "\n\r",
ExpectedEOL: lfcr.String,
ExpectedSize: 2,
},
{ {
Value: "\v", Value: "\v",
ExpectedEOL: vt.String, ExpectedEOL: vt.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\n\r", Value: "\f",
ExpectedEOL: lfcr.String, ExpectedEOL: ff.String,
ExpectedSize: 2, ExpectedSize: 1,
}, },
{ {
Value: "\r", Value: "\r",
@ -68,33 +74,38 @@ func TestReadEOL(t *testing.T) {
ExpectedEOL: lf.String, ExpectedEOL: lf.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{
Value: "\n\rapple banana cherry",
ExpectedEOL: lfcr.String,
ExpectedSize: 2,
},
{ {
Value: "\vapple banana cherry", Value: "\vapple banana cherry",
ExpectedEOL: vt.String, ExpectedEOL: vt.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\n\rapple banana cherr", Value: "\fapple banana cherry",
ExpectedEOL: lfcr.String, ExpectedEOL: ff.String,
ExpectedSize: 2, ExpectedSize: 1,
}, },
{ {
Value: "\rapple banana cherr", Value: "\rapple banana cherry",
ExpectedEOL: cr.String, ExpectedEOL: cr.String,
ExpectedSize: 1, ExpectedSize: 1,
}, },
{ {
Value: "\r\napple banana cherr", Value: "\r\napple banana cherry",
ExpectedEOL: crlf.String, ExpectedEOL: crlf.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u0085apple banana cherr", Value: "\u0085apple banana cherry",
ExpectedEOL: nel.String, ExpectedEOL: nel.String,
ExpectedSize: 2, ExpectedSize: 2,
}, },
{ {
Value: "\u2028apple banana cherr", Value: "\u2028apple banana cherry",
ExpectedEOL: ls.String, ExpectedEOL: ls.String,
ExpectedSize: 3, ExpectedSize: 3,
}, },

24
readff.go 100644
View File

@ -0,0 +1,24 @@
package eol
import (
"io"
"sourcecode.social/reiver/go-opt"
"sourcecode.social/reiver/go-eol/ff"
)
// ReadFF tries to read the "\f" (i.e., form-feed) end-of-line sequence.
//
// If successful, it returns the number-of-bytes read (to read in end-of-line sequence "\f").
//
// If the character read is not a '\f', then ReadFF will try to unread the character.
//
// Example usage:
//
// size, err := eol.ReadFF(runescanner)
func ReadFF(runescanner io.RuneScanner) (size int, err error) {
const characterNumber uint64 = 1
var circumstance internalCircumstance = specifyCircumstance(opt.Something(ff.String), characterNumber)
return readthisrune(circumstance, runescanner, ff.Rune)
}

209
readff_test.go 100644
View File

@ -0,0 +1,209 @@
package eol_test
import (
"testing"
"io"
"strings"
"sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-eol"
)
func TestReadFF(t *testing.T) {
tests := []struct{
Value string
ExpectedSize int
}{
{
Value: "\f",
ExpectedSize: 1,
},
{
Value: "\fapple banana cherry",
ExpectedSize: 1,
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualSize, err := eol.ReadFF(runescanner)
if nil != err {
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("VALUE: %q", test.Value)
continue
}
{
expected := test.ExpectedSize
actual := actualSize
if expected != actual {
t.Errorf("For test #%d, the actual size is not what was expected.", testNumber)
t.Logf("EXPECTED: %d", expected)
t.Logf("ACTUAL: %d", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
}
}
func TestReadFF_fail(t *testing.T) {
tests := []struct{
Value string
ExpectedError string
}{
{
Value: "",
ExpectedError: `eol: problem reading character №1 of end-of-line sequence "\f": EOF`,
},
{
Value: "\n",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\n' (U+000A)`,
},
{
Value: "\u0085",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\u0085' (U+0085)`,
},
{
Value: "\u2028",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\u2028' (U+2028)`,
},
{
Value: "😈",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '😈' (U+1F608)`,
},
{
Value: "\napple banana cherry",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\n' (U+000A)`,
},
{
Value: "\u0085apple banana cherry",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\u0085' (U+0085)`,
},
{
Value: "\u2028apple banana cherry",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '\u2028' (U+2028)`,
},
{
Value: "😈apple banana cherry",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '😈' (U+1F608)`,
},
{
Value: " \n",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found ' ' (U+0020)`,
},
{
Value: " \r",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found ' ' (U+0020)`,
},
{
Value: " \u0085",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found ' ' (U+0020)`,
},
{
Value: " \u2028",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found ' ' (U+0020)`,
},
{
Value: " 😈",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found ' ' (U+0020)`,
},
{
Value: ".\n",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '.' (U+002E)`,
},
{
Value: ".\r",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '.' (U+002E)`,
},
{
Value: ".\u0085",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '.' (U+002E)`,
},
{
Value: ".\u2028",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '.' (U+002E)`,
},
{
Value: ".😈",
ExpectedError: `eol: form-feed (FF) character ('\f') (U+000C) not found for end-of-line sequence "\f" character №1 — instead found '.' (U+002E)`,
},
}
for testNumber, test := range tests {
var reader io.Reader = strings.NewReader(test.Value)
var runescanner io.RuneScanner = utf8.NewRuneScanner(reader)
actualSize, err := eol.ReadFF(runescanner)
if nil == err {
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
t.Logf("EXPECTED-ERROR: %q", test.ExpectedError)
t.Logf("VALUE: %q", test.Value)
continue
}
{
expected := test.ExpectedError
actual := err.Error()
if expected != actual {
t.Errorf("For test #%d, the actual error is not what was expected.", testNumber)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
{
expected := 0
actual := actualSize
if expected != actual {
t.Errorf("For test #%d, the actual size is not what was expected.", testNumber)
t.Logf("EXPECTED: %d", expected)
t.Logf("ACTUAL: %d", actual)
t.Logf("VALUE: %q", test.Value)
continue
}
}
}
}

View File

@ -8,11 +8,11 @@ import (
"sourcecode.social/reiver/go-eol/vt" "sourcecode.social/reiver/go-eol/vt"
) )
// ReadVT tries to read the "\r" (i.e., carriage-return) end-of-line sequence. // ReadVT tries to read the "\v" (i.e., carriage-return) end-of-line sequence.
// //
// If successful, it returns the number-of-bytes read (to read in end-of-line sequence "\r"). // If successful, it returns the number-of-bytes read (to read in end-of-line sequence "\v").
// //
// If the character read is not a '\r', then ReadVT will try to unread the character. // If the character read is not a '\v', then ReadVT will try to unread the character.
// //
// Example usage: // Example usage:
// //