79 lines
2.4 KiB
Plaintext
79 lines
2.4 KiB
Plaintext
import io
|
|
import vector
|
|
import string
|
|
|
|
fun regex(in: char*):regex {
|
|
return regex(string::string(in))
|
|
}
|
|
fun regex(in: string::string):regex {
|
|
var out.construct(in):regex
|
|
return out
|
|
}
|
|
|
|
obj regexState(Object) {
|
|
var character: char
|
|
var next_states: vector::vector<regexState*>
|
|
fun construct(charIn:char): regexState* {
|
|
character = charIn
|
|
next_states.construct()
|
|
return this
|
|
}
|
|
fun construct(): regexState* {
|
|
return construct(0)
|
|
}
|
|
fun copy_construct(old:regexState*): void {
|
|
character = regexState->character
|
|
next_states.copy_construct(®exState->next_states)
|
|
}
|
|
fun destruct():void {
|
|
next_states.destruct()
|
|
}
|
|
fun match(input: char): vector::vector<regexState*> {
|
|
return next_states.filter(fun(it:regexState*):bool { return it->character == input; } )
|
|
}
|
|
}
|
|
|
|
obj regex(Object) {
|
|
var regexString: string::string
|
|
var begin: regexState
|
|
fun construct(regexStringIn: string::string): regex* {
|
|
regexState.construct()
|
|
regexString.copy_construct(®exStringIn)
|
|
|
|
var traverse = &begin
|
|
for (var i = 0; i < regexString.length(); i++;) {
|
|
var next = new<regexState>()->construct(regexString[i])
|
|
traverse->next_states->add(next)
|
|
traverse = next
|
|
}
|
|
traverse->next_states->add(new<regexState>()->construct(1))
|
|
|
|
return this
|
|
}
|
|
fun copy_construct(old:regex*):void {
|
|
begin.copy_construct(&old->begin)
|
|
regexString.copy_construct(&old->regexString)
|
|
}
|
|
fun destruct():void {
|
|
begin.destruct()
|
|
regexString.destruct()
|
|
}
|
|
fun long_match(to_match: char*): int { return long_match(string::string(to_match)); }
|
|
fun long_match(to_match: string::string): int {
|
|
var next = vector::vector(&begin)
|
|
var longest = 0
|
|
for (var i = 0; i < to_match.length(); i++;) {
|
|
if (next.size == 0)
|
|
return longest
|
|
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
|
longest = i
|
|
next = next.flatten_map(fun(state: regexState*): vector::vector<regexState*> { return state->match(to_match[i]); })
|
|
}
|
|
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
|
return to_match.length()
|
|
return longest
|
|
}
|
|
}
|
|
|
|
|