diff --git a/flx/__init__.py b/flx/__init__.py index 84eba1a..dea2b96 100644 --- a/flx/__init__.py +++ b/flx/__init__.py @@ -1,8 +1,7 @@ """ flx: Fuzzy matching... a la Sublime Text. - """ -__shortname__ = "flx" -__longname__ = "flx: Fuzzy matching... a la Sublime Text" -__version__ = "0.1.0" +__shortname__ = "flx" +__longname__ = "flx: Fuzzy matching... a la Sublime Text" +__version__ = "0.1.0" diff --git a/flx/flx.py b/flx/flx.py index 4090cfa..1489433 100644 --- a/flx/flx.py +++ b/flx/flx.py @@ -1,3 +1,5 @@ +from . import util + word_separators = [ ' ', '-', '_', ':', '.', '/', '\\', ] default_score = -35 @@ -30,4 +32,38 @@ def boundary(last_ch, ch): def inc_vec(vec, inc, beg, end): """Increment each element in `vec` between `beg` and `end` by `inc`.""" - pass + inc = inc or 1 + beg = beg or 0 + end = end or len(vec) + + while beg < end: + vec[beg] += inc + beg += 1 + + return vec + +def get_hash_for_string(str): + """Return hash-table for string where keys are characters. + + Value is a sorted list of indexes for character occurrences. + """ + result = {} + + str_len = len(str) + index = str_len - 1 + + while 0 <= index: + ch = str[index] + + if capital(ch): + result = util.dict_insert(result, ch, index) + + down_ch = ch.lower() + else: + down_ch = ch + + result = util.dict_insert(result, down_ch, index) + + index -= 1 + + return result diff --git a/flx/util.py b/flx/util.py new file mode 100644 index 0000000..a098925 --- /dev/null +++ b/flx/util.py @@ -0,0 +1,24 @@ +def dict_set(dict, key, val): + if key is None: + return + + dict[key] = val + + return dict + +def dict_get(dict, key): + if key is None: + return None + + if not key in dict.keys(): + return None + + return dict[key] + +def dict_insert(dict, key, val): + if not key in dict.keys(): + dict[key] = [] + + dict[key].insert(0, val) + + return dict diff --git a/test/test_flx.py b/test/test_flx.py index 63fad8d..a77debb 100644 --- a/test/test_flx.py +++ b/test/test_flx.py @@ -16,3 +16,26 @@ def test_capital(): assert flx.capital('c') == False assert flx.capital(' ') == False pass + +def test_inc_vec(): + assert flx.inc_vec([1, 2, 3], 1, 0, None) == [2, 3, 4] + assert flx.inc_vec([1, 2, 3], 1, 1, None) == [1, 3, 4] + pass + +def test_get_hash_for_string(): + assert flx.get_hash_for_string("switch-to-buffer") == { + 'r': [15], + 'e': [14], + 'f': [12, 13], + 'u': [11], + 'b': [10], + '-': [6, 9], + 'o': [8], + 't': [3, 7], + 'h': [5], + 'c': [4], + 'i': [2], + 'w': [1], + 's': [0] + } + pass