diff options
Diffstat (limited to 'docs/examples/tutorial/string')
21 files changed, 199 insertions, 198 deletions
diff --git a/docs/examples/tutorial/string/api_func.pyx b/docs/examples/tutorial/string/api_func.pyx index ec6b27751..c9e05f9e3 100644 --- a/docs/examples/tutorial/string/api_func.pyx +++ b/docs/examples/tutorial/string/api_func.pyx @@ -1,5 +1,5 @@ -from to_unicode cimport _text
-
-def api_func(s):
- text_input = _text(s)
- # ...
+from to_unicode cimport _text + +def api_func(s): + text_input = _text(s) + # ... diff --git a/docs/examples/tutorial/string/arg_memview.pyx b/docs/examples/tutorial/string/arg_memview.pyx index 63a18f943..e2b6d75be 100644 --- a/docs/examples/tutorial/string/arg_memview.pyx +++ b/docs/examples/tutorial/string/arg_memview.pyx @@ -1,5 +1,5 @@ -def process_byte_data(unsigned char[:] data):
- length = data.shape[0]
- first_byte = data[0]
- slice_view = data[1:-1]
- # ...
+def process_byte_data(unsigned char[:] data): + length = data.shape[0] + first_byte = data[0] + slice_view = data[1:-1] + # ... diff --git a/docs/examples/tutorial/string/auto_conversion_1.pyx b/docs/examples/tutorial/string/auto_conversion_1.pyx index 929c03d68..ee2b116e4 100644 --- a/docs/examples/tutorial/string/auto_conversion_1.pyx +++ b/docs/examples/tutorial/string/auto_conversion_1.pyx @@ -1,9 +1,9 @@ -# cython: c_string_type=unicode, c_string_encoding=utf8
-
-cdef char* c_string = 'abcdefg'
-
-# implicit decoding:
-cdef object py_unicode_object = c_string
-
-# explicit conversion to Python bytes:
-py_bytes_object = <bytes>c_string
+# cython: c_string_type=unicode, c_string_encoding=utf8 + +cdef char* c_string = 'abcdefg' + +# implicit decoding: +cdef object py_unicode_object = c_string + +# explicit conversion to Python bytes: +py_bytes_object = <bytes>c_string diff --git a/docs/examples/tutorial/string/auto_conversion_2.pyx b/docs/examples/tutorial/string/auto_conversion_2.pyx index 84436661d..9f7a5ad04 100644 --- a/docs/examples/tutorial/string/auto_conversion_2.pyx +++ b/docs/examples/tutorial/string/auto_conversion_2.pyx @@ -1,12 +1,12 @@ -# cython: c_string_type=str, c_string_encoding=ascii
-
-cdef char* c_string = 'abcdefg'
-
-# implicit decoding in Py3, bytes conversion in Py2:
-cdef object py_str_object = c_string
-
-# explicit conversion to Python bytes:
-py_bytes_object = <bytes>c_string
-
-# explicit conversion to Python unicode:
-py_bytes_object = <unicode>c_string
+# cython: c_string_type=str, c_string_encoding=ascii + +cdef char* c_string = 'abcdefg' + +# implicit decoding in Py3, bytes conversion in Py2: +cdef object py_str_object = c_string + +# explicit conversion to Python bytes: +py_bytes_object = <bytes>c_string + +# explicit conversion to Python unicode: +py_bytes_object = <unicode>c_string diff --git a/docs/examples/tutorial/string/auto_conversion_3.pyx b/docs/examples/tutorial/string/auto_conversion_3.pyx index 509d7e324..b9a1b7517 100644 --- a/docs/examples/tutorial/string/auto_conversion_3.pyx +++ b/docs/examples/tutorial/string/auto_conversion_3.pyx @@ -1,6 +1,6 @@ -# cython: c_string_type=unicode, c_string_encoding=ascii
-
-def func():
- ustring = u'abc'
- cdef char* s = ustring
- return s[0] # returns u'a'
+# cython: c_string_type=unicode, c_string_encoding=ascii + +def func(): + ustring = u'abc' + cdef char* s = ustring + return s[0] # returns u'a' diff --git a/docs/examples/tutorial/string/c_func.pyx b/docs/examples/tutorial/string/c_func.pyx index a456b815b..4763f2671 100644 --- a/docs/examples/tutorial/string/c_func.pyx +++ b/docs/examples/tutorial/string/c_func.pyx @@ -1,22 +1,23 @@ -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))
- if not c_string:
- raise MemoryError()
- 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))
- if not c_string_ptr[0]:
- raise MemoryError()
-
- strcpy(c_string_ptr[0], hello_world)
- length[0] = n
+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)) + if not c_string: + return NULL # malloc failed + + 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)) + if not c_string_ptr[0]: + return # malloc failed + + strcpy(c_string_ptr[0], hello_world) + length[0] = n diff --git a/docs/examples/tutorial/string/const.pyx b/docs/examples/tutorial/string/const.pyx index e066c77ac..0b89b9e41 100644 --- a/docs/examples/tutorial/string/const.pyx +++ b/docs/examples/tutorial/string/const.pyx @@ -1,4 +1,4 @@ -cdef extern from "someheader.h":
- ctypedef const char specialChar
- int process_string(const char* s)
- const unsigned char* look_up_cached_string(const unsigned char* key)
+cdef extern from "someheader.h": + ctypedef const char specialChar + int process_string(const char* s) + const unsigned char* look_up_cached_string(const unsigned char* key) diff --git a/docs/examples/tutorial/string/cpp_string.pyx b/docs/examples/tutorial/string/cpp_string.pyx index b6c9e44f9..313e72a67 100644 --- a/docs/examples/tutorial/string/cpp_string.pyx +++ b/docs/examples/tutorial/string/cpp_string.pyx @@ -1,12 +1,12 @@ -# distutils: language = c++
-
-from libcpp.string cimport string
-
-def get_bytes():
- py_bytes_object = b'hello world'
- cdef string s = py_bytes_object
-
- s.append('abc')
- py_bytes_object = s
- return py_bytes_object
-
+# distutils: language = c++ + +from libcpp.string cimport string + +def get_bytes(): + py_bytes_object = b'hello world' + cdef string s = py_bytes_object + + s.append('abc') + py_bytes_object = s + return py_bytes_object + diff --git a/docs/examples/tutorial/string/decode.pyx b/docs/examples/tutorial/string/decode.pyx index c3e2faf68..79fc41835 100644 --- a/docs/examples/tutorial/string/decode.pyx +++ b/docs/examples/tutorial/string/decode.pyx @@ -1,9 +1,9 @@ -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 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') diff --git a/docs/examples/tutorial/string/decode_cpp_string.pyx b/docs/examples/tutorial/string/decode_cpp_string.pyx index 8f1d01af8..52861c209 100644 --- a/docs/examples/tutorial/string/decode_cpp_string.pyx +++ b/docs/examples/tutorial/string/decode_cpp_string.pyx @@ -1,10 +1,10 @@ -# distutils: language = c++
-
-from libcpp.string cimport string
-
-def get_ustrings():
- cdef string s = string(b'abcdefg')
-
- ustring1 = s.decode('UTF-8')
- ustring2 = s[2:-2].decode('UTF-8')
- return ustring1, ustring2
+# distutils: language = c++ + +from libcpp.string cimport string + +def get_ustrings(): + cdef string s = string(b'abcdefg') + + ustring1 = s.decode('UTF-8') + ustring2 = s[2:-2].decode('UTF-8') + return ustring1, ustring2 diff --git a/docs/examples/tutorial/string/for_bytes.pyx b/docs/examples/tutorial/string/for_bytes.pyx index 69e9202ae..d4d3e1f81 100644 --- a/docs/examples/tutorial/string/for_bytes.pyx +++ b/docs/examples/tutorial/string/for_bytes.pyx @@ -1,6 +1,6 @@ -cdef bytes bytes_string = b"hello to A bytes' world"
-
-cdef char c
-for c in bytes_string:
- if c == 'A':
- print("Found the letter A")
+cdef bytes bytes_string = b"hello to A bytes' world" + +cdef char c +for c in bytes_string: + if c == 'A': + print("Found the letter A") diff --git a/docs/examples/tutorial/string/for_char.pyx b/docs/examples/tutorial/string/for_char.pyx index adc16bcd8..81abae97c 100644 --- a/docs/examples/tutorial/string/for_char.pyx +++ b/docs/examples/tutorial/string/for_char.pyx @@ -1,6 +1,6 @@ -cdef char* c_string = "Hello to A C-string's world"
-
-cdef char c
-for c in c_string[:11]:
- if c == 'A':
- print("Found the letter A")
+cdef char* c_string = "Hello to A C-string's world" + +cdef char c +for c in c_string[:11]: + if c == 'A': + print("Found the letter A") diff --git a/docs/examples/tutorial/string/for_unicode.pyx b/docs/examples/tutorial/string/for_unicode.pyx index aabd562e4..0f8ab0c7d 100644 --- a/docs/examples/tutorial/string/for_unicode.pyx +++ b/docs/examples/tutorial/string/for_unicode.pyx @@ -1,6 +1,6 @@ -cdef unicode ustring = u'Hello world'
-
-# NOTE: no typing required for 'uchar' !
-for uchar in ustring:
- if uchar == u'A':
- print("Found the letter A")
+cdef unicode ustring = u'Hello world' + +# NOTE: no typing required for 'uchar' ! +for uchar in ustring: + if uchar == u'A': + print("Found the letter A") diff --git a/docs/examples/tutorial/string/if_char_in.pyx b/docs/examples/tutorial/string/if_char_in.pyx index 73521b2de..e33e18d59 100644 --- a/docs/examples/tutorial/string/if_char_in.pyx +++ b/docs/examples/tutorial/string/if_char_in.pyx @@ -1,5 +1,5 @@ -cpdef void is_in(Py_UCS4 uchar_val):
- if uchar_val in u'abcABCxY':
- print("The character is in the string.")
- else:
- print("The character is not in the string")
+cpdef void is_in(Py_UCS4 uchar_val): + if uchar_val in u'abcABCxY': + print("The character is in the string.") + else: + print("The character is not in the string") diff --git a/docs/examples/tutorial/string/naive_decode.pyx b/docs/examples/tutorial/string/naive_decode.pyx index 186d2affa..b3c44d9af 100644 --- a/docs/examples/tutorial/string/naive_decode.pyx +++ b/docs/examples/tutorial/string/naive_decode.pyx @@ -1,4 +1,4 @@ -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')
+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') diff --git a/docs/examples/tutorial/string/return_memview.pyx b/docs/examples/tutorial/string/return_memview.pyx index f6233436a..be9b597a4 100644 --- a/docs/examples/tutorial/string/return_memview.pyx +++ b/docs/examples/tutorial/string/return_memview.pyx @@ -1,9 +1,9 @@ -def process_byte_data(unsigned char[:] data):
- # ... process the data, here, dummy processing.
- cdef bint return_all = (data[0] == 108)
-
- if return_all:
- return bytes(data)
- else:
- # example for returning a slice
- return bytes(data[5:7])
+def process_byte_data(unsigned char[:] data): + # ... process the data, here, dummy processing. + cdef bint return_all = (data[0] == 108) + + if return_all: + return bytes(data) + else: + # example for returning a slice + return bytes(data[5:7]) diff --git a/docs/examples/tutorial/string/slicing_c_string.pyx b/docs/examples/tutorial/string/slicing_c_string.pyx index 2e937430e..f8d272e32 100644 --- a/docs/examples/tutorial/string/slicing_c_string.pyx +++ b/docs/examples/tutorial/string/slicing_c_string.pyx @@ -1,15 +1,15 @@ -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)
-
- try:
- py_bytes_string = c_string[:length] # Performs a copy of the data
- finally:
- free(c_string)
+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) + + try: + py_bytes_string = c_string[:length] # Performs a copy of the data + finally: + free(c_string) diff --git a/docs/examples/tutorial/string/to_char.pyx b/docs/examples/tutorial/string/to_char.pyx index 5e4a16161..7912ea485 100644 --- a/docs/examples/tutorial/string/to_char.pyx +++ b/docs/examples/tutorial/string/to_char.pyx @@ -1,8 +1,8 @@ -# define a global name for whatever char type is used in the module
-ctypedef unsigned char char_type
-
-cdef char_type[:] _chars(s):
- if isinstance(s, unicode):
- # encode to the specific encoding used inside of the module
- s = (<unicode>s).encode('utf8')
- return s
+# define a global name for whatever char type is used in the module +ctypedef unsigned char char_type + +cdef char_type[:] _chars(s): + if isinstance(s, unicode): + # encode to the specific encoding used inside of the module + s = (<unicode>s).encode('utf8') + return s diff --git a/docs/examples/tutorial/string/to_unicode.pyx b/docs/examples/tutorial/string/to_unicode.pyx index 8ab8f2662..188a8776e 100644 --- a/docs/examples/tutorial/string/to_unicode.pyx +++ b/docs/examples/tutorial/string/to_unicode.pyx @@ -1,22 +1,22 @@ -# to_unicode.pyx
-
-from cpython.version cimport PY_MAJOR_VERSION
-
-cdef unicode _text(s):
- if type(s) is unicode:
- # Fast path for most common case(s).
- return <unicode>s
-
- elif PY_MAJOR_VERSION < 3 and isinstance(s, bytes):
- # Only accept byte strings as text input in Python 2.x, not in Py3.
- return (<bytes>s).decode('ascii')
-
- elif isinstance(s, unicode):
- # We know from the fast path above that 's' can only be a subtype here.
- # An evil cast to <unicode> might still work in some(!) cases,
- # depending on what the further processing does. To be safe,
- # we can always create a copy instead.
- return unicode(s)
-
- else:
- raise TypeError("Could not convert to unicode.")
+# to_unicode.pyx + +from cpython.version cimport PY_MAJOR_VERSION + +cdef unicode _text(s): + if type(s) is unicode: + # Fast path for most common case(s). + return <unicode>s + + elif PY_MAJOR_VERSION < 3 and isinstance(s, bytes): + # Only accept byte strings as text input in Python 2.x, not in Py3. + return (<bytes>s).decode('ascii') + + elif isinstance(s, unicode): + # We know from the fast path above that 's' can only be a subtype here. + # An evil cast to <unicode> might still work in some(!) cases, + # depending on what the further processing does. To be safe, + # we can always create a copy instead. + return unicode(s) + + else: + raise TypeError("Could not convert to unicode.") diff --git a/docs/examples/tutorial/string/try_finally.pyx b/docs/examples/tutorial/string/try_finally.pyx index aea786f5b..4cf943e56 100644 --- a/docs/examples/tutorial/string/try_finally.pyx +++ b/docs/examples/tutorial/string/try_finally.pyx @@ -1,9 +1,9 @@ -from libc.stdlib cimport free
-from c_func cimport c_call_returning_a_c_string
-
-cdef bytes py_string
-cdef char* c_string = c_call_returning_a_c_string()
-try:
- py_string = c_string
-finally:
- free(c_string)
+from libc.stdlib cimport free +from c_func cimport c_call_returning_a_c_string + +cdef bytes py_string +cdef char* c_string = c_call_returning_a_c_string() +try: + py_string = c_string +finally: + free(c_string) diff --git a/docs/examples/tutorial/string/utf_eight.pyx b/docs/examples/tutorial/string/utf_eight.pyx index 7113947f4..654c51ae8 100644 --- a/docs/examples/tutorial/string/utf_eight.pyx +++ b/docs/examples/tutorial/string/utf_eight.pyx @@ -1,15 +1,15 @@ -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:
+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 |