From e97990e479989bd05f9d6377cfb327fccdcc1ed9 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 4 Nov 2012 07:56:56 +0000 Subject: Generalise RegexpCache, refactor, add tests. This is a no-op, currently. It's just setting up the RegexpCache interface for subsequent development. --- background_scripts/completion.coffee | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'background_scripts/completion.coffee') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 9e12d497..9314d9be 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -307,12 +307,16 @@ RegexpCache = clear: -> @cache = {} - get: (string) -> + # Get rexexp for string from cache, creating the regexp if necessary. + # Regexp meta-characters in string are escaped. + # Regexp is wrapped in prefix/suffix, which may contain meta-characters. + get: (string, prefix="", suffix="") -> @init() unless @initialized - @cache[string] ||= @escapeRegexp(string) - - # Creates a Regexp from the given string, with all special Regexp characters escaped. - escapeRegexp: (string) -> new RegExp(string.replace(@escapeRegExp, "\\$&"), "i") + regexpString = string.replace(@escapeRegExp, "\\$&") + # Avoid cost of constructing new strings if prefix/suffix are empty (which is expected to be a common case). + regexpString = prefix + regexpString if prefix + regexpString = regexpString + suffix if suffix + @cache[regexpString] ||= new RegExp(regexpString, "i") # Provides cached access to Chrome's history. As the user browses to new pages, we add those pages to this # history cache. @@ -382,3 +386,4 @@ root.DomainCompleter = DomainCompleter root.TabCompleter = TabCompleter root.HistoryCache = HistoryCache root.RankingUtils = RankingUtils +root.RegexpCache = RegexpCache -- cgit v1.2.3