aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Prager <splinterofchaos@gmail.com>2014-09-04 12:37:16 -0400
committerScott Prager <splinterofchaos@gmail.com>2014-12-11 14:39:59 -0500
commitc6271f57671e9dae22675fc76e1f2e3e30a2bda0 (patch)
tree4075fb6f3d3f6eaecb505b6aa4d7b0b9522aeff8
parent951d00a492c58449d3c241fa710a83051f45dcb7 (diff)
downloadrneovim-c6271f57671e9dae22675fc76e1f2e3e30a2bda0.tar.gz
rneovim-c6271f57671e9dae22675fc76e1f2e3e30a2bda0.tar.bz2
rneovim-c6271f57671e9dae22675fc76e1f2e3e30a2bda0.zip
memory: memcnt and strcnt.
-rw-r--r--src/nvim/memory.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index f959ea55e4..c221b13f8b 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -234,6 +234,43 @@ void memchrsub(void *data, char c, char x, size_t len)
}
}
+/// Counts the number of occurrences of `c` in `str`.
+///
+/// @warning Unsafe if `c == NUL`.
+///
+/// @param str Pointer to the string to search.
+/// @param c The byte to search for.
+/// @returns the number of occurrences of `c` in `str`.
+size_t strcnt(const char *str, char c, size_t len)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
+{
+ assert(c != 0);
+ size_t cnt = 0;
+ while ((str = strchr(str, c))) {
+ cnt++;
+ str++; // Skip the instance of c.
+ }
+ return cnt;
+}
+
+/// Counts the number of occurrences of byte `c` in `data[len]`.
+///
+/// @param data Pointer to the data to search.
+/// @param c The byte to search for.
+/// @param len The length of `data`.
+/// @returns the number of occurrences of `c` in `data[len]`.
+size_t memcnt(const void *data, char c, size_t len)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
+{
+ size_t cnt = 0;
+ const char *ptr = data, *end = ptr + len;
+ while ((ptr = memchr(ptr, c, (size_t)(end - ptr))) != NULL) {
+ cnt++;
+ ptr++; // Skip the instance of c.
+ }
+ return cnt;
+}
+
/// The xstpcpy() function shall copy the string pointed to by src (including
/// the terminating NUL character) into the array pointed to by dst.
///