From c99cadbe02f659264cecb44c053d96383631147d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 17 Nov 2015 05:50:23 +0100 Subject: rearrange files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/string/mem/memcasecmp.c | 50 +++++++++++++++++++++++++ src/string/mem/memcasemem.c | 55 +++++++++++++++++++++++++++ src/string/mem/memccpy.c | 45 ++++++++++++++++++++++ src/string/mem/memchr.c | 42 +++++++++++++++++++++ src/string/mem/memcmove.c | 47 +++++++++++++++++++++++ src/string/mem/memcmp.c | 42 +++++++++++++++++++++ src/string/mem/memcpy.c | 38 +++++++++++++++++++ src/string/mem/memdup.c | 40 ++++++++++++++++++++ src/string/mem/memmem.c | 52 ++++++++++++++++++++++++++ src/string/mem/memmove.c | 42 +++++++++++++++++++++ src/string/mem/mempcpy.c | 36 ++++++++++++++++++ src/string/mem/mempmove.c | 37 ++++++++++++++++++ src/string/mem/memrchr.c | 46 +++++++++++++++++++++++ src/string/mem/memset.c | 37 ++++++++++++++++++ src/string/mem/rawmemchr.c | 42 +++++++++++++++++++++ src/string/mem/substring.h | 85 ++++++++++++++++++++++++++++++++++++++++++ src/string/memcasecmp.c | 50 ------------------------- src/string/memcasemem.c | 55 --------------------------- src/string/memccpy.c | 45 ---------------------- src/string/memchr.c | 42 --------------------- src/string/memcmove.c | 47 ----------------------- src/string/memcmp.c | 42 --------------------- src/string/memcpy.c | 38 ------------------- src/string/memdup.c | 40 -------------------- src/string/memmem.c | 52 -------------------------- src/string/memmove.c | 42 --------------------- src/string/mempcpy.c | 36 ------------------ src/string/mempmove.c | 37 ------------------ src/string/memrchr.c | 46 ----------------------- src/string/memset.c | 37 ------------------ src/string/rawmemchr.c | 42 --------------------- src/string/rawstrcasestr.c | 38 ------------------- src/string/rawstrstr.c | 37 ------------------ src/string/stpcpy.c | 34 ----------------- src/string/stpmove.c | 37 ------------------ src/string/stpncpy.c | 46 ----------------------- src/string/stpnmove.c | 47 ----------------------- src/string/str/rawstrcasestr.c | 38 +++++++++++++++++++ src/string/str/rawstrstr.c | 37 ++++++++++++++++++ src/string/str/stpcpy.c | 34 +++++++++++++++++ src/string/str/stpmove.c | 37 ++++++++++++++++++ src/string/str/stpncpy.c | 46 +++++++++++++++++++++++ src/string/str/stpnmove.c | 47 +++++++++++++++++++++++ src/string/str/strcasecmp.c | 37 ++++++++++++++++++ src/string/str/strcaseends.c | 41 ++++++++++++++++++++ src/string/str/strcasestarts.c | 41 ++++++++++++++++++++ src/string/str/strcasestr.c | 35 +++++++++++++++++ src/string/str/strcat.c | 37 ++++++++++++++++++ src/string/str/strccpy.c | 45 ++++++++++++++++++++++ src/string/str/strchr.c | 46 +++++++++++++++++++++++ src/string/str/strchrnul.c | 47 +++++++++++++++++++++++ src/string/str/strcmove.c | 45 ++++++++++++++++++++++ src/string/str/strcmp.c | 36 ++++++++++++++++++ src/string/str/strcncpy.c | 54 +++++++++++++++++++++++++++ src/string/str/strcnmove.c | 54 +++++++++++++++++++++++++++ src/string/str/strcpy.c | 34 +++++++++++++++++ src/string/str/strcspn.c | 44 ++++++++++++++++++++++ src/string/str/strdup.c | 38 +++++++++++++++++++ src/string/str/strends.c | 41 ++++++++++++++++++++ src/string/str/strlen.c | 35 +++++++++++++++++ src/string/str/strmove.c | 36 ++++++++++++++++++ src/string/str/strncasecmp.c | 50 +++++++++++++++++++++++++ src/string/str/strncasestr.c | 38 +++++++++++++++++++ src/string/str/strncat.c | 43 +++++++++++++++++++++ src/string/str/strncmp.c | 39 +++++++++++++++++++ src/string/str/strncpy.c | 43 +++++++++++++++++++++ src/string/str/strndup.c | 43 +++++++++++++++++++++ src/string/str/strnlen.c | 36 ++++++++++++++++++ src/string/str/strnmove.c | 43 +++++++++++++++++++++ src/string/str/strnstr.c | 39 +++++++++++++++++++ src/string/str/strpbrk.c | 47 +++++++++++++++++++++++ src/string/str/strrchr.c | 48 ++++++++++++++++++++++++ src/string/str/strsep.c | 51 +++++++++++++++++++++++++ src/string/str/strset.c | 39 +++++++++++++++++++ src/string/str/strspn.c | 44 ++++++++++++++++++++++ src/string/str/strstarts.c | 41 ++++++++++++++++++++ src/string/str/strstr.c | 37 ++++++++++++++++++ src/string/str/strstrcpy.c | 47 +++++++++++++++++++++++ src/string/str/strstrmove.c | 47 +++++++++++++++++++++++ src/string/str/strstrncpy.c | 55 +++++++++++++++++++++++++++ src/string/str/strstrnmove.c | 54 +++++++++++++++++++++++++++ src/string/str/strtok.c | 44 ++++++++++++++++++++++ src/string/str/strtok_r.c | 56 ++++++++++++++++++++++++++++ src/string/strcasecmp.c | 37 ------------------ src/string/strcaseends.c | 41 -------------------- src/string/strcasestarts.c | 41 -------------------- src/string/strcasestr.c | 35 ----------------- src/string/strcat.c | 37 ------------------ src/string/strccpy.c | 45 ---------------------- src/string/strchr.c | 46 ----------------------- src/string/strchrnul.c | 47 ----------------------- src/string/strcmove.c | 45 ---------------------- src/string/strcmp.c | 36 ------------------ src/string/strcncpy.c | 54 --------------------------- src/string/strcnmove.c | 54 --------------------------- src/string/strcpy.c | 34 ----------------- src/string/strcspn.c | 44 ---------------------- src/string/strdup.c | 38 ------------------- src/string/strends.c | 41 -------------------- src/string/strlen.c | 35 ----------------- src/string/strmove.c | 36 ------------------ src/string/strncasecmp.c | 50 ------------------------- src/string/strncasestr.c | 38 ------------------- src/string/strncat.c | 43 --------------------- src/string/strncmp.c | 39 ------------------- src/string/strncpy.c | 43 --------------------- src/string/strndup.c | 43 --------------------- src/string/strnlen.c | 36 ------------------ src/string/strnmove.c | 43 --------------------- src/string/strnstr.c | 39 ------------------- src/string/strpbrk.c | 47 ----------------------- src/string/strrchr.c | 48 ------------------------ src/string/strsep.c | 51 ------------------------- src/string/strset.c | 39 ------------------- src/string/strspn.c | 44 ---------------------- src/string/strstarts.c | 41 -------------------- src/string/strstr.c | 37 ------------------ src/string/strstrcpy.c | 47 ----------------------- src/string/strstrmove.c | 47 ----------------------- src/string/strstrncpy.c | 55 --------------------------- src/string/strstrnmove.c | 54 --------------------------- src/string/strtok.c | 44 ---------------------- src/string/strtok_r.c | 56 ---------------------------- src/string/substring.h | 85 ------------------------------------------ 124 files changed, 2705 insertions(+), 2705 deletions(-) create mode 100644 src/string/mem/memcasecmp.c create mode 100644 src/string/mem/memcasemem.c create mode 100644 src/string/mem/memccpy.c create mode 100644 src/string/mem/memchr.c create mode 100644 src/string/mem/memcmove.c create mode 100644 src/string/mem/memcmp.c create mode 100644 src/string/mem/memcpy.c create mode 100644 src/string/mem/memdup.c create mode 100644 src/string/mem/memmem.c create mode 100644 src/string/mem/memmove.c create mode 100644 src/string/mem/mempcpy.c create mode 100644 src/string/mem/mempmove.c create mode 100644 src/string/mem/memrchr.c create mode 100644 src/string/mem/memset.c create mode 100644 src/string/mem/rawmemchr.c create mode 100644 src/string/mem/substring.h delete mode 100644 src/string/memcasecmp.c delete mode 100644 src/string/memcasemem.c delete mode 100644 src/string/memccpy.c delete mode 100644 src/string/memchr.c delete mode 100644 src/string/memcmove.c delete mode 100644 src/string/memcmp.c delete mode 100644 src/string/memcpy.c delete mode 100644 src/string/memdup.c delete mode 100644 src/string/memmem.c delete mode 100644 src/string/memmove.c delete mode 100644 src/string/mempcpy.c delete mode 100644 src/string/mempmove.c delete mode 100644 src/string/memrchr.c delete mode 100644 src/string/memset.c delete mode 100644 src/string/rawmemchr.c delete mode 100644 src/string/rawstrcasestr.c delete mode 100644 src/string/rawstrstr.c delete mode 100644 src/string/stpcpy.c delete mode 100644 src/string/stpmove.c delete mode 100644 src/string/stpncpy.c delete mode 100644 src/string/stpnmove.c create mode 100644 src/string/str/rawstrcasestr.c create mode 100644 src/string/str/rawstrstr.c create mode 100644 src/string/str/stpcpy.c create mode 100644 src/string/str/stpmove.c create mode 100644 src/string/str/stpncpy.c create mode 100644 src/string/str/stpnmove.c create mode 100644 src/string/str/strcasecmp.c create mode 100644 src/string/str/strcaseends.c create mode 100644 src/string/str/strcasestarts.c create mode 100644 src/string/str/strcasestr.c create mode 100644 src/string/str/strcat.c create mode 100644 src/string/str/strccpy.c create mode 100644 src/string/str/strchr.c create mode 100644 src/string/str/strchrnul.c create mode 100644 src/string/str/strcmove.c create mode 100644 src/string/str/strcmp.c create mode 100644 src/string/str/strcncpy.c create mode 100644 src/string/str/strcnmove.c create mode 100644 src/string/str/strcpy.c create mode 100644 src/string/str/strcspn.c create mode 100644 src/string/str/strdup.c create mode 100644 src/string/str/strends.c create mode 100644 src/string/str/strlen.c create mode 100644 src/string/str/strmove.c create mode 100644 src/string/str/strncasecmp.c create mode 100644 src/string/str/strncasestr.c create mode 100644 src/string/str/strncat.c create mode 100644 src/string/str/strncmp.c create mode 100644 src/string/str/strncpy.c create mode 100644 src/string/str/strndup.c create mode 100644 src/string/str/strnlen.c create mode 100644 src/string/str/strnmove.c create mode 100644 src/string/str/strnstr.c create mode 100644 src/string/str/strpbrk.c create mode 100644 src/string/str/strrchr.c create mode 100644 src/string/str/strsep.c create mode 100644 src/string/str/strset.c create mode 100644 src/string/str/strspn.c create mode 100644 src/string/str/strstarts.c create mode 100644 src/string/str/strstr.c create mode 100644 src/string/str/strstrcpy.c create mode 100644 src/string/str/strstrmove.c create mode 100644 src/string/str/strstrncpy.c create mode 100644 src/string/str/strstrnmove.c create mode 100644 src/string/str/strtok.c create mode 100644 src/string/str/strtok_r.c delete mode 100644 src/string/strcasecmp.c delete mode 100644 src/string/strcaseends.c delete mode 100644 src/string/strcasestarts.c delete mode 100644 src/string/strcasestr.c delete mode 100644 src/string/strcat.c delete mode 100644 src/string/strccpy.c delete mode 100644 src/string/strchr.c delete mode 100644 src/string/strchrnul.c delete mode 100644 src/string/strcmove.c delete mode 100644 src/string/strcmp.c delete mode 100644 src/string/strcncpy.c delete mode 100644 src/string/strcnmove.c delete mode 100644 src/string/strcpy.c delete mode 100644 src/string/strcspn.c delete mode 100644 src/string/strdup.c delete mode 100644 src/string/strends.c delete mode 100644 src/string/strlen.c delete mode 100644 src/string/strmove.c delete mode 100644 src/string/strncasecmp.c delete mode 100644 src/string/strncasestr.c delete mode 100644 src/string/strncat.c delete mode 100644 src/string/strncmp.c delete mode 100644 src/string/strncpy.c delete mode 100644 src/string/strndup.c delete mode 100644 src/string/strnlen.c delete mode 100644 src/string/strnmove.c delete mode 100644 src/string/strnstr.c delete mode 100644 src/string/strpbrk.c delete mode 100644 src/string/strrchr.c delete mode 100644 src/string/strsep.c delete mode 100644 src/string/strset.c delete mode 100644 src/string/strspn.c delete mode 100644 src/string/strstarts.c delete mode 100644 src/string/strstr.c delete mode 100644 src/string/strstrcpy.c delete mode 100644 src/string/strstrmove.c delete mode 100644 src/string/strstrncpy.c delete mode 100644 src/string/strstrnmove.c delete mode 100644 src/string/strtok.c delete mode 100644 src/string/strtok_r.c delete mode 100644 src/string/substring.h (limited to 'src/string') diff --git a/src/string/mem/memcasecmp.c b/src/string/mem/memcasecmp.c new file mode 100644 index 0000000..d8ea014 --- /dev/null +++ b/src/string/mem/memcasecmp.c @@ -0,0 +1,50 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Compare two memory segments alphabetically in a case insensitive manner. + * + * This is a slibc extension added because it was useful + * in implementing slibc itself. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @param size The size of the segments. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int memcasecmp(const void* a, const void* b, size_t size) +{ + const signed char* s1 = a; + const signed char* s2 = b; + int c1, c2; + for (; size--; s1++, s2++) + if (*s1 != *s2) + { + c1 = isalpha(*s1) ? tolower(*s1) : (int)*s1; + c2 = isalpha(*s2) ? tolower(*s2) : (int)*s2; + if ((c1 -= c2)) + return c1; + } + return 0; +} + diff --git a/src/string/mem/memcasemem.c b/src/string/mem/memcasemem.c new file mode 100644 index 0000000..3fb24dc --- /dev/null +++ b/src/string/mem/memcasemem.c @@ -0,0 +1,55 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Finds the first occurrence of a substring. + * This search is case insensitive. + * + * This is a slibc extension added because it was useful + * in implementing slibc itself. + * + * @param haystack The string to search. + * @param haystack_length The number of character to search. + * @param needle The sought after substring. + * @param needle_length The length of `needle`. + * @return Pointer to the first occurrence of + * the substring, `NULL` if not found. + */ +void* (memcasemem)(const void* __haystack, size_t haystack_length, + const void* __needle, size_t needle_length) +{ + const char* haystack = __haystack; + const char* needle = __needle; + if (haystack_length < needle_length) + return NULL; + if (haystack_length == needle_length) + return !(memcasecmp)(haystack, needle, haystack_length) ? haystack : NULL; +#define CASE +#include "substring.h" +#undef CASE +} + diff --git a/src/string/mem/memccpy.c b/src/string/mem/memccpy.c new file mode 100644 index 0000000..18e423d --- /dev/null +++ b/src/string/mem/memccpy.c @@ -0,0 +1,45 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * but stop if a specific byte is encountered. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The byte to stop at if encountered. + * @param size The maximum number of bytes to copy. + * @return `NULL` if `c` was not encountered, otherwise + * the possition of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written character. + */ +void* (memccpy)(void* restrict whither, const void* restrict whence, int c, size_t size) +{ + char* stop = (memchr)(whence, c, size); + void* r = NULL; + if (stop != NULL) + size = (size_t)(stop - (const char*)whence), r = whither + size; + memcpy(whither, whence, size); + return r; +} + diff --git a/src/string/mem/memchr.c b/src/string/mem/memchr.c new file mode 100644 index 0000000..380fa0f --- /dev/null +++ b/src/string/mem/memchr.c @@ -0,0 +1,42 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the first occurrence of a byte in a memory segment. + * + * @param segment The memory segment to search. + * @param c The sought after character. + * @param size The size of the memory segment. + * @return Pointer to the first occurrence of `c`, + * `NULL` if none were found. + */ +void* (memchr)(const void* segment, int c, size_t size) +{ + char* s = segment; + while (size--) + if (*s++ == c) + return s - 1; + return NULL; +} + diff --git a/src/string/mem/memcmove.c b/src/string/mem/memcmove.c new file mode 100644 index 0000000..50ed1f9 --- /dev/null +++ b/src/string/mem/memcmove.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * but stop if a specific byte is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The byte to stop at if encountered. + * @param size The maximum number of bytes to copy. + * @return `NULL` if `c` was not encountered, otherwise + * the possition of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written character. + */ +void* (memcmove)(void* whither, const void* whence, int c, size_t size) +{ + char* stop = (memchr)(whence, c, size); + void* r = NULL; + if (stop != NULL) + size = (size_t)(stop - (const char*)whence), r = whither + size; + memmove(whither, whence, size); + return r; +} + diff --git a/src/string/mem/memcmp.c b/src/string/mem/memcmp.c new file mode 100644 index 0000000..112cdd1 --- /dev/null +++ b/src/string/mem/memcmp.c @@ -0,0 +1,42 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Compare two memory segments alphabetically in a case sensitive manner. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @param size The size of the segments. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int memcmp(const void* a, const void* b, size_t size) +{ + const signed char* s1 = a; + const signed char* s2 = b; + while (size--) + if (*s1 == *s2) + s1++, s2++; + else + return (int)(*s1 - *s2); + return 0; +} + diff --git a/src/string/mem/memcpy.c b/src/string/mem/memcpy.c new file mode 100644 index 0000000..fcda29d --- /dev/null +++ b/src/string/mem/memcpy.c @@ -0,0 +1,38 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param size The number of bytes to copy. + * @return `whither` is returned. + */ +void* memcpy(void* restrict whither, const void* restrict whence, size_t size) +{ + char* d = whither; + const char* s = whence; + while (size--) + *d++ = *s++; + return whither; +} + diff --git a/src/string/mem/memdup.c b/src/string/mem/memdup.c new file mode 100644 index 0000000..3b301dc --- /dev/null +++ b/src/string/mem/memdup.c @@ -0,0 +1,40 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Duplicate a memory segment. + * + * This is a slibc extension. + * + * @param segment The memory segment to duplicate. + * @param size The size of the memory segment. + * @return The new segment. `NULL` is returned on error + * and `errno` is set to indicate the error. + * + * @throws ENOMEM The process could not allocate sufficient amount of memory. + */ +void* memdup(const void* segment, size_t size) +{ + void* r = malloc(size); + return r == NULL ? NULL : memcpy(r, segment, size); +} + diff --git a/src/string/mem/memmem.c b/src/string/mem/memmem.c new file mode 100644 index 0000000..62739d4 --- /dev/null +++ b/src/string/mem/memmem.c @@ -0,0 +1,52 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Finds the first occurrence of a substring. + * This search is case sensitive. + * + * This is a GNU-compliant slibc extension. It was useful + * in implementing slibc itself. + * + * @param haystack The string to search. + * @param haystack_length The number of character to search. + * @param needle The sought after substring. + * @param needle_length The length of `needle`. + * @return Pointer to the first occurrence of + * the substring, `NULL` if not found. + */ +void* (memmem)(const void* __haystack, size_t haystack_length, + const void* __needle, size_t needle_length) +{ + const char* haystack = __haystack; + const char* needle = __needle; + if (haystack_length < needle_length) + return NULL; + if (haystack_length == needle_length) + return !(memcmp)(haystack, needle, haystack_length) ? haystack : NULL; +#include "substring.h" +} + diff --git a/src/string/mem/memmove.c b/src/string/mem/memmove.c new file mode 100644 index 0000000..41871c6 --- /dev/null +++ b/src/string/mem/memmove.c @@ -0,0 +1,42 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param size The number of bytes to copy. + * @return `whither` is returned. + */ +void* memmove(void* whither, const void* whence, size_t size) +{ + char* d = whither; + const char* s = whence; + if ((size_t)(d - s) < size) + while (size--) + d[size] = s[size]; + else + while (size--) + *d++ = *s++; + return whither; +} + diff --git a/src/string/mem/mempcpy.c b/src/string/mem/mempcpy.c new file mode 100644 index 0000000..2072dc3 --- /dev/null +++ b/src/string/mem/mempcpy.c @@ -0,0 +1,36 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment. + * + * This is a GNU extension. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param size The number of bytes to copy. + * @return `whither + size` is returned. + */ +void* mempcpy(void* restrict whither, const void* restrict whence, size_t size) +{ + return (char*)memcpy(whither, whence, size) + size; +} + diff --git a/src/string/mem/mempmove.c b/src/string/mem/mempmove.c new file mode 100644 index 0000000..f837032 --- /dev/null +++ b/src/string/mem/mempmove.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param size The number of bytes to copy. + * @return `whither + size` is returned. + */ +void* mempmove(void* whither, const void* whence, size_t size) +{ + return (char*)memmove(whither, whence, size) + size; +} + diff --git a/src/string/mem/memrchr.c b/src/string/mem/memrchr.c new file mode 100644 index 0000000..7325049 --- /dev/null +++ b/src/string/mem/memrchr.c @@ -0,0 +1,46 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the last occurrence of a byte in a memory segment. + * + * For improved performace, use this function instead + * of `strrchr` if you already know the length of the + * string. + * + * @param segment The memory segment to search. + * @param c The sought after character. + * @param size The size of the memory segment. + * @return Pointer to the last occurrence of `c`, + * `NULL` if none were found. + */ +void* (memrchr)(const void* segment, int c, size_t size) +{ + char* s = segment; + while (size--) + if (s[size] == c) + return s + size; + return NULL; +} + diff --git a/src/string/mem/memset.c b/src/string/mem/memset.c new file mode 100644 index 0000000..df36052 --- /dev/null +++ b/src/string/mem/memset.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Override a memory segment with a repeated character. + * + * @param segment The beginning of the memory segment. + * @param c The character (8 bits wide.) + * @param size The size of the memory segment. + * @return `segment` is returned. + */ +void* memset(void* segment, int c, size_t size) +{ + char* s = segment; + while (size--) + *s++ = (char)c; + return segment; +} + diff --git a/src/string/mem/rawmemchr.c b/src/string/mem/rawmemchr.c new file mode 100644 index 0000000..8f5c03e --- /dev/null +++ b/src/string/mem/rawmemchr.c @@ -0,0 +1,42 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the first occurrence of a byte in a memory segment. + * The memory segment must be known to contain the sought after byte. + * + * This is a GNU-compliant slibc extension. + * + * @param segment The memory segment to search. + * @param c The sought after character. + * @return Pointer to the first occurrence of `c`. + */ +void* (rawmemchr)(const void* segment, int c) +{ + char* s = segment; + for (;;) + if (*s++ == c) + return s - 1; +} + diff --git a/src/string/mem/substring.h b/src/string/mem/substring.h new file mode 100644 index 0000000..05ef7cc --- /dev/null +++ b/src/string/mem/substring.h @@ -0,0 +1,85 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +/* This file is intended to be included inside a + * [w]mem[r][case]mem function. `haystack` and + * `needle` shall be defined the same pointer type, + * but not as `void*`. `haystack_length` and + * `needle_length` shall be defined in the `size_t` + * type. CASE shall be defined iff case insensitive + * search shall be used. RIGHT shall be defined iff + * the last occurrence shall be find. WIDE shall be + * defined iff wide characters are used. It is assumed + * that `needle_length` <= `haystack_length` */ + + +/* The Knuth–Morris–Pratt algorithm. */ + + +/** + * Test whether to characters are equal. + * Case-sensitivity depends on `CASE` being defined. + * + * @param a One of the characters. + * @param b The other character. + * @return 1 if `a` and `b` or equal, 0 otherwise. + * The comparison is case-insensitive if + * `CASE` is defined. + */ +#if !defined(CASE) +# define CHREQ(a, b) (a == b) +#elif !defined(WIDE) +# define CHREQ(a, b) ((islower(a) ? tolower(a) : (a)) == tolower(b)) +#else +# define CHREQ(a, b) ((iswlower(a) ? towlower(a) : (a)) == towlower(b)) +#endif + + +/* TODO add support for RIGHT */ + + +/* The implementation of the algorithm, read + * elsewhere for documentation/explanation. */ +{ + ssize_t* next_map = alloca((needle_length + 1) * sizeof(ssize_t)); + ssize_t hay, ned, skp; + + ned = 0, skp = next_map[0] = -1; + while (ned < (ssize_t)needle_length) + { + while ((skp > -1) && !CHREQ(needle[ned], needle[skp])) + skp = next_map[skp]; + ned++, skp++; + next_map[ned] = CHREQ(needle[ned], needle[skp]) ? next_map[skp] : skp; + } + + hay = ned = 0; + while (hay < (ssize_t)haystack_length) + { + while ((ned > -1) && !CHREQ(haystack[hay], needle[ned])) + ned = next_map[ned]; + hay++, ned++; + if (ned >= (ssize_t)needle_length) + return needle + (hay - ned); + } + + return NULL; +} + + +#undef CHREQ + diff --git a/src/string/memcasecmp.c b/src/string/memcasecmp.c deleted file mode 100644 index d8ea014..0000000 --- a/src/string/memcasecmp.c +++ /dev/null @@ -1,50 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Compare two memory segments alphabetically in a case insensitive manner. - * - * This is a slibc extension added because it was useful - * in implementing slibc itself. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @param size The size of the segments. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int memcasecmp(const void* a, const void* b, size_t size) -{ - const signed char* s1 = a; - const signed char* s2 = b; - int c1, c2; - for (; size--; s1++, s2++) - if (*s1 != *s2) - { - c1 = isalpha(*s1) ? tolower(*s1) : (int)*s1; - c2 = isalpha(*s2) ? tolower(*s2) : (int)*s2; - if ((c1 -= c2)) - return c1; - } - return 0; -} - diff --git a/src/string/memcasemem.c b/src/string/memcasemem.c deleted file mode 100644 index 3fb24dc..0000000 --- a/src/string/memcasemem.c +++ /dev/null @@ -1,55 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include -#include -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Finds the first occurrence of a substring. - * This search is case insensitive. - * - * This is a slibc extension added because it was useful - * in implementing slibc itself. - * - * @param haystack The string to search. - * @param haystack_length The number of character to search. - * @param needle The sought after substring. - * @param needle_length The length of `needle`. - * @return Pointer to the first occurrence of - * the substring, `NULL` if not found. - */ -void* (memcasemem)(const void* __haystack, size_t haystack_length, - const void* __needle, size_t needle_length) -{ - const char* haystack = __haystack; - const char* needle = __needle; - if (haystack_length < needle_length) - return NULL; - if (haystack_length == needle_length) - return !(memcasecmp)(haystack, needle, haystack_length) ? haystack : NULL; -#define CASE -#include "substring.h" -#undef CASE -} - diff --git a/src/string/memccpy.c b/src/string/memccpy.c deleted file mode 100644 index 18e423d..0000000 --- a/src/string/memccpy.c +++ /dev/null @@ -1,45 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * but stop if a specific byte is encountered. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The byte to stop at if encountered. - * @param size The maximum number of bytes to copy. - * @return `NULL` if `c` was not encountered, otherwise - * the possition of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written character. - */ -void* (memccpy)(void* restrict whither, const void* restrict whence, int c, size_t size) -{ - char* stop = (memchr)(whence, c, size); - void* r = NULL; - if (stop != NULL) - size = (size_t)(stop - (const char*)whence), r = whither + size; - memcpy(whither, whence, size); - return r; -} - diff --git a/src/string/memchr.c b/src/string/memchr.c deleted file mode 100644 index 380fa0f..0000000 --- a/src/string/memchr.c +++ /dev/null @@ -1,42 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the first occurrence of a byte in a memory segment. - * - * @param segment The memory segment to search. - * @param c The sought after character. - * @param size The size of the memory segment. - * @return Pointer to the first occurrence of `c`, - * `NULL` if none were found. - */ -void* (memchr)(const void* segment, int c, size_t size) -{ - char* s = segment; - while (size--) - if (*s++ == c) - return s - 1; - return NULL; -} - diff --git a/src/string/memcmove.c b/src/string/memcmove.c deleted file mode 100644 index 50ed1f9..0000000 --- a/src/string/memcmove.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * but stop if a specific byte is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The byte to stop at if encountered. - * @param size The maximum number of bytes to copy. - * @return `NULL` if `c` was not encountered, otherwise - * the possition of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written character. - */ -void* (memcmove)(void* whither, const void* whence, int c, size_t size) -{ - char* stop = (memchr)(whence, c, size); - void* r = NULL; - if (stop != NULL) - size = (size_t)(stop - (const char*)whence), r = whither + size; - memmove(whither, whence, size); - return r; -} - diff --git a/src/string/memcmp.c b/src/string/memcmp.c deleted file mode 100644 index 112cdd1..0000000 --- a/src/string/memcmp.c +++ /dev/null @@ -1,42 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Compare two memory segments alphabetically in a case sensitive manner. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @param size The size of the segments. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int memcmp(const void* a, const void* b, size_t size) -{ - const signed char* s1 = a; - const signed char* s2 = b; - while (size--) - if (*s1 == *s2) - s1++, s2++; - else - return (int)(*s1 - *s2); - return 0; -} - diff --git a/src/string/memcpy.c b/src/string/memcpy.c deleted file mode 100644 index fcda29d..0000000 --- a/src/string/memcpy.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param size The number of bytes to copy. - * @return `whither` is returned. - */ -void* memcpy(void* restrict whither, const void* restrict whence, size_t size) -{ - char* d = whither; - const char* s = whence; - while (size--) - *d++ = *s++; - return whither; -} - diff --git a/src/string/memdup.c b/src/string/memdup.c deleted file mode 100644 index 3b301dc..0000000 --- a/src/string/memdup.c +++ /dev/null @@ -1,40 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Duplicate a memory segment. - * - * This is a slibc extension. - * - * @param segment The memory segment to duplicate. - * @param size The size of the memory segment. - * @return The new segment. `NULL` is returned on error - * and `errno` is set to indicate the error. - * - * @throws ENOMEM The process could not allocate sufficient amount of memory. - */ -void* memdup(const void* segment, size_t size) -{ - void* r = malloc(size); - return r == NULL ? NULL : memcpy(r, segment, size); -} - diff --git a/src/string/memmem.c b/src/string/memmem.c deleted file mode 100644 index 62739d4..0000000 --- a/src/string/memmem.c +++ /dev/null @@ -1,52 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Finds the first occurrence of a substring. - * This search is case sensitive. - * - * This is a GNU-compliant slibc extension. It was useful - * in implementing slibc itself. - * - * @param haystack The string to search. - * @param haystack_length The number of character to search. - * @param needle The sought after substring. - * @param needle_length The length of `needle`. - * @return Pointer to the first occurrence of - * the substring, `NULL` if not found. - */ -void* (memmem)(const void* __haystack, size_t haystack_length, - const void* __needle, size_t needle_length) -{ - const char* haystack = __haystack; - const char* needle = __needle; - if (haystack_length < needle_length) - return NULL; - if (haystack_length == needle_length) - return !(memcmp)(haystack, needle, haystack_length) ? haystack : NULL; -#include "substring.h" -} - diff --git a/src/string/memmove.c b/src/string/memmove.c deleted file mode 100644 index 41871c6..0000000 --- a/src/string/memmove.c +++ /dev/null @@ -1,42 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param size The number of bytes to copy. - * @return `whither` is returned. - */ -void* memmove(void* whither, const void* whence, size_t size) -{ - char* d = whither; - const char* s = whence; - if ((size_t)(d - s) < size) - while (size--) - d[size] = s[size]; - else - while (size--) - *d++ = *s++; - return whither; -} - diff --git a/src/string/mempcpy.c b/src/string/mempcpy.c deleted file mode 100644 index 2072dc3..0000000 --- a/src/string/mempcpy.c +++ /dev/null @@ -1,36 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment. - * - * This is a GNU extension. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param size The number of bytes to copy. - * @return `whither + size` is returned. - */ -void* mempcpy(void* restrict whither, const void* restrict whence, size_t size) -{ - return (char*)memcpy(whither, whence, size) + size; -} - diff --git a/src/string/mempmove.c b/src/string/mempmove.c deleted file mode 100644 index f837032..0000000 --- a/src/string/mempmove.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param size The number of bytes to copy. - * @return `whither + size` is returned. - */ -void* mempmove(void* whither, const void* whence, size_t size) -{ - return (char*)memmove(whither, whence, size) + size; -} - diff --git a/src/string/memrchr.c b/src/string/memrchr.c deleted file mode 100644 index 7325049..0000000 --- a/src/string/memrchr.c +++ /dev/null @@ -1,46 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the last occurrence of a byte in a memory segment. - * - * For improved performace, use this function instead - * of `strrchr` if you already know the length of the - * string. - * - * @param segment The memory segment to search. - * @param c The sought after character. - * @param size The size of the memory segment. - * @return Pointer to the last occurrence of `c`, - * `NULL` if none were found. - */ -void* (memrchr)(const void* segment, int c, size_t size) -{ - char* s = segment; - while (size--) - if (s[size] == c) - return s + size; - return NULL; -} - diff --git a/src/string/memset.c b/src/string/memset.c deleted file mode 100644 index df36052..0000000 --- a/src/string/memset.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Override a memory segment with a repeated character. - * - * @param segment The beginning of the memory segment. - * @param c The character (8 bits wide.) - * @param size The size of the memory segment. - * @return `segment` is returned. - */ -void* memset(void* segment, int c, size_t size) -{ - char* s = segment; - while (size--) - *s++ = (char)c; - return segment; -} - diff --git a/src/string/rawmemchr.c b/src/string/rawmemchr.c deleted file mode 100644 index 8f5c03e..0000000 --- a/src/string/rawmemchr.c +++ /dev/null @@ -1,42 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the first occurrence of a byte in a memory segment. - * The memory segment must be known to contain the sought after byte. - * - * This is a GNU-compliant slibc extension. - * - * @param segment The memory segment to search. - * @param c The sought after character. - * @return Pointer to the first occurrence of `c`. - */ -void* (rawmemchr)(const void* segment, int c) -{ - char* s = segment; - for (;;) - if (*s++ == c) - return s - 1; -} - diff --git a/src/string/rawstrcasestr.c b/src/string/rawstrcasestr.c deleted file mode 100644 index 7e4cf9b..0000000 --- a/src/string/rawstrcasestr.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case insensitive. - * It must already be known that such a substring exists. - * - * This is a slibc extension. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @return Pointer to the first occurrence of the substring. - */ -char* (rawstrcasestr)(const char* haystack, const char* needle) -{ - return (memcasemem)(haystack, SIZE_MAX, needle, strlen(needle)); -} - diff --git a/src/string/rawstrstr.c b/src/string/rawstrstr.c deleted file mode 100644 index 9c3f186..0000000 --- a/src/string/rawstrstr.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case sensitive. - * It must already be known that such a substring exists. - * - * This is a slibc extension. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @return Pointer to the first occurrence of the substring. - */ -char* (rawstrstr)(const char* haystack, const char* needle) -{ - return (memmem)(haystack, SIZE_MAX, needle, strlen(needle)); -} - diff --git a/src/string/stpcpy.c b/src/string/stpcpy.c deleted file mode 100644 index a0d5f79..0000000 --- a/src/string/stpcpy.c +++ /dev/null @@ -1,34 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte is encountered. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @return `whither + strlen(whence)` is returned. - */ -char* stpcpy(char* restrict whither, const char* restrict whence) -{ - return mempcpy(whither, whence, strlen(whence) + 1) - 1; -} - diff --git a/src/string/stpmove.c b/src/string/stpmove.c deleted file mode 100644 index 8818c30..0000000 --- a/src/string/stpmove.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @return `whither + strlen(whence)` is returned. - */ -char* stpmove(char* whither, const char* whence) -{ - return mempmove(whither, whence, strlen(whence) + 1) - 1; -} - - diff --git a/src/string/stpncpy.c b/src/string/stpncpy.c deleted file mode 100644 index 876eaf0..0000000 --- a/src/string/stpncpy.c +++ /dev/null @@ -1,46 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte is encountered. - * - * This is a GNU extension. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `whither` plus the number of written bytes, - * excluding NUL bytes, is returned. - */ -char* stpncpy(char* restrict whither, const char* restrict whence, size_t maxlen) -{ - size_t n = strnlen(whence, maxlen); - memcpy(whither, whence, n); - memset(whither, 0, maxlen - n); - return whither + n; -} - diff --git a/src/string/stpnmove.c b/src/string/stpnmove.c deleted file mode 100644 index a0a0de9..0000000 --- a/src/string/stpnmove.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte is encountered. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `whither` plus the number of written bytes, - * excluding NUL bytes, is returned. - */ -char* stpnmove(char* whither, const char* whence, size_t maxlen) -{ - size_t n = strnlen(whence, maxlen); - memmove(whither, whence, n); - memset(whither, 0, maxlen - n); - return whither + n; -} - diff --git a/src/string/str/rawstrcasestr.c b/src/string/str/rawstrcasestr.c new file mode 100644 index 0000000..7e4cf9b --- /dev/null +++ b/src/string/str/rawstrcasestr.c @@ -0,0 +1,38 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case insensitive. + * It must already be known that such a substring exists. + * + * This is a slibc extension. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @return Pointer to the first occurrence of the substring. + */ +char* (rawstrcasestr)(const char* haystack, const char* needle) +{ + return (memcasemem)(haystack, SIZE_MAX, needle, strlen(needle)); +} + diff --git a/src/string/str/rawstrstr.c b/src/string/str/rawstrstr.c new file mode 100644 index 0000000..9c3f186 --- /dev/null +++ b/src/string/str/rawstrstr.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case sensitive. + * It must already be known that such a substring exists. + * + * This is a slibc extension. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @return Pointer to the first occurrence of the substring. + */ +char* (rawstrstr)(const char* haystack, const char* needle) +{ + return (memmem)(haystack, SIZE_MAX, needle, strlen(needle)); +} + diff --git a/src/string/str/stpcpy.c b/src/string/str/stpcpy.c new file mode 100644 index 0000000..a0d5f79 --- /dev/null +++ b/src/string/str/stpcpy.c @@ -0,0 +1,34 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte is encountered. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @return `whither + strlen(whence)` is returned. + */ +char* stpcpy(char* restrict whither, const char* restrict whence) +{ + return mempcpy(whither, whence, strlen(whence) + 1) - 1; +} + diff --git a/src/string/str/stpmove.c b/src/string/str/stpmove.c new file mode 100644 index 0000000..8818c30 --- /dev/null +++ b/src/string/str/stpmove.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @return `whither + strlen(whence)` is returned. + */ +char* stpmove(char* whither, const char* whence) +{ + return mempmove(whither, whence, strlen(whence) + 1) - 1; +} + + diff --git a/src/string/str/stpncpy.c b/src/string/str/stpncpy.c new file mode 100644 index 0000000..876eaf0 --- /dev/null +++ b/src/string/str/stpncpy.c @@ -0,0 +1,46 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte is encountered. + * + * This is a GNU extension. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `whither` plus the number of written bytes, + * excluding NUL bytes, is returned. + */ +char* stpncpy(char* restrict whither, const char* restrict whence, size_t maxlen) +{ + size_t n = strnlen(whence, maxlen); + memcpy(whither, whence, n); + memset(whither, 0, maxlen - n); + return whither + n; +} + diff --git a/src/string/str/stpnmove.c b/src/string/str/stpnmove.c new file mode 100644 index 0000000..a0a0de9 --- /dev/null +++ b/src/string/str/stpnmove.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte is encountered. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `whither` plus the number of written bytes, + * excluding NUL bytes, is returned. + */ +char* stpnmove(char* whither, const char* whence, size_t maxlen) +{ + size_t n = strnlen(whence, maxlen); + memmove(whither, whence, n); + memset(whither, 0, maxlen - n); + return whither + n; +} + diff --git a/src/string/str/strcasecmp.c b/src/string/str/strcasecmp.c new file mode 100644 index 0000000..eb02084 --- /dev/null +++ b/src/string/str/strcasecmp.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Compare two strings alphabetically in a case insensitive manner. + * Be aware, only ASCII characters are case insensitive, non-ASCII + * characters are case sensitive. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int strcasecmp(const char* a, const char* b) +{ + return strncasecmp(a, b, SIZE_MAX); +} + diff --git a/src/string/str/strcaseends.c b/src/string/str/strcaseends.c new file mode 100644 index 0000000..9155ea8 --- /dev/null +++ b/src/string/str/strcaseends.c @@ -0,0 +1,41 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Check whether a string ends with a specific string. + * This check is case insensitive. + * + * This is a slibc extension. + * + * @param string The string to inspect. + * @param desired The desired ending of the string. + * @return The `string`, where `desired` beings if + * `string` ends with `desired`, `NULL` otherwise. + */ +char* (strcaseends)(const char* string, const char* desired) +{ + size_t n = strlen(string); + size_t m = strlen(desired); + if (n < m) + return NULL; + return (memcasecmp)(string + (n - m), desired, m) ? NULL : (string + n); +} + diff --git a/src/string/str/strcasestarts.c b/src/string/str/strcasestarts.c new file mode 100644 index 0000000..f584da4 --- /dev/null +++ b/src/string/str/strcasestarts.c @@ -0,0 +1,41 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Check whether a string starts with a specific string. + * This check is case insensitive. + * + * This is a slibc extension. + * + * @param string The string to inspect. + * @param desired The desired beginning of the string. + * @return `string` if `string` begins with + * `desired`, `NULL` otherwise. + */ +char* (strcasestarts)(const char* string, const char* desired) +{ + size_t n = strlen(string); + size_t m = strlen(desired); + if (n < m) + return NULL; + return (memcasecmp)(string, desired, m) ? NULL : string; +} + diff --git a/src/string/str/strcasestr.c b/src/string/str/strcasestr.c new file mode 100644 index 0000000..6468750 --- /dev/null +++ b/src/string/str/strcasestr.c @@ -0,0 +1,35 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case insensitive. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @return Pointer to the first occurrence of the + * substring, `NULL` if not found. + */ +char* (strcasestr)(const char* haystack, const char* needle) +{ + return (memcasemem)(haystack, strlen(haystack), needle, strlen(needle)); +} + diff --git a/src/string/str/strcat.c b/src/string/str/strcat.c new file mode 100644 index 0000000..de5c63e --- /dev/null +++ b/src/string/str/strcat.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Concatenate a string to the end of another string. + * The resulting strings must not overlap with the appended string. + * + * The use of this function is often a bad idea. + * + * @param whither The string to extend. + * @param whence The string to append. + * @return `whither` is returned. + */ +char* strcat(char* restrict whither, const char* restrict whence) +{ + strcpy(whither + strlen(whither), whence); + return whither; +} + diff --git a/src/string/str/strccpy.c b/src/string/str/strccpy.c new file mode 100644 index 0000000..2d1bcd8 --- /dev/null +++ b/src/string/str/strccpy.c @@ -0,0 +1,45 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte or a specified byte is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The stop byte. + * @return `NULL` if `c` was not encountered, otherwise + * the position of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strccpy(char* restrict whither, const char* restrict whence, int c) +{ + char* r = memccpy(whither, whence, c, strlen(whence) + 1); + if (r) + *r = 0; + return r; +} + diff --git a/src/string/str/strchr.c b/src/string/str/strchr.c new file mode 100644 index 0000000..0ed53b4 --- /dev/null +++ b/src/string/str/strchr.c @@ -0,0 +1,46 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the first occurrence of a byte in a string. + * + * `s = strchr(s, 0)` is a faster alternative to + * `s = s + strlen(s)`. + * + * @param string The string to search. + * The terminating NUL character is + * considered a part of the string. + * @param c The sought after character. + * @return Pointer to the first occurrence of `c`, + * `NULL` if none were found. + */ +char* (strchr)(const char* string, int c) +{ + for (;;) + if (*string == c) + return string; + else if (!*string++) + return NULL; +} + diff --git a/src/string/str/strchrnul.c b/src/string/str/strchrnul.c new file mode 100644 index 0000000..50dedfe --- /dev/null +++ b/src/string/str/strchrnul.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the first occurrence of a byte in a string, or + * if there is no such byte, the end of the string. + * + * This is a GNU-compliant slibc extension. + * + * @param string The string to search. + * The terminating NUL character is + * considered a part of the string. + * @param c The sought after character. + * @return Pointer to the first occurrence of `c`, + * Pointer to the terminating NUL character + * if none were found. + */ +char* (strchrnul)(const char* string, int c) +{ + for (;; string++) + if (*string == c) + return string; + else if (!*string) + return string; +} + diff --git a/src/string/str/strcmove.c b/src/string/str/strcmove.c new file mode 100644 index 0000000..8310b27 --- /dev/null +++ b/src/string/str/strcmove.c @@ -0,0 +1,45 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte or a specified byte is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The stop byte. + * @return `NULL` if `c` was not encountered, otherwise + * the position of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strcmove(char* whither, const char* whence, int c) +{ + char* r = memcmove(whither, whence, c, strlen(whence) + 1); + if (r) + *r = 0; + return r; +} + diff --git a/src/string/str/strcmp.c b/src/string/str/strcmp.c new file mode 100644 index 0000000..3099d71 --- /dev/null +++ b/src/string/str/strcmp.c @@ -0,0 +1,36 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Compare two strings alphabetically in a case sensitive manner. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int strcmp(const char* a, const char* b) +{ + size_t n = strlen(a); + size_t m = strlen(b); + return memcmp(a, b, (n < m ? n : m) + 1); +} + diff --git a/src/string/str/strcncpy.c b/src/string/str/strcncpy.c new file mode 100644 index 0000000..5036c7d --- /dev/null +++ b/src/string/str/strcncpy.c @@ -0,0 +1,54 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte or a specified byte is encountered. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The stop byte. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `NULL` if `c` was not encountered, otherwise + * the position of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strcncpy(char* restrict whither, const char* restrict whence, int c, size_t maxlen) +{ + const char* stop = memchr(whence, c, maxlen); + size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memcpy(whither, whence, n); + memset(whither, 0, maxlen - n); + return r; +} + diff --git a/src/string/str/strcnmove.c b/src/string/str/strcnmove.c new file mode 100644 index 0000000..f418e92 --- /dev/null +++ b/src/string/str/strcnmove.c @@ -0,0 +1,54 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte or a specified byte is encountered. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param c The stop byte. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `NULL` if `c` was not encountered, otherwise + * the position of `c` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strcnmove(char* whither, const char* whence, int c, size_t maxlen) +{ + const char* stop = memchr(whence, c, maxlen); + size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memmove(whither, whence, n); + memset(whither, 0, maxlen - n); + return r; +} + diff --git a/src/string/str/strcpy.c b/src/string/str/strcpy.c new file mode 100644 index 0000000..75d88e7 --- /dev/null +++ b/src/string/str/strcpy.c @@ -0,0 +1,34 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte is encountered. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @return `whither` is returned. + */ +char* strcpy(char* restrict whither, const char* restrict whence) +{ + return memcpy(whither, whence, strlen(whence) + 1); +} + diff --git a/src/string/str/strcspn.c b/src/string/str/strcspn.c new file mode 100644 index 0000000..35b3404 --- /dev/null +++ b/src/string/str/strcspn.c @@ -0,0 +1,44 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Returns length of the initial substring + * that consists entirely of the complement + * of a set of specified bytes. + * + * @param string The string. + * @param stopset Bytes disallowed in the substring. + * @return The length of the substring. + */ +size_t strcspn(const char* string, const char* stopset) +{ + char set[256]; + char c; + const char* s = string; + memset(set, 0, 256); + while ((c = *stopset++)) + set[(size_t)c] = 1; + while ((c = *s++)) + if (!set[(size_t)c]) + break; + return (size_t)(s - 1 - string); +} + diff --git a/src/string/str/strdup.c b/src/string/str/strdup.c new file mode 100644 index 0000000..b7155d7 --- /dev/null +++ b/src/string/str/strdup.c @@ -0,0 +1,38 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Duplicate a string. + * + * @param string The string to duplicate. + * @return The new string. `NULL` is returned on error + * and `errno` is set to indicate the error. + * + * @throws ENOMEM The process could not allocate sufficient amount of memory. + */ +char* strdup(const char* string) +{ + size_t n = strlen(string) + 1; + char* r = malloc(n * sizeof(char)); + return r == NULL ? NULL : memcpy(r, string, n * sizeof(char)); +} + diff --git a/src/string/str/strends.c b/src/string/str/strends.c new file mode 100644 index 0000000..957301c --- /dev/null +++ b/src/string/str/strends.c @@ -0,0 +1,41 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Check whether a string ends with a specific string. + * This check is case sensitive. + * + * This is a slibc extension. + * + * @param string The string to inspect. + * @param desired The desired ending of the string. + * @return The `string`, where `desired` beings if + * `string` ends with `desired`, `NULL` otherwise. + */ +char* (strends)(const char* string, const char* desired) +{ + size_t n = strlen(string); + size_t m = strlen(desired); + if (n < m) + return NULL; + return (memcmp)(string + (n - m), desired, m) ? NULL : (string + n); +} + diff --git a/src/string/str/strlen.c b/src/string/str/strlen.c new file mode 100644 index 0000000..2092ddc --- /dev/null +++ b/src/string/str/strlen.c @@ -0,0 +1,35 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Returns the number of bytes in a NUL-terminated + * string. This excludes the NUL byte. + * + * @param str The string. + * @return The number of bytes before the first NUL byte. + */ +size_t strlen(const char* str) +{ + const char* s = str; + while (*str++); + return (size_t)(s - 1 - str); +} + diff --git a/src/string/str/strmove.c b/src/string/str/strmove.c new file mode 100644 index 0000000..4bd52f4 --- /dev/null +++ b/src/string/str/strmove.c @@ -0,0 +1,36 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @return `whither` is returned. + */ +char* strmove(char* whither, const char* whence) +{ + return memmove(whither, whence, strlen(whence) + 1); +} + diff --git a/src/string/str/strncasecmp.c b/src/string/str/strncasecmp.c new file mode 100644 index 0000000..919ff36 --- /dev/null +++ b/src/string/str/strncasecmp.c @@ -0,0 +1,50 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Compare two strings alphabetically in a case insensitive manner. + * Be aware, only ASCII characters are case insensitive, non-ASCII + * characters are case sensitive. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @param length The maximum number of characters to compare. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int strncasecmp(const char* a, const char* b, size_t length) +{ + int c1, c2; + for (; length--; a++, b++) + if (*a != *b) + { + c1 = isalpha(*a) ? tolower(*a) : (int)*a; + c2 = isalpha(*b) ? tolower(*b) : (int)*b; + if ((c1 -= c2)) + return c1; + } + else if (!*a && !*b) return 0; + else if (!*a) return -1; + else if (!*b) return +1; + return 0; +} + diff --git a/src/string/str/strncasestr.c b/src/string/str/strncasestr.c new file mode 100644 index 0000000..e95dc4b --- /dev/null +++ b/src/string/str/strncasestr.c @@ -0,0 +1,38 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case insensitive. + * + * This is a slibc extension added for completeness. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @param maxlen The maximum number of character to search. + * @return Pointer to the first occurrence of the + * substring, `NULL` if not found. + */ +char* (strncasestr)(const char* haystack, const char* needle, size_t maxlen) +{ + return (memcasemem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle)); +} + diff --git a/src/string/str/strncat.c b/src/string/str/strncat.c new file mode 100644 index 0000000..e7db0f2 --- /dev/null +++ b/src/string/str/strncat.c @@ -0,0 +1,43 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Concatenate a string to the end of another string. + * The resulting strings must not overlap with the appended string. + * + * The use of this function is often a really bad idea. + * + * @param whither The string to extend. + * @param whence The string to append. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `whither` is returned. + */ +char* strncat(char* restrict whither, const char* restrict whence, size_t maxlen) +{ + strncpy(whither + strlen(whither), whence, maxlen); + return whither; +} + diff --git a/src/string/str/strncmp.c b/src/string/str/strncmp.c new file mode 100644 index 0000000..daf5be5 --- /dev/null +++ b/src/string/str/strncmp.c @@ -0,0 +1,39 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Compare two strings alphabetically in a case sensitive manner. + * + * @param a A negative value is returned if this is the lesser. + * @param b A positive value is returned if this is the lesser. + * @param length The maximum number of characters to compare. + * @return Zero is returned if `a` and `b` are equal, otherwise, + * see the specifications for `a` and `b`. + */ +int strncmp(const char* a, const char* b, size_t length) +{ + size_t n = strnlen(a, length); + size_t m = strnlen(b, length); + int r = memcmp(a, b, (n < m ? n : m)); + return r ? r : n == m ? 0 : n < m ? -1 : +1; +} + + diff --git a/src/string/str/strncpy.c b/src/string/str/strncpy.c new file mode 100644 index 0000000..f3eba84 --- /dev/null +++ b/src/string/str/strncpy.c @@ -0,0 +1,43 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte is encountered. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `whither` is returned. + */ +char* strncpy(char* restrict whither, const char* restrict whence, size_t maxlen) +{ + size_t n = strnlen(whence, maxlen); + memcpy(whither, whence, n); + memset(whither, 0, maxlen - n); + return whither; +} + diff --git a/src/string/str/strndup.c b/src/string/str/strndup.c new file mode 100644 index 0000000..ca3b3f0 --- /dev/null +++ b/src/string/str/strndup.c @@ -0,0 +1,43 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include + + + +/** + * Duplicate a string. + * + * This is a GNU extension. + * + * @param string The string to duplicate. + * @param maxlen Truncate the string to this length, if it is longer. + * A NUL byte is guaranteed to always be written + * upon successful completion. + * @return The new string. `NULL` is returned on error + * and `errno` is set to indicate the error. + * + * @throws ENOMEM The process could not allocate sufficient amount of memory. + */ +char* strndup(const char* string, size_t maxlen) +{ + size_t n = strnlen(string, maxlen) + 1; + char* r = malloc(n * sizeof(char)); + return r == NULL ? NULL : memcpy(r, string, n * sizeof(char)); +} + diff --git a/src/string/str/strnlen.c b/src/string/str/strnlen.c new file mode 100644 index 0000000..5473682 --- /dev/null +++ b/src/string/str/strnlen.c @@ -0,0 +1,36 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Variant of `strlen` that only inspects the + * beginning of a string. + * + * @param str The string. + * @param maxlen The number of bytes to inspect, at most. + * @return The number of bytes before, the first NUL byte. + * `maxlen` if no NUL byte was found. + */ +size_t strnlen(const char* str, size_t maxlen) +{ + const char* end = memchr(str, 0, maxlen); + return end == NULL ? maxlen : (size_t)(end - str); +} + diff --git a/src/string/str/strnmove.c b/src/string/str/strnmove.c new file mode 100644 index 0000000..a7ec028 --- /dev/null +++ b/src/string/str/strnmove.c @@ -0,0 +1,43 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte is encountered. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param maxlen The maximum number of bytes to copy. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @return `whither` is returned. + */ +char* strnmove(char* whither, const char* whence, size_t maxlen) +{ + size_t n = strnlen(whence, maxlen); + memmove(whither, whence, n); + memset(whither, 0, maxlen - n); + return whither; +} + diff --git a/src/string/str/strnstr.c b/src/string/str/strnstr.c new file mode 100644 index 0000000..8db61ac --- /dev/null +++ b/src/string/str/strnstr.c @@ -0,0 +1,39 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case sensitive. + * + * This is a slibc extension added for because it was useful + * in implementing slibc itself. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @param maxlen The maximum number of character to search. + * @return Pointer to the first occurrence of the + * substring, `NULL` if not found. + */ +char* (strnstr)(const char* haystack, const char* needle, size_t maxlen) +{ + return (memmem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle)); +} + diff --git a/src/string/str/strpbrk.c b/src/string/str/strpbrk.c new file mode 100644 index 0000000..8499d31 --- /dev/null +++ b/src/string/str/strpbrk.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * This function works like `strcspn`, + * except it returns the pointer to the + * location of the first found non-matching + * byte. + * + * @param string The string. + * @param stopset Bytes disallowed in the substring. + * @return A pointer to the first occurrence in + * `string` of a byte found in `stopset`. + * `NULL` is returned if none is found. + */ +char* (strpbrk)(const char* string, const char* stopset) +{ + char set[256]; + char c; + const char* s = string; + memset(set, 0, 256); + while ((c = *stopset++)) + set[(size_t)c] = 1; + while ((c = *s++)) + if (!set[(size_t)c]) + break; + return c ? (s - 1) : NULL; +} + diff --git a/src/string/str/strrchr.c b/src/string/str/strrchr.c new file mode 100644 index 0000000..716a442 --- /dev/null +++ b/src/string/str/strrchr.c @@ -0,0 +1,48 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + +# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + + + +/** + * Find the last occurrence of a byte in a string. + * + * For improved performace, use `memrchr` instead of + * this function if you already know the length of the + * string. + * + * @param string The string to search. + * The terminating NUL character is + * considered a part of the string. + * @param c The sought after character. + * @return Pointer to the last occurrence of `c`, + * `NULL` if none were found. + */ +char* (strrchr)(const char* string, int c) +{ + char* r = NULL; + for (;;) + if (*string == c) + r = string; + else if (!*string++) + return c ? r : (string - 1); +} + diff --git a/src/string/str/strsep.c b/src/string/str/strsep.c new file mode 100644 index 0000000..9fa0cff --- /dev/null +++ b/src/string/str/strsep.c @@ -0,0 +1,51 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Tokenise a string. + * + * @param string Pointer to the string to tokenise on the first call, + * will be updated to keep track of the state. + * All bytes found in `delimiters` will + * be overriden with NUL bytes. + * @param delimiters Delimiting bytes (not characters). + * @return The next, possibly empty, string that does + * not contain a byte from `delimiters`. The + * returned string will be as long as possible. + * `NULL` is returned the search as reached + * the end of the string, and there therefore + * are no more tokens. + */ +char* strsep(char** restrict string, const char* restrict delimiters) +{ + char* r = *string; + char* next; + if (r == NULL) + return NULL; + + next = strpbrk(r, delimiters); + if (next != NULL) + *next++ = 0; + *string = next; + + return r; +} + diff --git a/src/string/str/strset.c b/src/string/str/strset.c new file mode 100644 index 0000000..c1eca65 --- /dev/null +++ b/src/string/str/strset.c @@ -0,0 +1,39 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Override a NUL byte-terminated memory segment + * with a repeated character. + * + * This is a slibc extension. + * + * @param str The beginning of the memory segment. + * @param c The character (8 bits wide.) + * @return `str` is returned. + */ +char* strset(char* str, int c) +{ + char* s = segment; + while (*s) + *s++ = (char)c; + return segment; +} + diff --git a/src/string/str/strspn.c b/src/string/str/strspn.c new file mode 100644 index 0000000..1b2e034 --- /dev/null +++ b/src/string/str/strspn.c @@ -0,0 +1,44 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Returns length of the initial substring + * that consists entirely of a set of specified + * bytes. + * + * @param string The string. + * @param skipset Bytes allowed in the substring. + * @return The length of the substring. + */ +size_t strspn(const char* string, const char* skipset) +{ + char set[256]; + char c; + const char* s = string; + memset(set, 0, 256); + while ((c = *skipset++)) + set[(size_t)c] = 1; + while ((c = *s++)) + if (set[(size_t)c]) + break; + return (size_t)(s - 1 - string); +} + diff --git a/src/string/str/strstarts.c b/src/string/str/strstarts.c new file mode 100644 index 0000000..e132b68 --- /dev/null +++ b/src/string/str/strstarts.c @@ -0,0 +1,41 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Check whether a string starts with a specific string. + * This check is case sensitive. + * + * This is a slibc extension. + * + * @param string The string to inspect. + * @param desired The desired beginning of the string. + * @return `string` if `string` begins with + * `desired`, `NULL` otherwise. + */ +char* (strstarts)(const char* string, const char* desired) +{ + size_t n = strlen(string); + size_t m = strlen(desired); + if (n < m) + return NULL; + return (memcmp)(string, desired, m) ? NULL : string; +} + diff --git a/src/string/str/strstr.c b/src/string/str/strstr.c new file mode 100644 index 0000000..dadbc73 --- /dev/null +++ b/src/string/str/strstr.c @@ -0,0 +1,37 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Finds the first occurrence of a substring. + * This search is case sensitive. + * + * @param haystack The string to search. + * @param needle The sought after substring. + * @return Pointer to the first occurrence of the + * substring, `NULL` if not found. + */ +char* (strstr)(const char* haystack, const char* needle) +{ + if (*needle && !(needle[1])) + return (strchr)(haystack, *needle); + return (memmem)(haystack, strlen(haystack), needle, strlen(needle)); +} + diff --git a/src/string/str/strstrcpy.c b/src/string/str/strstrcpy.c new file mode 100644 index 0000000..9dc9105 --- /dev/null +++ b/src/string/str/strstrcpy.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte or a specified substring is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param str The substring, ignored if `NULL`. + * @return `NULL` if `str` was not encountered, otherwise + * the position of `str` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strstrcpy(char* restrict whither, const char* restrict whence, const char* restrict str) +{ + const char* stop = str == NULL ? NULL : strstr(whence, str); + size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memcpy(whither, whence, n); + whither[n] = 0; + return r; +} + diff --git a/src/string/str/strstrmove.c b/src/string/str/strstrmove.c new file mode 100644 index 0000000..19969cd --- /dev/null +++ b/src/string/str/strstrmove.c @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte or a specified substring is encountered. + * + * This is a slibc extension added for completeness. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param str The substring, ignored if `NULL`. + * @return `NULL` if `str` was not encountered, otherwise + * the position of `str` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strstrmove(char* whither, const char* whence, const char* restrict str) +{ + const char* stop = str == NULL ? NULL : strstr(whence, str); + size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memmove(whither, whence, n); + whither[n] = 0; + return r; +} + diff --git a/src/string/str/strstrncpy.c b/src/string/str/strstrncpy.c new file mode 100644 index 0000000..0beb8ec --- /dev/null +++ b/src/string/str/strstrncpy.c @@ -0,0 +1,55 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, non-overlapping, segment, + * stop when a NUL byte or a specified substring is encountered. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param str The substring, ignored if `NULL`. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @param maxlen The maximum number of bytes to copy. + * @return `NULL` if `str` was not encountered, otherwise + * the position of `str` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strstrncpy(char* restrict whither, const char* restrict whence, + const char* restrict str, size_t maxlen) +{ + const char* stop = strnstr(whence, str, maxlen); + size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memcpy(whither, whence, n); + memset(whither, 0, maxlen - n); + return r; +} + diff --git a/src/string/str/strstrnmove.c b/src/string/str/strstrnmove.c new file mode 100644 index 0000000..9bec614 --- /dev/null +++ b/src/string/str/strstrnmove.c @@ -0,0 +1,54 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Copy a memory segment to another, possibly overlapping, segment, + * stop when a NUL byte or a specified substring is encountered. + * + * This is a slibc extension added for completeness. + * It is only available if GNU extensions are available. + * + * @param whither The destination memory segment. + * @param whence The source memory segment. + * @param str The substring, ignored if `NULL`. + * NOTE that if the resulting string at least this + * long, no NUL byte will be written to `whither'. + * On the otherhand, if the resultnig string is + * shorter, `whither` will be filled with NUL bytes + * until this amount of bytes have been written. + * @param maxlen The maximum number of bytes to copy. + * @return `NULL` if `str` was not encountered, otherwise + * the position of `str` translated to `whither`, + * that is, the address of `whither` plus the + * number of copied characters; the address of + * one character passed the last written non-NUL + * character. + */ +char* strstrnmove(char* whither, const char* whence, const char* restrict str, size_t maxlen) +{ + const char* stop = strnstr(whence, str, maxlen); + size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); + char* r = stop == NULL ? NULL : (whither + n); + memmove(whither, whence, n); + memset(whither, 0, maxlen - n); + return r; +} + diff --git a/src/string/str/strtok.c b/src/string/str/strtok.c new file mode 100644 index 0000000..b1b56b9 --- /dev/null +++ b/src/string/str/strtok.c @@ -0,0 +1,44 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Tokenise a string. + * + * @param string The string to tokenise on the first, + * `NULL` on subsequent calls. + * All bytes found in `delimiters` will + * be overriden with NUL bytes. + * @param delimiters Delimiting bytes (not characters). + * @return The next non-empty string that does not + * contain a byte from `delimiters`. The + * returned string will be as long as possible. + * `NULL` is returned the search as reached + * the end of the string, and there therefore + * are no more tokens. + */ +char* strtok(char* restrict string, const char* restrict delimiters) +{ + static char* state = NULL; + if (string == NULL) + state = NULL; + return strtok_r(string, delimiters, &state); +} + diff --git a/src/string/str/strtok_r.c b/src/string/str/strtok_r.c new file mode 100644 index 0000000..3005ac7 --- /dev/null +++ b/src/string/str/strtok_r.c @@ -0,0 +1,56 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Tokenise a string. + * + * @param string The string to tokenise on the first, + * `NULL` on subsequent calls. + * All bytes found in `delimiters` will + * be overriden with NUL bytes. + * @param delimiters Delimiting bytes (not characters). + * @param state Pointer to a `char*` that the function + * can use to keep track of its state. + * It is reasonable to make it point to `NULL` + * on the first call. + * @return The next non-empty string that does not + * contain a byte from `delimiters`. The + * returned string will be as long as possible. + * `NULL` is returned the search as reached + * the end of the string, and there therefore + * are no more tokens. + */ +char* strtok_r(char* restrict string, const char* restrict delimiters, + char** restrict state) +{ + char* r; + if (string == NULL) + *state = string; + for (;;) + { + r = strsep(state, delimiters); + if (r == NULL) + return NULL; + if (*r) + return r; + } +} + diff --git a/src/string/strcasecmp.c b/src/string/strcasecmp.c deleted file mode 100644 index eb02084..0000000 --- a/src/string/strcasecmp.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Compare two strings alphabetically in a case insensitive manner. - * Be aware, only ASCII characters are case insensitive, non-ASCII - * characters are case sensitive. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int strcasecmp(const char* a, const char* b) -{ - return strncasecmp(a, b, SIZE_MAX); -} - diff --git a/src/string/strcaseends.c b/src/string/strcaseends.c deleted file mode 100644 index 9155ea8..0000000 --- a/src/string/strcaseends.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Check whether a string ends with a specific string. - * This check is case insensitive. - * - * This is a slibc extension. - * - * @param string The string to inspect. - * @param desired The desired ending of the string. - * @return The `string`, where `desired` beings if - * `string` ends with `desired`, `NULL` otherwise. - */ -char* (strcaseends)(const char* string, const char* desired) -{ - size_t n = strlen(string); - size_t m = strlen(desired); - if (n < m) - return NULL; - return (memcasecmp)(string + (n - m), desired, m) ? NULL : (string + n); -} - diff --git a/src/string/strcasestarts.c b/src/string/strcasestarts.c deleted file mode 100644 index f584da4..0000000 --- a/src/string/strcasestarts.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Check whether a string starts with a specific string. - * This check is case insensitive. - * - * This is a slibc extension. - * - * @param string The string to inspect. - * @param desired The desired beginning of the string. - * @return `string` if `string` begins with - * `desired`, `NULL` otherwise. - */ -char* (strcasestarts)(const char* string, const char* desired) -{ - size_t n = strlen(string); - size_t m = strlen(desired); - if (n < m) - return NULL; - return (memcasecmp)(string, desired, m) ? NULL : string; -} - diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c deleted file mode 100644 index 6468750..0000000 --- a/src/string/strcasestr.c +++ /dev/null @@ -1,35 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case insensitive. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @return Pointer to the first occurrence of the - * substring, `NULL` if not found. - */ -char* (strcasestr)(const char* haystack, const char* needle) -{ - return (memcasemem)(haystack, strlen(haystack), needle, strlen(needle)); -} - diff --git a/src/string/strcat.c b/src/string/strcat.c deleted file mode 100644 index de5c63e..0000000 --- a/src/string/strcat.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Concatenate a string to the end of another string. - * The resulting strings must not overlap with the appended string. - * - * The use of this function is often a bad idea. - * - * @param whither The string to extend. - * @param whence The string to append. - * @return `whither` is returned. - */ -char* strcat(char* restrict whither, const char* restrict whence) -{ - strcpy(whither + strlen(whither), whence); - return whither; -} - diff --git a/src/string/strccpy.c b/src/string/strccpy.c deleted file mode 100644 index 2d1bcd8..0000000 --- a/src/string/strccpy.c +++ /dev/null @@ -1,45 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte or a specified byte is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The stop byte. - * @return `NULL` if `c` was not encountered, otherwise - * the position of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strccpy(char* restrict whither, const char* restrict whence, int c) -{ - char* r = memccpy(whither, whence, c, strlen(whence) + 1); - if (r) - *r = 0; - return r; -} - diff --git a/src/string/strchr.c b/src/string/strchr.c deleted file mode 100644 index 0ed53b4..0000000 --- a/src/string/strchr.c +++ /dev/null @@ -1,46 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the first occurrence of a byte in a string. - * - * `s = strchr(s, 0)` is a faster alternative to - * `s = s + strlen(s)`. - * - * @param string The string to search. - * The terminating NUL character is - * considered a part of the string. - * @param c The sought after character. - * @return Pointer to the first occurrence of `c`, - * `NULL` if none were found. - */ -char* (strchr)(const char* string, int c) -{ - for (;;) - if (*string == c) - return string; - else if (!*string++) - return NULL; -} - diff --git a/src/string/strchrnul.c b/src/string/strchrnul.c deleted file mode 100644 index 50dedfe..0000000 --- a/src/string/strchrnul.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the first occurrence of a byte in a string, or - * if there is no such byte, the end of the string. - * - * This is a GNU-compliant slibc extension. - * - * @param string The string to search. - * The terminating NUL character is - * considered a part of the string. - * @param c The sought after character. - * @return Pointer to the first occurrence of `c`, - * Pointer to the terminating NUL character - * if none were found. - */ -char* (strchrnul)(const char* string, int c) -{ - for (;; string++) - if (*string == c) - return string; - else if (!*string) - return string; -} - diff --git a/src/string/strcmove.c b/src/string/strcmove.c deleted file mode 100644 index 8310b27..0000000 --- a/src/string/strcmove.c +++ /dev/null @@ -1,45 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte or a specified byte is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The stop byte. - * @return `NULL` if `c` was not encountered, otherwise - * the position of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strcmove(char* whither, const char* whence, int c) -{ - char* r = memcmove(whither, whence, c, strlen(whence) + 1); - if (r) - *r = 0; - return r; -} - diff --git a/src/string/strcmp.c b/src/string/strcmp.c deleted file mode 100644 index 3099d71..0000000 --- a/src/string/strcmp.c +++ /dev/null @@ -1,36 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Compare two strings alphabetically in a case sensitive manner. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int strcmp(const char* a, const char* b) -{ - size_t n = strlen(a); - size_t m = strlen(b); - return memcmp(a, b, (n < m ? n : m) + 1); -} - diff --git a/src/string/strcncpy.c b/src/string/strcncpy.c deleted file mode 100644 index 5036c7d..0000000 --- a/src/string/strcncpy.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte or a specified byte is encountered. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The stop byte. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `NULL` if `c` was not encountered, otherwise - * the position of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strcncpy(char* restrict whither, const char* restrict whence, int c, size_t maxlen) -{ - const char* stop = memchr(whence, c, maxlen); - size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memcpy(whither, whence, n); - memset(whither, 0, maxlen - n); - return r; -} - diff --git a/src/string/strcnmove.c b/src/string/strcnmove.c deleted file mode 100644 index f418e92..0000000 --- a/src/string/strcnmove.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte or a specified byte is encountered. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param c The stop byte. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `NULL` if `c` was not encountered, otherwise - * the position of `c` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strcnmove(char* whither, const char* whence, int c, size_t maxlen) -{ - const char* stop = memchr(whence, c, maxlen); - size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memmove(whither, whence, n); - memset(whither, 0, maxlen - n); - return r; -} - diff --git a/src/string/strcpy.c b/src/string/strcpy.c deleted file mode 100644 index 75d88e7..0000000 --- a/src/string/strcpy.c +++ /dev/null @@ -1,34 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte is encountered. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @return `whither` is returned. - */ -char* strcpy(char* restrict whither, const char* restrict whence) -{ - return memcpy(whither, whence, strlen(whence) + 1); -} - diff --git a/src/string/strcspn.c b/src/string/strcspn.c deleted file mode 100644 index 35b3404..0000000 --- a/src/string/strcspn.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Returns length of the initial substring - * that consists entirely of the complement - * of a set of specified bytes. - * - * @param string The string. - * @param stopset Bytes disallowed in the substring. - * @return The length of the substring. - */ -size_t strcspn(const char* string, const char* stopset) -{ - char set[256]; - char c; - const char* s = string; - memset(set, 0, 256); - while ((c = *stopset++)) - set[(size_t)c] = 1; - while ((c = *s++)) - if (!set[(size_t)c]) - break; - return (size_t)(s - 1 - string); -} - diff --git a/src/string/strdup.c b/src/string/strdup.c deleted file mode 100644 index b7155d7..0000000 --- a/src/string/strdup.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Duplicate a string. - * - * @param string The string to duplicate. - * @return The new string. `NULL` is returned on error - * and `errno` is set to indicate the error. - * - * @throws ENOMEM The process could not allocate sufficient amount of memory. - */ -char* strdup(const char* string) -{ - size_t n = strlen(string) + 1; - char* r = malloc(n * sizeof(char)); - return r == NULL ? NULL : memcpy(r, string, n * sizeof(char)); -} - diff --git a/src/string/strends.c b/src/string/strends.c deleted file mode 100644 index 957301c..0000000 --- a/src/string/strends.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Check whether a string ends with a specific string. - * This check is case sensitive. - * - * This is a slibc extension. - * - * @param string The string to inspect. - * @param desired The desired ending of the string. - * @return The `string`, where `desired` beings if - * `string` ends with `desired`, `NULL` otherwise. - */ -char* (strends)(const char* string, const char* desired) -{ - size_t n = strlen(string); - size_t m = strlen(desired); - if (n < m) - return NULL; - return (memcmp)(string + (n - m), desired, m) ? NULL : (string + n); -} - diff --git a/src/string/strlen.c b/src/string/strlen.c deleted file mode 100644 index 2092ddc..0000000 --- a/src/string/strlen.c +++ /dev/null @@ -1,35 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Returns the number of bytes in a NUL-terminated - * string. This excludes the NUL byte. - * - * @param str The string. - * @return The number of bytes before the first NUL byte. - */ -size_t strlen(const char* str) -{ - const char* s = str; - while (*str++); - return (size_t)(s - 1 - str); -} - diff --git a/src/string/strmove.c b/src/string/strmove.c deleted file mode 100644 index 4bd52f4..0000000 --- a/src/string/strmove.c +++ /dev/null @@ -1,36 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @return `whither` is returned. - */ -char* strmove(char* whither, const char* whence) -{ - return memmove(whither, whence, strlen(whence) + 1); -} - diff --git a/src/string/strncasecmp.c b/src/string/strncasecmp.c deleted file mode 100644 index 919ff36..0000000 --- a/src/string/strncasecmp.c +++ /dev/null @@ -1,50 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Compare two strings alphabetically in a case insensitive manner. - * Be aware, only ASCII characters are case insensitive, non-ASCII - * characters are case sensitive. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @param length The maximum number of characters to compare. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int strncasecmp(const char* a, const char* b, size_t length) -{ - int c1, c2; - for (; length--; a++, b++) - if (*a != *b) - { - c1 = isalpha(*a) ? tolower(*a) : (int)*a; - c2 = isalpha(*b) ? tolower(*b) : (int)*b; - if ((c1 -= c2)) - return c1; - } - else if (!*a && !*b) return 0; - else if (!*a) return -1; - else if (!*b) return +1; - return 0; -} - diff --git a/src/string/strncasestr.c b/src/string/strncasestr.c deleted file mode 100644 index e95dc4b..0000000 --- a/src/string/strncasestr.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case insensitive. - * - * This is a slibc extension added for completeness. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @param maxlen The maximum number of character to search. - * @return Pointer to the first occurrence of the - * substring, `NULL` if not found. - */ -char* (strncasestr)(const char* haystack, const char* needle, size_t maxlen) -{ - return (memcasemem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle)); -} - diff --git a/src/string/strncat.c b/src/string/strncat.c deleted file mode 100644 index e7db0f2..0000000 --- a/src/string/strncat.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Concatenate a string to the end of another string. - * The resulting strings must not overlap with the appended string. - * - * The use of this function is often a really bad idea. - * - * @param whither The string to extend. - * @param whence The string to append. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `whither` is returned. - */ -char* strncat(char* restrict whither, const char* restrict whence, size_t maxlen) -{ - strncpy(whither + strlen(whither), whence, maxlen); - return whither; -} - diff --git a/src/string/strncmp.c b/src/string/strncmp.c deleted file mode 100644 index daf5be5..0000000 --- a/src/string/strncmp.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Compare two strings alphabetically in a case sensitive manner. - * - * @param a A negative value is returned if this is the lesser. - * @param b A positive value is returned if this is the lesser. - * @param length The maximum number of characters to compare. - * @return Zero is returned if `a` and `b` are equal, otherwise, - * see the specifications for `a` and `b`. - */ -int strncmp(const char* a, const char* b, size_t length) -{ - size_t n = strnlen(a, length); - size_t m = strnlen(b, length); - int r = memcmp(a, b, (n < m ? n : m)); - return r ? r : n == m ? 0 : n < m ? -1 : +1; -} - - diff --git a/src/string/strncpy.c b/src/string/strncpy.c deleted file mode 100644 index f3eba84..0000000 --- a/src/string/strncpy.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte is encountered. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `whither` is returned. - */ -char* strncpy(char* restrict whither, const char* restrict whence, size_t maxlen) -{ - size_t n = strnlen(whence, maxlen); - memcpy(whither, whence, n); - memset(whither, 0, maxlen - n); - return whither; -} - diff --git a/src/string/strndup.c b/src/string/strndup.c deleted file mode 100644 index ca3b3f0..0000000 --- a/src/string/strndup.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - - - -/** - * Duplicate a string. - * - * This is a GNU extension. - * - * @param string The string to duplicate. - * @param maxlen Truncate the string to this length, if it is longer. - * A NUL byte is guaranteed to always be written - * upon successful completion. - * @return The new string. `NULL` is returned on error - * and `errno` is set to indicate the error. - * - * @throws ENOMEM The process could not allocate sufficient amount of memory. - */ -char* strndup(const char* string, size_t maxlen) -{ - size_t n = strnlen(string, maxlen) + 1; - char* r = malloc(n * sizeof(char)); - return r == NULL ? NULL : memcpy(r, string, n * sizeof(char)); -} - diff --git a/src/string/strnlen.c b/src/string/strnlen.c deleted file mode 100644 index 5473682..0000000 --- a/src/string/strnlen.c +++ /dev/null @@ -1,36 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Variant of `strlen` that only inspects the - * beginning of a string. - * - * @param str The string. - * @param maxlen The number of bytes to inspect, at most. - * @return The number of bytes before, the first NUL byte. - * `maxlen` if no NUL byte was found. - */ -size_t strnlen(const char* str, size_t maxlen) -{ - const char* end = memchr(str, 0, maxlen); - return end == NULL ? maxlen : (size_t)(end - str); -} - diff --git a/src/string/strnmove.c b/src/string/strnmove.c deleted file mode 100644 index a7ec028..0000000 --- a/src/string/strnmove.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte is encountered. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param maxlen The maximum number of bytes to copy. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @return `whither` is returned. - */ -char* strnmove(char* whither, const char* whence, size_t maxlen) -{ - size_t n = strnlen(whence, maxlen); - memmove(whither, whence, n); - memset(whither, 0, maxlen - n); - return whither; -} - diff --git a/src/string/strnstr.c b/src/string/strnstr.c deleted file mode 100644 index 8db61ac..0000000 --- a/src/string/strnstr.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case sensitive. - * - * This is a slibc extension added for because it was useful - * in implementing slibc itself. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @param maxlen The maximum number of character to search. - * @return Pointer to the first occurrence of the - * substring, `NULL` if not found. - */ -char* (strnstr)(const char* haystack, const char* needle, size_t maxlen) -{ - return (memmem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle)); -} - diff --git a/src/string/strpbrk.c b/src/string/strpbrk.c deleted file mode 100644 index 8499d31..0000000 --- a/src/string/strpbrk.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * This function works like `strcspn`, - * except it returns the pointer to the - * location of the first found non-matching - * byte. - * - * @param string The string. - * @param stopset Bytes disallowed in the substring. - * @return A pointer to the first occurrence in - * `string` of a byte found in `stopset`. - * `NULL` is returned if none is found. - */ -char* (strpbrk)(const char* string, const char* stopset) -{ - char set[256]; - char c; - const char* s = string; - memset(set, 0, 256); - while ((c = *stopset++)) - set[(size_t)c] = 1; - while ((c = *s++)) - if (!set[(size_t)c]) - break; - return c ? (s - 1) : NULL; -} - diff --git a/src/string/strrchr.c b/src/string/strrchr.c deleted file mode 100644 index 716a442..0000000 --- a/src/string/strrchr.c +++ /dev/null @@ -1,48 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - -# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - - - -/** - * Find the last occurrence of a byte in a string. - * - * For improved performace, use `memrchr` instead of - * this function if you already know the length of the - * string. - * - * @param string The string to search. - * The terminating NUL character is - * considered a part of the string. - * @param c The sought after character. - * @return Pointer to the last occurrence of `c`, - * `NULL` if none were found. - */ -char* (strrchr)(const char* string, int c) -{ - char* r = NULL; - for (;;) - if (*string == c) - r = string; - else if (!*string++) - return c ? r : (string - 1); -} - diff --git a/src/string/strsep.c b/src/string/strsep.c deleted file mode 100644 index 9fa0cff..0000000 --- a/src/string/strsep.c +++ /dev/null @@ -1,51 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Tokenise a string. - * - * @param string Pointer to the string to tokenise on the first call, - * will be updated to keep track of the state. - * All bytes found in `delimiters` will - * be overriden with NUL bytes. - * @param delimiters Delimiting bytes (not characters). - * @return The next, possibly empty, string that does - * not contain a byte from `delimiters`. The - * returned string will be as long as possible. - * `NULL` is returned the search as reached - * the end of the string, and there therefore - * are no more tokens. - */ -char* strsep(char** restrict string, const char* restrict delimiters) -{ - char* r = *string; - char* next; - if (r == NULL) - return NULL; - - next = strpbrk(r, delimiters); - if (next != NULL) - *next++ = 0; - *string = next; - - return r; -} - diff --git a/src/string/strset.c b/src/string/strset.c deleted file mode 100644 index c1eca65..0000000 --- a/src/string/strset.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Override a NUL byte-terminated memory segment - * with a repeated character. - * - * This is a slibc extension. - * - * @param str The beginning of the memory segment. - * @param c The character (8 bits wide.) - * @return `str` is returned. - */ -char* strset(char* str, int c) -{ - char* s = segment; - while (*s) - *s++ = (char)c; - return segment; -} - diff --git a/src/string/strspn.c b/src/string/strspn.c deleted file mode 100644 index 1b2e034..0000000 --- a/src/string/strspn.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Returns length of the initial substring - * that consists entirely of a set of specified - * bytes. - * - * @param string The string. - * @param skipset Bytes allowed in the substring. - * @return The length of the substring. - */ -size_t strspn(const char* string, const char* skipset) -{ - char set[256]; - char c; - const char* s = string; - memset(set, 0, 256); - while ((c = *skipset++)) - set[(size_t)c] = 1; - while ((c = *s++)) - if (set[(size_t)c]) - break; - return (size_t)(s - 1 - string); -} - diff --git a/src/string/strstarts.c b/src/string/strstarts.c deleted file mode 100644 index e132b68..0000000 --- a/src/string/strstarts.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Check whether a string starts with a specific string. - * This check is case sensitive. - * - * This is a slibc extension. - * - * @param string The string to inspect. - * @param desired The desired beginning of the string. - * @return `string` if `string` begins with - * `desired`, `NULL` otherwise. - */ -char* (strstarts)(const char* string, const char* desired) -{ - size_t n = strlen(string); - size_t m = strlen(desired); - if (n < m) - return NULL; - return (memcmp)(string, desired, m) ? NULL : string; -} - diff --git a/src/string/strstr.c b/src/string/strstr.c deleted file mode 100644 index dadbc73..0000000 --- a/src/string/strstr.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Finds the first occurrence of a substring. - * This search is case sensitive. - * - * @param haystack The string to search. - * @param needle The sought after substring. - * @return Pointer to the first occurrence of the - * substring, `NULL` if not found. - */ -char* (strstr)(const char* haystack, const char* needle) -{ - if (*needle && !(needle[1])) - return (strchr)(haystack, *needle); - return (memmem)(haystack, strlen(haystack), needle, strlen(needle)); -} - diff --git a/src/string/strstrcpy.c b/src/string/strstrcpy.c deleted file mode 100644 index 9dc9105..0000000 --- a/src/string/strstrcpy.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte or a specified substring is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param str The substring, ignored if `NULL`. - * @return `NULL` if `str` was not encountered, otherwise - * the position of `str` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strstrcpy(char* restrict whither, const char* restrict whence, const char* restrict str) -{ - const char* stop = str == NULL ? NULL : strstr(whence, str); - size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memcpy(whither, whence, n); - whither[n] = 0; - return r; -} - diff --git a/src/string/strstrmove.c b/src/string/strstrmove.c deleted file mode 100644 index 19969cd..0000000 --- a/src/string/strstrmove.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte or a specified substring is encountered. - * - * This is a slibc extension added for completeness. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param str The substring, ignored if `NULL`. - * @return `NULL` if `str` was not encountered, otherwise - * the position of `str` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strstrmove(char* whither, const char* whence, const char* restrict str) -{ - const char* stop = str == NULL ? NULL : strstr(whence, str); - size_t n = stop == NULL ? strlen(whence) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memmove(whither, whence, n); - whither[n] = 0; - return r; -} - diff --git a/src/string/strstrncpy.c b/src/string/strstrncpy.c deleted file mode 100644 index 0beb8ec..0000000 --- a/src/string/strstrncpy.c +++ /dev/null @@ -1,55 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, non-overlapping, segment, - * stop when a NUL byte or a specified substring is encountered. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param str The substring, ignored if `NULL`. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @param maxlen The maximum number of bytes to copy. - * @return `NULL` if `str` was not encountered, otherwise - * the position of `str` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strstrncpy(char* restrict whither, const char* restrict whence, - const char* restrict str, size_t maxlen) -{ - const char* stop = strnstr(whence, str, maxlen); - size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memcpy(whither, whence, n); - memset(whither, 0, maxlen - n); - return r; -} - diff --git a/src/string/strstrnmove.c b/src/string/strstrnmove.c deleted file mode 100644 index 9bec614..0000000 --- a/src/string/strstrnmove.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Copy a memory segment to another, possibly overlapping, segment, - * stop when a NUL byte or a specified substring is encountered. - * - * This is a slibc extension added for completeness. - * It is only available if GNU extensions are available. - * - * @param whither The destination memory segment. - * @param whence The source memory segment. - * @param str The substring, ignored if `NULL`. - * NOTE that if the resulting string at least this - * long, no NUL byte will be written to `whither'. - * On the otherhand, if the resultnig string is - * shorter, `whither` will be filled with NUL bytes - * until this amount of bytes have been written. - * @param maxlen The maximum number of bytes to copy. - * @return `NULL` if `str` was not encountered, otherwise - * the position of `str` translated to `whither`, - * that is, the address of `whither` plus the - * number of copied characters; the address of - * one character passed the last written non-NUL - * character. - */ -char* strstrnmove(char* whither, const char* whence, const char* restrict str, size_t maxlen) -{ - const char* stop = strnstr(whence, str, maxlen); - size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence); - char* r = stop == NULL ? NULL : (whither + n); - memmove(whither, whence, n); - memset(whither, 0, maxlen - n); - return r; -} - diff --git a/src/string/strtok.c b/src/string/strtok.c deleted file mode 100644 index b1b56b9..0000000 --- a/src/string/strtok.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Tokenise a string. - * - * @param string The string to tokenise on the first, - * `NULL` on subsequent calls. - * All bytes found in `delimiters` will - * be overriden with NUL bytes. - * @param delimiters Delimiting bytes (not characters). - * @return The next non-empty string that does not - * contain a byte from `delimiters`. The - * returned string will be as long as possible. - * `NULL` is returned the search as reached - * the end of the string, and there therefore - * are no more tokens. - */ -char* strtok(char* restrict string, const char* restrict delimiters) -{ - static char* state = NULL; - if (string == NULL) - state = NULL; - return strtok_r(string, delimiters, &state); -} - diff --git a/src/string/strtok_r.c b/src/string/strtok_r.c deleted file mode 100644 index 3005ac7..0000000 --- a/src/string/strtok_r.c +++ /dev/null @@ -1,56 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include - - - -/** - * Tokenise a string. - * - * @param string The string to tokenise on the first, - * `NULL` on subsequent calls. - * All bytes found in `delimiters` will - * be overriden with NUL bytes. - * @param delimiters Delimiting bytes (not characters). - * @param state Pointer to a `char*` that the function - * can use to keep track of its state. - * It is reasonable to make it point to `NULL` - * on the first call. - * @return The next non-empty string that does not - * contain a byte from `delimiters`. The - * returned string will be as long as possible. - * `NULL` is returned the search as reached - * the end of the string, and there therefore - * are no more tokens. - */ -char* strtok_r(char* restrict string, const char* restrict delimiters, - char** restrict state) -{ - char* r; - if (string == NULL) - *state = string; - for (;;) - { - r = strsep(state, delimiters); - if (r == NULL) - return NULL; - if (*r) - return r; - } -} - diff --git a/src/string/substring.h b/src/string/substring.h deleted file mode 100644 index 05ef7cc..0000000 --- a/src/string/substring.h +++ /dev/null @@ -1,85 +0,0 @@ -/** - * slibc — Yet another C library - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -/* This file is intended to be included inside a - * [w]mem[r][case]mem function. `haystack` and - * `needle` shall be defined the same pointer type, - * but not as `void*`. `haystack_length` and - * `needle_length` shall be defined in the `size_t` - * type. CASE shall be defined iff case insensitive - * search shall be used. RIGHT shall be defined iff - * the last occurrence shall be find. WIDE shall be - * defined iff wide characters are used. It is assumed - * that `needle_length` <= `haystack_length` */ - - -/* The Knuth–Morris–Pratt algorithm. */ - - -/** - * Test whether to characters are equal. - * Case-sensitivity depends on `CASE` being defined. - * - * @param a One of the characters. - * @param b The other character. - * @return 1 if `a` and `b` or equal, 0 otherwise. - * The comparison is case-insensitive if - * `CASE` is defined. - */ -#if !defined(CASE) -# define CHREQ(a, b) (a == b) -#elif !defined(WIDE) -# define CHREQ(a, b) ((islower(a) ? tolower(a) : (a)) == tolower(b)) -#else -# define CHREQ(a, b) ((iswlower(a) ? towlower(a) : (a)) == towlower(b)) -#endif - - -/* TODO add support for RIGHT */ - - -/* The implementation of the algorithm, read - * elsewhere for documentation/explanation. */ -{ - ssize_t* next_map = alloca((needle_length + 1) * sizeof(ssize_t)); - ssize_t hay, ned, skp; - - ned = 0, skp = next_map[0] = -1; - while (ned < (ssize_t)needle_length) - { - while ((skp > -1) && !CHREQ(needle[ned], needle[skp])) - skp = next_map[skp]; - ned++, skp++; - next_map[ned] = CHREQ(needle[ned], needle[skp]) ? next_map[skp] : skp; - } - - hay = ned = 0; - while (hay < (ssize_t)haystack_length) - { - while ((ned > -1) && !CHREQ(haystack[hay], needle[ned])) - ned = next_map[ned]; - hay++, ned++; - if (ned >= (ssize_t)needle_length) - return needle + (hay - ned); - } - - return NULL; -} - - -#undef CHREQ - -- cgit v1.2.3-70-g09d2