diff options
| author | Noralf Trønnes <noralf@tronnes.org> | 2019-02-12 22:42:59 +0100 |
|---|---|---|
| committer | Noralf Trønnes <noralf@tronnes.org> | 2019-02-14 15:42:28 +0100 |
| commit | 0865c9d38193b61898d631081161cac4d3365437 (patch) | |
| tree | cc8d25044ba20631cae49b2c8525c030a0916fce /py | |
| parent | 20a787adb4140002acb8e5ea21536069e184ffaf (diff) | |
extmod/ure: Support search/match() pos and endpos parameters
MICROPY_PY_URE_MATCH_SPAN_START_END is used to enable the functionality
since it's similar.
Diffstat (limited to 'py')
| -rw-r--r-- | py/objstr.c | 9 | ||||
| -rw-r--r-- | py/objstr.h | 2 | ||||
| -rw-r--r-- | py/objstrunicode.c | 20 |
3 files changed, 31 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c index 7236d9772..ebd11d5cc 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -408,6 +408,15 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i #if !MICROPY_PY_BUILTINS_STR_UNICODE // objstrunicode defines own version +size_t str_offset_to_index(const mp_obj_type_t *type, const byte *self_data, size_t self_len, + size_t offset) { + if (offset > self_len) { + mp_raise_ValueError(translate("offset out of bounds")); + } + + return offset; +} + const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, mp_obj_t index, bool is_slice) { size_t index_val = mp_get_index(type, self_len, index, is_slice); diff --git a/py/objstr.h b/py/objstr.h index 895130446..0efe62a80 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -71,6 +71,8 @@ mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, siz mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); +size_t str_offset_to_index(const mp_obj_type_t *type, const byte *self_data, size_t self_len, + size_t offset); const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, mp_obj_t index, bool is_slice); const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction); diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 03106f987..30000a51e 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -112,6 +112,26 @@ STATIC mp_obj_t uni_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } } +size_t str_offset_to_index(const mp_obj_type_t *type, const byte *self_data, size_t self_len, + size_t offset) { + if (offset > self_len) { + mp_raise_ValueError(translate("offset out of bounds")); + } + + if (type == &mp_type_bytes) { + return offset; + } + + size_t index_val = 0; + const byte *s = self_data; + for (size_t i = 0; i < offset; i++, s++) { + if (!UTF8_IS_CONT(*s)) { + ++index_val; + } + } + return index_val; +} + // Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or // be capped to the first/last character of the string, depending on is_slice. const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, |
