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
3f5848fc
Commit
3f5848fc
authored
Jun 21, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved examples from string.rst and completed them to make them runnable.
parent
04780910
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
16 deletions
+23
-16
docs/examples/tutorial/string/for_bytes.pyx
docs/examples/tutorial/string/for_bytes.pyx
+6
-0
docs/examples/tutorial/string/for_unicode.pyx
docs/examples/tutorial/string/for_unicode.pyx
+6
-0
docs/examples/tutorial/string/if_char_in.pyx
docs/examples/tutorial/string/if_char_in.pyx
+5
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+6
-16
No files found.
docs/examples/tutorial/string/for_bytes.pyx
0 → 100644
View file @
3f5848fc
cdef
bytes
bytes_string
=
b'hello world'
cdef
char
c
for
c
in
bytes_string
:
if
c
==
'A'
:
print
(
"Found the letter A"
)
docs/examples/tutorial/string/for_unicode.pyx
0 → 100644
View file @
3f5848fc
cdef
unicode
ustring
=
u'Hello world'
# NOTE: no typing required for 'uchar' !
for
uchar
in
ustring
:
if
uchar
==
u'A'
:
print
(
"Found the letter A"
)
docs/examples/tutorial/string/if_char_in.pyx
0 → 100644
View file @
3f5848fc
cpdef
void
is_in
(
Py_UCS4
uchar_val
):
if
uchar_val
in
u'abcABCxY'
:
print
(
"The character is in the string."
)
else
:
print
(
"The character isn't in the string"
)
docs/src/tutorial/strings.rst
View file @
3f5848fc
...
...
@@ -676,22 +676,14 @@ C code::
for c in c_string[:100]:
if c == 'A': ...
The same applies to bytes objects:
:
The same applies to bytes objects:
cdef bytes bytes_string = ...
cdef char c
for c in bytes_string:
if c == 'A': ...
.. literalinclude:: ../../examples/tutorial/string/for_bytes.pyx
For unicode objects, Cython will automatically infer the type of the
loop variable as :c:type:`Py_UCS4`::
cdef unicode ustring = ...
loop variable as :c:type:`Py_UCS4`:
# NOTE: no typing required for 'uchar' !
for uchar in ustring:
if uchar == u'A': ...
.. literalinclude:: ../../examples/tutorial/string/for_unicode.pyx
The automatic type inference usually leads to much more efficient code
here. However, note that some unicode operations still require the
...
...
@@ -704,11 +696,9 @@ loop to enforce one-time coercion before running Python operations on
it.
There are also optimisations for ``in`` tests, so that the following
code will run in plain C code, (actually using a switch statement):
:
code will run in plain C code, (actually using a switch statement):
cdef Py_UCS4 uchar_val = get_a_unicode_character()
if uchar_val in u'abcABCxY':
...
.. literalinclude:: ../../examples/tutorial/string/if_char_in.pyx
Combined with the looping optimisation above, this can result in very
efficient character switching code, e.g. in unicode parsers.
...
...
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