Welcome to little lamb

Code » limb » commit a3bdb24

Add stralloc_remove()

author Olivier Brunel
2023-05-19 15:54:52 UTC
committer Olivier Brunel
2023-07-05 07:37:02 UTC
parent e21a8ef61951636f114ccbbc008ef8ef19350bf0

Add stralloc_remove()

src/doc/stralloc.h.0.md +4 -0
src/doc/stralloc.h/stralloc_remove.3.md +38 -0
src/liblimb/include/limb/stralloc.h +2 -0
src/liblimb/stralloc.h/stralloc_remove.c +19 -0

diff --git a/src/doc/stralloc.h.0.md b/src/doc/stralloc.h.0.md
index 2d4c6f4..8a729b6 100644
--- a/src/doc/stralloc.h.0.md
+++ b/src/doc/stralloc.h.0.md
@@ -129,6 +129,10 @@ The following functions/macros are defined :
 :: offset.
 
 
+: [stralloc_remove](3)
+:: Remove data from a *stralloc*.
+
+
 : [stralloc_reverse](3)
 :: Reverse the content of a *stralloc*.
 
diff --git a/src/doc/stralloc.h/stralloc_remove.3.md b/src/doc/stralloc.h/stralloc_remove.3.md
new file mode 100644
index 0000000..db3c522
--- /dev/null
+++ b/src/doc/stralloc.h/stralloc_remove.3.md
@@ -0,0 +1,38 @@
+% limb manual
+% stralloc_remove(3)
+
+# NAME
+
+stralloc\_remove - remove data from a stralloc
+
+# SYNOPSIS
+
+    #include <limb/stralloc.h>
+
+```pre hl
+int stralloc_remove(stralloc *<em>sa</em>, size_t <em>offset</em>, size_t <em>len</em>)
+```
+
+# DESCRIPTION
+
+The `stralloc_remove`() function removes data from the stralloc `sa` starting at
+byte `offset` and for `len` bytes.
+
+The member `len` will be adjusted and data afterwards moved as needed, but no
+re-allocation occurs.
+
+# RETURN VALUE
+
+The `stralloc_remove`() function return 1 on success. Otherwise they return 0
+and set `errno` to indicate the error.
+
+# ERRORS
+
+The `stralloc_remove`() function may fail if :
+
+: *EINVAL*
+:: The `offset` is too high.
+
+# SEE ALSO
+
+[stralloc_shrunk](3)
diff --git a/src/liblimb/include/limb/stralloc.h b/src/liblimb/include/limb/stralloc.h
index e31dbd5..c8b929c 100644
--- a/src/liblimb/include/limb/stralloc.h
+++ b/src/liblimb/include/limb/stralloc.h
@@ -10,4 +10,6 @@
 #define stralloc_cats0(sa, s)       stralloc_catb(sa, (s), strlen(s) + 1)
 #define stralloc_inserts0(sa, o, s) stralloc_insertb(sa, o, (s), strlen(s) + 1)
 
+extern int stralloc_remove(stralloc *sa, size_t offset, size_t len);
+
 #endif /* LIMB_STRALLOC_H */
diff --git a/src/liblimb/stralloc.h/stralloc_remove.c b/src/liblimb/stralloc.h/stralloc_remove.c
new file mode 100644
index 0000000..12934d2
--- /dev/null
+++ b/src/liblimb/stralloc.h/stralloc_remove.c
@@ -0,0 +1,19 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <errno.h>
+#include <string.h>
+#include <limb/stralloc.h>
+
+int
+stralloc_remove(stralloc *sa, size_t offset, size_t len)
+{
+    if (offset + len > sa->len)
+        return (errno = EINVAL, 0);
+
+    if (offset + len < sa->len)
+        memmove(sa->s + offset, sa->s + offset + len, sa->len - offset - len);
+
+    sa->len -= len;
+    return 1;
+}