Skip to content

Commit cfedd81

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 26a0008 + c3e72a8 commit cfedd81

5 files changed

Lines changed: 33 additions & 1 deletion

File tree

py/obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void mp_obj_print_exception(mp_obj_t exc) {
6666
}
6767

6868
bool mp_obj_is_callable(mp_obj_t o_in) {
69-
if (MP_OBJ_IS_SMALL_INT(o_in)) {
69+
if (!MP_OBJ_IS_OBJ(o_in)) {
7070
return false;
7171
} else {
7272
mp_obj_base_t *o = o_in;

py/objstr.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
278278
}
279279
}
280280

281+
// TODO: (Much) more variety in args
282+
static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
283+
GET_STR_DATA_LEN(self_in, str, str_len);
284+
GET_STR_DATA_LEN(arg, prefix, prefix_len);
285+
if (prefix_len > str_len) {
286+
return mp_const_false;
287+
}
288+
return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
289+
}
290+
281291
static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
282292
for (size_t i = 0; i < str_len; i++) {
283293
if (str[i] == c) {
@@ -364,13 +374,15 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
364374
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
365375
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
366376
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
377+
static MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
367378
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
368379
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
369380

370381
static const mp_method_t str_type_methods[] = {
371382
{ "find", &str_find_obj },
372383
{ "join", &str_join_obj },
373384
{ "split", &str_split_obj },
385+
{ "startswith", &str_startswith_obj },
374386
{ "strip", &str_strip_obj },
375387
{ "format", &str_format_obj },
376388
{ NULL, NULL }, // end-of-list sentinel

stm/string0.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ void *memset(void *s, int c, size_t n) {
3434
return s;
3535
}
3636

37+
int memcmp(const char *s1, const char *s2, size_t n) {
38+
while (n--) {
39+
char c1 = *s1++;
40+
char c2 = *s2++;
41+
if (c1 < c2) return -1;
42+
else if (c1 > c2) return 1;
43+
}
44+
return 0;
45+
}
46+
3747
size_t strlen(const char *str) {
3848
int len = 0;
3949
for (const char *s = str; *s; s++) {

tests/basics/builtin-callable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
print(callable(1))
3+
print(callable("dfsd"))
4+
print(callable(callable))
5+
print(callable(sys))

tests/basics/string_startswith.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
print("foobar".startswith("foo"))
2+
print("foobar".startswith("Foo"))
3+
print("foobar".startswith("foo1"))
4+
print("foobar".startswith("foobar"))
5+
print("foobar".startswith(""))

0 commit comments

Comments
 (0)