diff options
| author | Randy Palamar <randy@rnpnr.xyz> | 2025-12-06 08:54:34 -0700 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2025-12-08 09:49:00 -0700 |
| commit | b389733ab0fa46f2cecd302cc5826fc7dc322c8e (patch) | |
| tree | e8489b83900fbb1b1ae53ebf8e1a25ffdaa73342 /text-iterator.c | |
| parent | 6ead3cc87f960c1816ed4ee37da3b19d60700bbc (diff) | |
| download | vis-b389733ab0fa46f2cecd302cc5826fc7dc322c8e.tar.gz vis-b389733ab0fa46f2cecd302cc5826fc7dc322c8e.tar.xz | |
util: replace memrchr with internal version
The amount of code we need to detect if this is present and handle
the fallback is more than if we just provide it ourselves.
Also we are passing in a difference of pointers so the argument
type should be ptrdiff_t. This avoids a C brain damage of having
unsigned size type which can wrap around if the caller is careful.
Diffstat (limited to 'text-iterator.c')
| -rw-r--r-- | text-iterator.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/text-iterator.c b/text-iterator.c index e080ca5..b61c609 100644 --- a/text-iterator.c +++ b/text-iterator.c @@ -1,6 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE /* memrchr(3) is non-standard */ -#endif #include <limits.h> #include <stddef.h> #include <errno.h> @@ -72,9 +69,19 @@ bool text_iterator_byte_prev(Iterator *it, char *b) { return true; } +static void *scan_memory_reverse(const void *memory, int byte, ptrdiff_t n) +{ + void *result = 0; + if (n > 0) { + const signed char *s = memory; + while (n) if (s[--n] == byte) { result = (void *)(s + n); break; } + } + return result; +} + bool text_iterator_byte_find_prev(Iterator *it, char b) { while (it->text) { - const char *match = memrchr(it->start, b, it->text - it->start); + const char *match = scan_memory_reverse(it->start, b, it->text - it->start); if (match) { it->pos -= it->text - match; it->text = match; |
