def filter(self, context): complete_str = context['complete_str'] if context['ignorecase']: complete_str = complete_str.lower() if context['is_sorted']: begin = binary_search_begin( context['candidates'], complete_str) end = binary_search_end( context['candidates'], complete_str) if begin < 0 or end < 0: return [] candidates = context['candidates'][begin:end+1] if context['ignorecase']: return candidates else: candidates = context['candidates'] if context['ignorecase']: return [x for x in context['candidates'] if x['word'].lower().startswith(complete_str)] else: return [x for x in context['candidates'] if x['word'].startswith(complete_str)]
def filter(self, context: UserContext) -> Candidates: complete_str = context['complete_str'] if context['ignorecase']: complete_str = complete_str.lower() if context['is_sorted']: begin = binary_search_begin(context['candidates'], complete_str) end = binary_search_end(context['candidates'], complete_str) if begin < 0 or end < 0: return [] candidates = context['candidates'][begin:end + 1] if context['ignorecase']: return list(candidates) else: candidates = context['candidates'] if context['ignorecase']: return [ x for x in context['candidates'] if x['word'].lower().startswith(complete_str) ] else: return [ x for x in context['candidates'] if x['word'].startswith(complete_str) ]
def test_binary_search(): assert util.binary_search_begin([], '') == -1 assert util.binary_search_begin([{'word': 'abc'}], 'abc') == 0 assert util.binary_search_begin([ {'word': 'aaa'}, {'word': 'abc'}, ], 'abc') == 1 assert util.binary_search_begin([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'abc'}, ], 'abc') == 2 assert util.binary_search_begin([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'AbC'}, ], 'abc') == 2 assert util.binary_search_begin([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'abc'}, ], 'b') == -1 assert util.binary_search_begin([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'aac'}, {'word': 'abc'}, ], 'aa') == 1 assert util.binary_search_end([], '') == -1 assert util.binary_search_end([{'word': 'abc'}], 'abc') == 0 assert util.binary_search_end([ {'word': 'aaa'}, {'word': 'abc'}, ], 'abc') == 1 assert util.binary_search_end([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'abc'}, ], 'abc') == 2 assert util.binary_search_end([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'abc'}, ], 'b') == -1 assert util.binary_search_end([ {'word': 'a'}, {'word': 'aaa'}, {'word': 'aac'}, {'word': 'abc'}, ], 'aa') == 2
def filter(self, context): complete_str = context['complete_str'] if context['ignorecase']: complete_str = complete_str.lower() if context['is_sorted']: begin = binary_search_begin( context['candidates'], complete_str[0]) end = binary_search_end( context['candidates'], complete_str[0]) if begin < 0 or end < 0: return [] candidates = context['candidates'][begin:end+1] else: candidates = context['candidates'] p = re.compile(fuzzy_escape(complete_str, context['camelcase'])) if context['ignorecase']: return [x for x in candidates if p.match(x['word'].lower())] else: return [x for x in candidates if p.match(x['word'])]
def filter(self, context): complete_str = context['complete_str'] if context['ignorecase']: complete_str = complete_str.lower() if not complete_str: return context['candidates'] if context['is_sorted']: begin = binary_search_begin( context['candidates'], complete_str[0]) end = binary_search_end( context['candidates'], complete_str[0]) if begin < 0 or end < 0: return [] candidates = context['candidates'][begin:end+1] else: candidates = context['candidates'] p = re.compile(fuzzy_escape(complete_str, context['camelcase'])) if context['ignorecase']: return [x for x in candidates if p.match(x['word'].lower())] else: return [x for x in candidates if p.match(x['word'])]
def test_binary_search(): assert util.binary_search_begin([], '') == -1 assert util.binary_search_begin([{'word': 'abc'}], 'abc') == 0 assert util.binary_search_begin([ { 'word': 'aaa' }, { 'word': 'abc' }, ], 'abc') == 1 assert util.binary_search_begin([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'abc' }, ], 'abc') == 2 assert util.binary_search_begin([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'AbC' }, ], 'abc') == 2 assert util.binary_search_begin([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'abc' }, ], 'b') == -1 assert util.binary_search_begin([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'aac' }, { 'word': 'abc' }, ], 'aa') == 1 assert util.binary_search_end([], '') == -1 assert util.binary_search_end([{'word': 'abc'}], 'abc') == 0 assert util.binary_search_end([ { 'word': 'aaa' }, { 'word': 'abc' }, ], 'abc') == 1 assert util.binary_search_end([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'abc' }, ], 'abc') == 2 assert util.binary_search_end([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'abc' }, ], 'b') == -1 assert util.binary_search_end([ { 'word': 'a' }, { 'word': 'aaa' }, { 'word': 'aac' }, { 'word': 'abc' }, ], 'aa') == 2