summaryrefslogtreecommitdiff
path: root/docs/examples/tutorial/string
diff options
context:
space:
mode:
authorgabrieldemarmiesse <gabriel.demarmiesse@teraki.com>2018-06-16 19:08:11 +0200
committergabrieldemarmiesse <gabriel.demarmiesse@teraki.com>2018-06-16 19:08:11 +0200
commit9cbd59ed0a01b622e3895b1909c27f43adf873e0 (patch)
tree59e10a6e575f30278076079865b05ab7ff623857 /docs/examples/tutorial/string
parent3d291a58b10e3f27a1933299b8787da69791e035 (diff)
downloadcython-9cbd59ed0a01b622e3895b1909c27f43adf873e0.tar.gz
Extended the examples of string.rst and put them in the examples directory for testing.
Diffstat (limited to 'docs/examples/tutorial/string')
-rw-r--r--docs/examples/tutorial/string/assignment.pyx12
-rw-r--r--docs/examples/tutorial/string/c_func.pxd2
-rw-r--r--docs/examples/tutorial/string/c_func.pyx18
-rw-r--r--docs/examples/tutorial/string/slicing_c_string.pyx16
4 files changed, 48 insertions, 0 deletions
diff --git a/docs/examples/tutorial/string/assignment.pyx b/docs/examples/tutorial/string/assignment.pyx
new file mode 100644
index 000000000..aaf3fef4f
--- /dev/null
+++ b/docs/examples/tutorial/string/assignment.pyx
@@ -0,0 +1,12 @@
+from libc.stdlib cimport free
+from c_func cimport c_call_returning_a_c_string
+
+
+def main():
+ cdef char* c_string = c_call_returning_a_c_string()
+ cdef bytes py_string = c_string
+
+ # A type cast to `object` or `bytes` will do the same thing:
+ py_string = <bytes> c_string
+
+ free(c_string)
diff --git a/docs/examples/tutorial/string/c_func.pxd b/docs/examples/tutorial/string/c_func.pxd
new file mode 100644
index 000000000..1a31aabb3
--- /dev/null
+++ b/docs/examples/tutorial/string/c_func.pxd
@@ -0,0 +1,2 @@
+cdef char* c_call_returning_a_c_string()
+cdef void get_a_c_string(char** c_string, Py_ssize_t *length)
diff --git a/docs/examples/tutorial/string/c_func.pyx b/docs/examples/tutorial/string/c_func.pyx
new file mode 100644
index 000000000..c428220a2
--- /dev/null
+++ b/docs/examples/tutorial/string/c_func.pyx
@@ -0,0 +1,18 @@
+from libc.stdlib cimport malloc
+from libc.string cimport strcpy, strlen
+
+cdef char* hello_world = 'hello world'
+cdef Py_ssize_t n = strlen(hello_world)
+
+
+cdef char* c_call_returning_a_c_string():
+ cdef char* c_string = <char *> malloc((n + 1) * sizeof(char))
+ strcpy(c_string, hello_world)
+ return c_string
+
+
+cdef void get_a_c_string(char** c_string_ptr, Py_ssize_t *length):
+ c_string_ptr[0] = <char *> malloc((n + 1) * sizeof(char))
+
+ strcpy(c_string_ptr[0], hello_world)
+ length[0] = n
diff --git a/docs/examples/tutorial/string/slicing_c_string.pyx b/docs/examples/tutorial/string/slicing_c_string.pyx
new file mode 100644
index 000000000..efe0cc0df
--- /dev/null
+++ b/docs/examples/tutorial/string/slicing_c_string.pyx
@@ -0,0 +1,16 @@
+from libc.stdlib cimport free
+from c_func cimport get_a_c_string
+
+
+def main():
+ 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)
+
+ py_bytes_string = c_string[:length]
+
+ free(c_string)
+ print(py_bytes_string) # py_bytes_string is still available
+