Last active
August 29, 2015 14:10
-
-
Save Inndy/229bbd3daad00799a8d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var string_functions = require("./string_functions"); | |
| var tests = { | |
| "*": ["", "a", "*", "a*", "cdef"], | |
| "abc*": ["abc", "ddj", "abcd"], | |
| "asdf": ["asdf", "asdfg"], | |
| "as*df": ["asddf", "asqwertdf", "asqwertddf"], | |
| "*eer": ["asdjfklajsklej;jfk;lseer", "eer", "aaaeer", " eer"] | |
| }, | |
| results = []; | |
| for (var pattern in tests) { | |
| var test_data = tests[pattern]; | |
| for (var i = 0; i < test_data.length; i++) { | |
| results.push([pattern, test_data[i], string_functions.star_match(pattern, test_data[i])]); | |
| } | |
| } | |
| console.log(results); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function (global) { | |
| function star_match(pattern, src) { | |
| var i = 0, j = 0, flag_star = false; | |
| while (j < pattern.length) { | |
| if (pattern[j] == '*') { | |
| flag_star = true; | |
| j++; | |
| } | |
| if (flag_star) { | |
| if (src[i] == pattern[j]) { | |
| var next_i = src.indexOf(pattern[j], i + 1); | |
| if (next_i > 0 && star_match(pattern.substr(j), src.substr(next_i))) | |
| return true; | |
| flag_star = false; | |
| j++; | |
| } | |
| i++; | |
| } else if (src[i] == pattern[j]) { | |
| i++; j++; | |
| } else { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }; | |
| global.star_match = star_match; | |
| })(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment