all repos — mgba @ 9c92a29b28d1f81224ba28d5fc83a9481eccd5eb

mGBA Game Boy Advance Emulator

src/util/string.c (view raw)

 1/* Copyright (c) 2013-2014 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "util/string.h"
 7
 8#include <string.h>
 9
10#ifndef HAVE_STRNDUP
11char* strndup(const char* start, size_t len) {
12	// This is suboptimal, but anything recent should have strndup
13	char* out = malloc((len + 1) * sizeof(char));
14	strncpy(out, start, len);
15	out[len] = '\0';
16	return out;
17}
18#endif
19
20char* strnrstr(const char* restrict haystack, const char* restrict needle, size_t len) {
21	char* last = 0;
22	const char* next = haystack;
23	size_t needleLen = strlen(needle);
24	for (; len >= needleLen; --len, ++next) {
25		if (strncmp(needle, next, needleLen) == 0) {
26			last = (char*) next;
27		}
28	}
29	return last;
30}