Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
39389bdc
Commit
39389bdc
authored
Aug 11, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
escape C digraphs, trigraphs and other special characters in strings
parent
f40f8078
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
12 deletions
+44
-12
Cython/Utils.py
Cython/Utils.py
+19
-1
tests/run/strescapes.pyx
tests/run/strescapes.pyx
+25
-11
No files found.
Cython/Utils.py
View file @
39389bdc
...
...
@@ -99,8 +99,26 @@ class EncodedString(unicode):
# return unicode.__eq__(self, other) and \
# getattr(other, 'encoding', '') == self.encoding
def
_to_oct_sequence
(
s
):
return
''
.
join
([
'
\
\
%03o'
%
ord
(
c
)
for
c
in
s
])
_c_special
=
(
'
\
0
'
,
'??'
,
'<:'
,
':>'
,
'<%'
,
'%>'
,
'%:'
,
'%:'
)
_c_special_replacements
=
zip
(
_c_special
,
map
(
_to_oct_sequence
,
_c_special
))
def
_build_special_test
():
subexps
=
[]
for
special
in
_c_special
+
(
'
\
n
'
,
'
\
r
'
,
'
\
t
'
):
regexp
=
''
.
join
([
'[%s]'
%
c
for
c
in
special
])
subexps
.
append
(
regexp
)
return
re
.
compile
(
'('
+
'|'
.
join
(
subexps
)
+
')'
).
search
_has_specials
=
_build_special_test
()
def
escape_byte_string
(
s
):
s
=
s
.
replace
(
'
\
0
'
,
r'\000'
).
replace
(
'
\
x0A
'
,
r'\012'
).
replace
(
'
\
x0C
'
,
r'\014'
)
if
_has_specials
(
s
):
s
=
s
.
replace
(
'
\
n
'
,
r'\n'
).
replace
(
'
\
r
'
,
r'\r'
).
replace
(
'
\
t
'
,
r'\t'
)
for
special
,
replacement
in
_c_special_replacements
:
s
=
s
.
replace
(
special
,
replacement
)
try
:
s
.
decode
(
"ASCII"
)
return
s
...
...
tests/run/strescapes.pyx
View file @
39389bdc
...
...
@@ -6,13 +6,23 @@ __doc__ = u"""
... b'
\
\
x0A57',
... b'abc
\
\
x12def',
... u'
\
\
u1234',
... u'
\
\
U000
4
1234',
... u'
\
\
U000
0
1234',
... b'
\
\
u1234',
... b'
\
\
U00041234',
... b'
\
\
U00001234',
... b'
\
\
n
\
\
r
\
\
t',
... b':>',
... b'??>',
... b'
\
\
0
\
\
0
\
\
0',
... ]
>>> for i, (py_string,
c_string
) in enumerate(zip(py_strings, c_strings)):
>>> for i, (py_string,
(c_string, length)
) in enumerate(zip(py_strings, c_strings)):
... assert py_string == c_string, "%d: %r != %r" % (i, py_string, c_string)
... assert len(py_string) == length, (
... "%d: wrong length of %r, got %d, expected %d" % (
... i, py_string, len(py_string), length))
... assert len(c_string) == length, (
... "%d: wrong length of %r, got %d, expected %d" % (
... i, c_string, len(c_string), length))
"""
...
...
@@ -23,12 +33,16 @@ else:
__doc__
=
__doc__
.
replace
(
u" u'"
,
u" '"
)
c_strings
=
[
b'
\
x12
34'
,
b'
\
x0A
12
\
x0C
34'
,
b'
\
x0A
57'
,
b'abc
\
x12
def'
,
u'
\
u1234
'
,
u'
\
U00041234
'
,
b'
\
u1234
'
,
b'
\
U00041234
'
,
(
b'
\
x12
34'
,
3
),
(
b'
\
x0A
12
\
x0C
34'
,
6
),
(
b'
\
x0A
57'
,
3
),
(
b'abc
\
x12
def'
,
7
),
(
u'
\
u1234
'
,
1
),
(
u'
\
U00001234
'
,
1
),
(
b'
\
u1234
'
,
6
),
(
b'
\
U00001234
'
,
10
),
(
b'
\
n
\
r
\
t
'
,
3
),
(
b':>'
,
2
),
(
b'??>'
,
3
),
(
b'
\
0
\
0
\
0
'
,
3
),
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment