1515
1616from quality_snapshot import SnapshotError , parse_lcov
1717
18+ RAW_STRING_START = re .compile (r'(?:br|cr|r)(#*)"' )
19+ CHARACTER_LITERAL = re .compile (r"'(?:[^'\\\n]|\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.))'" )
20+ ITEM_ATTRIBUTE = re .compile (r'\s*#\s*\[' )
21+
1822
1923def run (* args : str , cwd : Path ) -> str :
2024 return subprocess .check_output (args , cwd = cwd , text = True , encoding = 'utf-8' )
@@ -112,10 +116,10 @@ def code_mask(source: str) -> str:
112116 if depth :
113117 raise SnapshotError ('Unterminated Rust block comment' )
114118 else :
115- raw = re .match (r'(?:br|cr|r)(#*)"' , source [ i :] )
119+ raw = RAW_STRING_START .match (source , i )
116120 if raw :
117121 close = '"' + raw [1 ]
118- end = source .find (close , i + raw .end ())
122+ end = source .find (close , raw .end ())
119123 if end == - 1 :
120124 raise SnapshotError ('Unterminated Rust raw string' )
121125 end += len (close )
@@ -132,9 +136,9 @@ def code_mask(source: str) -> str:
132136 else :
133137 raise SnapshotError ('Unterminated Rust string' )
134138 elif source [i ] == "'" :
135- char = re .match (r"'(?:[^'\\\n]|\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.))'" , source [ i :] )
139+ char = CHARACTER_LITERAL .match (source , i )
136140 if char :
137- end = i + char .end ()
141+ end = char .end ()
138142 if end > i :
139143 for n in range (i , end ):
140144 if chars [n ] != '\n ' :
@@ -153,10 +157,10 @@ def test_lines(source: str) -> set[int]:
153157 end = match .end ()
154158 # Other attributes belong to this same item, not its body.
155159 while True :
156- attr = re .match (r'\s*#\s*\[' , masked [ end :] )
160+ attr = ITEM_ATTRIBUTE .match (masked , end )
157161 if not attr :
158162 break
159- end + = attr .end ()
163+ end = attr .end ()
160164 depth = 1
161165 while depth and end < len (masked ):
162166 depth += (masked [end ] == '[' ) - (masked [end ] == ']' )
0 commit comments