aboutsummaryrefslogtreecommitdiffstats
path: root/src/string/mem
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/string/memcasecmp.c50
-rw-r--r--src/string/memccpy.c27
-rw-r--r--src/string/memcmove.c47
-rw-r--r--src/string/memcmp.c42
-rw-r--r--src/string/memcpy.c21
-rw-r--r--src/string/memdup.c40
-rw-r--r--src/string/memmove.c17
-rw-r--r--src/string/mempcpy.c36
-rw-r--r--src/string/mempmove.c37
9 files changed, 253 insertions, 64 deletions
diff --git a/src/string/memcasecmp.c b/src/string/memcasecmp.c
new file mode 100644
index 0000000..d8ea014
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+#include <ctype.h>
+
+
+
+/**
+ * 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/memccpy.c b/src/string/memccpy.c
index 0c73f8b..18e423d 100644
--- a/src/string/memccpy.c
+++ b/src/string/memccpy.c
@@ -43,30 +43,3 @@ void* (memccpy)(void* restrict whither, const void* restrict whence, int c, size
return r;
}
-
-/**
- * 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/memcmove.c b/src/string/memcmove.c
new file mode 100644
index 0000000..50ed1f9
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * 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
new file mode 100644
index 0000000..112cdd1
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * 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
index 54482fb..fcda29d 100644
--- a/src/string/memcpy.c
+++ b/src/string/memcpy.c
@@ -18,9 +18,6 @@
#include <string.h>
-# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
-
-
/**
* Copy a memory segment to another, non-overlapping, segment.
@@ -33,25 +30,9 @@
void* memcpy(void* restrict whither, const void* restrict whence, size_t size)
{
char* d = whither;
- char* s = whence;
+ const char* s = whence;
while (size--)
*d++ = *s++;
return whither;
}
-
-/**
- * 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/memdup.c b/src/string/memdup.c
new file mode 100644
index 0000000..3b301dc
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+#include <stdlib.h>
+
+
+
+/**
+ * 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/memmove.c b/src/string/memmove.c
index 0acc47b..41871c6 100644
--- a/src/string/memmove.c
+++ b/src/string/memmove.c
@@ -40,20 +40,3 @@ void* memmove(void* whither, const void* whence, size_t size)
return whither;
}
-
-/**
- * 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/mempcpy.c b/src/string/mempcpy.c
new file mode 100644
index 0000000..2072dc3
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * 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
new file mode 100644
index 0000000..f837032
--- /dev/null
+++ b/src/string/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 <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * 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;
+}
+