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"
|
2014-05-03 18:27:38 -04:00
|
|
|
|
2014-04-13 18:45:06 -04:00
|
|
|
typedef struct _mp_obj_str_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-22 13:34:28 -04:00
|
|
|
mp_uint_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;
|
|
|
|
|
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) \
|
2014-10-03 14:52:22 -04:00
|
|
|
mp_uint_t str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) \
|
2015-11-27 12:01:44 -05:00
|
|
|
{ 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) \
|
2015-11-27 07:23:18 -05:00
|
|
|
size_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \
|
2015-11-27 12:01:44 -05:00
|
|
|
{ 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
|
2015-10-18 18:09:04 -04:00
|
|
|
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
|
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) \
|
2015-11-27 07:23:18 -05: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) \
|
2015-11-27 07:23:18 -05:00
|
|
|
const byte *str_data; size_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \
|
2014-06-13 15:01:26 -04:00
|
|
|
{ str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \
|
2015-11-27 12:01:44 -05:00
|
|
|
else { 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
|
|
|
|
2019-01-14 18:14:40 -05:00
|
|
|
mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_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);
|
2017-11-15 21:53:04 -05:00
|
|
|
mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte* data, size_t len);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, size_t len);
|
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
|
|
|
|
2019-02-12 16:42:59 -05:00
|
|
|
size_t str_offset_to_index(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
|
|
|
|
size_t offset);
|
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
|
|
|
|
2019-07-31 00:30:24 -04:00
|
|
|
const char nibble_to_hex_upper[16];
|
|
|
|
const char nibble_to_hex_lower[16];
|
|
|
|
|
2016-10-17 20:06:20 -04:00
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_2(str_join_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_KW(str_splitlines_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_KW(str_format_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_2(str_partition_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_2(str_rpartition_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_2(str_center_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_lower_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_upper_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_isspace_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_isalpha_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_isdigit_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_isupper_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_1(str_islower_obj);
|
2015-01-01 15:27:54 -05: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
|