Mercurial > hg
comparison mercurial/match.py @ 40782:3984409e144b
match: drop unnecessary wrapping of regex in group
It seems the regexes have been wrapped in an unnamed group since
b6c42714d900 (Add locate command., 2005-07-05). In that commit, the
grouping was needed because there was a "head" ('^') added before the
group and a "tail" (os.sep) added after it. It seems the head was
moved inside the group in 1c0c413cccdd (Get add and locate to use new
repo and dirstate walk code., 2005-07-18) and the tail was moved
inside the group in 89985a1b3427 (Clean up walk and changes code to
use normalised names properly., 2005-07-31), So it seems to me that
we've carried around the unnecessary group for 13 years. This patch
removes it.
Differential Revision: https://phab.mercurial-scm.org/D5352
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Sun, 02 Dec 2018 13:09:46 -0800 |
parents | e115a6452b41 |
children | 9e462fb88f79 |
comparison
equal
deleted
inserted
replaced
40781:e115a6452b41 | 40782:3984409e144b |
---|---|
1183 return regex, matchfuncs[0] | 1183 return regex, matchfuncs[0] |
1184 else: | 1184 else: |
1185 return regex, lambda f: any(mf(f) for mf in matchfuncs) | 1185 return regex, lambda f: any(mf(f) for mf in matchfuncs) |
1186 | 1186 |
1187 MAX_RE_SIZE = 20000 | 1187 MAX_RE_SIZE = 20000 |
1188 _BASE_SIZE = len('(?:)') | |
1189 | 1188 |
1190 def _joinregexes(regexps): | 1189 def _joinregexes(regexps): |
1191 """gather multiple regular expressions into a single one""" | 1190 """gather multiple regular expressions into a single one""" |
1192 return '(?:%s)' % '|'.join(regexps) | 1191 return '|'.join(regexps) |
1193 | 1192 |
1194 def _buildregexmatch(kindpats, globsuffix): | 1193 def _buildregexmatch(kindpats, globsuffix): |
1195 """Build a match function from a list of kinds and kindpats, | 1194 """Build a match function from a list of kinds and kindpats, |
1196 return regexp string and a matcher function. | 1195 return regexp string and a matcher function. |
1197 | 1196 |
1207 allgroups = [] | 1206 allgroups = [] |
1208 regexps = [_regex(k, p, globsuffix) for (k, p, s) in kindpats] | 1207 regexps = [_regex(k, p, globsuffix) for (k, p, s) in kindpats] |
1209 fullregexp = _joinregexes(regexps) | 1208 fullregexp = _joinregexes(regexps) |
1210 | 1209 |
1211 startidx = 0 | 1210 startidx = 0 |
1212 groupsize = _BASE_SIZE | 1211 groupsize = 0 |
1213 for idx, r in enumerate(regexps): | 1212 for idx, r in enumerate(regexps): |
1214 piecesize = len(r) | 1213 piecesize = len(r) |
1215 if (piecesize + _BASE_SIZE) > MAX_RE_SIZE: | 1214 if piecesize > MAX_RE_SIZE: |
1216 msg = _("matcher pattern is too long (%d bytes)") % piecesize | 1215 msg = _("matcher pattern is too long (%d bytes)") % piecesize |
1217 raise error.Abort(msg) | 1216 raise error.Abort(msg) |
1218 elif (groupsize + piecesize) > MAX_RE_SIZE: | 1217 elif (groupsize + piecesize) > MAX_RE_SIZE: |
1219 group = regexps[startidx:idx] | 1218 group = regexps[startidx:idx] |
1220 allgroups.append(_joinregexes(group)) | 1219 allgroups.append(_joinregexes(group)) |
1221 startidx = idx | 1220 startidx = idx |
1222 groupsize = _BASE_SIZE | 1221 groupsize = 0 |
1223 groupsize += piecesize + 1 | 1222 groupsize += piecesize + 1 |
1224 | 1223 |
1225 if startidx == 0: | 1224 if startidx == 0: |
1226 func = _rematcher(fullregexp) | 1225 func = _rematcher(fullregexp) |
1227 else: | 1226 else: |
1231 func = lambda s: any(m(s) for m in allmatchers) | 1230 func = lambda s: any(m(s) for m in allmatchers) |
1232 return fullregexp, func | 1231 return fullregexp, func |
1233 except re.error: | 1232 except re.error: |
1234 for k, p, s in kindpats: | 1233 for k, p, s in kindpats: |
1235 try: | 1234 try: |
1236 _rematcher('(?:%s)' % _regex(k, p, globsuffix)) | 1235 _rematcher(_regex(k, p, globsuffix)) |
1237 except re.error: | 1236 except re.error: |
1238 if s: | 1237 if s: |
1239 raise error.Abort(_("%s: invalid pattern (%s): %s") % | 1238 raise error.Abort(_("%s: invalid pattern (%s): %s") % |
1240 (s, k, p)) | 1239 (s, k, p)) |
1241 else: | 1240 else: |