Commit 7aeed3fc authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2382 from gabrieldemarmiesse/test_string_8

Adding tests for "Unicode and passing strings" part 8
parents 226f53c8 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')
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')
...@@ -327,20 +327,13 @@ With a Python byte string object, you would normally just call the ...@@ -327,20 +327,13 @@ With a Python byte string object, you would normally just call the
ustring = byte_string.decode('UTF-8') ustring = byte_string.decode('UTF-8')
Cython allows you to do the same for a C string, as long as it 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() .. literalinclude:: ../../examples/tutorial/string/naive_decode.pyx
ustring = some_c_string.decode('UTF-8')
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 .. literalinclude:: ../../examples/tutorial/string/decode.pyx
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')
The same should be used when the string contains null bytes, e.g. when 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 it uses an encoding like UCS-4, where each character is encoded in four
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment