Skip to content

Commit d098c6b

Browse files
committed
objstr: Implement .endswith().
1 parent 561789d commit d098c6b

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ STATIC mp_obj_t str_startswith(uint n_args, const mp_obj_t *args) {
619619
return MP_BOOL(memcmp(str + index_val, prefix, prefix_len) == 0);
620620
}
621621

622+
STATIC mp_obj_t str_endswith(uint n_args, const mp_obj_t *args) {
623+
GET_STR_DATA_LEN(args[0], str, str_len);
624+
GET_STR_DATA_LEN(args[1], suffix, suffix_len);
625+
assert(n_args == 2);
626+
627+
if (suffix_len > str_len) {
628+
return mp_const_false;
629+
}
630+
return MP_BOOL(memcmp(str + (str_len - suffix_len), suffix, suffix_len) == 0);
631+
}
632+
622633
enum { LSTRIP, RSTRIP, STRIP };
623634

624635
STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
@@ -1541,6 +1552,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
15411552
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
15421553
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
15431554
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith);
1555+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
15441556
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
15451557
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj, 1, 2, str_lstrip);
15461558
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj, 1, 2, str_rstrip);
@@ -1565,6 +1577,7 @@ STATIC const mp_map_elem_t str_locals_dict_table[] = {
15651577
{ MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj },
15661578
{ MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj },
15671579
{ MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
1580+
{ MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj },
15681581
{ MP_OBJ_NEW_QSTR(MP_QSTR_strip), (mp_obj_t)&str_strip_obj },
15691582
{ MP_OBJ_NEW_QSTR(MP_QSTR_lstrip), (mp_obj_t)&str_lstrip_obj },
15701583
{ MP_OBJ_NEW_QSTR(MP_QSTR_rstrip), (mp_obj_t)&str_rstrip_obj },

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Q(rindex)
239239
Q(split)
240240
Q(rsplit)
241241
Q(startswith)
242+
Q(endswith)
242243
Q(replace)
243244
Q(partition)
244245
Q(rpartition)

tests/basics/string_endswith.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print("foobar".endswith("bar"))
2+
print("foobar".endswith("baR"))
3+
print("foobar".endswith("bar1"))
4+
print("foobar".endswith("foobar"))
5+
print("foobar".endswith(""))
6+
7+
#print("1foobar".startswith("foo", 1))
8+
#print("1foo".startswith("foo", 1))
9+
#print("1foo".startswith("1foo", 1))
10+
#print("1fo".startswith("foo", 1))
11+
#print("1fo".startswith("foo", 10))

0 commit comments

Comments
 (0)