Skip to content

Commit

Permalink
remove duplicate characters inside []
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Jan 4, 2020
1 parent 4c252f4 commit f600534
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lex_charclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func (g *charsetGroupGenerator) Entropy() (float64, error) {
return entropy, nil
}

func removeDuplicateRunes(list []rune) []rune {
set := map[rune]bool{}
newlist := make([]rune, 0, len(list))
for _, c := range list {
if set[c] {
continue
}
set[c] = true
newlist = append(newlist, c)
}
return newlist
}

func lexRange(s *State) (LexType, error) {
if s.end() {
return nil, s.errorSyntax("'[' not closed")
Expand All @@ -68,8 +81,10 @@ func lexRange(s *State) (LexType, error) {
case '-':
return lexRangeDash, nil
case ']':
charset := s.patternBuff
charset = removeDuplicateRunes(charset)
s.lastGen = &charsetGroupGenerator{
charsets: [][]rune{s.patternBuff},
charsets: [][]rune{charset},
}
err := s.lastGen.Generate(s)
if err != nil {
Expand Down

0 comments on commit f600534

Please sign in to comment.