comparison mercurial/util.py @ 26481:7d132557e44a

util: extract stringmatcher() from revset This is used to match against tags, bookmarks, etc in revsets. It will be used in a future patch to do the same tag matching in templater.
author Matt Harbison <matt_harbison@yahoo.com>
date Sat, 22 Aug 2015 22:52:18 -0400
parents 6ae14d1ca3aa
children 3a0bb61371c5
comparison
equal deleted inserted replaced
26480:6ae14d1ca3aa 26481:7d132557e44a
1603 return lambda x: x >= start and x <= stop 1603 return lambda x: x >= start and x <= stop
1604 else: 1604 else:
1605 start, stop = lower(date), upper(date) 1605 start, stop = lower(date), upper(date)
1606 return lambda x: x >= start and x <= stop 1606 return lambda x: x >= start and x <= stop
1607 1607
1608 def stringmatcher(pattern):
1609 """
1610 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1611 returns the matcher name, pattern, and matcher function.
1612 missing or unknown prefixes are treated as literal matches.
1613
1614 helper for tests:
1615 >>> def test(pattern, *tests):
1616 ... kind, pattern, matcher = stringmatcher(pattern)
1617 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1618
1619 exact matching (no prefix):
1620 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1621 ('literal', 'abcdefg', [False, False, True])
1622
1623 regex matching ('re:' prefix)
1624 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
1625 ('re', 'a.+b', [False, False, True])
1626
1627 force exact matches ('literal:' prefix)
1628 >>> test('literal:re:foobar', 'foobar', 're:foobar')
1629 ('literal', 're:foobar', [False, True])
1630
1631 unknown prefixes are ignored and treated as literals
1632 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
1633 ('literal', 'foo:bar', [False, False, True])
1634 """
1635 if pattern.startswith('re:'):
1636 pattern = pattern[3:]
1637 try:
1638 regex = remod.compile(pattern)
1639 except remod.error as e:
1640 raise error.ParseError(_('invalid regular expression: %s')
1641 % e)
1642 return 're', pattern, regex.search
1643 elif pattern.startswith('literal:'):
1644 pattern = pattern[8:]
1645 return 'literal', pattern, pattern.__eq__
1646
1608 def shortuser(user): 1647 def shortuser(user):
1609 """Return a short representation of a user name or email address.""" 1648 """Return a short representation of a user name or email address."""
1610 f = user.find('@') 1649 f = user.find('@')
1611 if f >= 0: 1650 if f >= 0:
1612 user = user[:f] 1651 user = user[:f]