src/util/string.c (view raw)
1#include "util/string.h"
2
3#include <string.h>
4
5#ifndef HAVE_STRNDUP
6char* strndup(const char* start, size_t len) {
7 // This is suboptimal, but anything recent should have strndup
8 char* out = malloc((len + 1) * sizeof(char));
9 strncpy(out, start, len);
10 out[len] = '\0';
11 return out;
12}
13#endif
14
15char* strnrstr(const char* restrict haystack, const char* restrict needle, size_t len) {
16 char* last = 0;
17 const char* next = haystack;
18 size_t needleLen = strlen(needle);
19 for (; len >= needleLen; --len, ++next) {
20 if (strncmp(needle, next, needleLen) == 0) {
21 last = (char*) next;
22 }
23 }
24 return last;
25}