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,
)
| 234 | |
| 235 | |
| 236 | def 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 | |
| 272 | def unescape_special_areas(data: str): |
nothing calls this directly
no test coverage detected
searching dependent graphs…