class PrefixMachine
Constants
- VERSION
Attributes
count[R]
Public Class Methods
new()
click to toggle source
VALUE method_prefix_machine_initialize(VALUE self) { struct trie* trie = xmalloc(sizeof(struct trie)); char* empty_str = xmalloc(sizeof(char)); empty_str[0] = '\0'; trie->trace = empty_str; trie->matched = FALSE; for (size_t i = 0; i < TOKENS; i++) { trie->children[i] = NULL; } VALUE rules = TypedData_Wrap_Struct(kPrefixMachineTrie, &type_trie, trie); rb_iv_set(self, "@rules", rules); rb_iv_set(self, "@count", SIZET2NUM(0)); return self; }
Public Instance Methods
guard!(str)
click to toggle source
# File lib/prefix-machine.rb, line 7 def guard!(str) raise NoMatchingPatternError.new('No Such Patterns') unless str =~ /[0-9a-z.]*/ end
insert(rule)
click to toggle source
# File lib/prefix-machine.rb, line 11 def insert(rule) s = rule.downcase guard!(s) trie_insert(s) end
Also aliased as: <<
match(str)
click to toggle source
# File lib/prefix-machine.rb, line 17 def match(str) s = str.downcase guard!(s) trie_match(s) end
Private Instance Methods
trie_insert(p1)
click to toggle source
VALUE method_private_prefix_machine_insert_trie(VALUE self, VALUE prefix) { struct trie* root = internal_trie_get(rb_iv_get(self, "@rules")); char* prefix_str = StringValueCStr(prefix); size_t len = strlen(prefix_str); internal_prefix_machine_insert_trie(root, prefix_str, len, 0); rb_iv_set(self, "@count", SIZET2NUM(NUM2SIZET(rb_iv_get(self, "@count")) + 1)); return Qnil; }
trie_match(p1)
click to toggle source
VALUE method_private_prefix_machine_match(VALUE self, VALUE str) { struct trie* root = internal_trie_get(rb_iv_get(self, "@rules")); char* str_ptr = StringValueCStr(str); size_t len = strlen(str_ptr); char* result = internal_prefix_machine_match(root, str_ptr, len, 0); if (result == NULL) { return Qnil; } return rb_str_new2(result); }