2018-11-02 12:12:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-17 13:41:38 +00:00
|
|
|
"flag"
|
2018-11-02 12:12:31 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/lib/encoder"
|
2018-11-02 12:12:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
edgeLeft = iota
|
|
|
|
edgeRight
|
|
|
|
)
|
|
|
|
|
|
|
|
type mapping struct {
|
2020-01-14 21:22:02 +00:00
|
|
|
mask encoder.MultiEncoder
|
2018-11-02 12:12:31 +00:00
|
|
|
src, dst []rune
|
|
|
|
}
|
|
|
|
type stringPair struct {
|
|
|
|
a, b string
|
|
|
|
}
|
|
|
|
|
|
|
|
const header = `// Code generated by ./internal/gen/main.go. DO NOT EDIT.
|
|
|
|
|
|
|
|
` + `//go:generate go run ./internal/gen/main.go
|
|
|
|
|
|
|
|
package encoder
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var maskBits = []struct {
|
2020-01-14 21:22:02 +00:00
|
|
|
mask encoder.MultiEncoder
|
2018-11-02 12:12:31 +00:00
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{encoder.EncodeZero, "EncodeZero"},
|
|
|
|
{encoder.EncodeSlash, "EncodeSlash"},
|
2019-09-27 16:44:23 +00:00
|
|
|
{encoder.EncodeSingleQuote, "EncodeSingleQuote"},
|
|
|
|
{encoder.EncodeBackQuote, "EncodeBackQuote"},
|
|
|
|
{encoder.EncodeLtGt, "EncodeLtGt"},
|
2021-06-23 14:05:39 +00:00
|
|
|
{encoder.EncodeSquareBracket, "EncodeSquareBracket"},
|
2019-09-27 16:44:23 +00:00
|
|
|
{encoder.EncodeDollar, "EncodeDollar"},
|
|
|
|
{encoder.EncodeDoubleQuote, "EncodeDoubleQuote"},
|
|
|
|
{encoder.EncodeColon, "EncodeColon"},
|
|
|
|
{encoder.EncodeQuestion, "EncodeQuestion"},
|
|
|
|
{encoder.EncodeAsterisk, "EncodeAsterisk"},
|
|
|
|
{encoder.EncodePipe, "EncodePipe"},
|
|
|
|
{encoder.EncodeHash, "EncodeHash"},
|
|
|
|
{encoder.EncodePercent, "EncodePercent"},
|
2018-11-02 12:12:31 +00:00
|
|
|
{encoder.EncodeBackSlash, "EncodeBackSlash"},
|
2019-09-20 22:04:31 +00:00
|
|
|
{encoder.EncodeCrLf, "EncodeCrLf"},
|
2018-11-02 12:12:31 +00:00
|
|
|
{encoder.EncodeDel, "EncodeDel"},
|
|
|
|
{encoder.EncodeCtl, "EncodeCtl"},
|
|
|
|
{encoder.EncodeLeftSpace, "EncodeLeftSpace"},
|
2019-09-30 15:41:48 +00:00
|
|
|
{encoder.EncodeLeftPeriod, "EncodeLeftPeriod"},
|
2018-11-02 12:12:31 +00:00
|
|
|
{encoder.EncodeLeftTilde, "EncodeLeftTilde"},
|
2019-05-17 13:41:38 +00:00
|
|
|
{encoder.EncodeLeftCrLfHtVt, "EncodeLeftCrLfHtVt"},
|
2018-11-02 12:12:31 +00:00
|
|
|
{encoder.EncodeRightSpace, "EncodeRightSpace"},
|
|
|
|
{encoder.EncodeRightPeriod, "EncodeRightPeriod"},
|
2019-05-17 13:41:38 +00:00
|
|
|
{encoder.EncodeRightCrLfHtVt, "EncodeRightCrLfHtVt"},
|
2018-11-02 12:12:31 +00:00
|
|
|
{encoder.EncodeInvalidUtf8, "EncodeInvalidUtf8"},
|
2019-09-27 16:44:23 +00:00
|
|
|
{encoder.EncodeDot, "EncodeDot"},
|
2018-11-02 12:12:31 +00:00
|
|
|
}
|
2019-05-17 13:41:38 +00:00
|
|
|
|
|
|
|
type edge struct {
|
2020-01-14 21:22:02 +00:00
|
|
|
mask encoder.MultiEncoder
|
2018-11-02 12:12:31 +00:00
|
|
|
name string
|
|
|
|
edge int
|
2019-05-17 13:41:38 +00:00
|
|
|
orig []rune
|
|
|
|
replace []rune
|
|
|
|
}
|
|
|
|
|
|
|
|
var allEdges = []edge{
|
|
|
|
{encoder.EncodeLeftSpace, "EncodeLeftSpace", edgeLeft, []rune{' '}, []rune{'␠'}},
|
2019-09-30 15:41:48 +00:00
|
|
|
{encoder.EncodeLeftPeriod, "EncodeLeftPeriod", edgeLeft, []rune{'.'}, []rune{'.'}},
|
2019-05-17 13:41:38 +00:00
|
|
|
{encoder.EncodeLeftTilde, "EncodeLeftTilde", edgeLeft, []rune{'~'}, []rune{'~'}},
|
|
|
|
{encoder.EncodeLeftCrLfHtVt, "EncodeLeftCrLfHtVt", edgeLeft,
|
|
|
|
[]rune{'\t', '\n', '\v', '\r'},
|
|
|
|
[]rune{'␀' + '\t', '␀' + '\n', '␀' + '\v', '␀' + '\r'},
|
|
|
|
},
|
|
|
|
{encoder.EncodeRightSpace, "EncodeRightSpace", edgeRight, []rune{' '}, []rune{'␠'}},
|
|
|
|
{encoder.EncodeRightPeriod, "EncodeRightPeriod", edgeRight, []rune{'.'}, []rune{'.'}},
|
|
|
|
{encoder.EncodeRightCrLfHtVt, "EncodeRightCrLfHtVt", edgeRight,
|
|
|
|
[]rune{'\t', '\n', '\v', '\r'},
|
|
|
|
[]rune{'␀' + '\t', '␀' + '\n', '␀' + '\v', '␀' + '\r'},
|
|
|
|
},
|
2018-11-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var allMappings = []mapping{{
|
|
|
|
encoder.EncodeZero, []rune{
|
|
|
|
0,
|
|
|
|
}, []rune{
|
|
|
|
'␀',
|
|
|
|
}}, {
|
2019-09-27 16:44:23 +00:00
|
|
|
encoder.EncodeSlash, []rune{
|
|
|
|
'/',
|
|
|
|
}, []rune{
|
|
|
|
'/',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeLtGt, []rune{
|
|
|
|
'<', '>',
|
|
|
|
}, []rune{
|
|
|
|
'<', '>',
|
|
|
|
}}, {
|
2021-06-23 14:05:39 +00:00
|
|
|
encoder.EncodeSquareBracket, []rune{
|
|
|
|
'[', ']',
|
|
|
|
}, []rune{
|
|
|
|
'[', ']',
|
|
|
|
}}, {
|
2019-09-27 16:44:23 +00:00
|
|
|
encoder.EncodeDoubleQuote, []rune{
|
|
|
|
'"',
|
2018-11-02 12:12:31 +00:00
|
|
|
}, []rune{
|
2019-09-27 16:44:23 +00:00
|
|
|
'"',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeSingleQuote, []rune{
|
|
|
|
'\'',
|
|
|
|
}, []rune{
|
|
|
|
''',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeBackQuote, []rune{
|
|
|
|
'`',
|
|
|
|
}, []rune{
|
|
|
|
'`',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeDollar, []rune{
|
|
|
|
'$',
|
|
|
|
}, []rune{
|
|
|
|
'$',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeColon, []rune{
|
|
|
|
':',
|
|
|
|
}, []rune{
|
|
|
|
':',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeQuestion, []rune{
|
|
|
|
'?',
|
|
|
|
}, []rune{
|
|
|
|
'?',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeAsterisk, []rune{
|
|
|
|
'*',
|
|
|
|
}, []rune{
|
|
|
|
'*',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodePipe, []rune{
|
|
|
|
'|',
|
|
|
|
}, []rune{
|
|
|
|
'|',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeHash, []rune{
|
|
|
|
'#',
|
|
|
|
}, []rune{
|
|
|
|
'#',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodePercent, []rune{
|
|
|
|
'%',
|
|
|
|
}, []rune{
|
|
|
|
'%',
|
2018-11-02 12:12:31 +00:00
|
|
|
}}, {
|
|
|
|
encoder.EncodeSlash, []rune{
|
|
|
|
'/',
|
|
|
|
}, []rune{
|
|
|
|
'/',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeBackSlash, []rune{
|
|
|
|
'\\',
|
|
|
|
}, []rune{
|
|
|
|
'\',
|
|
|
|
}}, {
|
2019-09-20 22:04:31 +00:00
|
|
|
encoder.EncodeCrLf, []rune{
|
|
|
|
rune(0x0D), rune(0x0A),
|
|
|
|
}, []rune{
|
|
|
|
'␍', '␊',
|
|
|
|
}}, {
|
2018-11-02 12:12:31 +00:00
|
|
|
encoder.EncodeDel, []rune{
|
|
|
|
0x7F,
|
|
|
|
}, []rune{
|
|
|
|
'␡',
|
|
|
|
}}, {
|
|
|
|
encoder.EncodeCtl,
|
|
|
|
runeRange(0x01, 0x1F),
|
|
|
|
runeRange('␁', '␟'),
|
|
|
|
}}
|
|
|
|
|
|
|
|
var (
|
2019-05-17 13:41:38 +00:00
|
|
|
rng *rand.Rand
|
2018-11-02 12:12:31 +00:00
|
|
|
|
|
|
|
printables = runeRange(0x20, 0x7E)
|
|
|
|
fullwidthPrintables = runeRange(0xFF00, 0xFF5E)
|
|
|
|
encodables = collectEncodables(allMappings)
|
|
|
|
encoded = collectEncoded(allMappings)
|
|
|
|
greek = runeRange(0x03B1, 0x03C9)
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-05-17 13:41:38 +00:00
|
|
|
seed := flag.Int64("s", 42, "random seed")
|
|
|
|
flag.Parse()
|
|
|
|
rng = rand.New(rand.NewSource(*seed))
|
|
|
|
|
2018-11-02 12:12:31 +00:00
|
|
|
fd, err := os.Create("encoder_cases_test.go")
|
|
|
|
fatal(err, "Unable to open encoder_cases_test.go:")
|
|
|
|
defer func() {
|
|
|
|
fatal(fd.Close(), "Failed to close encoder_cases_test.go:")
|
|
|
|
}()
|
|
|
|
fatalW(fd.WriteString(header))("Failed to write header:")
|
|
|
|
|
|
|
|
fatalW(fd.WriteString("var testCasesSingle = []testCase{\n\t"))("Write:")
|
|
|
|
_i := 0
|
|
|
|
i := func() (r int) {
|
|
|
|
r, _i = _i, _i+1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, m := range maskBits {
|
|
|
|
if len(getMapping(m.mask).src) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _i != 0 {
|
|
|
|
fatalW(fd.WriteString(" "))("Write:")
|
|
|
|
}
|
|
|
|
in, out := buildTestString(
|
|
|
|
[]mapping{getMapping(m.mask)}, // pick
|
|
|
|
[]mapping{getMapping(0)}, // quote
|
|
|
|
printables, fullwidthPrintables, encodables, encoded, greek) // fill
|
|
|
|
fatalW(fmt.Fprintf(fd, `{ // %d
|
|
|
|
mask: %s,
|
|
|
|
in: %s,
|
|
|
|
out: %s,
|
|
|
|
},`, i(), m.name, strconv.Quote(in), strconv.Quote(out)))("Error writing test case:")
|
|
|
|
}
|
|
|
|
fatalW(fd.WriteString(`
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCasesSingleEdge = []testCase{
|
|
|
|
`))("Write:")
|
|
|
|
_i = 0
|
2019-05-17 13:41:38 +00:00
|
|
|
for _, e := range allEdges {
|
|
|
|
for idx, orig := range e.orig {
|
|
|
|
if _i != 0 {
|
|
|
|
fatalW(fd.WriteString(" "))("Write:")
|
|
|
|
}
|
|
|
|
fatalW(fmt.Fprintf(fd, `{ // %d
|
2018-11-02 12:12:31 +00:00
|
|
|
mask: %s,
|
|
|
|
in: %s,
|
|
|
|
out: %s,
|
2019-05-17 13:41:38 +00:00
|
|
|
},`, i(), e.name, strconv.Quote(string(orig)), strconv.Quote(string(e.replace[idx]))))("Error writing test case:")
|
|
|
|
}
|
2018-11-02 12:12:31 +00:00
|
|
|
for _, m := range maskBits {
|
2019-05-17 13:41:38 +00:00
|
|
|
if len(getMapping(m.mask).src) == 0 || invalidMask(e.mask|m.mask) {
|
2018-11-02 12:12:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-05-17 13:41:38 +00:00
|
|
|
for idx, orig := range e.orig {
|
|
|
|
replace := e.replace[idx]
|
|
|
|
pairs := buildEdgeTestString(
|
|
|
|
[]edge{e}, []mapping{getMapping(0), getMapping(m.mask)}, // quote
|
|
|
|
[][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill
|
|
|
|
func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) {
|
|
|
|
testL := len(rIn)
|
|
|
|
skipOrig := false
|
|
|
|
for _, m := range testMappings {
|
|
|
|
if runePos(orig, m.src) != -1 || runePos(orig, m.dst) != -1 {
|
|
|
|
skipOrig = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !skipOrig {
|
|
|
|
rIn[10], rOut[10], quoteOut[10] = orig, orig, false
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, stringPair{string(rIn), quotedToString(rOut, quoteOut)})
|
|
|
|
for _, i := range []int{0, 1, testL - 2, testL - 1} {
|
|
|
|
for _, j := range []int{1, testL - 2, testL - 1} {
|
|
|
|
if j < i {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rIn := append([]rune{}, rIn...)
|
|
|
|
rOut := append([]rune{}, rOut...)
|
|
|
|
quoteOut := append([]bool{}, quoteOut...)
|
|
|
|
|
|
|
|
for _, in := range []rune{orig, replace} {
|
|
|
|
expect, quote := in, false
|
|
|
|
if i == 0 && e.edge == edgeLeft ||
|
|
|
|
i == testL-1 && e.edge == edgeRight {
|
|
|
|
expect, quote = replace, in == replace
|
|
|
|
}
|
|
|
|
rIn[i], rOut[i], quoteOut[i] = in, expect, quote
|
|
|
|
|
|
|
|
if i != j {
|
|
|
|
for _, in := range []rune{orig, replace} {
|
|
|
|
expect, quote = in, false
|
|
|
|
if j == testL-1 && e.edge == edgeRight {
|
|
|
|
expect, quote = replace, in == replace
|
|
|
|
}
|
|
|
|
rIn[j], rOut[j], quoteOut[j] = in, expect, quote
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = append(out, stringPair{string(rIn), quotedToString(rOut, quoteOut)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
for _, p := range pairs {
|
|
|
|
fatalW(fmt.Fprintf(fd, ` { // %d
|
2018-11-02 12:12:31 +00:00
|
|
|
mask: %s | %s,
|
|
|
|
in: %s,
|
|
|
|
out: %s,
|
|
|
|
},`, i(), m.name, e.name, strconv.Quote(p.a), strconv.Quote(p.b)))("Error writing test case:")
|
2019-05-17 13:41:38 +00:00
|
|
|
}
|
2018-11-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fatalW(fmt.Fprintf(fd, ` { // %d
|
|
|
|
mask: EncodeLeftSpace,
|
|
|
|
in: " ",
|
|
|
|
out: "␠ ",
|
2019-09-30 15:41:48 +00:00
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftPeriod,
|
|
|
|
in: "..",
|
|
|
|
out: "..",
|
2018-11-02 12:12:31 +00:00
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftTilde,
|
|
|
|
in: "~~",
|
|
|
|
out: "~~",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeRightSpace,
|
|
|
|
in: " ",
|
|
|
|
out: " ␠",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeRightPeriod,
|
|
|
|
in: "..",
|
|
|
|
out: "..",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftSpace | EncodeRightPeriod,
|
|
|
|
in: " .",
|
|
|
|
out: "␠.",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftSpace | EncodeRightSpace,
|
|
|
|
in: " ",
|
|
|
|
out: "␠",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftSpace | EncodeRightSpace,
|
|
|
|
in: " ",
|
|
|
|
out: "␠␠",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftSpace | EncodeRightSpace,
|
|
|
|
in: " ",
|
|
|
|
out: "␠ ␠",
|
2019-09-30 15:41:48 +00:00
|
|
|
}, { // %d
|
|
|
|
mask: EncodeLeftPeriod | EncodeRightPeriod,
|
|
|
|
in: "...",
|
|
|
|
out: "...",
|
2019-05-17 13:41:38 +00:00
|
|
|
}, { // %d
|
|
|
|
mask: EncodeRightPeriod | EncodeRightSpace,
|
|
|
|
in: "a. ",
|
|
|
|
out: "a.␠",
|
|
|
|
}, { // %d
|
|
|
|
mask: EncodeRightPeriod | EncodeRightSpace,
|
|
|
|
in: "a .",
|
|
|
|
out: "a .",
|
2018-11-02 12:12:31 +00:00
|
|
|
},
|
|
|
|
}
|
2019-05-17 13:41:38 +00:00
|
|
|
|
|
|
|
var testCasesDoubleEdge = []testCase{
|
2019-09-30 15:41:48 +00:00
|
|
|
`, i(), i(), i(), i(), i(), i(), i(), i(), i(), i(), i(), i()))("Error writing test case:")
|
2019-05-17 13:41:38 +00:00
|
|
|
_i = 0
|
|
|
|
for _, e1 := range allEdges {
|
|
|
|
for _, e2 := range allEdges {
|
|
|
|
if e1.mask == e2.mask {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, m := range maskBits {
|
|
|
|
if len(getMapping(m.mask).src) == 0 || invalidMask(m.mask|e1.mask|e2.mask) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
orig, replace := e1.orig[0], e1.replace[0]
|
|
|
|
edges := []edge{e1, e2}
|
|
|
|
pairs := buildEdgeTestString(
|
|
|
|
edges, []mapping{getMapping(0), getMapping(m.mask)}, // quote
|
|
|
|
[][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill
|
|
|
|
func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) {
|
|
|
|
testL := len(rIn)
|
|
|
|
for _, i := range []int{0, testL - 1} {
|
|
|
|
for _, secondOrig := range e2.orig {
|
|
|
|
rIn := append([]rune{}, rIn...)
|
|
|
|
rOut := append([]rune{}, rOut...)
|
|
|
|
quoteOut := append([]bool{}, quoteOut...)
|
|
|
|
|
|
|
|
rIn[1], rOut[1], quoteOut[1] = secondOrig, secondOrig, false
|
|
|
|
rIn[testL-2], rOut[testL-2], quoteOut[testL-2] = secondOrig, secondOrig, false
|
|
|
|
|
|
|
|
for _, in := range []rune{orig, replace} {
|
|
|
|
rIn[i], rOut[i], quoteOut[i] = in, in, false
|
|
|
|
fixEdges(rIn, rOut, quoteOut, edges)
|
|
|
|
|
|
|
|
out = append(out, stringPair{string(rIn), quotedToString(rOut, quoteOut)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, p := range pairs {
|
|
|
|
if _i != 0 {
|
|
|
|
fatalW(fd.WriteString(" "))("Write:")
|
|
|
|
}
|
|
|
|
fatalW(fmt.Fprintf(fd, `{ // %d
|
|
|
|
mask: %s | %s | %s,
|
|
|
|
in: %s,
|
|
|
|
out: %s,
|
|
|
|
},`, i(), m.name, e1.name, e2.name, strconv.Quote(p.a), strconv.Quote(p.b)))("Error writing test case:")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fatalW(fmt.Fprint(fd, "\n}\n"))("Error writing test case:")
|
2018-11-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fatal(err error, s ...interface{}) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(append(s, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func fatalW(_ int, err error) func(...interface{}) {
|
|
|
|
if err != nil {
|
|
|
|
return func(s ...interface{}) {
|
|
|
|
log.Fatalln(append(s, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(s ...interface{}) {}
|
|
|
|
}
|
|
|
|
|
2020-01-14 21:22:02 +00:00
|
|
|
func invalidMask(mask encoder.MultiEncoder) bool {
|
2019-09-20 22:04:31 +00:00
|
|
|
return mask&(encoder.EncodeCtl|encoder.EncodeCrLf) != 0 && mask&(encoder.EncodeLeftCrLfHtVt|encoder.EncodeRightCrLfHtVt) != 0
|
2019-05-17 13:41:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 12:12:31 +00:00
|
|
|
// construct a slice containing the runes between (l)ow (inclusive) and (h)igh (inclusive)
|
|
|
|
func runeRange(l, h rune) []rune {
|
|
|
|
if h < l {
|
|
|
|
panic("invalid range")
|
|
|
|
}
|
|
|
|
out := make([]rune, h-l+1)
|
|
|
|
for i := range out {
|
|
|
|
out[i] = l + rune(i)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-01-14 21:22:02 +00:00
|
|
|
func getMapping(mask encoder.MultiEncoder) mapping {
|
2018-11-02 12:12:31 +00:00
|
|
|
for _, m := range allMappings {
|
|
|
|
if m.mask == mask {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mapping{}
|
|
|
|
}
|
|
|
|
func collectEncodables(m []mapping) (out []rune) {
|
|
|
|
for _, s := range m {
|
|
|
|
for _, r := range s.src {
|
|
|
|
out = append(out, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func collectEncoded(m []mapping) (out []rune) {
|
|
|
|
for _, s := range m {
|
|
|
|
for _, r := range s.dst {
|
|
|
|
out = append(out, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildTestString(mappings, testMappings []mapping, fill ...[]rune) (string, string) {
|
|
|
|
combinedMappings := append(mappings, testMappings...)
|
|
|
|
var (
|
|
|
|
rIn []rune
|
|
|
|
rOut []rune
|
|
|
|
)
|
|
|
|
for _, m := range mappings {
|
|
|
|
if len(m.src) == 0 || len(m.src) != len(m.dst) {
|
|
|
|
panic("invalid length")
|
|
|
|
}
|
|
|
|
rIn = append(rIn, m.src...)
|
|
|
|
rOut = append(rOut, m.dst...)
|
|
|
|
}
|
|
|
|
inL := len(rIn)
|
|
|
|
testL := inL * 3
|
|
|
|
if testL < 30 {
|
|
|
|
testL = 30
|
|
|
|
}
|
|
|
|
rIn = append(rIn, make([]rune, testL-inL)...)
|
|
|
|
rOut = append(rOut, make([]rune, testL-inL)...)
|
|
|
|
quoteOut := make([]bool, testL)
|
|
|
|
set := func(i int, in, out rune, quote bool) {
|
|
|
|
rIn[i] = in
|
|
|
|
rOut[i] = out
|
|
|
|
quoteOut[i] = quote
|
|
|
|
}
|
|
|
|
for i, r := range rOut[:inL] {
|
|
|
|
set(inL+i, r, r, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for pos := inL * 2; pos < testL; pos++ {
|
|
|
|
m := pos % len(fill)
|
|
|
|
i := rng.Intn(len(fill[m]))
|
|
|
|
r := fill[m][i]
|
|
|
|
for _, m := range combinedMappings {
|
|
|
|
if pSrc := runePos(r, m.src); pSrc != -1 {
|
|
|
|
set(pos, r, m.dst[pSrc], false)
|
|
|
|
continue outer
|
|
|
|
} else if pDst := runePos(r, m.dst); pDst != -1 {
|
|
|
|
set(pos, r, r, true)
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set(pos, r, r, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
rng.Shuffle(testL, func(i, j int) {
|
|
|
|
rIn[i], rIn[j] = rIn[j], rIn[i]
|
|
|
|
rOut[i], rOut[j] = rOut[j], rOut[i]
|
|
|
|
quoteOut[i], quoteOut[j] = quoteOut[j], quoteOut[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
var bOut strings.Builder
|
|
|
|
bOut.Grow(testL)
|
|
|
|
for i, r := range rOut {
|
|
|
|
if quoteOut[i] {
|
|
|
|
bOut.WriteRune(encoder.QuoteRune)
|
|
|
|
}
|
|
|
|
bOut.WriteRune(r)
|
|
|
|
}
|
|
|
|
return string(rIn), bOut.String()
|
|
|
|
}
|
|
|
|
|
2019-05-17 13:41:38 +00:00
|
|
|
func buildEdgeTestString(edges []edge, testMappings []mapping, fill [][]rune,
|
|
|
|
gen func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) []stringPair,
|
|
|
|
) []stringPair {
|
2018-11-02 12:12:31 +00:00
|
|
|
testL := 30
|
2019-05-17 13:41:38 +00:00
|
|
|
rIn := make([]rune, testL) // test input string
|
|
|
|
rOut := make([]rune, testL) // test output string without quote runes
|
|
|
|
quoteOut := make([]bool, testL) // if true insert quote rune before the output rune
|
2018-11-02 12:12:31 +00:00
|
|
|
|
|
|
|
set := func(i int, in, out rune, quote bool) {
|
|
|
|
rIn[i] = in
|
|
|
|
rOut[i] = out
|
|
|
|
quoteOut[i] = quote
|
|
|
|
}
|
|
|
|
|
2019-05-17 13:41:38 +00:00
|
|
|
// populate test strings with values from the `fill` set
|
2018-11-02 12:12:31 +00:00
|
|
|
outer:
|
|
|
|
for pos := 0; pos < testL; pos++ {
|
|
|
|
m := pos % len(fill)
|
|
|
|
i := rng.Intn(len(fill[m]))
|
|
|
|
r := fill[m][i]
|
|
|
|
for _, m := range testMappings {
|
|
|
|
if pSrc := runePos(r, m.src); pSrc != -1 {
|
|
|
|
set(pos, r, m.dst[pSrc], false)
|
|
|
|
continue outer
|
|
|
|
} else if pDst := runePos(r, m.dst); pDst != -1 {
|
|
|
|
set(pos, r, r, true)
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set(pos, r, r, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
rng.Shuffle(testL, func(i, j int) {
|
|
|
|
rIn[i], rIn[j] = rIn[j], rIn[i]
|
|
|
|
rOut[i], rOut[j] = rOut[j], rOut[i]
|
|
|
|
quoteOut[i], quoteOut[j] = quoteOut[j], quoteOut[i]
|
|
|
|
})
|
2019-05-17 13:41:38 +00:00
|
|
|
fixEdges(rIn, rOut, quoteOut, edges)
|
|
|
|
return gen(rIn, rOut, quoteOut, testMappings)
|
|
|
|
}
|
2018-11-02 12:12:31 +00:00
|
|
|
|
2019-05-17 13:41:38 +00:00
|
|
|
func fixEdges(rIn, rOut []rune, quoteOut []bool, edges []edge) {
|
|
|
|
testL := len(rIn)
|
|
|
|
for _, e := range edges {
|
|
|
|
for idx, o := range e.orig {
|
|
|
|
r := e.replace[idx]
|
|
|
|
if e.edge == edgeLeft && rIn[0] == o {
|
|
|
|
rOut[0], quoteOut[0] = r, false
|
|
|
|
} else if e.edge == edgeLeft && rIn[0] == r {
|
|
|
|
quoteOut[0] = true
|
|
|
|
} else if e.edge == edgeRight && rIn[testL-1] == o {
|
|
|
|
rOut[testL-1], quoteOut[testL-1] = r, false
|
|
|
|
} else if e.edge == edgeRight && rIn[testL-1] == r {
|
|
|
|
quoteOut[testL-1] = true
|
2018-11-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runePos(r rune, s []rune) int {
|
|
|
|
for i, c := range s {
|
|
|
|
if c == r {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// quotedToString returns a string for the chars slice where an encoder.QuoteRune is
|
2018-11-02 12:12:31 +00:00
|
|
|
// inserted before a char[i] when quoted[i] is true.
|
|
|
|
func quotedToString(chars []rune, quoted []bool) string {
|
|
|
|
var out strings.Builder
|
|
|
|
out.Grow(len(chars))
|
|
|
|
for i, r := range chars {
|
|
|
|
if quoted[i] {
|
|
|
|
out.WriteRune(encoder.QuoteRune)
|
|
|
|
}
|
|
|
|
out.WriteRune(r)
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|