aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-11-19 01:38:10 +0100
committerNeels Hofmeyr <neels@hofmeyr.de>2019-11-24 19:59:35 +0100
commit06356fd9c3234574bd26d2f123acd334b4275f3c (patch)
treefd4eb841235213ec85745a77b95c0b39aa53bd12 /src
parentff65d24ec4cac038e07a16692c3ae64d6e5fbc3c (diff)
utils: add osmo_strnchr()
When finding a char in a string, I want to be able to limit the search area by size, not only by nul terminator. Change-Id: I48f8ace9f51f8a06796648883afcabe3b4e8b537
Diffstat (limited to 'src')
-rw-r--r--src/utils.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index 4378431b..c3e3efbf 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -525,6 +525,28 @@ size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
return ret;
}
+/*! Find first occurence of a char in a size limited string.
+ * Like strchr() but with a buffer size limit.
+ * \param[in] str String buffer to examine.
+ * \param[in] str_size sizeof(str).
+ * \param[in] c Character to look for.
+ * \return Pointer to the matched char, or NULL if not found.
+ */
+const char *osmo_strnchr(const char *str, size_t str_size, char c)
+{
+ const char *end = str + str_size;
+ const char *pos;
+ if (!str)
+ return NULL;
+ for (pos = str; pos < end; pos++) {
+ if (c == *pos)
+ return pos;
+ if (!*pos)
+ return NULL;
+ }
+ return NULL;
+}
+
/*! Validate that a given string is a hex string within given size limits.
* Note that each hex digit amounts to a nibble, so if checking for a hex
* string to result in N bytes, pass amount of digits as 2*N.