Merge remote-tracking branch 'upstream/master' into makefile-tweaks

This commit is contained in:
Andrew Scheller 2014-04-07 01:41:24 +01:00
commit 5e443f4b70
9 changed files with 64 additions and 35 deletions

View File

@ -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));

View File

@ -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

View File

@ -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))

View File

@ -4,4 +4,4 @@ s = {1}
print(s)
s = {3, 4, 3, 1}
print(s)
print(sorted(s))

View File

@ -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)

View File

@ -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)

View File

@ -1 +1 @@
print({1}.union({2}))
print(sorted({1}.union({2})))

View File

@ -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"))

View File

@ -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"