Skip to content

Commit

Permalink
py: use pre-compiled regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jul 20, 2019
1 parent e190e30 commit 9234150
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions py/vimlfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class AttributeDict(dict):
"^[0-9A-Fa-f][^0-9A-Fa-f]$" : "^[0-9A-Fa-f][^0-9A-Fa-f]$",
}

_pat_compiled = {}

def viml_add(lst, item):
lst.append(item)

Expand All @@ -100,14 +102,23 @@ def viml_empty(obj):
def viml_equalci(a, b):
return a.lower() == b.lower()

def _get_compiled_pat(reg, flags):
key = (reg, flags)
try:
return _pat_compiled[key]
except KeyError:
pat = re.compile(reg, flags)
_pat_compiled[key] = pat
return pat

def viml_eqreg(s, reg):
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
return _get_compiled_pat(reg, re.IGNORECASE).search(s)

def viml_eqregh(s, reg):
return re.search(pat_vim2py[reg], s)
return _get_compiled_pat(reg, 0).search(s)

def viml_eqregq(s, reg):
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
return _get_compiled_pat(reg, re.IGNORECASE).search(s)

def viml_escape(s, chars):
r = ''
Expand Down
17 changes: 14 additions & 3 deletions py/vimlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class AttributeDict(dict):
"^[0-9A-Fa-f][^0-9A-Fa-f]$" : "^[0-9A-Fa-f][^0-9A-Fa-f]$",
}

_pat_compiled = {}

def viml_add(lst, item):
lst.append(item)

Expand All @@ -100,14 +102,23 @@ def viml_empty(obj):
def viml_equalci(a, b):
return a.lower() == b.lower()

def _get_compiled_pat(reg, flags):
key = (reg, flags)
try:
return _pat_compiled[key]
except KeyError:
pat = re.compile(pat_vim2py[reg], flags)
_pat_compiled[key] = pat
return pat

def viml_eqreg(s, reg):
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
return _get_compiled_pat(reg, re.IGNORECASE).search(s)

def viml_eqregh(s, reg):
return re.search(pat_vim2py[reg], s)
return _get_compiled_pat(reg, 0).search(s)

def viml_eqregq(s, reg):
return re.search(pat_vim2py[reg], s, re.IGNORECASE)
return _get_compiled_pat(reg, re.IGNORECASE).search(s)

def viml_escape(s, chars):
r = ''
Expand Down

0 comments on commit 9234150

Please sign in to comment.