lib/encoder: add CrLf encoding

s3-about
Nick Craig-Wood 2019-09-20 23:04:31 +01:00
parent 6e053ecbd0
commit f55a99218c
3 changed files with 9183 additions and 8639 deletions

View File

@ -39,6 +39,7 @@ const (
EncodeSlash uint = 1 << iota // /
EncodeWin // :?"*<>|
EncodeBackSlash // \
EncodeCrLf // CR(0x0D), LF(0x0A)
EncodeHashPercent // #%
EncodeDel // DEL(0x7F)
EncodeCtl // CTRL(0x01-0x1F)
@ -86,6 +87,7 @@ func (mask MultiEncoder) Encode(in string) string {
encodeWin = uint(mask)&EncodeWin != 0
encodeSlash = uint(mask)&EncodeSlash != 0
encodeBackSlash = uint(mask)&EncodeBackSlash != 0
encodeCrLf = uint(mask)&EncodeCrLf != 0
encodeHashPercent = uint(mask)&EncodeHashPercent != 0
encodeDel = uint(mask)&EncodeDel != 0
encodeCtl = uint(mask)&EncodeCtl != 0
@ -202,6 +204,13 @@ func (mask MultiEncoder) Encode(in string) string {
return true
}
}
if encodeCrLf { // CR LF
switch r {
case rune(0x0D), rune(0x0A),
'␍', '␊':
return true
}
}
if encodeHashPercent { // #%
switch r {
case '#', '%',
@ -294,6 +303,17 @@ func (mask MultiEncoder) Encode(in string) string {
continue
}
}
if encodeCrLf { // CR LF
switch r {
case rune(0x0D), rune(0x0A):
out.WriteRune(r + symbolOffset)
continue
case '␍', '␊':
out.WriteRune(QuoteRune)
out.WriteRune(r)
continue
}
}
if encodeHashPercent { // #%
switch r {
case '#', '%':
@ -338,6 +358,7 @@ func (mask MultiEncoder) Decode(in string) string {
encodeWin = uint(mask)&EncodeWin != 0
encodeSlash = uint(mask)&EncodeSlash != 0
encodeBackSlash = uint(mask)&EncodeBackSlash != 0
encodeCrLf = uint(mask)&EncodeCrLf != 0
encodeHashPercent = uint(mask)&EncodeHashPercent != 0
encodeDel = uint(mask)&EncodeDel != 0
encodeCtl = uint(mask)&EncodeCtl != 0
@ -432,6 +453,12 @@ func (mask MultiEncoder) Decode(in string) string {
return true
}
}
if encodeCrLf { // CR LF
switch r {
case '␍', '␊':
return true
}
}
if encodeHashPercent { // #%
switch r {
case '', '':
@ -521,6 +548,17 @@ func (mask MultiEncoder) Decode(in string) string {
continue
}
}
if encodeCrLf { // CR LF
switch r {
case '␍', '␊':
if unquote {
out.WriteRune(r)
} else {
out.WriteRune(r - symbolOffset)
}
continue
}
}
if encodeHashPercent { // #%
switch r {
case '', '':

File diff suppressed because it is too large Load Diff

View File

@ -43,6 +43,7 @@ var maskBits = []struct {
{encoder.EncodeWin, "EncodeWin"},
{encoder.EncodeSlash, "EncodeSlash"},
{encoder.EncodeBackSlash, "EncodeBackSlash"},
{encoder.EncodeCrLf, "EncodeCrLf"},
{encoder.EncodeHashPercent, "EncodeHashPercent"},
{encoder.EncodeDel, "EncodeDel"},
{encoder.EncodeCtl, "EncodeCtl"},
@ -99,6 +100,11 @@ var allMappings = []mapping{{
}, []rune{
'',
}}, {
encoder.EncodeCrLf, []rune{
rune(0x0D), rune(0x0A),
}, []rune{
'␍', '␊',
}}, {
encoder.EncodeHashPercent, []rune{
'#', '%',
}, []rune{
@ -354,7 +360,7 @@ func fatalW(_ int, err error) func(...interface{}) {
}
func invalidMask(mask uint) bool {
return mask&encoder.EncodeCtl != 0 && mask&(encoder.EncodeLeftCrLfHtVt|encoder.EncodeRightCrLfHtVt) != 0
return mask&(encoder.EncodeCtl|encoder.EncodeCrLf) != 0 && mask&(encoder.EncodeLeftCrLfHtVt|encoder.EncodeRightCrLfHtVt) != 0
}
// construct a slice containing the runes between (l)ow (inclusive) and (h)igh (inclusive)