Merge branch 'master' of github.com:micropython/micropython
This commit is contained in:
commit
d139c489ba
39
py/objstr.c
39
py/objstr.c
@ -493,7 +493,9 @@ STATIC mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
|
|||||||
return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
|
return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
|
enum { LSTRIP, RSTRIP, STRIP };
|
||||||
|
|
||||||
|
STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
|
||||||
assert(1 <= n_args && n_args <= 2);
|
assert(1 <= n_args && n_args <= 2);
|
||||||
assert(MP_OBJ_IS_STR(args[0]));
|
assert(MP_OBJ_IS_STR(args[0]));
|
||||||
|
|
||||||
@ -516,14 +518,29 @@ STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
|
|||||||
machine_uint_t first_good_char_pos = 0;
|
machine_uint_t first_good_char_pos = 0;
|
||||||
bool first_good_char_pos_set = false;
|
bool first_good_char_pos_set = false;
|
||||||
machine_uint_t last_good_char_pos = 0;
|
machine_uint_t last_good_char_pos = 0;
|
||||||
for (machine_uint_t i = 0; i < orig_str_len; i++) {
|
machine_uint_t i = 0;
|
||||||
|
machine_int_t delta = 1;
|
||||||
|
if (type == RSTRIP) {
|
||||||
|
i = orig_str_len - 1;
|
||||||
|
delta = -1;
|
||||||
|
}
|
||||||
|
for (machine_uint_t len = orig_str_len; len > 0; len--) {
|
||||||
if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
|
if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
|
||||||
last_good_char_pos = i;
|
|
||||||
if (!first_good_char_pos_set) {
|
if (!first_good_char_pos_set) {
|
||||||
first_good_char_pos = i;
|
first_good_char_pos = i;
|
||||||
|
if (type == LSTRIP) {
|
||||||
|
last_good_char_pos = orig_str_len - 1;
|
||||||
|
break;
|
||||||
|
} else if (type == RSTRIP) {
|
||||||
|
first_good_char_pos = 0;
|
||||||
|
last_good_char_pos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
first_good_char_pos_set = true;
|
first_good_char_pos_set = true;
|
||||||
}
|
}
|
||||||
|
last_good_char_pos = i;
|
||||||
}
|
}
|
||||||
|
i += delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (first_good_char_pos == 0 && last_good_char_pos == 0) {
|
if (first_good_char_pos == 0 && last_good_char_pos == 0) {
|
||||||
@ -537,6 +554,18 @@ STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
|
|||||||
return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
|
return mp_obj_new_str(orig_str + first_good_char_pos, stripped_len, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
|
||||||
|
return str_uni_strip(STRIP, n_args, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC mp_obj_t str_lstrip(uint n_args, const mp_obj_t *args) {
|
||||||
|
return str_uni_strip(LSTRIP, n_args, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC mp_obj_t str_rstrip(uint n_args, const mp_obj_t *args) {
|
||||||
|
return str_uni_strip(RSTRIP, n_args, args);
|
||||||
|
}
|
||||||
|
|
||||||
// Takes an int arg, but only parses unsigned numbers, and only changes
|
// Takes an int arg, but only parses unsigned numbers, and only changes
|
||||||
// *num if at least one digit was parsed.
|
// *num if at least one digit was parsed.
|
||||||
static int str_to_int(const char *str, int *num) {
|
static int str_to_int(const char *str, int *num) {
|
||||||
@ -1354,6 +1383,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
|
|||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
|
||||||
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj, 1, 2, str_lstrip);
|
||||||
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj, 1, 2, str_rstrip);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, mp_obj_str_format);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, mp_obj_str_format);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
||||||
@ -1373,6 +1404,8 @@ STATIC const mp_map_elem_t str_locals_dict_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj },
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&str_format_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&str_replace_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&str_count_obj },
|
||||||
|
@ -180,6 +180,8 @@ Q(pop)
|
|||||||
Q(sort)
|
Q(sort)
|
||||||
Q(join)
|
Q(join)
|
||||||
Q(strip)
|
Q(strip)
|
||||||
|
Q(lstrip)
|
||||||
|
Q(rstrip)
|
||||||
Q(format)
|
Q(format)
|
||||||
Q(key)
|
Q(key)
|
||||||
Q(reverse)
|
Q(reverse)
|
||||||
|
@ -42,10 +42,11 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
|
|||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint sz;
|
mp_buffer_info_t bufinfo;
|
||||||
const char *buf = mp_obj_str_get_data(arg, &sz);
|
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
machine_int_t out_sz = o->type->stream_p->write(self_in, buf, sz, &error);
|
machine_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
|
||||||
if (out_sz == -1) {
|
if (out_sz == -1) {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,3 +4,9 @@ print(" T E S T".strip())
|
|||||||
print("abcabc".strip("ce"))
|
print("abcabc".strip("ce"))
|
||||||
print("aaa".strip("b"))
|
print("aaa".strip("b"))
|
||||||
print("abc efg ".strip("g a"))
|
print("abc efg ".strip("g a"))
|
||||||
|
|
||||||
|
print(' spacious '.lstrip())
|
||||||
|
print('www.example.com'.lstrip('cmowz.'))
|
||||||
|
|
||||||
|
print(' spacious '.rstrip())
|
||||||
|
print('mississippi'.rstrip('ipz'))
|
||||||
|
@ -148,9 +148,9 @@ STATIC mp_obj_t socket_send(uint n_args, const mp_obj_t *args) {
|
|||||||
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
flags = MP_OBJ_SMALL_INT_VALUE(args[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint sz;
|
mp_buffer_info_t bufinfo;
|
||||||
const char *buf = mp_obj_str_get_data(args[1], &sz);
|
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
||||||
int out_sz = send(self->fd, buf, sz, flags);
|
int out_sz = send(self->fd, bufinfo.buf, bufinfo.len, flags);
|
||||||
RAISE_ERRNO(out_sz, errno);
|
RAISE_ERRNO(out_sz, errno);
|
||||||
|
|
||||||
return MP_OBJ_NEW_SMALL_INT((machine_int_t)out_sz);
|
return MP_OBJ_NEW_SMALL_INT((machine_int_t)out_sz);
|
||||||
|
Loading…
Reference in New Issue
Block a user