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
7aeed3fc
Commit
7aeed3fc
authored
Jun 20, 2018
by
scoder
Committed by
GitHub
Jun 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2382 from gabrieldemarmiesse/test_string_8
Adding tests for "Unicode and passing strings" part 8
parents
226f53c8
2da25846
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 @
7aeed3fc
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 @
7aeed3fc
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 @
7aeed3fc
...
...
@@ -327,20 +327,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