comparison mercurial/utils/stringutil.py @ 37157:f8e1f48de118

stringutil: add isauthorwellformed function The regular expression for this function formerly lived at https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hghooks/mozhghooks/author_format.py#l13 Differential Revision: https://phab.mercurial-scm.org/D2959
author Connor Sheehan <sheehan@mozilla.com>
date Thu, 22 Mar 2018 09:48:22 -0400
parents f99d64e8a4e4
children fb7140f1d09d
comparison
equal deleted inserted replaced
37156:f51c2780db3a 37157:f8e1f48de118
129 r = author.find('>') 129 r = author.find('>')
130 if r == -1: 130 if r == -1:
131 r = None 131 r = None
132 return author[author.find('<') + 1:r] 132 return author[author.find('<') + 1:r]
133 133
134 _correctauthorformat = remod.compile(br'^[^<]+\s\<[^<>]+@[^<>]+\>$')
135
136 def isauthorwellformed(author):
137 '''Return True if the author field is well formed
138 (ie "Contributor Name <contrib@email.dom>")
139
140 >>> isauthorwellformed(b'Good Author <good@author.com>')
141 True
142 >>> isauthorwellformed(b'Author <good@author.com>')
143 True
144 >>> isauthorwellformed(b'Bad Author')
145 False
146 >>> isauthorwellformed(b'Bad Author <author@author.com')
147 False
148 >>> isauthorwellformed(b'Bad Author author@author.com')
149 False
150 >>> isauthorwellformed(b'<author@author.com>')
151 False
152 >>> isauthorwellformed(b'Bad Author <author>')
153 False
154 '''
155 return _correctauthorformat.match(author) is not None
156
134 def ellipsis(text, maxlength=400): 157 def ellipsis(text, maxlength=400):
135 """Trim string to at most maxlength (default: 400) columns in display.""" 158 """Trim string to at most maxlength (default: 400) columns in display."""
136 return encoding.trim(text, maxlength, ellipsis='...') 159 return encoding.trim(text, maxlength, ellipsis='...')
137 160
138 def escapestr(s): 161 def escapestr(s):