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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
a391c3a3
Commit
a391c3a3
authored
Jan 10, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
preprocess byte string literal escaping instead of doing repeated replacements at runtime
parent
f989876b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
11 deletions
+15
-11
Cython/Compiler/StringEncoding.py
Cython/Compiler/StringEncoding.py
+15
-11
No files found.
Cython/Compiler/StringEncoding.py
View file @
a391c3a3
...
@@ -165,6 +165,9 @@ char_from_escape_sequence = {
...
@@ -165,6 +165,9 @@ char_from_escape_sequence = {
r'\v'
:
u'
\
v
'
,
r'\v'
:
u'
\
v
'
,
}.
get
}.
get
_c_special
=
(
'
\
\
'
,
'??'
,
'"'
)
+
tuple
(
map
(
chr
,
range
(
32
)))
def
_to_escape_sequence
(
s
):
def
_to_escape_sequence
(
s
):
if
s
in
'
\
n
\
r
\
t
'
:
if
s
in
'
\
n
\
r
\
t
'
:
return
repr
(
s
)[
1
:
-
1
]
return
repr
(
s
)[
1
:
-
1
]
...
@@ -176,19 +179,23 @@ def _to_escape_sequence(s):
...
@@ -176,19 +179,23 @@ def _to_escape_sequence(s):
# within a character sequence, oct passes much better than hex
# within a character sequence, oct passes much better than hex
return
''
.
join
([
'
\
\
%03o'
%
ord
(
c
)
for
c
in
s
])
return
''
.
join
([
'
\
\
%03o'
%
ord
(
c
)
for
c
in
s
])
_c_special
=
(
'
\
\
'
,
'??'
,
'"'
)
+
tuple
(
map
(
chr
,
range
(
32
)))
_c_special_replacements
=
[(
orig
.
encode
(
'ASCII'
),
_to_escape_sequence
(
orig
).
encode
(
'ASCII'
))
for
orig
in
_c_special
]
def
_build_specials_
test
():
def
_build_specials_
replacer
():
subexps
=
[]
subexps
=
[]
replacements
=
{}
for
special
in
_c_special
:
for
special
in
_c_special
:
regexp
=
''
.
join
([
'[%s]'
%
c
.
replace
(
'
\
\
'
,
'
\
\
\
\
'
)
for
c
in
special
])
regexp
=
''
.
join
([
'[%s]'
%
c
.
replace
(
'
\
\
'
,
'
\
\
\
\
'
)
for
c
in
special
])
subexps
.
append
(
regexp
)
subexps
.
append
(
regexp
)
return
re
.
compile
(
'|'
.
join
(
subexps
).
encode
(
'ASCII'
)).
search
replacements
[
special
.
encode
(
'ASCII'
)]
=
_to_escape_sequence
(
special
).
encode
(
'ASCII'
)
sub
=
re
.
compile
((
'(%s)'
%
'|'
.
join
(
subexps
)).
encode
(
'ASCII'
)).
sub
def
replace_specials
(
m
):
return
replacements
[
m
.
group
(
1
)]
def
replace
(
s
):
return
sub
(
replace_specials
,
s
)
return
replace
_replace_specials
=
_build_specials_replacer
()
_has_specials
=
_build_specials_test
()
def
escape_char
(
c
):
def
escape_char
(
c
):
if
IS_PYTHON3
:
if
IS_PYTHON3
:
...
@@ -210,10 +217,7 @@ def escape_byte_string(s):
...
@@ -210,10 +217,7 @@ def escape_byte_string(s):
encoded as ISO-8859-1, will result in the correct byte sequence
encoded as ISO-8859-1, will result in the correct byte sequence
being written.
being written.
"""
"""
if
_has_specials
(
s
):
s
=
_replace_specials
(
s
)
for
special
,
replacement
in
_c_special_replacements
:
if
special
in
s
:
s
=
s
.
replace
(
special
,
replacement
)
try
:
try
:
return
s
.
decode
(
"ASCII"
)
# trial decoding: plain ASCII => done
return
s
.
decode
(
"ASCII"
)
# trial decoding: plain ASCII => done
except
UnicodeDecodeError
:
except
UnicodeDecodeError
:
...
...
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