summaryrefslogtreecommitdiffstatshomepage
path: root/py/objstr.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-24 22:46:51 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-24 22:46:51 +0300
commitd098c6bf852eacb4e364dd8f5dfce1a472795ff9 (patch)
treee946fecbd07b2f14e2f542df08b0b87137692c7c /py/objstr.c
parent561789d7182e8354761e4dc9f40ed7dd1a72686a (diff)
downloadmicropython-d098c6bf852eacb4e364dd8f5dfce1a472795ff9.tar.gz
micropython-d098c6bf852eacb4e364dd8f5dfce1a472795ff9.zip
objstr: Implement .endswith().
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 61fda12a3f..323f329575 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -619,6 +619,17 @@ STATIC mp_obj_t str_startswith(uint n_args, const mp_obj_t *args) {
return MP_BOOL(memcmp(str + index_val, prefix, prefix_len) == 0);
}
+STATIC mp_obj_t str_endswith(uint n_args, const mp_obj_t *args) {
+ GET_STR_DATA_LEN(args[0], str, str_len);
+ GET_STR_DATA_LEN(args[1], suffix, suffix_len);
+ assert(n_args == 2);
+
+ if (suffix_len > str_len) {
+ return mp_const_false;
+ }
+ return MP_BOOL(memcmp(str + (str_len - suffix_len), suffix, suffix_len) == 0);
+}
+
enum { LSTRIP, RSTRIP, STRIP };
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);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith);
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
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);
@@ -1565,6 +1577,7 @@ 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_rsplit), (mp_obj_t)&str_rsplit_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_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 },