Skip to content

Commit 20ae6a9

Browse files
authored
Merge pull request #449 from FlorianBracq/florian/pairwise-todo
Implement pairwise TODO in converstion/base.py
2 parents 598a73e + d489358 commit 20ae6a9

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

sigma/conversion/base.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from abc import ABC, abstractmethod
33
from collections import ChainMap, defaultdict
44
from contextlib import contextmanager
5+
from itertools import pairwise
56
from typing import Any, Callable, ClassVar, Iterator, Pattern, cast
67

78
from typing_extensions import Self
@@ -1717,15 +1718,11 @@ def escape_and_quote_field(self, field_name: str) -> str:
17171718
match_positions.update((match.start() for match in re_quote.finditer(field_name)))
17181719

17191720
if len(match_positions) > 0: # found matches, escape them
1720-
r = [0] + list(sorted(match_positions)) + [len(field_name)]
1721-
escaped_field_name = ""
1722-
for i in range(
1723-
len(r) - 1
1724-
): # TODO: from Python 3.10 this can be replaced with itertools.pairwise(), but for now we keep support for Python <3.10
1725-
if i == 0: # The first range is passed to the result without escaping
1726-
escaped_field_name += field_name[r[i] : r[i + 1]]
1727-
else: # Subsequent ranges are positions of matches and therefore are prepended with field_escape
1728-
escaped_field_name += self.field_escape + field_name[r[i] : r[i + 1]]
1721+
indices = [0, *sorted(match_positions), len(field_name)]
1722+
escaped_field_name = self.field_escape.join(
1723+
field_name[first_index:second_index]
1724+
for (first_index, second_index) in pairwise(indices)
1725+
)
17291726
else: # no matches, just pass original field name without escaping
17301727
escaped_field_name = field_name
17311728
else:

0 commit comments

Comments
 (0)