Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/utils/escape.py: 25%
14 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1# Copyright (c) 2010-2022 openpyxl
3"""
4OOXML has non-standard escaping for characters < \031
5"""
7import re
10def escape(value):
11 r"""
12 Convert ASCII < 31 to OOXML: \n == _x + hex(ord(\n)) + _
13 """
15 CHAR_REGEX = re.compile(r"[\001-\031]")
17 def _sub(match):
18 """
19 Callback to escape chars
20 """
21 return "_x{:0>4x}_".format(ord(match.group(0)))
23 return CHAR_REGEX.sub(_sub, value)
26def unescape(value):
27 r"""
28 Convert escaped strings to ASCIII: _x000a_ == \n
29 """
32 ESCAPED_REGEX = re.compile("_x([0-9A-Fa-f]{4})_")
34 def _sub(match):
35 """
36 Callback to unescape chars
37 """
38 return chr(int(match.group(1), 16))
40 if "_x" in value:
41 value = ESCAPED_REGEX.sub(_sub, value)
43 return value