Commit b1667e9e authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2384 from gabrieldemarmiesse/test_string_9

Adding tests for "Unicode and passing strings" part 9
parents d385e3f3 715d093b
from libc.stdlib cimport free
cdef unicode tounicode(char* s):
return s.decode('UTF-8', 'strict')
cdef unicode tounicode_with_length(
char* s, size_t length):
return s[:length].decode('UTF-8', 'strict')
cdef unicode tounicode_with_length_and_free(
char* s, size_t length):
try:
return s[:length].decode('UTF-8', 'strict')
finally:
free(s)
\ No newline at end of file
......@@ -306,23 +306,9 @@ that are 'obviously' correct than to rely on the data to be as expected.
It is common practice to wrap string conversions (and non-trivial type
conversions in general) in dedicated functions, as this needs to be
done in exactly the same way whenever receiving text from C. This
could look as follows::
could look as follows:
from libc.stdlib cimport free
cdef unicode tounicode(char* s):
return s.decode('UTF-8', 'strict')
cdef unicode tounicode_with_length(
char* s, size_t length):
return s[:length].decode('UTF-8', 'strict')
cdef unicode tounicode_with_length_and_free(
char* s, size_t length):
try:
return s[:length].decode('UTF-8', 'strict')
finally:
free(s)
.. literalinclude:: ../../examples/tutorial/string/utf_eight.pyx
Most likely, you will prefer shorter function names in your code based
on the kind of string being handled. Different types of content often
......
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