8000 Added public domain implementations of strchr and strstr. · ronc/micropython@c8effff · GitHub
[go: up one dir, main page]

Skip to content

Commit c8effff

Browse files
committed
Added public domain implementations of strchr and strstr.
1 parent 34f813e commit c8effff

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

py/objstr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdarg.h>
44
#include <string.h>
55
#include <assert.h>
6+
#include <sys/types.h>
67

78
#include "nlr.h"
89
#include "misc.h"

stm/std.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ int strncmp(const char *s1, const char *s2, size_t n);
1717
char *strndup(const char *s, size_t n);
1818
char *strcpy(char *dest, const char *src);
1919
char *strcat(char *dest, const char *src);
20+
char *strchr(const char *s, int c);
21+
char *strstr(const char *haystack, const char *needle);
2022

2123
int printf(const char *fmt, ...);
2224
int snprintf(char *str, size_t size, const char *fmt, ...);

stm/string0.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,31 @@ char *strcat(char *dest, const char *src) {
108108
*d = '\0';
109109
return dest;
110110
}
111+
112+
// Public Domain implementation of strchr from:
113+
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function
114+
char *strchr(const char *s, int c)
115+
{
116+
/* Scan s for the character. When this loop is finished,
117+
s will either point to the end of the string or the
118+
character we were looking for. */
119+
while (*s != '\0' && *s != (char)c)
120+
s++;
121+
return ((*s == c) ? (char *) s : 0);
122+
}
123+
124+
125+
// Public Domain implementation of strstr from:
126+
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function
127+
char *strstr(const char *haystack, const char *needle)
128+
{
129+
size_t needlelen;
130+
/* Check for the null needle case. */
131+
if (*needle == '\0')
132+
return (char *) haystack;
133+
needlelen = strlen(needle);
134+
for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
135+
if (strncmp(haystack, needle, needlelen) == 0)
136+
return (char *) haystack;
137+
return 0;
138+
}

0 commit comments

Comments
 (0)
0