diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-12-11 21:35:34 -0500 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-12-11 21:35:34 -0500 |
commit | 4c7fe20befec9b2d23dd11abfe2494ec38a2ca23 (patch) | |
tree | c912cf32d8b8146551bcd8290a532074c74e1814 /src/nvim/memory.c | |
parent | ab1f0bd119a7adf36c59ee389dc7115d0204eacf (diff) | |
parent | a192865f908f4b9b66a73d41053cc3ee2d9ad22f (diff) | |
download | rneovim-4c7fe20befec9b2d23dd11abfe2494ec38a2ca23.tar.gz rneovim-4c7fe20befec9b2d23dd11abfe2494ec38a2ca23.tar.bz2 rneovim-4c7fe20befec9b2d23dd11abfe2494ec38a2ca23.zip |
Merge pull request #1134 from splinterofchaos/getreg-nl
vim-patch:7.4.242 + vim-patch:7.4.243
Diffstat (limited to 'src/nvim/memory.c')
-rw-r--r-- | src/nvim/memory.c | 37 |
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. /// |