diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-13 11:43:57 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-13 11:43:57 -0800 |
commit | f88a72a88e78ce1cfbd25f0cbf1fbff5e6fd43cb (patch) | |
tree | 819b59b7c3dc69052bfbb8528fccda4bbe1c006c /stm | |
parent | 2300537c79dd642a7187018334a1a697a415f589 (diff) | |
parent | c8effff937442be01d36d56c77054bbdee3f3aff (diff) | |
download | micropython-f88a72a88e78ce1cfbd25f0cbf1fbff5e6fd43cb.tar.gz micropython-f88a72a88e78ce1cfbd25f0cbf1fbff5e6fd43cb.zip |
Merge pull request #168 from dhylands/add-strstr
Added public domain implementations of strchr and strstr.
Diffstat (limited to 'stm')
-rw-r--r-- | stm/std.h | 2 | ||||
-rw-r--r-- | stm/string0.c | 28 |
2 files changed, 30 insertions, 0 deletions
@@ -17,6 +17,8 @@ int strncmp(const char *s1, const char *s2, size_t n); char *strndup(const char *s, size_t n); char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); +char *strchr(const char *s, int c); +char *strstr(const char *haystack, const char *needle); int printf(const char *fmt, ...); int snprintf(char *str, size_t size, const char *fmt, ...); diff --git a/stm/string0.c b/stm/string0.c index 2a5f255971..d67c5f2b17 100644 --- a/stm/string0.c +++ b/stm/string0.c @@ -108,3 +108,31 @@ char *strcat(char *dest, const char *src) { *d = '\0'; return dest; } + +// Public Domain implementation of strchr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function +char *strchr(const char *s, int c) +{ + /* Scan s for the character. When this loop is finished, + s will either point to the end of the string or the + character we were looking for. */ + while (*s != '\0' && *s != (char)c) + s++; + return ((*s == c) ? (char *) s : 0); +} + + +// Public Domain implementation of strstr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function +char *strstr(const char *haystack, const char *needle) +{ + size_t needlelen; + /* Check for the null needle case. */ + if (*needle == '\0') + return (char *) haystack; + needlelen = strlen(needle); + for (; (haystack = strchr(haystack, *needle)) != 0; haystack++) + if (strncmp(haystack, needle, needlelen) == 0) + return (char *) haystack; + return 0; +} |