Skip to content

Commit c0d9500

Browse files
committed
py/objstr: Convert mp_uint_t to size_t (and use int) where appropriate.
1 parent 68cd3a9 commit c0d9500

4 files changed

Lines changed: 33 additions & 33 deletions

File tree

py/obj.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,9 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
610610
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base);
611611
mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
612612
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
613-
mp_obj_t mp_obj_new_str(const char* data, mp_uint_t len, bool make_qstr_if_not_already);
613+
mp_obj_t mp_obj_new_str(const char* data, size_t len, bool make_qstr_if_not_already);
614614
mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
615-
mp_obj_t mp_obj_new_bytes(const byte* data, mp_uint_t len);
615+
mp_obj_t mp_obj_new_bytes(const byte* data, size_t len);
616616
mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
617617
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
618618
#if MICROPY_PY_BUILTINS_FLOAT
@@ -700,7 +700,7 @@ qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway conve
700700
const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
701701
const char *mp_obj_str_get_data(mp_obj_t self_in, mp_uint_t *len);
702702
mp_obj_t mp_obj_str_intern(mp_obj_t str);
703-
void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_t str_len, bool is_bytes);
703+
void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
704704

705705
#if MICROPY_PY_BUILTINS_FLOAT
706706
// float

py/objstr.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
4444
/******************************************************************************/
4545
/* str */
4646

47-
void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_t str_len, bool is_bytes) {
47+
void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes) {
4848
// this escapes characters, but it will be very slow to print (calling print many times)
4949
bool has_single_quote = false;
5050
bool has_double_quote = false;
@@ -251,9 +251,9 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
251251

252252
// like strstr but with specified length and allows \0 bytes
253253
// TODO replace with something more efficient/standard
254-
const byte *find_subbytes(const byte *haystack, mp_uint_t hlen, const byte *needle, mp_uint_t nlen, mp_int_t direction) {
254+
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction) {
255255
if (hlen >= nlen) {
256-
mp_uint_t str_index, str_index_end;
256+
size_t str_index, str_index_end;
257257
if (direction > 0) {
258258
str_index = 0;
259259
str_index_end = hlen - nlen;
@@ -333,7 +333,7 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
333333
// size and execution time so we don't.
334334

335335
const byte *rhs_data;
336-
mp_uint_t rhs_len;
336+
size_t rhs_len;
337337
if (lhs_type == mp_obj_get_type(rhs_in)) {
338338
GET_STR_DATA_LEN(rhs_in, rhs_data_, rhs_len_);
339339
rhs_data = rhs_data_;
@@ -441,8 +441,8 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
441441
}
442442

443443
// count required length
444-
mp_uint_t required_len = 0;
445-
for (mp_uint_t i = 0; i < seq_len; i++) {
444+
size_t required_len = 0;
445+
for (size_t i = 0; i < seq_len; i++) {
446446
if (mp_obj_get_type(seq_items[i]) != self_type) {
447447
mp_raise_TypeError(
448448
"join expects a list of str/bytes objects consistent with self object");
@@ -458,7 +458,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
458458
vstr_t vstr;
459459
vstr_init_len(&vstr, required_len);
460460
byte *data = (byte*)vstr.buf;
461-
for (mp_uint_t i = 0; i < seq_len; i++) {
461+
for (size_t i = 0; i < seq_len; i++) {
462462
if (i > 0) {
463463
memcpy(data, sep_str, sep_len);
464464
data += sep_len;
@@ -644,7 +644,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
644644
}
645645
if (idx != 0) {
646646
// We split less parts than split limit, now go cleanup surplus
647-
mp_int_t used = org_splits + 1 - idx;
647+
size_t used = org_splits + 1 - idx;
648648
memmove(res->items, &res->items[idx], used * sizeof(mp_obj_t));
649649
mp_seq_clear(res->items, used, res->alloc, sizeof(*res->items));
650650
res->len = used;
@@ -654,7 +654,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
654654
return MP_OBJ_FROM_PTR(res);
655655
}
656656

657-
STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, mp_int_t direction, bool is_index) {
657+
STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) {
658658
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
659659
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
660660

@@ -762,16 +762,16 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
762762

763763
GET_STR_DATA_LEN(args[0], orig_str, orig_str_len);
764764

765-
mp_uint_t first_good_char_pos = 0;
765+
size_t first_good_char_pos = 0;
766766
bool first_good_char_pos_set = false;
767-
mp_uint_t last_good_char_pos = 0;
768-
mp_uint_t i = 0;
769-
mp_int_t delta = 1;
767+
size_t last_good_char_pos = 0;
768+
size_t i = 0;
769+
int delta = 1;
770770
if (type == RSTRIP) {
771771
i = orig_str_len - 1;
772772
delta = -1;
773773
}
774-
for (mp_uint_t len = orig_str_len; len > 0; len--) {
774+
for (size_t len = orig_str_len; len > 0; len--) {
775775
if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
776776
if (!first_good_char_pos_set) {
777777
first_good_char_pos_set = true;
@@ -801,7 +801,7 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
801801

802802
assert(last_good_char_pos >= first_good_char_pos);
803803
//+1 to accomodate the last character
804-
mp_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
804+
size_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
805805
if (stripped_len == orig_str_len) {
806806
// If nothing was stripped, don't bother to dup original string
807807
// TODO: watch out for this case when we'll get to bytearray.strip()
@@ -1588,11 +1588,11 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
15881588
// first pass computes the required length of the replaced string
15891589
// second pass does the replacements
15901590
for (;;) {
1591-
mp_uint_t replaced_str_index = 0;
1592-
mp_uint_t num_replacements_done = 0;
1591+
size_t replaced_str_index = 0;
1592+
size_t num_replacements_done = 0;
15931593
const byte *old_occurrence;
15941594
const byte *offset_ptr = str;
1595-
mp_uint_t str_len_remain = str_len;
1595+
size_t str_len_remain = str_len;
15961596
if (old_len == 0) {
15971597
// if old_str is empty, copy new_str to start of replaced string
15981598
// copy the replacement string
@@ -1602,7 +1602,7 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
16021602
replaced_str_index += new_len;
16031603
num_replacements_done++;
16041604
}
1605-
while (num_replacements_done != (mp_uint_t)max_rep && str_len_remain > 0 && (old_occurrence = find_subbytes(offset_ptr, str_len_remain, old, old_len, 1)) != NULL) {
1605+
while (num_replacements_done != (size_t)max_rep && str_len_remain > 0 && (old_occurrence = find_subbytes(offset_ptr, str_len_remain, old, old_len, 1)) != NULL) {
16061606
if (old_len == 0) {
16071607
old_occurrence += 1;
16081608
}
@@ -1688,7 +1688,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
16881688
}
16891689

16901690
#if MICROPY_PY_BUILTINS_STR_PARTITION
1691-
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t direction) {
1691+
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) {
16921692
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(self_in));
16931693
mp_obj_type_t *self_type = mp_obj_get_type(self_in);
16941694
if (self_type != mp_obj_get_type(arg)) {
@@ -1721,7 +1721,7 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t directi
17211721

17221722
const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
17231723
if (position_ptr != NULL) {
1724-
mp_uint_t position = position_ptr - str;
1724+
size_t position = position_ptr - str;
17251725
result[0] = mp_obj_new_str_of_type(self_type, str, position);
17261726
result[1] = arg;
17271727
result[2] = mp_obj_new_str_of_type(self_type, str + position + sep_len, str_len - position - sep_len);
@@ -1745,7 +1745,7 @@ STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) {
17451745
vstr_t vstr;
17461746
vstr_init_len(&vstr, self_len);
17471747
byte *data = (byte*)vstr.buf;
1748-
for (mp_uint_t i = 0; i < self_len; i++) {
1748+
for (size_t i = 0; i < self_len; i++) {
17491749
*data++ = op(*self_data++);
17501750
}
17511751
return mp_obj_new_str_from_vstr(mp_obj_get_type(self_in), &vstr);
@@ -1767,15 +1767,15 @@ STATIC mp_obj_t str_uni_istype(bool (*f)(unichar), mp_obj_t self_in) {
17671767
}
17681768

17691769
if (f != unichar_isupper && f != unichar_islower) {
1770-
for (mp_uint_t i = 0; i < self_len; i++) {
1770+
for (size_t i = 0; i < self_len; i++) {
17711771
if (!f(*self_data++)) {
17721772
return mp_const_false;
17731773
}
17741774
}
17751775
} else {
17761776
bool contains_alpha = false;
17771777

1778-
for (mp_uint_t i = 0; i < self_len; i++) { // only check alphanumeric characters
1778+
for (size_t i = 0; i < self_len; i++) { // only check alphanumeric characters
17791779
if (unichar_isalpha(*self_data++)) {
17801780
contains_alpha = true;
17811781
if (!f(*(self_data - 1))) { // -1 because we already incremented above
@@ -2019,7 +2019,7 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) {
20192019
return MP_OBJ_FROM_PTR(o);
20202020
}
20212021

2022-
mp_obj_t mp_obj_new_str(const char* data, mp_uint_t len, bool make_qstr_if_not_already) {
2022+
mp_obj_t mp_obj_new_str(const char* data, size_t len, bool make_qstr_if_not_already) {
20232023
if (make_qstr_if_not_already) {
20242024
// use existing, or make a new qstr
20252025
return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
@@ -2040,7 +2040,7 @@ mp_obj_t mp_obj_str_intern(mp_obj_t str) {
20402040
return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
20412041
}
20422042

2043-
mp_obj_t mp_obj_new_bytes(const byte* data, mp_uint_t len) {
2043+
mp_obj_t mp_obj_new_bytes(const byte* data, size_t len) {
20442044
return mp_obj_new_str_of_type(&mp_type_bytes, data, len);
20452045
}
20462046

@@ -2126,7 +2126,7 @@ typedef struct _mp_obj_str8_it_t {
21262126
mp_obj_base_t base;
21272127
mp_fun_1_t iternext;
21282128
mp_obj_t str;
2129-
mp_uint_t cur;
2129+
size_t cur;
21302130
} mp_obj_str8_it_t;
21312131

21322132
#if !MICROPY_PY_BUILTINS_STR_UNICODE

py/objstr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct _mp_obj_str_t {
3232
mp_obj_base_t base;
3333
mp_uint_t hash;
3434
// len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
35-
mp_uint_t len;
35+
size_t len;
3636
const byte *data;
3737
} mp_obj_str_t;
3838

@@ -72,7 +72,7 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u
7272

7373
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
7474
mp_obj_t index, bool is_slice);
75-
const byte *find_subbytes(const byte *haystack, mp_uint_t hlen, const byte *needle, mp_uint_t nlen, mp_int_t direction);
75+
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
7676

7777
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj);
7878
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj);

py/objstrunicode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ typedef struct _mp_obj_str_it_t {
284284
mp_obj_base_t base;
285285
mp_fun_1_t iternext;
286286
mp_obj_t str;
287-
mp_uint_t cur;
287+
size_t cur;
288288
} mp_obj_str_it_t;
289289

290290
STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {

0 commit comments

Comments
 (0)