summaryrefslogtreecommitdiff
path: root/src/lib/string.c
diff options
context:
space:
mode:
authorYuji Sasaki <sasakiy@chromium.org>2019-06-12 17:42:19 -0700
committerMartin Roth <martinroth@google.com>2019-08-10 01:32:19 +0000
commit6b212d8fcff125a831c0ba068903afd0b22d292c (patch)
tree4b9bc204291f383a17a8a176190a8c3bf9bd39c5 /src/lib/string.c
parente2c24f783d78c582fb56625768b9ac424b5943c9 (diff)
string: implement strspn, strcspn, atol
Change-Id: Id8fa880357124b620bde8884949bd8ffff7d0762 Signed-off-by: Patrick Georgi <pgeorgi@google.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/34450 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'src/lib/string.c')
-rw-r--r--src/lib/string.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/string.c b/src/lib/string.c
index eb6adb67c7..f0c24edcfc 100644
--- a/src/lib/string.c
+++ b/src/lib/string.c
@@ -132,3 +132,55 @@ unsigned int skip_atoi(char **s)
i = i*10 + *((*s)++) - '0';
return i;
}
+
+int strspn(const char *str, const char *spn)
+{
+ int ret = 0;
+
+ while (*str != 0) {
+ const char *p;
+ for (p = spn; *str != *p; p++)
+ if (*p == '\0')
+ return ret;
+ ret++;
+ str++;
+ }
+ return ret;
+}
+
+int strcspn(const char *str, const char *spn)
+{
+ int ret = 0;
+
+ while (*str != 0) {
+ const char *p;
+ for (p = spn; *p != '\0'; p++)
+ if (*p == *str)
+ return ret;
+ ret++;
+ str++;
+ }
+ return ret;
+}
+
+long atol(const char *str)
+{
+ long ret = 0;
+ long sign = 1;
+
+ str += strspn(str, " \t\n\r\f\v");
+
+ if (*str == '+') {
+ sign = 1;
+ str++;
+ } else if (*str == '-') {
+ sign = -1;
+ str++;
+ }
+
+ while (isdigit(*str)) {
+ ret *= 10;
+ ret += *str++ - '0';
+ }
+ return ret * sign;
+}