Skip to content

Commit 0644b53

Browse files
committed
Simplify single-character regex alternations to character classes
Replace single-character alternations like (a|b) and (?:a|b) with the equivalent character class [ab] across several lexers. While technically a character class compiles to a single IN op in CPython's sre engine, whereas an alternation compiles to a slower BRANCH, there is no measurable performance improvements, even from removing the useless capture groups. The change is purely cosmetic, as reading regexps is tedious enough as it is without the need to add useless grouping. This was found via `git grep '(.|.)' | grep -v bygroup`
1 parent 8549c5a commit 0644b53

16 files changed

Lines changed: 26 additions & 26 deletions

File tree

‎pygments/lexers/amdgpu.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AMDGPULexer(RegexLexer):
4545
'lit', 'unorm'), suffix=r'\b'), Name.Attribute),
4646
(r'(label_[a-z0-9]+)', Keyword),
4747
(r'(_L[0-9]*)', Name.Variable),
48-
(r'(s|v)_[a-z0-9_]+', Keyword),
48+
(r'[sv]_[a-z0-9_]+', Keyword),
4949
(r'(v[0-9.]+|vcc|exec|v)', Name.Variable),
5050
(r's[0-9.]+|s', Name.Variable),
5151
(r'[0-9]+\.[^0-9]+', Number.Float),

‎pygments/lexers/asm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class HsailLexer(RegexLexer):
258258
# Numeric Constant
259259
float = r'((\d+\.)|(\d*\.\d+))[eE][+-]?\d+'
260260
hexfloat = r'0[xX](([0-9a-fA-F]+\.[0-9a-fA-F]*)|([0-9a-fA-F]*\.[0-9a-fA-F]+))[pP][+-]?\d+'
261-
ieeefloat = r'0((h|H)[0-9a-fA-F]{4}|(f|F)[0-9a-fA-F]{8}|(d|D)[0-9a-fA-F]{16})'
261+
ieeefloat = r'0(?:[hH][0-9a-fA-F]{4}|[fF][0-9a-fA-F]{8}|[dD][0-9a-fA-F]{16})'
262262

263263
tokens = {
264264
'root': [

‎pygments/lexers/business.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class ABAPLexer(RegexLexer):
285285
(r'(\s+)([\w\-]+)([=\-]>)([\w\-~]+)',
286286
bygroups(Whitespace, Name.Variable, Operator, Name.Function)),
287287
# call methodnames returning style
288-
(r'(?<=(=|-)>)([\w\-~]+)(?=\()', Name.Function),
288+
(r'(?<=[=-]>)([\w\-~]+)(?=\()', Name.Function),
289289

290290
# text elements
291291
(r'(TEXT)(-)(\d{3})',

‎pygments/lexers/cplint.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CplintLexer(PrologLexer):
3636
'finite')), Name.Builtin),
3737
# annotations of atoms
3838
(r'([a-z]+)(:)', bygroups(String.Atom, Punctuation)),
39-
(r':(-|=)|::?|~=?|=>', Operator),
39+
(r':[-=]|::?|~=?|=>', Operator),
4040
(r'\?', Name.Builtin),
4141
inherit,
4242
],

‎pygments/lexers/dsls.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class ZeekLexer(RegexLexer):
287287
# operator.
288288
(r'/(?=.*/)', String.Regex, 'regex'),
289289

290-
(r'(T|F)\b', Keyword.Constant),
290+
(r'[TF]\b', Keyword.Constant),
291291

292292
# Port
293293
(r'\d{1,5}/(udp|tcp|icmp|unknown)\b', Number),

‎pygments/lexers/esoteric.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class CapDLLexer(RegexLexer):
218218

219219
# Literals
220220
(r'0[xX][\da-fA-F]+', Number.Hex),
221-
(r'\d+(\.\d+)?(k|M)?', Number),
221+
(r'\d+(\.\d+)?[kM]?', Number),
222222
(words(('bits',), suffix=r'\b'), Number),
223223
(words(('cspace', 'vspace', 'reply_slot', 'caller_slot',
224224
'ipc_buffer_slot'), suffix=r'\b'), Number),

‎pygments/lexers/graphics.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ class PostScriptLexer(RegexLexer):
335335
# Numbers
336336
(r'<[0-9A-Fa-f]+>' + delimiter_end, Number.Hex),
337337
# Slight abuse: use Oct to signify any explicit base system
338-
(r'[0-9]+\#(\-|\+)?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)'
339-
r'((e|E)[0-9]+)?' + delimiter_end, Number.Oct),
340-
(r'(\-|\+)?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)((e|E)[0-9]+)?'
338+
(r'[0-9]+\#[+-]?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)'
339+
r'([eE][0-9]+)?' + delimiter_end, Number.Oct),
340+
(r'[+-]?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)([eE][0-9]+)?'
341341
+ delimiter_end, Number.Float),
342-
(r'(\-|\+)?[0-9]+' + delimiter_end, Number.Integer),
342+
(r'[+-]?[0-9]+' + delimiter_end, Number.Integer),
343343

344344
# References
345345
(rf'\/{valid_name}', Name.Variable),

‎pygments/lexers/html.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class DtdLexer(RegexLexer):
141141

142142
'common': [
143143
(r'\s+', Text),
144-
(r'(%|&)[^;]*;', Name.Entity),
144+
(r'[%&][^;]*;', Name.Entity),
145145
('<!--', Comment, 'comment'),
146146
(r'[(|)*,?+]', Operator),
147147
(r'"[^"]*"', String.Double),

‎pygments/lexers/idl.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class IDLLexer(RegexLexer):
259259
(r'\b(mod|lt|le|eq|ne|ge|gt|not|and|or|xor)\b', Operator),
260260
(r'"[^\"]*"', String.Double),
261261
(r"'[^\']*'", String.Single),
262-
(r'\b[+\-]?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)(D|E)?([+\-]?[0-9]+)?\b',
262+
(r'\b[+\-]?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)[DE]?([+\-]?[0-9]+)?\b',
263263
Number.Float),
264264
(r'\b\'[+\-]?[0-9A-F]+\'X(U?(S?|L{1,2})|B)\b', Number.Hex),
265265
(r'\b\'[+\-]?[0-7]+\'O(U?(S?|L{1,2})|B)\b', Number.Oct),

‎pygments/lexers/javascript.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,8 @@ class ObjectiveJLexer(RegexLexer):
860860
(r'\n', Whitespace, '#pop'),
861861
],
862862
'statements': [
863-
(r'(L|@)?"', String, 'string'),
864-
(r"(L|@)?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
863+
(r'[L@]?"', String, 'string'),
864+
(r"[L@]?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'",
865865
String.Char),
866866
(r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
867867
(r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),

0 commit comments

Comments
 (0)