Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Fixed mistakes in globmatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
eprovst committed Dec 21, 2018
1 parent 95430b8 commit 4557a87
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions pages/globmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
package pages

import (
"fmt"
"strings"
)

// TODO: Work on rune level instead of bytes? Because '?' currently
// does not support characters longer than one byte.

// We use two special ASCII codes in our pattern building
const (
asciiGS = rune(0x1D)
Expand Down Expand Up @@ -130,29 +128,45 @@ func NewGlobMatcher(pattern string) *GlobMatcher {
// Initialise the DFA
dfa := make([]state, numberOfStates)

for s := range dfa {
dfa[s].match = make(map[rune]int)
}

// Now build the DFA
fail := -1
s := 0
x, s := -1, 0
for _, c := range pattern {
switch c {
case asciiGS:
fail = s
x = s

case asciiUS:
if x != -1 {
fmt.Println("error: '?' after '*' is not supported at this time")
}

dfa[s].defaultNext = s + 1
s++

default:
dfa[s].defaultNext = fail
dfa[s].match = c
dfa[s].specificNext = s + 1
if x == -1 {
dfa[s].defaultNext = -1

} else {
for r, n := range dfa[x].match {
dfa[s].match[r] = n
}

x = dfa[x].getNext(c)
}

dfa[s].match[c] = s + 1
s++
}
}

// Finally set the accepting state
acceptingState := numberOfStates - 1
dfa[acceptingState].defaultNext = fail
dfa[acceptingState].defaultNext = x

return &GlobMatcher{
simplePat: false,
Expand All @@ -164,14 +178,13 @@ func NewGlobMatcher(pattern string) *GlobMatcher {

// state represents a state in a DFA
type state struct {
defaultNext int
match rune
specificNext int
defaultNext int
match map[rune]int
}

func (s state) getNext(c rune) int {
if s.match == c {
return s.specificNext
if n, e := s.match[c]; e {
return n
}

return s.defaultNext
Expand Down

0 comments on commit 4557a87

Please sign in to comment.