Skip to content

Commit

Permalink
lib/encoder: add LeftPeriod encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Sep 30, 2019
1 parent 0f7a3e1 commit 45dc8ea
Show file tree
Hide file tree
Showing 4 changed files with 26,863 additions and 20,960 deletions.
1 change: 1 addition & 0 deletions fstest/fstests/fstests.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ func Run(t *testing.T, opt *Opt) {
{"leading LF", "\nleading LF"},
{"leading HT", "\tleading HT"},
{"leading VT", "\vleading VT"},
{"leading dot", ".leading dot"},
{"trailing space", "trailing space "},
{"trailing CR", "trailing CR\r"},
{"trailing LF", "trailing LF\n"},
Expand Down
12 changes: 12 additions & 0 deletions lib/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
EncodeDel // DEL(0x7F)
EncodeCtl // CTRL(0x01-0x1F)
EncodeLeftSpace // Leading SPACE
EncodeLeftPeriod // Leading .
EncodeLeftTilde // Leading ~
EncodeLeftCrLfHtVt // Leading CR LF HT VT
EncodeRightSpace // Trailing SPACE
Expand Down Expand Up @@ -127,6 +128,13 @@ func (mask MultiEncoder) Encode(in string) string {
prefix, in = string(QuoteRune)+"␠", in[l:] // SYMBOL FOR SPACE
}
}
if mask.Has(EncodeLeftPeriod) && prefix == "" { // Leading PERIOD
if in[0] == '.' {
prefix, in = ".", in[1:] // FULLWIDTH FULL STOP
} else if r, l := utf8.DecodeRuneInString(in); r == '.' { // FULLWIDTH FULL STOP
prefix, in = string(QuoteRune)+".", in[l:] // FULLWIDTH FULL STOP
}
}
if mask.Has(EncodeLeftTilde) && prefix == "" { // Leading ~
if in[0] == '~' {
prefix, in = string('~'+fullOffset), in[1:] // FULLWIDTH TILDE
Expand Down Expand Up @@ -533,13 +541,17 @@ func (mask MultiEncoder) Decode(in string) string {
prefix := ""
if r, l1 := utf8.DecodeRuneInString(in); mask.Has(EncodeLeftSpace) && r == '␠' { // SYMBOL FOR SPACE
prefix, in = " ", in[l1:]
} else if mask.Has(EncodeLeftPeriod) && r == '.' { // FULLWIDTH FULL STOP
prefix, in = ".", in[l1:]
} else if mask.Has(EncodeLeftTilde) && r == '~' { // FULLWIDTH TILDE
prefix, in = "~", in[l1:]
} else if mask.Has(EncodeLeftCrLfHtVt) && (r == '␀'+'\t' || r == '␀'+'\n' || r == '␀'+'\v' || r == '␀'+'\r') {
prefix, in = string(r-'␀'), in[l1:]
} else if r == QuoteRune {
if r, l2 := utf8.DecodeRuneInString(in[l1:]); mask.Has(EncodeLeftSpace) && r == '␠' { // SYMBOL FOR SPACE
prefix, in = "␠", in[l1+l2:]
} else if mask.Has(EncodeLeftPeriod) && r == '.' { // FULLWIDTH FULL STOP
prefix, in = ".", in[l1+l2:]
} else if mask.Has(EncodeLeftTilde) && r == '~' { // FULLWIDTH TILDE
prefix, in = "~", in[l1+l2:]
} else if mask.Has(EncodeLeftCrLfHtVt) && (r == '␀'+'\t' || r == '␀'+'\n' || r == '␀'+'\v' || r == '␀'+'\r') {
Expand Down
Loading

0 comments on commit 45dc8ea

Please sign in to comment.