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
Gwenaël Samain
cython
Commits
2b5b3776
Commit
2b5b3776
authored
Jun 03, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clarification on Py_UNICODE behaviour in 0.13
parent
db4876c8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
4 deletions
+14
-4
src/tutorial/strings.rst
src/tutorial/strings.rst
+14
-4
No files found.
src/tutorial/strings.rst
View file @
2b5b3776
...
...
@@ -235,7 +235,7 @@ coerce to a Python unicode object. The following will therefore print
the character ``A``::
cdef Py_UNICODE uchar_val = u'A'
assert uchar_val ==
ord(u'A') # 65
assert uchar_val ==
65 # character point value of u'A'
print( uchar_val )
Again, explicit casting will allow users to override this behaviour.
...
...
@@ -271,16 +271,26 @@ The same applies to bytes objects::
for c in bytes_string:
if c == 'A': ...
and unicode objects::
For unicode objects, Cython will automatically infer the type of the
loop variable as ``Py_UNICODE``::
cdef unicode ustring = ...
cdef Py_UNICODE uchar
# NOTE: no typing required for 'uchar' !
for uchar in ustring:
if uchar == u'A': ...
The automatic type inference usually leads to much more efficient code
here. However, note that some unicode operations still require the
value to be a Python object, so Cython may end up generating redundant
conversion code for the loop variable value inside of the loop. If
this leads to a performance degradation for a specific piece of code,
you can either type the loop variable as a Python object explicitly,
or assign it to a Python typed temporary variable to enforce one-time
coercion before running Python operations on it.
There is also an optimisation for ``in`` tests, so that the following
code will run in plain C code::
code will run in plain C code
, (actually using a switch statement)
::
cdef Py_UNICODE uchar_val = get_a_unicode_character()
if uchar_val in u'abcABCxY':
...
...
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