summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2019-02-14 10:26:57 -0800
committerGitHub <noreply@github.com>2019-02-14 10:26:57 -0800
commit35e3d99f62f908f6f371667b13120e20edb1142f (patch)
tree841621daaf3d9ab0bfc5d1523672f8f3ec28079f /py
parent379258112aad44255ef3acaada0749c556b7b09e (diff)
parent0edd739609cbcd42e4d77976f4d1e71003bf2b1a (diff)
Merge pull request #1544 from notro/ure_sub
extmod/ure: Extend functionality
Diffstat (limited to 'py')
-rwxr-xr-xpy/mpconfig.h12
-rw-r--r--py/objstr.c9
-rw-r--r--py/objstr.h2
-rw-r--r--py/objstrunicode.c20
4 files changed, 43 insertions, 0 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h
index a846f6a25..bb7952a8b 100755
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -1164,6 +1164,18 @@ typedef double mp_float_t;
#define MICROPY_PY_URE (0)
#endif
+#ifndef MICROPY_PY_URE_MATCH_GROUPS
+#define MICROPY_PY_URE_MATCH_GROUPS (0)
+#endif
+
+#ifndef MICROPY_PY_URE_MATCH_SPAN_START_END
+#define MICROPY_PY_URE_MATCH_SPAN_START_END (0)
+#endif
+
+#ifndef MICROPY_PY_URE_SUB
+#define MICROPY_PY_URE_SUB (0)
+#endif
+
#ifndef MICROPY_PY_UHEAPQ
#define MICROPY_PY_UHEAPQ (0)
#endif
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,