MCPcopy Create free account
hub / github.com/mitmproxy/mitmproxy / escape_special_areas

Function escape_special_areas

mitmproxy/utils/strutils.py:236–269  ·  view source on GitHub ↗

Escape all control characters present in special areas with UTF8 symbols in the private use plane (U+E000 t+ ord(char)). This is useful so that one can then use regex replacements on the resulting string without interfering with special areas. control_characters must be 0 < ord

(
    data: str,
    area_delimiter: Iterable[str],
    control_characters,
)

Source from the content-addressed store, hash-verified

234
235
236def escape_special_areas(
237 data: str,
238 area_delimiter: Iterable[str],
239 control_characters,
240):
241 """
242 Escape all control characters present in special areas with UTF8 symbols
243 in the private use plane (U+E000 t+ ord(char)).
244 This is useful so that one can then use regex replacements on the resulting string without
245 interfering with special areas.
246
247 control_characters must be 0 < ord(x) < 256.
248
249 Example:
250
251 >>> print(x)
252 if (true) { console.log('{}'); }
253 >>> x = escape_special_areas(x, "{", ["'" + SINGLELINE_CONTENT + "'"])
254 >>> print(x)
255 if (true) { console.log('�}'); }
256 >>> x = re.sub(r"\\s*{\\s*", " {\n ", x)
257 >>> x = unescape_special_areas(x)
258 >>> print(x)
259 if (true) {
260 console.log('{}'); }
261 """
262 buf = io.StringIO()
263 parts = split_special_areas(data, area_delimiter)
264 rex = re.compile(rf"[{control_characters}]")
265 for i, x in enumerate(parts):
266 if i % 2:
267 x = rex.sub(_move_to_private_code_plane, x)
268 buf.write(x)
269 return buf.getvalue()
270
271
272def unescape_special_areas(data: str):

Callers

nothing calls this directly

Calls 3

split_special_areasFunction · 0.85
getvalueMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…