github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/automaton/automata.go (about) 1 package automaton 2 3 import ( 4 "unicode" 5 ) 6 7 // util/automaton/Automata.java 8 9 // Returns a new (deterministic) automaton with the empty language. 10 func MakeEmpty() *Automaton { 11 a := newEmptyAutomaton() 12 a.finishState() 13 return a 14 } 15 16 // Returns a new (deterministic) automaton that accepts only the empty string. 17 func makeEmptyString() *Automaton { 18 panic("not implemented yet") 19 } 20 21 // Returns a new (deterministic) automaton that accepts any single codepoint. 22 func makeAnyChar() *Automaton { 23 return makeCharRange(MIN_CODE_POINT, unicode.MaxRune) 24 } 25 26 // Returns a new (deterministic) automaton that accepts a single codepoint of the given value. 27 func makeChar(c int) *Automaton { 28 return makeCharRange(c, c) 29 } 30 31 /* 32 Returns a new (deterministic) automaton that accepts a single rune 33 whose value is in the given interval (including both end points) 34 */ 35 func makeCharRange(min, max int) *Automaton { 36 if min > max { 37 return MakeEmpty() 38 } 39 40 a := newEmptyAutomaton() 41 s1 := a.createState() 42 s2 := a.createState() 43 a.setAccept(s2, true) 44 a.addTransitionRange(s1, s2, min, max) 45 a.finishState() 46 return a 47 } 48 49 // L237 50 // Returns a new (deterministic) automaton that accepts the single given string 51 func makeString(s string) *Automaton { 52 a := newEmptyAutomaton() 53 lastState := a.createState() 54 for _, r := range s { 55 state := a.createState() 56 a.addTransitionRange(lastState, state, int(r), int(r)) 57 lastState = state 58 } 59 60 a.setAccept(lastState, true) 61 a.finishState() 62 63 assert(a.deterministic) 64 assert(!hasDeadStates(a)) 65 66 return a 67 } 68 69 // L271 70 /* 71 Returns a new (deterministic and minimal) automaton that accepts the 72 union of the given collection of []byte representing UTF-8 encoded 73 strings. 74 */ 75 func makeStringUnion(utf8Strings [][]byte) *Automaton { 76 if len(utf8Strings) == 0 { 77 return MakeEmpty() 78 } 79 return buildDaciukMihovAutomaton(utf8Strings) 80 }