2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01: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 22:14:58 +01:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_OBJSTR_H
|
|
|
|
#define MICROPY_INCLUDED_PY_OBJSTR_H
|
2015-01-01 20:27:54 +00:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
2014-05-03 23:27:38 +01:00
|
|
|
|
2014-04-13 23:45:06 +01:00
|
|
|
typedef struct _mp_obj_str_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-22 18:34:28 +01:00
|
|
|
mp_uint_t hash;
|
2014-04-13 23:45:06 +01:00
|
|
|
// len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
|
2017-02-16 05:26:48 +00:00
|
|
|
size_t len;
|
2014-04-13 23:45:06 +01:00
|
|
|
const byte *data;
|
|
|
|
} mp_obj_str_t;
|
|
|
|
|
2015-04-09 16:29:54 +01: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 22:03:55 +01:00
|
|
|
|
2014-06-13 20:01:26 +01:00
|
|
|
// use this macro to extract the string hash
|
2016-09-02 05:42:53 +01:00
|
|
|
// warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data
|
2014-06-13 20:01:26 +01:00
|
|
|
#define GET_STR_HASH(str_obj_in, str_hash) \
|
2019-01-30 07:49:52 +00:00
|
|
|
mp_uint_t str_hash; if (mp_obj_is_qstr(str_obj_in)) \
|
2015-11-27 17:01:44 +00: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 20:01:26 +01:00
|
|
|
|
|
|
|
// use this macro to extract the string length
|
|
|
|
#define GET_STR_LEN(str_obj_in, str_len) \
|
2019-01-30 07:49:52 +00:00
|
|
|
size_t str_len; if (mp_obj_is_qstr(str_obj_in)) \
|
2015-11-27 17:01:44 +00: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 20:01:26 +01:00
|
|
|
|
|
|
|
// use this macro to extract the string data and length
|
2015-10-18 23:09:04 +01:00
|
|
|
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
|
2015-11-27 12:23:18 +00:00
|
|
|
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len);
|
2015-10-18 23:09:04 +01:00
|
|
|
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
|
2015-11-27 12:23:18 +00:00
|
|
|
size_t str_len; const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len);
|
2015-10-18 23:09:04 +01:00
|
|
|
#else
|
2014-06-13 20:01:26 +01:00
|
|
|
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
|
2019-01-30 07:49:52 +00:00
|
|
|
const byte *str_data; size_t str_len; if (mp_obj_is_qstr(str_obj_in)) \
|
2014-06-13 20:01:26 +01:00
|
|
|
{ str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \
|
2015-11-27 17:01:44 +00: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 23:09:04 +01:00
|
|
|
#endif
|
2014-06-13 20:01:26 +01:00
|
|
|
|
2016-01-03 15:55:55 +00: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 17:01:44 +00:00
|
|
|
void mp_str_print_json(const mp_print_t *print, const byte *str_data, size_t str_len);
|
2016-01-03 14:21:40 +00: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-16 02:53:04 +00:00
|
|
|
mp_obj_t mp_obj_new_str_copy(const mp_obj_type_t *type, const byte* data, size_t len);
|
2015-11-27 17:01:44 +00: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 20:01:26 +01:00
|
|
|
|
2017-08-29 04:04:01 +01: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 14:28:06 +01: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 20:01:26 +01:00
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
|
2014-06-14 22:35:09 +01:00
|
|
|
mp_obj_t index, bool is_slice);
|
2017-02-16 05:26:48 +00:00
|
|
|
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
|
2014-06-14 22:35:09 +01:00
|
|
|
|
2016-10-18 01:06:20 +01: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 20:27:54 +00: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 22:14:58 +01:00
|
|
|
#endif // MICROPY_INCLUDED_PY_OBJSTR_H
|