Commit 21771276 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved stuff from string.rst to the examples directory.

parent 5c04c1a8
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)
from libc.string cimport const_char, const_uchar
cdef extern from "someheader.h":
ctypedef const_char specialChar
int process_string(const_char* s)
const_uchar* look_up_cached_string(const_uchar* key)
cdef extern from "someheader.h":
int process_string(char* s) # note: looses API information!
typedef const char specialChar;
int process_string(const char* s);
const unsigned char* look_up_cached_string(const unsigned char* key);
...@@ -300,20 +300,13 @@ Many C libraries use the ``const`` modifier in their API to declare ...@@ -300,20 +300,13 @@ Many C libraries use the ``const`` modifier in their API to declare
that they will not modify a string, or to require that users must that they will not modify a string, or to require that users must
not modify a string they return, for example: not modify a string they return, for example:
.. code-block:: c .. literalinclude:: ../../examples/tutorial/string/someheader.h
typedef const char specialChar;
int process_string(const char* s);
const unsigned char* look_up_cached_string(const unsigned char* key);
Since version 0.18, Cython has support for the ``const`` modifier in Since version 0.18, Cython has support for the ``const`` modifier in
the language, so you can declare the above functions straight away as the language, so you can declare the above functions straight away as
follows:: follows:
cdef extern from "someheader.h": .. literalinclude:: ../../examples/tutorial/string/const.pyx
ctypedef const char specialChar
int process_string(const char* s)
const unsigned char* look_up_cached_string(const unsigned char* key)
Previous versions required users to make the necessary declarations Previous versions required users to make the necessary declarations
at a textual level. If you need to support older Cython versions, at a textual level. If you need to support older Cython versions,
...@@ -322,23 +315,17 @@ you can use the following approach. ...@@ -322,23 +315,17 @@ you can use the following approach.
In general, for arguments of external C functions, the ``const`` In general, for arguments of external C functions, the ``const``
modifier does not matter and can be left out in the Cython modifier does not matter and can be left out in the Cython
declaration (e.g. in a .pxd file). The C compiler will still do declaration (e.g. in a .pxd file). The C compiler will still do
the right thing, even if you declare this to Cython:: the right thing, even if you declare this to Cython:
cdef extern from "someheader.h": .. literalinclude:: ../../examples/tutorial/string/const_left_out.pyx
int process_string(char* s) # note: looses API information!
However, in most other situations, such as for return values and However, in most other situations, such as for return values and
variables that use specifically typedef-ed API types, it does matter variables that use specifically typedef-ed API types, it does matter
and the C compiler will emit at least a warning if used incorrectly. and the C compiler will emit at least a warning if used incorrectly.
To help with this, you can use the type definitions in the To help with this, you can use the type definitions in the
``libc.string`` module, e.g.:: ``libc.string`` module, e.g.:
from libc.string cimport const_char, const_uchar
cdef extern from "someheader.h": .. literalinclude:: ../../examples/tutorial/string/const_char.pyx
ctypedef const_char specialChar
int process_string(const_char* s)
const_uchar* look_up_cached_string(const_uchar* key)
Note: even if the API only uses ``const`` for function arguments, Note: even if the API only uses ``const`` for function arguments,
it is still preferable to properly declare them using these it is still preferable to properly declare them using these
......
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