author | Olivier Brunel
<jjk@jjacky.com> 2023-03-24 12:34:09 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-03-24 12:50:59 UTC |
parent | 5fa15f272dce16e1bbbb582d230fe974c67b90f1 |
doc/byte_str.3.md | +27 | -0 |
doc/bytestr.h.0.md | +25 | -0 |
include/limb/bytestr.h | +11 | -0 |
meta/libs/limb | +2 | -0 |
src/byte_str.c | +12 | -0 |
diff --git a/doc/byte_str.3.md b/doc/byte_str.3.md new file mode 100644 index 0000000..2a780eb --- /dev/null +++ b/doc/byte_str.3.md @@ -0,0 +1,27 @@ +% limb manual +% byte_str(3) + +# NAME + +byte\_str - locate a substring within a string + +# SYNOPSIS + + #include <limb/bytestr.h> + +```pre hl +size_t byte_str(const char *<em>haystack</em>, size_t <em>hlen</em>, const char *<em>needle</em>, size_t <em>nlen</em>) +``` + +# DESCRIPTION + +The `byte_str`() function looks for the first occurrence of the byte array +`needle` of length `nlen` in the byte array `haystack` of length `hlen`. + +Neither `haystack` nor `needle` need be NUL-terminated, and in fact can contain +themselves bytes of any value. + +# RETURN VALUE + +The `byte_str`() function returns the offset of the first occurrence of `needle` +within `haystack` if found. Otherwise, it returns `hlen`. diff --git a/doc/bytestr.h.0.md b/doc/bytestr.h.0.md new file mode 100644 index 0000000..ddb7b2f --- /dev/null +++ b/doc/bytestr.h.0.md @@ -0,0 +1,25 @@ +% limb manual +% bytestr.h(0) + +# NAME + +bytestr.h - byte arrays/strings functions + +# SYNOPSIS + + #include <limb/bytestr.h> + +# DESCRIPTION + +This header defines functions to work with byte arrays and/or strings. + +! INFO: skalibs +! This header is a complement to skalibs' own [skalibs/bytestr.h](0) and thusly +! includes said header. + +## Functions + +The following functions are defined : + +: [byte_str](3) +:: To locate a substring within a string diff --git a/include/limb/bytestr.h b/include/limb/bytestr.h new file mode 100644 index 0000000..3ab983b --- /dev/null +++ b/include/limb/bytestr.h @@ -0,0 +1,11 @@ +/* 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 */ +#ifndef LIMB_BYTESTR_H +#define LIMB_BYTESTR_H + +#include <skalibs/bytestr.h> + +extern size_t byte_str(const char *haystack, size_t hlen, const char *needle, size_t nlen); + +#endif /* LIMB_BYTESTR_H */ diff --git a/meta/libs/limb b/meta/libs/limb index 33e34cc..ec05868 100644 --- a/meta/libs/limb +++ b/meta/libs/limb @@ -20,6 +20,8 @@ obj/sareadlinkat0.o obj/sacoloff.o obj/saoff2ptr.o obj/sacolptr.o +# bytestr.h +obj/byte_str.o # buffer.h obj/buffer_putescall.o obj/buffer_putesc.o diff --git a/src/byte_str.c b/src/byte_str.c new file mode 100644 index 0000000..cb495aa --- /dev/null +++ b/src/byte_str.c @@ -0,0 +1,12 @@ +/* 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 <skalibs/posixplz.h> +#include "limb/bytestr.h" + +size_t +byte_str(const char *haystack, size_t hlen, const char *needle, size_t nlen) +{ + char *s = memmem(haystack, hlen, needle, nlen); + return (s) ? s - haystack : hlen; +}