diff --git a/py/objset.c b/py/objset.c index 9a7e6751f5..448f484d5a 100644 --- a/py/objset.c +++ b/py/objset.c @@ -128,7 +128,7 @@ STATIC mp_obj_t set_copy(mp_obj_t self_in) { mp_obj_set_t *other = m_new_obj(mp_obj_set_t); other->base.type = &mp_type_set; - mp_set_init(&other->set, self->set.alloc - 1); + mp_set_init(&other->set, self->set.alloc); other->set.used = self->set.used; memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); diff --git a/py/objstr.c b/py/objstr.c index 329dfe6dd9..4395757a46 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1106,22 +1106,31 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_STR(args[0])); - assert(MP_OBJ_IS_STR(args[1])); - assert(MP_OBJ_IS_STR(args[2])); - machine_int_t max_rep = 0; + machine_int_t max_rep = -1; if (n_args == 4) { - assert(MP_OBJ_IS_SMALL_INT(args[3])); - max_rep = MP_OBJ_SMALL_INT_VALUE(args[3]); + max_rep = mp_obj_get_int(args[3]); if (max_rep == 0) { return args[0]; } else if (max_rep < 0) { - max_rep = 0; + max_rep = -1; } } // if max_rep is still 0 by this point we will need to do all possible replacements + // check argument types + + if (!MP_OBJ_IS_STR(args[1])) { + bad_implicit_conversion(args[1]); + } + + if (!MP_OBJ_IS_STR(args[2])) { + bad_implicit_conversion(args[2]); + } + + // extract string data + GET_STR_DATA_LEN(args[0], str, str_len); GET_STR_DATA_LEN(args[1], old, old_len); GET_STR_DATA_LEN(args[2], new, new_len); @@ -1143,8 +1152,20 @@ STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) { machine_uint_t num_replacements_done = 0; const byte *old_occurrence; const byte *offset_ptr = str; - machine_uint_t offset_num = 0; - while ((old_occurrence = find_subbytes(offset_ptr, str_len - offset_num, old, old_len, 1)) != NULL) { + machine_uint_t str_len_remain = str_len; + if (old_len == 0) { + // if old_str is empty, copy new_str to start of replaced string + // copy the replacement string + if (data != NULL) { + memcpy(data, new, new_len); + } + replaced_str_index += new_len; + num_replacements_done++; + } + while (num_replacements_done != max_rep && str_len_remain > 0 && (old_occurrence = find_subbytes(offset_ptr, str_len_remain, old, old_len, 1)) != NULL) { + if (old_len == 0) { + old_occurrence += 1; + } // copy from just after end of last occurrence of to-be-replaced string to right before start of next occurrence if (data != NULL) { memcpy(data + replaced_str_index, offset_ptr, old_occurrence - offset_ptr); @@ -1156,19 +1177,15 @@ STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) { } replaced_str_index += new_len; offset_ptr = old_occurrence + old_len; - offset_num = offset_ptr - str; - + str_len_remain = str + str_len - offset_ptr; num_replacements_done++; - if (max_rep != 0 && num_replacements_done == max_rep){ - break; - } } // copy from just after end of last occurrence of to-be-replaced string to end of old string if (data != NULL) { - memcpy(data + replaced_str_index, offset_ptr, str_len - offset_num); + memcpy(data + replaced_str_index, offset_ptr, str_len_remain); } - replaced_str_index += str_len - offset_num; + replaced_str_index += str_len_remain; if (data == NULL) { // first pass @@ -1178,6 +1195,7 @@ STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) { } else { // substr found, allocate new string replaced_str = mp_obj_str_builder_start(mp_obj_get_type(args[0]), replaced_str_index, &data); + assert(data != NULL); } } else { // second pass, we are done diff --git a/tests/basics/iter-of-iter.py b/tests/basics/iter-of-iter.py index ba55356294..70282aa97e 100644 --- a/tests/basics/iter-of-iter.py +++ b/tests/basics/iter-of-iter.py @@ -5,4 +5,4 @@ print(list(i)) i = iter(iter({1:2, 3:4, 5:6})) print(sorted(i)) i = iter(iter({1, 2, 3})) -print(list(i)) +print(sorted(i)) diff --git a/tests/basics/set1.py b/tests/basics/set1.py index c54fbd8379..399c33eb68 100644 --- a/tests/basics/set1.py +++ b/tests/basics/set1.py @@ -4,4 +4,4 @@ s = {1} print(s) s = {3, 4, 3, 1} -print(s) +print(sorted(s)) diff --git a/tests/basics/set_binop.py b/tests/basics/set_binop.py index d0d0b8027b..1acb810f0f 100644 --- a/tests/basics/set_binop.py +++ b/tests/basics/set_binop.py @@ -5,26 +5,26 @@ def r(s): sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}] for s in sets: for t in sets: - print(s, '|', t, '=', r(s | t)) - print(s, '^', t, '=', r(s ^ t)) - print(s, '&', t, '=', r(s & t)) - print(s, '-', t, '=', r(s - t)) + print(r(s), '|', r(t), '=', r(s | t)) + print(r(s), '^', r(t), '=', r(s ^ t)) + print(r(s), '&', r(t), '=', r(s & t)) + print(r(s), '-', r(t), '=', r(s - t)) u = s.copy() u |= t - print(s, "|=", t, '-->', r(u)) + print(r(s), "|=", r(t), '-->', r(u)) u = s.copy() u ^= t - print(s, "^=", t, '-->', r(u)) + print(r(s), "^=", r(t), '-->', r(u)) u = s.copy() u &= t - print(s, "&=", t, "-->", r(u)) + print(r(s), "&=", r(t), "-->", r(u)) u = s.copy() u -= t - print(s, "-=", t, "-->", r(u)) + print(r(s), "-=", r(t), "-->", r(u)) - print(s, '==', t, '=', s == t) - print(s, '!=', t, '=', s != t) - print(s, '>', t, '=', s > t) - print(s, '>=', t, '=', s >= t) - print(s, '<', t, '=', s < t) - print(s, '<=', t, '=', s <= t) + print(r(s), '==', r(t), '=', s == t) + print(r(s), '!=', r(t), '=', s != t) + print(r(s), '>', r(t), '=', s > t) + print(r(s), '>=', r(t), '=', s >= t) + print(r(s), '<', r(t), '=', s < t) + print(r(s), '<=', r(t), '=', s <= t) diff --git a/tests/basics/set_symmetric_difference.py b/tests/basics/set_symmetric_difference.py index acf298385a..d5f1df1ed7 100644 --- a/tests/basics/set_symmetric_difference.py +++ b/tests/basics/set_symmetric_difference.py @@ -1,5 +1,5 @@ -print({1,2}.symmetric_difference({2,3})) -print({1,2}.symmetric_difference([2,3])) +print(sorted({1,2}.symmetric_difference({2,3}))) +print(sorted({1,2}.symmetric_difference([2,3]))) s = {1,2} print(s.symmetric_difference_update({2,3})) l = list(s) diff --git a/tests/basics/set_union.py b/tests/basics/set_union.py index 2adcc972c0..572d12f66b 100644 --- a/tests/basics/set_union.py +++ b/tests/basics/set_union.py @@ -1 +1 @@ -print({1}.union({2})) +print(sorted({1}.union({2}))) diff --git a/tests/basics/string_replace.py b/tests/basics/string_replace.py index a1d0f973bb..8c4c171807 100644 --- a/tests/basics/string_replace.py +++ b/tests/basics/string_replace.py @@ -6,3 +6,8 @@ print("aabbaabbaabbaa".replace("aa", "cc", 3)) print("a".replace("aa", "bb")) print("testingtesting".replace("ing", "")) print("testINGtesting".replace("ing", "ING!")) + +print("".replace("", "1")) +print("A".replace("", "1")) +print("AB".replace("", "1")) +print("AB".replace("", "12")) diff --git a/tools/pip-micropython b/tools/pip-micropython index e27e859425..fab8809839 100755 --- a/tools/pip-micropython +++ b/tools/pip-micropython @@ -6,6 +6,12 @@ # ports (if PIP_MICROPY_DEST environment var is set). # +if [ "$1" != "install" ]; then + echo "Only install command is supported currently" + exit 1 +fi +shift + if [ -n "$PIP_MICROPY_DEST" ]; then dest="$PIP_MICROPY_DEST" echo "Destination snapshot directory: $dest"