From abf8700778322584e502f7dcf70aba7b9caff96f Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Sun, 14 Jul 2019 12:51:42 +0200 Subject: [PATCH] doc: Document MicroPython strings Signed-off-by: Rahix <rahix@rahix.de> --- Documentation/pycardium-guide.rst | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Documentation/pycardium-guide.rst b/Documentation/pycardium-guide.rst index db115a54..54be468c 100644 --- a/Documentation/pycardium-guide.rst +++ b/Documentation/pycardium-guide.rst @@ -287,9 +287,34 @@ To get really big integers you have to use Strings ~~~~~~~ -.. todo:: +As in CPython, MicroPython also has multiple string representations: There is +``str``, ``bytes``, and ``bytearray`` but also the above mentions ``QSTR``\ s. +Ideally, code should work with as many of these as possible and to ensure this, +please use these generic functions: + +.. code-block:: cpp + + #include "py/obj.h" + + /* Create a new string object */ + char buf[] = "Hello MicroPython!"; + mp_obj_t str_obj = mp_obj_new_str(buf, sizeof(buf)); + + /* Check if an object is a string */ + if (mp_obj_is_str(str_obj)) { + /* Either str or QSTR */ + } + if (mp_obj_is_str_or_bytes) { + /* Either str, QSTR, or bytes */ + } - Strings + /* + * Get a char array from a string object. + * CAUTION! This string is not necessarily null terminated! + */ + char *str_data; + size_t str_len; + str_data = mp_obj_str_get_data(str_obj, &str_len); Lists & Tuples ~~~~~~~~~~~~~~ -- GitLab