Skip to content

Commit ea2c936

Browse files
committed
objstrunicode: Refactor str_index_to_ptr() following objstr.
1 parent 26fda6d commit ea2c936

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

py/objstr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,14 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
344344
return MP_OBJ_NULL; // op not supported
345345
}
346346

347+
#if !MICROPY_PY_BUILTINS_STR_UNICODE
348+
// objstrunicode defines own version
347349
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, uint self_len,
348350
mp_obj_t index, bool is_slice) {
349351
machine_uint_t index_val = mp_get_index(type, self_len, index, is_slice);
350352
return self_data + index_val;
351353
}
354+
#endif
352355

353356
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
354357
mp_obj_type_t *type = mp_obj_get_type(self_in);

py/objstr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uin
5757
mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
5858
machine_int_t str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, int flags);
5959

60+
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, uint self_len,
61+
mp_obj_t index, bool is_slice);
62+
6063
MP_DECLARE_CONST_FUN_OBJ(str_encode_obj);
6164
MP_DECLARE_CONST_FUN_OBJ(str_find_obj);
6265
MP_DECLARE_CONST_FUN_OBJ(str_rfind_obj);

py/objstrunicode.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
154154

155155
// Convert an index into a pointer to its lead byte. Out of bounds indexing will raise IndexError or
156156
// be capped to the first/last character of the string, depending on is_slice.
157-
STATIC const char *str_index_to_ptr(const char *self_data, uint self_len, mp_obj_t index, bool is_slice) {
157+
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, uint self_len,
158+
mp_obj_t index, bool is_slice) {
158159
machine_int_t i;
159160
// Copied from mp_get_index; I don't want bounds checking, just give me
160161
// the integer as-is. (I can't bounds-check without scanning the whole
@@ -164,7 +165,7 @@ STATIC const char *str_index_to_ptr(const char *self_data, uint self_len, mp_obj
164165
} else if (!mp_obj_get_int_maybe(index, &i)) {
165166
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "string indices must be integers, not %s", mp_obj_get_type_str(index)));
166167
}
167-
const char *s, *top = self_data + self_len;
168+
const byte *s, *top = self_data + self_len;
168169
if (i < 0)
169170
{
170171
// Negative indexing is performed by counting from the end of the string.
@@ -235,18 +236,18 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
235236
}
236237
return mp_obj_new_str_of_type(type, self_data + start, stop - start);
237238
}
238-
const char *pstart, *pstop;
239+
const byte *pstart, *pstop;
239240
if (ostart != mp_const_none) {
240-
pstart = str_index_to_ptr((const char *)self_data, self_len, ostart, true);
241+
pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
241242
} else {
242-
pstart = (const char *)self_data;
243+
pstart = self_data;
243244
}
244245
if (ostop != mp_const_none) {
245246
// pstop will point just after the stop character. This depends on
246247
// the \0 at the end of the string.
247-
pstop = str_index_to_ptr((const char *)self_data, self_len, ostop, true);
248+
pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
248249
} else {
249-
pstop = (const char *)self_data + self_len;
250+
pstop = self_data + self_len;
250251
}
251252
if (pstop < pstart) {
252253
return MP_OBJ_NEW_QSTR(MP_QSTR_);
@@ -258,15 +259,15 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
258259
uint index_val = mp_get_index(type, self_len, index, false);
259260
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);
260261
}
261-
const char *s = str_index_to_ptr((const char *)self_data, self_len, index, false);
262+
const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
262263
int len = 1;
263264
if (UTF8_IS_NONASCII(*s)) {
264265
// Count the number of 1 bits (after the first)
265266
for (char mask = 0x40; *s & mask; mask >>= 1) {
266267
++len;
267268
}
268269
}
269-
return mp_obj_new_str(s, len, true); // This will create a one-character string
270+
return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
270271
} else {
271272
return MP_OBJ_NULL; // op not supported
272273
}

0 commit comments

Comments
 (0)