48
48
import itertools
49
49
import math # for log
50
50
import os
51
- import re
51
+ import regex
52
52
import sre_compile
53
53
import string
54
54
import sys
55
55
56
56
# if empty, use defaults
57
- _header_regex = re .compile ("a^" )
57
+ _header_regex = regex .compile ("a^" )
58
58
59
59
# if empty, use defaults
60
- _source_regex = re .compile ("a^" )
60
+ _source_regex = regex .compile ("a^" )
61
61
62
62
63
63
# Files which match the regex are considered to be header
@@ -191,7 +191,7 @@ def IsSourceFile(filename):
191
191
# hard-coded international strings, which belong in a separate i18n file.
192
192
193
193
# Type names
194
- _TYPES = re .compile (
194
+ _TYPES = regex .compile (
195
195
r'^(?:'
196
196
# [dcl.type.simple]
197
197
r'(char(16_t|32_t)?)|wchar_t|'
@@ -208,11 +208,11 @@ def IsSourceFile(filename):
208
208
# - Anything not following google file name conventions (containing an
209
209
# uppercase character, such as Python.h or nsStringAPI.h, for example).
210
210
# - Lua headers.
211
- _THIRD_PARTY_HEADERS_PATTERN = re .compile (
211
+ _THIRD_PARTY_HEADERS_PATTERN = regex .compile (
212
212
r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$' )
213
213
214
214
# Pattern that matches only complete whitespace, possibly across multiple lines.
215
- _EMPTY_CONDITIONAL_BODY_PATTERN = re .compile (r'^\s*$' , re .DOTALL )
215
+ _EMPTY_CONDITIONAL_BODY_PATTERN = regex .compile (r'^\s*$' , regex .DOTALL )
216
216
217
217
# Alternative tokens and their replacements. For full list, see section 2.5
218
218
# Alternative tokens [lex.digraph] in the C++ standard.
@@ -238,7 +238,7 @@ def IsSourceFile(filename):
238
238
#
239
239
# False positives include C-style multi-line comments and multi-line strings
240
240
# but those have always been troublesome for cpplint.
241
- _ALT_TOKEN_REPLACEMENT_PATTERN = re .compile (
241
+ _ALT_TOKEN_REPLACEMENT_PATTERN = regex .compile (
242
242
r'[ =()](' + ('|' .join (_ALT_TOKEN_REPLACEMENT .keys ())) + r')(?=[ (]|$)' )
243
243
244
244
@@ -249,12 +249,12 @@ def IsSourceFile(filename):
249
249
_BLOCK_ASM = 3 # The whole block is an inline assembly block
250
250
251
251
# Match start of assembly blocks
252
- _MATCH_ASM = re .compile (r'^\s*(?:asm|_asm|__asm|__asm__)'
252
+ _MATCH_ASM = regex .compile (r'^\s*(?:asm|_asm|__asm|__asm__)'
253
253
r'(?:\s+(volatile|__volatile__))?'
254
254
r'\s*[{(]' )
255
255
256
256
# Match strings that indicate we're working on a C (not C++) file.
257
- _SEARCH_C_FILE = re .compile (r'\b(?:LINT_C_FILE|'
257
+ _SEARCH_C_FILE = regex .compile (r'\b(?:LINT_C_FILE|'
258
258
r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))' )
259
259
260
260
_regexp_compile_cache = {}
@@ -532,7 +532,7 @@ def Error(filename, linenum, category, confidence, message):
532
532
sys .stderr .write (final_message )
533
533
534
534
# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
535
- _RE_PATTERN_CLEANSE_LINE_ESCAPES = re .compile (
535
+ _RE_PATTERN_CLEANSE_LINE_ESCAPES = regex .compile (
536
536
r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)' )
537
537
# Match a single C style comment on the same line.
538
538
_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
@@ -544,7 +544,7 @@ def Error(filename, linenum, category, confidence, message):
544
544
# end of the line. Otherwise, we try to remove spaces from the right side,
545
545
# if this doesn't work we try on left side but only if there's a non-character
546
546
# on the right.
547
- _RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re .compile (
547
+ _RE_PATTERN_CLEANSE_LINE_C_COMMENTS = regex .compile (
548
548
r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
549
549
_RE_PATTERN_C_COMMENTS + r'\s+|' +
550
550
r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
@@ -1117,7 +1117,7 @@ def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1117
1117
1118
1118
# Matches invalid increment: *count++, which moves pointer instead of
1119
1119
# incrementing a value.
1120
- _RE_PATTERN_INVALID_INCREMENT = re .compile (
1120
+ _RE_PATTERN_INVALID_INCREMENT = regex .compile (
1121
1121
r'^\s*\*\w+(\+\+|--);' )
1122
1122
1123
1123
@@ -1309,7 +1309,7 @@ def CheckEnd(self, filename, clean_lines, linenum, error):
1309
1309
if self .name :
1310
1310
# Named namespace
1311
1311
if not Match ((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
1312
- re .escape (self .name ) + r'[\*/\.\\\s]*$' ),
1312
+ regex .escape (self .name ) + r'[\*/\.\\\s]*$' ),
1313
1313
line ):
1314
1314
error (filename , linenum , 'readability/namespace' , 5 ,
1315
1315
'Namespace should be terminated with "// namespace %s"' %
@@ -1766,7 +1766,7 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum,
1766
1766
explicit_constructor_match = Match (
1767
1767
r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
1768
1768
r'\(((?:[^()]|\([^()]*\))*)\)'
1769
- % re .escape (base_classname ),
1769
+ % regex .escape (base_classname ),
1770
1770
line )
1771
1771
1772
1772
if explicit_constructor_match :
@@ -1810,7 +1810,7 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum,
1810
1810
copy_constructor = bool (
1811
1811
onearg_constructor and
1812
1812
Match (r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
1813
- % re .escape (base_classname ), constructor_args [0 ].strip ()))
1813
+ % regex .escape (base_classname ), constructor_args [0 ].strip ()))
1814
1814
1815
1815
if (not is_marked_explicit and
1816
1816
onearg_constructor and
@@ -1912,7 +1912,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum,
1912
1912
function_state .Count () # Count non-blank/non-comment lines.
1913
1913
1914
1914
1915
- _RE_PATTERN_TODO = re .compile (r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?' )
1915
+ _RE_PATTERN_TODO = regex .compile (r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?' )
1916
1916
1917
1917
1918
1918
def CheckComment (line , filename , linenum , next_line_start , error ):
@@ -1928,7 +1928,7 @@ def CheckComment(line, filename, linenum, next_line_start, error):
1928
1928
commentpos = line .find ('//' )
1929
1929
if commentpos != - 1 :
1930
1930
# Check if the // may be in quotes. If so, ignore it
1931
- if re .sub (r'\\.' , '' , line [0 :commentpos ]).count ('"' ) % 2 == 0 :
1931
+ if regex .sub (r'\\.' , '' , line [0 :commentpos ]).count ('"' ) % 2 == 0 :
1932
1932
# Checks for common mistakes in TODO comments.
1933
1933
comment = line [commentpos :]
1934
1934
match = _RE_PATTERN_TODO .match (comment )
@@ -2088,7 +2088,7 @@ def _IsType(clean_lines, nesting_state, expr):
2088
2088
# Try a bit harder to match templated types. Walk up the nesting
2089
2089
# stack until we find something that resembles a typename
2090
2090
# declaration for what we are looking for.
2091
- typename_pattern = (r'\b(?:typename|class|struct)\s+' + re .escape (token ) +
2091
+ typename_pattern = (r'\b(?:typename|class|struct)\s+' + regex .escape (token ) +
2092
2092
r'\b' )
2093
2093
block_index = len (nesting_state .stack ) - 1
2094
2094
while block_index >= 0 :
@@ -2664,13 +2664,13 @@ def CheckStyle(filename, clean_lines, linenum, is_header, nesting_state,
2664
2664
CheckSectionSpacing (filename , clean_lines , classinfo , linenum , error )
2665
2665
2666
2666
2667
- _RE_PATTERN_INCLUDE = re .compile (r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$' )
2667
+ _RE_PATTERN_INCLUDE = regex .compile (r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$' )
2668
2668
# Matches the first component of a filename delimited by -s and _s. That is:
2669
2669
# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
2670
2670
# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
2671
2671
# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
2672
2672
# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
2673
- _RE_FIRST_COMPONENT = re .compile (r'^[^-_.]+' )
2673
+ _RE_FIRST_COMPONENT = regex .compile (r'^[^-_.]+' )
2674
2674
2675
2675
2676
2676
def CheckIncludeLine (filename , clean_lines , linenum , include_state , error ):
@@ -2730,7 +2730,7 @@ def _GetTextInside(text, start_pattern):
2730
2730
closing_punctuation = set (itervalues (matching_punctuation ))
2731
2731
2732
2732
# Find the position to start extracting text.
2733
- match = re .search (start_pattern , text , re .M )
2733
+ match = regex .search (start_pattern , text , regex .M )
2734
2734
if not match : # start_pattern not found in text.
2735
2735
return None
2736
2736
start_position = match .end (0 )
@@ -2774,7 +2774,7 @@ def _GetTextInside(text, start_pattern):
2774
2774
r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
2775
2775
r'::)+' )
2776
2776
# A call-by-reference parameter ends with '& identifier'.
2777
- _RE_PATTERN_REF_PARAM = re .compile (
2777
+ _RE_PATTERN_REF_PARAM = regex .compile (
2778
2778
r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
2779
2779
r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]' )
2780
2780
# A call-by-const-reference parameter either ends with 'const& identifier'
@@ -2868,8 +2868,8 @@ def CheckLanguage(filename, clean_lines, linenum, is_header,
2868
2868
if printf_args :
2869
2869
match = Match (r'([\w.\->()]+)$' , printf_args )
2870
2870
if match and match .group (1 ) != '__VA_ARGS__' :
2871
- function_name = re .search (r'\b((?:string)?printf)\s*\(' ,
2872
- line , re .I ).group (1 )
2871
+ function_name = regex .search (r'\b((?:string)?printf)\s*\(' ,
2872
+ line , regex .I ).group (1 )
2873
2873
error (filename , linenum , 'runtime/printf' , 4 ,
2874
2874
'Potential format string bug. Do %s("%%s", %s) instead.'
2875
2875
% (function_name , match .group (1 )))
@@ -2888,7 +2888,7 @@ def CheckLanguage(filename, clean_lines, linenum, is_header,
2888
2888
# Split the size using space and arithmetic operators as delimiters.
2889
2889
# If any of the resulting tokens are not compile time constants then
2890
2890
# report the error.
2891
- tokens = re .split (r'\s|\+|\-|\*|\/|<<|>>]' , match .group (3 ))
2891
+ tokens = regex .split (r'\s|\+|\-|\*|\/|<<|>>]' , match .group (3 ))
2892
2892
is_const = True
2893
2893
skip_next = False
2894
2894
for tok in tokens :
@@ -3227,15 +3227,15 @@ def ExpectingFunctionArgs(clean_lines, linenum):
3227
3227
('<utility>' , ('forward' , 'make_pair' , 'move' , 'swap' )),
3228
3228
)
3229
3229
3230
- _RE_PATTERN_STRING = re .compile (r'\bstring\b' )
3230
+ _RE_PATTERN_STRING = regex .compile (r'\bstring\b' )
3231
3231
3232
3232
_re_pattern_headers_maybe_templates = []
3233
3233
for _header , _templates in _HEADERS_MAYBE_TEMPLATES :
3234
3234
for _template in _templates :
3235
3235
# Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
3236
3236
# type::max().
3237
3237
_re_pattern_headers_maybe_templates .append (
3238
- (re .compile (r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]' ),
3238
+ (regex .compile (r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]' ),
3239
3239
_template ,
3240
3240
_header ))
3241
3241
@@ -3244,7 +3244,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
3244
3244
for _header , _templates in _HEADERS_CONTAINING_TEMPLATES :
3245
3245
for _template in _templates :
3246
3246
_re_pattern_templates .append (
3247
- (re .compile (r'(\<|\b)' + _template + r'\s*\<' ),
3247
+ (regex .compile (r'(\<|\b)' + _template + r'\s*\<' ),
3248
3248
_template + '<>' ,
3249
3249
_header ))
3250
3250
@@ -3422,7 +3422,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
3422
3422
'Add #include ' + required_header_unstripped + ' for ' + template )
3423
3423
3424
3424
3425
- _RE_PATTERN_EXPLICIT_MAKEPAIR = re .compile (r'\bmake_pair\s*<' )
3425
+ _RE_PATTERN_EXPLICIT_MAKEPAIR = regex .compile (r'\bmake_pair\s*<' )
3426
3426
3427
3427
3428
3428
def CheckMakePairUsesDeduction (filename , clean_lines , linenum , error ):
@@ -3694,13 +3694,13 @@ def ParseArguments(args):
3694
3694
elif opt == '--srcs' :
3695
3695
global _source_regex
3696
3696
try :
3697
- _source_regex = re .compile (val )
3697
+ _source_regex = regex .compile (val )
3698
3698
except ValueError :
3699
3699
PrintUsage ('Extensions must be comma seperated list.' )
3700
3700
elif opt == '--headers' :
3701
3701
global _header_regex
3702
3702
try :
3703
- _header_regex = re .compile (val )
3703
+ _header_regex = regex .compile (val )
3704
3704
except ValueError :
3705
3705
PrintUsage ('Extensions must be comma seperated list.' )
3706
3706
0 commit comments