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
2da25846
Commit
2da25846
authored
Jun 19, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Put the decoding examples of string.rst in the examples directory.
parent
5c04c1a8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
11 deletions
+17
-11
docs/examples/tutorial/string/decode.pyx
docs/examples/tutorial/string/decode.pyx
+9
-0
docs/examples/tutorial/string/naive_decode.pyx
docs/examples/tutorial/string/naive_decode.pyx
+4
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+4
-11
No files found.
docs/examples/tutorial/string/decode.pyx
0 → 100644
View file @
2da25846
from
c_func
cimport
get_a_c_string
cdef
char
*
c_string
=
NULL
cdef
Py_ssize_t
length
=
0
# get pointer and length from a C function
get_a_c_string
(
&
c_string
,
&
length
)
ustring
=
c_string
[:
length
].
decode
(
'UTF-8'
)
docs/examples/tutorial/string/naive_decode.pyx
0 → 100644
View file @
2da25846
from
c_func
cimport
c_call_returning_a_c_string
cdef
char
*
some_c_string
=
c_call_returning_a_c_string
()
ustring
=
some_c_string
.
decode
(
'UTF-8'
)
docs/src/tutorial/strings.rst
View file @
2da25846
...
...
@@ -363,20 +363,13 @@ With a Python byte string object, you would normally just call the
ustring = byte_string.decode('UTF-8')
Cython allows you to do the same for a C string, as long as it
contains no null bytes:
:
contains no null bytes:
cdef char* some_c_string = c_call_returning_a_c_string()
ustring = some_c_string.decode('UTF-8')
.. literalinclude:: ../../examples/tutorial/string/naive_decode.pyx
And, more efficiently, for strings where the length is known:
:
And, more efficiently, for strings where the length is known:
cdef char* c_string = NULL
cdef Py_ssize_t length = 0
# get pointer and length from a C function
get_a_c_string(&c_string, &length)
ustring = c_string[:length].decode('UTF-8')
.. literalinclude:: ../../examples/tutorial/string/decode.pyx
The same should be used when the string contains null bytes, e.g. when
it uses an encoding like UCS-4, where each character is encoded in four
...
...
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