Skip to content

Instantly share code, notes, and snippets.

@satyr
Created June 16, 2009 21:37
Show Gist options
  • Save satyr/130917 to your computer and use it in GitHub Desktop.
Save satyr/130917 to your computer and use it in GitHub Desktop.
// http://search.cpan.org/~dankogai/Regexp-Trie-0.02/lib/Regexp/Trie.pm
function RegexpTrie(strs, prefixes) {
var me = {$: {}, __proto__: arguments.callee.fn};
if (strs) {
let add = prefixes ? "addPrefixes" : "add";
for each (let str in strs) me[add](str);
}
return me;
}
RegexpTrie.fn = {
add: function add(str) {
var ref = this.$;
for each (let char in str) ref = ref[char] || (ref[char] = {});
ref[""] = 1; // {"": 1} as terminator
return this;
},
_regexp: function _regexp($) {
if ("" in $ && $.__count__ === 1) return "";
var alt = [], cc = [], q;
for (let char in $) {
if ($[char] !== 1) {
let qchar = char.replace(/[.?*+^$|()\{\[\]\\]/, "\\$&");
let recurse = _regexp($[char]);
(recurse ? alt : cc).push(qchar + recurse);
} else
q = 1;
}
var cconly = !alt.length;
if (cc.length) alt.push(1 in cc ? "[" + cc.join("") + "]" : cc[0]);
var result = 1 in alt ? "(?:" + alt.join("|") + ")" : alt[0];
if (q) result = cconly ? result + "?" : "(?:" + result + ")?";
return result;
},
toString: function toString() this._regexp(this.$),
get regexp() RegExp(this),
// adds every prefix of str.
// i.e. rt.addPrefixes("str") => rt.add("s").add("st").add("str")
addPrefixes: function addPrefixes(str) {
var ref = this.$;
for each (let char in str) ref = ref[char] || (ref[char] = {"": 1});
return this;
},
};
/*
function say(x) { dump(x + "\n") }
var strs = <![CDATA[
program
programist
programistic
programma
programmar
programmatic
programmatically
programmatist
programmer
]]>.match(/\S+/g);
say(RegexpTrie(strs).regexp);
var cmds = ["exit firefox", "restart firefox", "close window", "fullscreen", "switch ", "close ", "close all tabs that match", "count tabs", "refresh", "bookmark", "print", "go back", "go forward", "go home", "zoom", "tag", "run ", "undo close tab", "check ", "twitter", "digg", "tinyurl", "share-on-delicious", "rypple-ask", "syntax-highlight", "convert", "escape-html-entities", "selector-selector", "view source", "delete", "undelete", "edit page", "stop editing page", "save", "remove annotations", "word-count", "link-to-wikipedia", "calculate", "gcalculate", "sparkline", "translate", "create-bookmarklet-command-from", "create-new-search-command", "bold", "italic", "underline", "undo", "redo", "highlight", "email", "get-last-email-from", "get-email-address-for", "add ", "check-calendar", "map", "map-these", "search", "google", "wikipedia", "imdb", "yahoo-search", "amazon", "VideoSurf", "youtube", "flickr", "bugzilla", "msn-search", "ebay-search", "ask-search", "answers-search", "yelp", "weather", "define", "image-search", "rotate-image", "rotate-page", "flip-page", "desaturate-image", "invert-image", "edge-detect-image", "googl", "gimage", "subscribe-locally", "preview-browser-security-check", "help", "open ", "write ubiquity commands", "list ubiquity commands", "change ubiquity settings", "report a bug", "enable ", "disable ", "command history", "eijiro"];
dump(RegexpTrie(cmds, 1));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment