summaryrefslogtreecommitdiff
path: root/py/objstrunicode.c
diff options
context:
space:
mode:
authorNoralf Trønnes <noralf@tronnes.org>2019-02-12 22:42:59 +0100
committerNoralf Trønnes <noralf@tronnes.org>2019-02-14 15:42:28 +0100
commit0865c9d38193b61898d631081161cac4d3365437 (patch)
treecc8d25044ba20631cae49b2c8525c030a0916fce /py/objstrunicode.c
parent20a787adb4140002acb8e5ea21536069e184ffaf (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/objstrunicode.c')
-rw-r--r--py/objstrunicode.c20
1 files changed, 20 insertions, 0 deletions
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,