aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/utils
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2017-08-31 02:14:22 +0200
committerMaximilian Hils <git@maximilianhils.com>2017-09-03 21:02:29 +0200
commite1877577bc058cfd0b9141b250595b38e95a6ecf (patch)
tree57befbce91a25349e2de0f6d2c99f393ed1f770b /mitmproxy/utils
parent8030f5003d83fd520c4267c117e1c60d83f6bef5 (diff)
downloadmitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.tar.gz
mitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.tar.bz2
mitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.zip
make split_special_areas more flexible, refs #2537
(cherry picked from commit 31ef7f149e4553eb9403634c0eec6de4d0123386)
Diffstat (limited to 'mitmproxy/utils')
-rw-r--r--mitmproxy/utils/strutils.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/mitmproxy/utils/strutils.py b/mitmproxy/utils/strutils.py
index 37bed7de..71d1c54c 100644
--- a/mitmproxy/utils/strutils.py
+++ b/mitmproxy/utils/strutils.py
@@ -1,7 +1,7 @@
import io
import re
import codecs
-from typing import AnyStr, Optional, cast, Iterable, Tuple
+from typing import AnyStr, Optional, cast, Iterable
def always_bytes(str_or_bytes: Optional[AnyStr], *encode_args) -> Optional[bytes]:
@@ -153,11 +153,14 @@ def _restore_from_private_code_plane(matchobj):
NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
+MULTILINE_CONTENT = r"[\s\S]*?"
+SINGLELINE_CONTENT = r".*?"
+MULTILINE_CONTENT_LINE_CONTINUATION = r"(?:.|(?<=\\)\n)*?"
def split_special_areas(
data: str,
- area_delimiter: Iterable[Tuple[str, str]],
+ area_delimiter: Iterable[str],
):
"""
Split a string of code into a [code, special area, code, special area, ..., code] list.
@@ -166,18 +169,13 @@ def split_special_areas(
>>> split_special_areas(
>>> "test /* don't modify me */ foo",
- >>> [(r"/\*", r"\*/")]) # (left delimiter regex, right delimiter regex)
+ >>> [r"/\*[\s\S]*?\*/"]) # (regex matching comments)
["test ", "/* don't modify me */", " foo"]
"".join(split_special_areas(x, ...)) == x always holds true.
"""
- patterns = "|".join(
- r"{lchar}[\s\S]*?{rchar}".format(
- lchar=a,
- rchar=b,
- ) for (a, b) in area_delimiter)
return re.split(
- "({})".format(patterns),
+ "({})".format("|".join(area_delimiter)),
data,
flags=re.MULTILINE
)
@@ -185,7 +183,7 @@ def split_special_areas(
def escape_special_areas(
data: str,
- area_delimiter: Iterable[Tuple[str, str]],
+ area_delimiter: Iterable[str],
control_characters,
):
"""
@@ -200,11 +198,11 @@ def escape_special_areas(
>>> print(x)
if (true) { console.log('{}'); }
- >>> x = escape_special_areas(x, "{", [("'", "'")])
+ >>> x = escape_special_areas(x, "{", ["'" + SINGLELINE_CONTENT + "'"])
>>> print(x)
if (true) { console.log('�}'); }
>>> x = re.sub(r"\s*{\s*", " {\n ", x)
- >>> x = unescape_special_areas(x, "{", [("'", "'")])
+ >>> x = unescape_special_areas(x)
>>> print(x)
if (true) {
console.log('{}'); }