Skip to content
Snippets Groups Projects
Select Git revision
  • 63086117f0275c69c41ea78400b58ce6fde811f5
  • master default protected
  • backslash
  • nickname-match-configs
  • genofire/leds_rgb_get_state
  • genofire/rockets-state
  • genofire/ble-follow-py
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • genofire/haule-ble-fs-deactive
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • ios-workarounds
  • schneider/fundamental-test
  • schneider/ble-buffers
  • schneider/maxim-sdk-update
  • ch3/splashscreen
  • koalo/bhi160-works-but-dirty
  • koalo/wip/i2c-for-python
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
34 results

menu.py

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    objstr.c 54.39 KiB
    #include <stdbool.h>
    #include <string.h>
    #include <assert.h>
    
    #include "nlr.h"
    #include "misc.h"
    #include "mpconfig.h"
    #include "qstr.h"
    #include "obj.h"
    #include "runtime0.h"
    #include "runtime.h"
    #include "pfenv.h"
    #include "objstr.h"
    
    STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args);
    const mp_obj_t mp_const_empty_bytes;
    
    // use this macro to extract the string hash
    #define GET_STR_HASH(str_obj_in, str_hash) uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; }
    
    // use this macro to extract the string length
    #define GET_STR_LEN(str_obj_in, str_len) uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; }
    
    // use this macro to extract the string data and length
    #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) const byte *str_data; uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; str_data = ((mp_obj_str_t*)str_obj_in)->data; }
    
    STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
    STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
    STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
    STATIC void bad_implicit_conversion(mp_obj_t self_in) __attribute__((noreturn));
    
    /******************************************************************************/
    /* str                                                                        */
    
    void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
        // this escapes characters, but it will be very slow to print (calling print many times)
        bool has_single_quote = false;
        bool has_double_quote = false;
        for (const byte *s = str_data, *top = str_data + str_len; (!has_single_quote || !has_double_quote) && s < top; s++) {
            if (*s == '\'') {
                has_single_quote = true;
            } else if (*s == '"') {
                has_double_quote = true;
            }
        }
        int quote_char = '\'';
        if (has_single_quote && !has_double_quote) {
            quote_char = '"';
        }
        print(env, "%c", quote_char);
        for (const byte *s = str_data, *top = str_data + str_len; s < top; s++) {
            if (*s == quote_char) {
                print(env, "\\%c", quote_char);
            } else if (*s == '\\') {
                print(env, "\\\\");
            } else if (32 <= *s && *s <= 126) {
                print(env, "%c", *s);
            } else if (*s == '\n') {
                print(env, "\\n");
            } else if (*s == '\r') {
                print(env, "\\r");
            } else if (*s == '\t') {
                print(env, "\\t");
            } else {
                print(env, "\\x%02x", *s);
            }
        }
        print(env, "%c", quote_char);
    }