##// END OF EJS Templates
stringutil: extract helper function that splits stringmatcher() pattern
Yuya Nishihara -
r46314:d502caab default
parent child Browse files
Show More
@@ -307,6 +307,14 def binary(s):
307 307 return bool(s and b'\0' in s)
308 308
309 309
310 def _splitpattern(pattern):
311 if pattern.startswith(b're:'):
312 return b're', pattern[3:]
313 elif pattern.startswith(b'literal:'):
314 return b'literal', pattern[8:]
315 return b'literal', pattern
316
317
310 318 def stringmatcher(pattern, casesensitive=True):
311 319 """
312 320 accepts a string, possibly starting with 're:' or 'literal:' prefix.
@@ -345,8 +353,8 def stringmatcher(pattern, casesensitive
345 353 >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
346 354 ('literal', 'ABCDEFG', [False, False, True])
347 355 """
348 if pattern.startswith(b're:'):
349 pattern = pattern[3:]
356 kind, pattern = _splitpattern(pattern)
357 if kind == b're':
350 358 try:
351 359 flags = 0
352 360 if not casesensitive:
@@ -354,16 +362,16 def stringmatcher(pattern, casesensitive
354 362 regex = remod.compile(pattern, flags)
355 363 except remod.error as e:
356 364 raise error.ParseError(_(b'invalid regular expression: %s') % e)
357 return b're', pattern, regex.search
358 elif pattern.startswith(b'literal:'):
359 pattern = pattern[8:]
360
365 return kind, pattern, regex.search
366 elif kind == b'literal':
367 if casesensitive:
361 368 match = pattern.__eq__
362
363 if not casesensitive:
369 else:
364 370 ipat = encoding.lower(pattern)
365 371 match = lambda s: ipat == encoding.lower(s)
366 return b'literal', pattern, match
372 return kind, pattern, match
373
374 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
367 375
368 376
369 377 def shortuser(user):
General Comments 0
You need to be logged in to leave comments. Login now