Welcome to little lamb

Code » limb » commit b12e896

Add siovec_seek_{bytechr,bytein,gather}

author Olivier Brunel
2023-03-14 18:36:41 UTC
committer Olivier Brunel
2023-03-17 14:39:22 UTC
parent 0a09b6f0e812237a4e681b4175134d997f3f36e7

Add siovec_seek_{bytechr,bytein,gather}

Some little helper function to their regular siovec_* counterpart, but
with an offset to start the searching/gathering from.

Also add siovec.h with macros siov_* w/ slightly different interfaces,
to keep consistent with our "destination first" rule.

doc/siovec.h.0.md +70 -0
doc/siovec_seek_bytechr.3.md +33 -0
doc/siovec_seek_bytein.3.md +35 -0
doc/siovec_seek_gather.3.md +32 -0
include/limb/siovec.h +26 -0
meta/libs/limb +4 -0
src/siovec_seek_bytechr.c +13 -0
src/siovec_seek_bytein.c +14 -0
src/siovec_seek_gather.c +13 -0

diff --git a/doc/siovec.h.0.md b/doc/siovec.h.0.md
new file mode 100644
index 0000000..85410b4
--- /dev/null
+++ b/doc/siovec.h.0.md
@@ -0,0 +1,70 @@
+% limb manual
+% siovec.h(0)
+
+# NAME
+
+siovec.h - manipulating struct iovec
+
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+
+# DESCRIPTION
+
+The header defines the needed functions to manipulate `struct iovec` containing
+range of bytes in similar manner as byte arrays.
+
+It also defines macros offering a slightly different interface, to remain
+consistent within limb.
+
+! INFO: skalibs
+! This header is a complement to skalibs' own [skalibs/siovec.h](0) and thusly
+! includes said header.
+
+## Functions
+
+The following functions/macros are defined :
+
+: [siov_len](3)
+:: Same as [siovec_len](3)
+
+: [siov_gather](3)
+:: Same as [siovec_gather](3) but with destination as first arguments
+
+: [siov_scatter](3)
+:: Same as [siovec_scatter](3)
+
+: [siov_deal](3)
+:: Same as [siovec_deal](3) but with destination as first arguments
+
+: [siov_seek](3)
+:: Same as [siovec_seek](3)
+
+: [siov_trunc](3)
+:: Same as [siovec_trunc](3)
+
+: [siov_bytechr](3)
+:: Same as [siovec_bytechr](3)
+
+: [siov_bytein](3)
+:: Same as [siovec_bytein](3)
+
+: [siov_seek_gather](3)
+:: Same as [siovec_seek_gather](3) but with destination as first arguments
+
+: [siov_seek_bytechr](3)
+:: Same as [siovec_seek_bytechr](3)
+
+: [siov_seek_bytein](3)
+:: Same a [siovec_seek_bytein](3)
+
+: [siovec_seek_gather](3)
+:: Same as [siovec_gather](3) but from a given offset
+
+: [siovec_seek_bytechr](3)
+:: Same as [siovec_bytechr](3) but from a given offset
+
+: [siovec_seek_bytein](3)
+:: Same as [siovec_bytein](3) but from a given offset
diff --git a/doc/siovec_seek_bytechr.3.md b/doc/siovec_seek_bytechr.3.md
new file mode 100644
index 0000000..74b3250
--- /dev/null
+++ b/doc/siovec_seek_bytechr.3.md
@@ -0,0 +1,33 @@
+% limb manual
+% siovec_seek_bytechr(3)
+
+# NAME
+
+siovec\_seek\_bytechr - scan data from *struct iovec* for a given byte
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+```pre hl
+size_t siovec_seek_bytechr(const struct iovec *<em>v</em>, unsigned int <em>n</em>, size_t <em>offset</em>, char <em>c</em>)
+```
+
+# DESCRIPTION
+
+The `siovec_seek_bytechr`() function scans data pointed by the array `v` of `n`
+*struct iovec* for the first instance of `c`, starting at byte `offset` within
+the data.
+
+This is the equivalent of [siovec_bytechr](3) but skipping the first `offset`
+bytes of data.
+
+# RETURN VALUE
+
+`siovec_seek_bytechr`() returns the position of the first occurrence of `c`
+within the data behind `v` if found. Otherwise it returns the total length of
+the data.
+
+# SEE ALSO
+
+[siovec_seek](3), [siovec_bytechr](3), [byte_chr](3)
diff --git a/doc/siovec_seek_bytein.3.md b/doc/siovec_seek_bytein.3.md
new file mode 100644
index 0000000..4d274ec
--- /dev/null
+++ b/doc/siovec_seek_bytein.3.md
@@ -0,0 +1,35 @@
+% limb manual
+% siovec_seek_bytein(3)
+
+# NAME
+
+siovec\_seek\_bytein - scan data from *struct iovec* for any of the byte from
+an array
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+```pre hl
+size_t siovec_seek_bytein(const struct iovec *<em>v</em>, unsigned int <em>n</em>, size_t <em>offset</em>,
+                          const char *<em>s</em>, size_t <em>l</em>)
+```
+
+# DESCRIPTION
+
+The `siovec_seek_bytein`() function scans data pointed by the array `v` of `n`
+*struct iovec* for the first instance of any of the `l` bytes pointed by `s`,
+starting at byte `offset` within the data.
+
+This is the equivalent of [siovec_bytein](3) but skipping the first `offset`
+bytes of data.
+
+# RETURN VALUE
+
+`siovec_seek_bytein`() returns the position of the first occurrence of any
+bytes from `s` within the data behind `v` if found. Otherwise it returns
+the total length of the data.
+
+# SEE ALSO
+
+[siovec_seek](3), [siovec_bytein](3), [byte_in](3)
diff --git a/doc/siovec_seek_gather.3.md b/doc/siovec_seek_gather.3.md
new file mode 100644
index 0000000..066eb27
--- /dev/null
+++ b/doc/siovec_seek_gather.3.md
@@ -0,0 +1,32 @@
+% limb manual
+% siovec_seek_gather(3)
+
+# NAME
+
+siovec\_seek\_gather - gather data from struct iovec into a byte array
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+```pre hl
+size_t siovec_seek_gather(const struct iovec *<em>v</em>, unsigned int <em>n</em>, size_t <em>offset</em>,
+                          char *<em>s</em>, size_t <em>max</em>)
+```
+
+# DESCRIPTION
+
+The `siovec_seek_gather`() function gathers data scattered across given array
+`v` of `n` *struct iovec* into the byte array pointed to by `s`, up to `max`
+bytes, starting at offset `offset` within the data.
+
+This is the equivalent of [siovec_gather](3) but skipping the first `offset`
+bytes of data.
+
+# RETURN VALUE
+
+`siovec_seek_gather`() returns the number of bytes copied into `s`.
+
+# SEE ALSO
+
+[siovec_seek](3), [siovec_gather](3)
diff --git a/include/limb/siovec.h b/include/limb/siovec.h
new file mode 100644
index 0000000..a8e074a
--- /dev/null
+++ b/include/limb/siovec.h
@@ -0,0 +1,26 @@
+#ifndef LIMB_SIOVEC_H
+#define LIMB_SIOVEC_H
+
+#include <skalibs/siovec.h>
+
+extern size_t siovec_seek_gather(const struct iovec *v, unsigned int n, size_t offset,
+                                 char *dst, size_t max);
+extern size_t siovec_seek_bytechr(const struct iovec *v, unsigned int n, size_t offset,
+                                  char c);
+extern size_t siovec_seek_bytein(const struct iovec *v, unsigned int n, size_t offset,
+                                 const char *sep, size_t len);
+
+#define siov_len(v,n)                   siovec_len(v, n)
+#define siov_gather(dst,max,v,n)        siovec_gather(v, n, dst, max)
+#define siov_scatter(v,n,sce,len)       siovec_scatter(v, n, sce, len)
+#define siov_deal(vdst,ndst,vsce,nsce)  siovec_deal(vdst, ndst, vsce, nsce)
+#define siov_seek(v,n,o)                siovec_seek(v, n, o)
+#define siov_trunc(v,n,l)               siovec_trunc(v, n, l)
+#define siov_bytechr(v,n,c)             siovec_bytechr(v, n, c)
+#define siov_bytein(v,n,s,l)            siovec_bytein(v, n, s, l)
+
+#define siov_seek_gather(dst,max,v,n,o) siovec_seek_gather(v, n, o, dst, max)
+#define siov_seek_bytechr(v,n,o,c)    siovec_seek_bytechr(v, n, o, c)
+#define siov_seek_bytein(v,n,o,s,l)     siovec_seek_bytein(v, n, o, s, l)
+
+#endif /* LIMB_SIOVEC_H */
diff --git a/meta/libs/limb b/meta/libs/limb
index 6398931..574549e 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -34,6 +34,10 @@ obj/uint64_unpack_trim.o
 # u64
 obj/u64_fmt_generic.o
 obj/u640_fmt_generic.o
+# siovec.h
+obj/siovec_seek_bytechr.o
+obj/siovec_seek_bytein.o
+obj/siovec_seek_gather.o
 # data-encoding (integers or blobs)
 obj/saencdata.o
 # content-based chunking
diff --git a/src/siovec_seek_bytechr.c b/src/siovec_seek_bytechr.c
new file mode 100644
index 0000000..26cb13b
--- /dev/null
+++ b/src/siovec_seek_bytechr.c
@@ -0,0 +1,13 @@
+#include "limb/siovec.h"
+
+size_t
+siovec_seek_bytechr(const struct iovec *v, unsigned int n, size_t offset, char c)
+{
+    struct iovec vv[n];
+
+    for (unsigned int i = 0; i < n; ++i)
+        vv[i] = v[i];
+
+    siovec_seek(vv, n, offset);
+    return siovec_bytechr(vv, n, c);
+}
diff --git a/src/siovec_seek_bytein.c b/src/siovec_seek_bytein.c
new file mode 100644
index 0000000..bc5d51c
--- /dev/null
+++ b/src/siovec_seek_bytein.c
@@ -0,0 +1,14 @@
+#include "limb/siovec.h"
+
+size_t
+siovec_seek_bytein(const struct iovec *v, unsigned int n, size_t offset,
+                   const char *s, size_t l)
+{
+    struct iovec vv[n];
+
+    for (unsigned int i = 0; i < n; ++i)
+        vv[i] = v[i];
+
+    siovec_seek(vv, n, offset);
+    return siovec_bytein(vv, n, s, l);
+}
diff --git a/src/siovec_seek_gather.c b/src/siovec_seek_gather.c
new file mode 100644
index 0000000..b1f876f
--- /dev/null
+++ b/src/siovec_seek_gather.c
@@ -0,0 +1,13 @@
+#include "limb/siovec.h"
+
+size_t
+siovec_seek_gather(const struct iovec *v, unsigned int n, size_t offset, char *s, size_t max)
+{
+    struct iovec vv[n];
+
+    for (unsigned int i = 0; i < n; ++i)
+        vv[i] = v[i];
+
+    siovec_seek(vv, n, offset);
+    return siovec_gather(vv, n, s, max);
+}