summaryrefslogtreecommitdiff
path: root/py/objstrunicode.c
diff options
context:
space:
mode:
authorBenjamin Shockley <benjaminshockley@hotmail.com>2020-08-20 13:48:15 -0500
committerGitHub <noreply@github.com>2020-08-20 13:48:15 -0500
commit0084f0af24e4e219bc040c52ee723959423de6e2 (patch)
tree1aebdb362f08bf6417caa87836e3e4e739afc964 /py/objstrunicode.c
parent9aebe2f1efc99871fcf283c304e3a8700050d736 (diff)
parent4d7b9cde33934a44c4c905a49d2f831339fc8bd2 (diff)
Merge pull request #2 from adafruit/master
Master
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,