objstr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_OBJSTR_H
  27. #define MICROPY_INCLUDED_PY_OBJSTR_H
  28. #include "py/obj.h"
  29. #include "py/objarray.h"
  30. typedef struct _mp_obj_str_t {
  31. mp_obj_base_t base;
  32. size_t hash;
  33. // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
  34. size_t len;
  35. const byte *data;
  36. } mp_obj_str_t;
  37. // This static assert is used to ensure that mp_obj_str_t and mp_obj_array_t are compatible,
  38. // meaning that their len and data/items entries are at the same offsets in the struct.
  39. // This allows the same code to be used for str/bytes and bytearray.
  40. #define MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE \
  41. MP_STATIC_ASSERT(offsetof(mp_obj_str_t, len) == offsetof(mp_obj_array_t, len) \
  42. && offsetof(mp_obj_str_t, data) == offsetof(mp_obj_array_t, items))
  43. #define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte *)str}
  44. // use this macro to extract the string hash
  45. // warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data
  46. #define GET_STR_HASH(str_obj_in, str_hash) \
  47. size_t str_hash; \
  48. if (mp_obj_is_qstr(str_obj_in)) { \
  49. str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); \
  50. } else { \
  51. str_hash = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->hash; \
  52. }
  53. // use this macro to extract the string length
  54. #define GET_STR_LEN(str_obj_in, str_len) \
  55. size_t str_len; \
  56. if (mp_obj_is_qstr(str_obj_in)) { \
  57. str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); \
  58. } else { \
  59. str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \
  60. }
  61. // use this macro to extract the string data and length
  62. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  63. const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len);
  64. #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
  65. size_t str_len; \
  66. const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len);
  67. #else
  68. #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
  69. const byte *str_data; \
  70. size_t str_len; \
  71. if (mp_obj_is_qstr(str_obj_in)) { \
  72. str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); \
  73. } else { \
  74. MP_STATIC_ASSERT_STR_ARRAY_COMPATIBLE; \
  75. str_len = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->len; \
  76. str_data = ((mp_obj_str_t *)MP_OBJ_TO_PTR(str_obj_in))->data; \
  77. }
  78. #endif
  79. 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);
  80. void mp_str_print_json(const mp_print_t *print, const byte *str_data, size_t str_len);
  81. mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
  82. mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args);
  83. 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
  84. 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)
  85. mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in);
  86. mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  87. void mp_obj_str_set_data(mp_obj_str_t *str, const byte *data, size_t len);
  88. const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
  89. mp_obj_t index, bool is_slice);
  90. const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
  91. #define MP_DEFINE_BYTES_OBJ(obj_name, target, len) mp_obj_str_t obj_name = {{&mp_type_bytes}, 0, (len), (const byte *)(target)}
  92. mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type_t *type);
  93. mp_obj_t mp_obj_bytes_fromhex(mp_obj_t type_in, mp_obj_t data);
  94. extern const mp_obj_dict_t mp_obj_str_locals_dict;
  95. #if MICROPY_PY_BUILTINS_MEMORYVIEW && MICROPY_PY_BUILTINS_BYTES_HEX
  96. extern const mp_obj_dict_t mp_obj_memoryview_locals_dict;
  97. #endif
  98. #if MICROPY_PY_BUILTINS_BYTEARRAY
  99. extern const mp_obj_dict_t mp_obj_bytearray_locals_dict;
  100. #endif
  101. #if MICROPY_PY_ARRAY
  102. extern const mp_obj_dict_t mp_obj_array_locals_dict;
  103. #endif
  104. #endif // MICROPY_INCLUDED_PY_OBJSTR_H