Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new TokeniseOption EnsureLF #336

Merged
merged 2 commits into from Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lexer.go
Expand Up @@ -6,7 +6,8 @@ import (

var (
defaultOptions = &TokeniseOptions{
State: "root",
State: "root",
EnsureLF: true,
}
)

Expand Down Expand Up @@ -80,6 +81,10 @@ type TokeniseOptions struct {
State string
// Nested tokenisation.
Nested bool

// If true, all EOLs are converted into LF
// by replacing CRLF and CR
EnsureLF bool
}

// A Lexer for tokenising source code.
Expand Down
22 changes: 22 additions & 0 deletions regexp.go
Expand Up @@ -410,6 +410,9 @@ func (r *RegexLexer) Tokenise(options *TokeniseOptions, text string) (Iterator,
if options == nil {
options = defaultOptions
}
if options.EnsureLF {
text = ensureLF(text)
}
if !options.Nested && r.config.EnsureNL && !strings.HasSuffix(text, "\n") {
text += "\n"
}
Expand Down Expand Up @@ -437,3 +440,22 @@ func matchRules(text []rune, pos int, rules []*CompiledRule) (int, *CompiledRule
}
return 0, &CompiledRule{}, nil
}

// replace \r and \r\n with \n
// same as strings.ReplaceAll but more efficient
func ensureLF(text string) string {
buf := make([]byte, len(text))
var j int
for i := 0; i < len(text); i++ {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is unicode safe is it? If a unicode code point contains \r\n this will mangle the codepoint.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this would iterate over the bytes (uint8s). We have to use a for range loop here, so we iterate over the individual runes.

c := text[i]
if c == '\r' {
if i < len(text)-1 && text[i+1] == '\n' {
continue
}
c = '\n'
}
buf[j] = c
j++
}
return string(buf[:j])
}
56 changes: 56 additions & 0 deletions regexp_test.go
Expand Up @@ -43,3 +43,59 @@ func TestMatchingAtStart(t *testing.T) {
[]Token{{Punctuation, "-"}, {NameEntity, "module"}, {Whitespace, " "}, {Operator, "->"}},
it.Tokens())
}

func TestEnsureLFOption(t *testing.T) {
l := Coalesce(MustNewLexer(&Config{}, Rules{
"root": {
{`(\w+)(\r?\n|\r)`, ByGroups(Keyword, Whitespace), nil},
},
}))
it, err := l.Tokenise(&TokeniseOptions{
State: "root",
EnsureLF: true,
}, "hello\r\nworld\r")
assert.NoError(t, err)
assert.Equal(t, []Token{
{Keyword, "hello"},
{Whitespace, "\n"},
{Keyword, "world"},
{Whitespace, "\n"},
}, it.Tokens())

l = Coalesce(MustNewLexer(nil, Rules{
"root": {
{`(\w+)(\r?\n|\r)`, ByGroups(Keyword, Whitespace), nil},
},
}))
it, err = l.Tokenise(&TokeniseOptions{
State: "root",
EnsureLF: false,
}, "hello\r\nworld\r")
assert.NoError(t, err)
assert.Equal(t, []Token{
{Keyword, "hello"},
{Whitespace, "\r\n"},
{Keyword, "world"},
{Whitespace, "\r"},
}, it.Tokens())
}

func TestEnsureLFFunc(t *testing.T) {
tests := []struct{ in, out string }{
{in: "", out: ""},
{in: "abc", out: "abc"},
{in: "\r", out: "\n"},
{in: "a\r", out: "a\n"},
{in: "\rb", out: "\nb"},
{in: "a\rb", out: "a\nb"},
{in: "\r\n", out: "\n"},
{in: "a\r\n", out: "a\n"},
{in: "\r\nb", out: "\nb"},
{in: "a\r\nb", out: "a\nb"},
{in: "\r\r\r\n\r", out: "\n\n\n\n"},
}
for _, test := range tests {
out := ensureLF(test.in)
assert.Equal(t, out, test.out)
}
}