summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-23 18:11:05 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-23 18:11:05 +0000
commitcfedd81c076a8c338a1a1b8f69008fb308e90bc8 (patch)
treecf89af8d3519c227fb3b91b13937516405f5c661
parent26a00085fedd5e45df79314774b650811dc05bed (diff)
parentc3e72a8cc818b8777c253d4fddcf92181bf06b23 (diff)
downloadmicropython-cfedd81c076a8c338a1a1b8f69008fb308e90bc8.tar.gz
micropython-cfedd81c076a8c338a1a1b8f69008fb308e90bc8.zip
Merge branch 'master' of github.com:micropython/micropython
-rw-r--r--py/obj.c2
-rw-r--r--py/objstr.c12
-rw-r--r--stm/string0.c10
-rw-r--r--tests/basics/builtin-callable.py5
-rw-r--r--tests/basics/string_startswith.py5
5 files changed, 33 insertions, 1 deletions
diff --git a/py/obj.c b/py/obj.c
index c2f726bb99..660df4dfa1 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -66,7 +66,7 @@ void mp_obj_print_exception(mp_obj_t exc) {
}
bool mp_obj_is_callable(mp_obj_t o_in) {
- if (MP_OBJ_IS_SMALL_INT(o_in)) {
+ if (!MP_OBJ_IS_OBJ(o_in)) {
return false;
} else {
mp_obj_base_t *o = o_in;
diff --git a/py/objstr.c b/py/objstr.c
index 09d4958fbb..337b42e70e 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -278,6 +278,16 @@ static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
}
}
+// TODO: (Much) more variety in args
+static mp_obj_t str_startswith(mp_obj_t self_in, mp_obj_t arg) {
+ GET_STR_DATA_LEN(self_in, str, str_len);
+ GET_STR_DATA_LEN(arg, prefix, prefix_len);
+ if (prefix_len > str_len) {
+ return mp_const_false;
+ }
+ return MP_BOOL(memcmp(str, prefix, prefix_len) == 0);
+}
+
static bool chr_in_str(const byte* const str, const size_t str_len, int c) {
for (size_t i = 0; i < str_len; i++) {
if (str[i] == c) {
@@ -364,6 +374,7 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
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_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(str_format_obj, 1, str_format);
@@ -371,6 +382,7 @@ static const mp_method_t str_type_methods[] = {
{ "find", &str_find_obj },
{ "join", &str_join_obj },
{ "split", &str_split_obj },
+ { "startswith", &str_startswith_obj },
{ "strip", &str_strip_obj },
{ "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel
diff --git a/stm/string0.c b/stm/string0.c
index 4899e7b0f5..79fd4cc097 100644
--- a/stm/string0.c
+++ b/stm/string0.c
@@ -34,6 +34,16 @@ void *memset(void *s, int c, size_t n) {
return s;
}
+int memcmp(const char *s1, const char *s2, size_t n) {
+ while (n--) {
+ char c1 = *s1++;
+ char c2 = *s2++;
+ if (c1 < c2) return -1;
+ else if (c1 > c2) return 1;
+ }
+ return 0;
+}
+
size_t strlen(const char *str) {
int len = 0;
for (const char *s = str; *s; s++) {
diff --git a/tests/basics/builtin-callable.py b/tests/basics/builtin-callable.py
new file mode 100644
index 0000000000..caddb885cf
--- /dev/null
+++ b/tests/basics/builtin-callable.py
@@ -0,0 +1,5 @@
+import sys
+print(callable(1))
+print(callable("dfsd"))
+print(callable(callable))
+print(callable(sys))
diff --git a/tests/basics/string_startswith.py b/tests/basics/string_startswith.py
new file mode 100644
index 0000000000..99d653efbb
--- /dev/null
+++ b/tests/basics/string_startswith.py
@@ -0,0 +1,5 @@
+print("foobar".startswith("foo"))
+print("foobar".startswith("Foo"))
+print("foobar".startswith("foo1"))
+print("foobar".startswith("foobar"))
+print("foobar".startswith(""))