diff options
author | Dave Hylands <dhylands@gmail.com> | 2014-01-12 22:34:58 -0800 |
---|---|---|
committer | Dave Hylands <dhylands@gmail.com> | 2014-01-12 22:34:58 -0800 |
commit | c8effff937442be01d36d56c77054bbdee3f3aff (patch) | |
tree | b84e6846a0a7441ef9cd05b5cb787dff02ada904 /stm/string0.c | |
parent | 34f813ee29c7191e3de455c3fc9c788496e3b29e (diff) |
Added public domain implementations of strchr and strstr.
Diffstat (limited to 'stm/string0.c')
-rw-r--r-- | stm/string0.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/stm/string0.c b/stm/string0.c index 2a5f25597..d67c5f2b1 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; +} |