[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH v3 1/2] lib/nolibc: Add strtok to string.h



Reviewed-by: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>

On 24.08.2018 11:53, Florian Schmidt wrote:
Add strtok and dependent functions to string.h.
These are required for the Xen network netfront driver.

Functions are taken from musl v1.1.19
musl git commit ref 55df09bfccbfe21fc9dd7d8f94550c0ff25ace04

Signed-off-by: Razvan Cojocaru <razvan.cojocaru93@xxxxxxxxx>
Signed-off-by: Florian Schmidt <florian.schmidt@xxxxxxxxx>
---
  lib/nolibc/include/string.h |   6 +-
  lib/nolibc/string.c         | 111 +++++++++++++++++++++++++++++++-----
  2 files changed, 101 insertions(+), 16 deletions(-)

diff --git a/lib/nolibc/include/string.h b/lib/nolibc/include/string.h
index 677f528..1d63ae9 100644
--- a/lib/nolibc/include/string.h
+++ b/lib/nolibc/include/string.h
@@ -54,9 +54,13 @@ char *strncpy(char *dst, const char *src, size_t len);
  char *strcpy(char *dst, const char *src);
  size_t strnlen(const char *str, size_t maxlen);
  size_t strlen(const char *str);
-const char *strchr(const char *str, int c);
+char *strchrnul(const char *s, int c);
+char *strchr(const char *str, int c);
  int strncmp(const char *str1, const char *str2, size_t len);
  int strcmp(const char *str1, const char *str2);
+size_t strcspn(const char *s, const char *c);
+size_t strspn(const char *s, const char *c);
+char *strtok(char *restrict s, const char *restrict sep);
#ifdef __cplusplus
  }
diff --git a/lib/nolibc/string.c b/lib/nolibc/string.c
index bf89106..eee3113 100644
--- a/lib/nolibc/string.c
+++ b/lib/nolibc/string.c
@@ -1,8 +1,7 @@
-/* SPDX-License-Identifier: BSD-3-Clause */
+/* SPDX-License-Identifier: BSD-3-Clause AND MIT */
  /*
   * Authors: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
   *
- *
   * Copyright (c) 2017, NEC Europe Ltd., NEC Corporation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
@@ -32,6 +31,31 @@
   *
   * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
   */
+/* For the parts taken from musl (marked as such below), the MIT licence
+ * applies instead:
+ * ----------------------------------------------------------------------
+ * Copyright (c) 2005-2014 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * ----------------------------------------------------------------------
+ */
#include <stdint.h>
  #include <string.h>
@@ -130,19 +154,6 @@ char *strcpy(char *dst, const char *src)
        return strncpy(dst, src, SIZE_MAX);
  }
-const char *strchr(const char *str, int c)
-{
-       const char *pos = str;
-
-       for (; *pos != '\0'; ++pos)
-               if (*pos == (char) c)
-                       return pos;
-       if (c == 0)
-               return pos;
-
-       return NULL;
-}
-
  int strncmp(const char *str1, const char *str2, size_t len)
  {
        const char *c1 = (const char *)str1;
@@ -166,3 +177,73 @@ int strcmp(const char *str1, const char *str2)
return __res;
  }
+
+/* The following code is taken from musl libc */
+#define ALIGN (sizeof(size_t))
+#define ONES ((size_t)-1/UCHAR_MAX)
+#define HIGHS (ONES * (UCHAR_MAX/2+1))
+#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
+#define BITOP(a,b,op) \
+ ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
+
+char *strchrnul(const char *s, int c)
+{
+       size_t *w, k;
+
+       c = (unsigned char)c;
+       if (!c) return (char *)s + strlen(s);
+
+       for (; (uintptr_t)s % ALIGN; s++)
+               if (!*s || *(unsigned char *)s == c) return (char *)s;
+       k = ONES * c;
+       for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
+       for (s = (void *)w; *s && *(unsigned char *)s != c; s++);
+       return (char *)s;
+}
+
+char *strchr(const char *s, int c)
+{
+       char *r = strchrnul(s, c);
+       return *(unsigned char *)r == (unsigned char)c ? r : 0;
+}
+
+size_t strcspn(const char *s, const char *c)
+{
+       const char *a = s;
+       size_t byteset[32/sizeof(size_t)];
+
+       if (!c[0] || !c[1]) return strchrnul(s, *c)-a;
+
+       memset(byteset, 0, sizeof byteset);
+       for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++);
+       for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++);
+       return s-a;
+}
+
+size_t strspn(const char *s, const char *c)
+{
+       const char *a = s;
+       size_t byteset[32/sizeof(size_t)] = { 0 };
+
+       if (!c[0]) return 0;
+       if (!c[1]) {
+               for (; *s == *c; s++);
+               return s-a;
+       }
+
+       for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++);
+       for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++);
+       return s-a;
+}
+
+char *strtok(char *restrict s, const char *restrict sep)
+{
+       static char *p;
+       if (!s && !(s = p)) return NULL;
+       s += strspn(s, sep);
+       if (!*s) return p = 0;
+       p = s + strcspn(s, sep);
+       if (*p) *p++ = 0;
+       else p = 0;
+       return s;
+}


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.