1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
"""
Utility functions and objects to ease Python 2/3 compatibility,
and different versions of support libraries.
"""
import codecs
import re
import warnings
from typing import Match
def cast_bytes(s, enc="utf-8"):
if isinstance(s, str):
return s.encode(enc)
return s
def ascii(stream):
return codecs.getreader("ascii")(stream)
def bopen(*args, **kwargs):
return open(*args, mode="rb", **kwargs)
long_type = int
def sign(n):
if n < 0:
return -1
if n > 0:
return 1
return 0
r_unicodeEscape = re.compile(r"(\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})")
def _unicodeExpand(s):
return r_unicodeEscape.sub(lambda m: chr(int(m.group(0)[2:], 16)), s)
def decodeStringEscape(s):
warnings.warn(
DeprecationWarning(
"rdflib.compat.decodeStringEscape() is deprecated, "
"it will be removed in rdflib 7.0.0. "
"This function is not used anywhere in rdflib anymore "
"and the utility that it does provide is not implemented correctly."
)
)
r"""
s is byte-string - replace \ escapes in string
"""
s = s.replace("\\t", "\t")
s = s.replace("\\n", "\n")
s = s.replace("\\r", "\r")
s = s.replace("\\b", "\b")
s = s.replace("\\f", "\f")
s = s.replace('\\"', '"')
s = s.replace("\\'", "'")
s = s.replace("\\\\", "\\")
return s
# return _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping
_string_escape_map = {
"t": "\t",
"b": "\b",
"n": "\n",
"r": "\r",
"f": "\f",
'"': '"',
"'": "'",
"\\": "\\",
}
def _turtle_escape_subber(match: Match[str]) -> str:
smatch, umatch = match.groups()
if smatch is not None:
return _string_escape_map[smatch]
else:
return chr(int(umatch[1:], 16))
_turtle_escape_pattern = re.compile(
r"""\\(?:([tbnrf"'\\])|(u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}))""",
)
def decodeUnicodeEscape(escaped: str) -> str:
if "\\" not in escaped:
# Most of times, there are no backslashes in strings.
return escaped
return _turtle_escape_pattern.sub(_turtle_escape_subber, escaped)
|