Mercurial > hg
changeset 36245:c6ce479f7a28
templater: add function to help substituting patterns in template string
This will be used to rewrite a filename pattern to a template string.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 07 Jan 2018 11:21:25 +0900 |
parents | 18bdfad8506e |
children | 9ee10b3284da |
files | mercurial/templater.py |
diffstat | 1 files changed, 20 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/templater.py Sun Jan 07 11:04:53 2018 +0900 +++ b/mercurial/templater.py Sun Jan 07 11:21:25 2018 +0900 @@ -172,6 +172,26 @@ raise error.ProgrammingError('unexpected type: %s' % typ) raise error.ProgrammingError('unterminated scanning of template') +def scantemplate(tmpl): + """Scan (type, start, end) positions of outermost elements in template + + >>> list(scantemplate(b'foo{bar}"baz')) + [('string', 0, 3), ('template', 3, 8), ('string', 8, 12)] + >>> list(scantemplate(b'outer{"inner"}outer')) + [('string', 0, 5), ('template', 5, 14), ('string', 14, 19)] + >>> list(scantemplate(b'foo\\{escaped}')) + [('string', 0, 5), ('string', 5, 13)] + """ + last = None + for typ, val, pos in _scantemplate(tmpl, 0, len(tmpl)): + if last: + yield last + (pos,) + if typ == 'end': + return + else: + last = (typ, pos) + raise error.ProgrammingError('unterminated scanning of template') + def _scantemplate(tmpl, start, stop, quote=''): """Parse template string into chunks of strings and template expressions""" sepchars = '{' + quote