diff options
Diffstat (limited to 'src/common/impl/memrchr.c')
| -rw-r--r-- | src/common/impl/memrchr.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/common/impl/memrchr.c b/src/common/impl/memrchr.c new file mode 100644 index 0000000..aaf97e6 --- /dev/null +++ b/src/common/impl/memrchr.c @@ -0,0 +1,21 @@ +#include "common/memrchr.h" +#include <stddef.h> +#include <stdint.h> + +void* memrchr(const void* s, int c, size_t n) { + if (n == 0) { + return NULL; + } + + const uint8_t uc = (uint8_t) c; + + const uint8_t* p = (const uint8_t*) s + n; + + while (n--) { + if (*--p == uc) { + return (void*) p; + } + } + + return NULL; +} |