|
2 | 2 |
|
3 | 3 | import json |
4 | 4 | import re |
5 | | -from typing import Dict, List |
| 5 | +from typing import Dict, List, Tuple |
6 | 6 | from app.utils.Replacement import Replacement |
7 | 7 |
|
8 | 8 |
|
| 9 | +class StringLiteralFinder: |
| 10 | + def __init__(self, line): |
| 11 | + self.string_literals = [] # type: List[Tuple[int, int]] |
| 12 | + |
| 13 | + for match in re.finditer(re.compile("\".*?\""), line): |
| 14 | + self.string_literals.append(match.span()) |
| 15 | + |
| 16 | + def is_in_string_literal(self, index): |
| 17 | + for (string_start, string_end) in self.string_literals: |
| 18 | + if string_start < index < string_end: |
| 19 | + return True |
| 20 | + |
| 21 | + return False |
| 22 | + |
| 23 | + |
| 24 | +############################################################################## |
| 25 | + |
9 | 26 | class SimplePattern: |
10 | 27 | def __init__(self, replacement_patterns): |
11 | 28 | self.replacement_patterns = replacement_patterns # type: Dict[str, List[str]] |
12 | 29 |
|
13 | 30 | def mutate(self, line): |
14 | 31 | result = [] # type: List[Replacement] |
15 | 32 |
|
| 33 | + string_finder = StringLiteralFinder(line) |
| 34 | + |
16 | 35 | for replacement_pattern in self.replacement_patterns.keys(): |
17 | 36 | for occurrence in [match for match in re.finditer(re.compile(replacement_pattern), line)]: |
| 37 | + if string_finder.is_in_string_literal(occurrence.start()): |
| 38 | + continue |
| 39 | + |
18 | 40 | for replacement_str in self.replacement_patterns[replacement_pattern]: |
19 | 41 | result.append(Replacement(start_col=occurrence.start(), |
20 | 42 | end_col=occurrence.end(), |
@@ -244,7 +266,12 @@ def __init__(self): |
244 | 266 | def find_mutations(self, line): |
245 | 267 | result = [] # type: List[Replacement] |
246 | 268 |
|
| 269 | + string_finder = StringLiteralFinder(line) |
| 270 | + |
247 | 271 | for occurrence in [match for match in re.finditer(re.compile(self.regex), line)]: |
| 272 | + if string_finder.is_in_string_literal(occurrence.start()): |
| 273 | + continue |
| 274 | + |
248 | 275 | number_str = occurrence.group(1) |
249 | 276 |
|
250 | 277 | # use JSON as means to cope with both int and double |
@@ -274,7 +301,12 @@ def __init__(self): |
274 | 301 | def find_mutations(self, line): |
275 | 302 | result = [] # type: List[Replacement] |
276 | 303 |
|
| 304 | + string_finder = StringLiteralFinder(line) |
| 305 | + |
277 | 306 | for occurrence in [match for match in re.finditer(re.compile(self.regex), line)]: |
| 307 | + if string_finder.is_in_string_literal(occurrence.start()): |
| 308 | + continue |
| 309 | + |
278 | 310 | number_str = occurrence.group() |
279 | 311 |
|
280 | 312 | # use JSON as means to cope with both int and double |
|
0 commit comments