##// 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 b' def binary(s):'
307 return bool(s and b'\0' in s)
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 def stringmatcher(pattern, casesensitive=True):
318 def stringmatcher(pattern, casesensitive=True):
311 """
319 """
312 accepts a string, possibly starting with 're:' or 'literal:' prefix.
320 accepts a string, possibly starting with 're:' or 'literal:' prefix.
@@ -345,8 +353,8 b' def stringmatcher(pattern, casesensitive'
345 >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
353 >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
346 ('literal', 'ABCDEFG', [False, False, True])
354 ('literal', 'ABCDEFG', [False, False, True])
347 """
355 """
348 if pattern.startswith(b're:'):
356 kind, pattern = _splitpattern(pattern)
349 pattern = pattern[3:]
357 if kind == b're':
350 try:
358 try:
351 flags = 0
359 flags = 0
352 if not casesensitive:
360 if not casesensitive:
@@ -354,16 +362,16 b' def stringmatcher(pattern, casesensitive'
354 regex = remod.compile(pattern, flags)
362 regex = remod.compile(pattern, flags)
355 except remod.error as e:
363 except remod.error as e:
356 raise error.ParseError(_(b'invalid regular expression: %s') % e)
364 raise error.ParseError(_(b'invalid regular expression: %s') % e)
357 return b're', pattern, regex.search
365 return kind, pattern, regex.search
358 elif pattern.startswith(b'literal:'):
366 elif kind == b'literal':
359 pattern = pattern[8:]
367 if casesensitive:
368 match = pattern.__eq__
369 else:
370 ipat = encoding.lower(pattern)
371 match = lambda s: ipat == encoding.lower(s)
372 return kind, pattern, match
360
373
361 match = pattern.__eq__
374 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
362
363 if not casesensitive:
364 ipat = encoding.lower(pattern)
365 match = lambda s: ipat == encoding.lower(s)
366 return b'literal', pattern, match
367
375
368
376
369 def shortuser(user):
377 def shortuser(user):
General Comments 0
You need to be logged in to leave comments. Login now