2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_OBJSTR_H
|
|
|
|
#define MICROPY_INCLUDED_PY_OBJSTR_H
|
2015-01-01 15:27:54 -05:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
2022-08-10 00:13:17 -04:00
|
|
|
#include "py/objarray.h"
|
2014-05-03 18:27:38 -04:00
|
|
|
|
2023-09-28 16:22:10 -04:00
|
|
|
// CIRCUITPY
|
|
|
|
extern const char nibble_to_hex_upper[16];
|
|
|
|
extern const char nibble_to_hex_lower[16];
|
|
|
|
|
2014-04-13 18:45:06 -04:00
|
|
|
typedef struct _mp_obj_str_t {
|
|
|
|
mp_obj_base_t base;
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t hash;
|
2014-04-13 18:45:06 -04:00
|
|
|
// len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
|
2017-02-16 00:26:48 -05:00
|
|
|
size_t len;
|
2014-04-13 18:45:06 -04:00
|
|
|
const byte *data;
|
|
|
|
} mp_obj_str_t;
|
|
|
|
|
2022-08-10 00:13:17 -04:00
|
|
|
// This static assert is used to ensure that mp_obj_str_t and mp_obj_array_t are compatible,
|
|
|
|
// meaning that their len and data/items entries are at the same offsets in the struct.
|
|
|
|
// This allows the same code to be used for str/bytes and bytearray.
|
|
|
|
#define MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE \
|
|
|
|
MP_STATIC_ASSERT(offsetof(mp_obj_str_t, len) == offsetof(mp_obj_array_t, len) \
|
|
|
|
&& offsetof(mp_obj_str_t, data) == offsetof(mp_obj_array_t, items))
|
|
|
|
|
2015-04-09 11:29:54 -04:00
|
|
|
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte *)str}
|
2014-04-15 17:03:55 -04:00
|
|
|
|
2014-06-13 15:01:26 -04:00
|
|
|
// use this macro to extract the string hash
|
2016-09-02 00:42:53 -04:00
|
|
|
// warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data
|
2014-06-13 15:01:26 -04:00
|
|
|
#define GET_STR_HASH(str_obj_in, str_hash) \
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t str_hash; \
|
2022-08-10 00:09:22 -04:00
|
|
|
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 *)MP_OBJ_TO_PTR(str_obj_in))->hash; \
|
|
|
|
}
|
2014-06-13 15:01:26 -04:00
|
|
|
|
|
|
|
// use this macro to extract the string length
|
|
|
|
#define GET_STR_LEN(str_obj_in, str_len) \
|
2022-08-10 00:09:22 -04:00
|
|
|
size_t 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 *)MP_OBJ_TO_PTR(str_obj_in))->len; \
|
|
|
|
}
|
2014-06-13 15:01:26 -04:00
|
|
|
|
|
|
|
// use this macro to extract the string data and length
|
2019-12-27 07:15:52 -05:00
|
|
|
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
|
2015-11-27 07:23:18 -05:00
|
|
|
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len);
|
2015-10-18 18:09:04 -04:00
|
|
|
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
|
2022-08-10 00:09:22 -04:00
|
|
|
size_t str_len; \
|
|
|
|
const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len);
|
2015-10-18 18:09:04 -04:00
|
|
|
#else
|
2014-06-13 15:01:26 -04:00
|
|
|
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
|
2022-08-10 00:09:22 -04:00
|
|
|
const byte *str_data; \
|
|
|
|
size_t str_len; \
|
|
|
|
if (mp_obj_is_qstr(str_obj_in)) { \
|
|
|
|
str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); \
|
|
|
|
} else { \
|
2022-08-10 00:13:17 -04:00
|
|
|
MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE; \
|
2022-08-10 00:09:22 -04:00
|
|
|
str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \
|
|
|
|
str_data = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->data; \
|
|
|
|
}
|
2015-10-18 18:09:04 -04:00
|
|
|
#endif
|
2014-06-13 15:01:26 -04:00
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
2015-11-27 12:01:44 -05:00
|
|
|
void mp_str_print_json(const mp_print_t *print, const byte *str_data, size_t str_len);
|
2016-01-03 09:21:40 -05:00
|
|
|
mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
|
|
|
|
mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args);
|
2022-08-23 22:22:57 -04:00
|
|
|
mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte *data, size_t len); // for type=str, input data must be valid utf-8
|
|
|
|
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len); // for type=str, will check utf-8 (raises UnicodeError)
|
2014-06-13 15:01:26 -04:00
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
2014-08-30 09:28:06 -04:00
|
|
|
mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
|
2014-06-13 15:01:26 -04:00
|
|
|
|
2022-10-06 20:06:43 -04:00
|
|
|
void mp_obj_str_set_data(mp_obj_str_t *str, const byte *data, size_t len);
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
|
2014-06-14 17:35:09 -04:00
|
|
|
mp_obj_t index, bool is_slice);
|
2017-02-16 00:26:48 -05:00
|
|
|
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
|
2014-06-14 17:35:09 -04:00
|
|
|
|
2021-07-13 04:01:12 -04:00
|
|
|
mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type);
|
|
|
|
mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data);
|
|
|
|
|
2022-08-10 00:13:17 -04:00
|
|
|
extern const mp_obj_dict_t mp_obj_str_locals_dict;
|
|
|
|
|
2021-07-13 04:01:12 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW && MICROPY_PY_BUILTINS_BYTES_HEX
|
|
|
|
extern const mp_obj_dict_t mp_obj_memoryview_locals_dict;
|
|
|
|
#endif
|
|
|
|
|
2022-08-10 00:13:17 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_BYTEARRAY
|
|
|
|
extern const mp_obj_dict_t mp_obj_bytearray_locals_dict;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_ARRAY
|
|
|
|
extern const mp_obj_dict_t mp_obj_array_locals_dict;
|
|
|
|
#endif
|
|
|
|
|
2023-09-22 11:40:11 -04:00
|
|
|
// CIRCUITPY
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj);
|
2023-09-22 14:43:58 -04:00
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_obj_bytes_hex_as_str_obj);
|
2023-09-22 11:40:11 -04:00
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#endif // MICROPY_INCLUDED_PY_OBJSTR_H
|