Save Manager
This commit is contained in:
35
src/dawn/util/string.hpp
Normal file
35
src/dawn/util/string.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
/**
|
||||
* Finds the next instance of a character within a string, safely (with a
|
||||
* limit). The returned pointer will be NULL if not found, or a pointer to a
|
||||
* point within the string where the instance is.
|
||||
*
|
||||
* @param haystack String to search.
|
||||
* @param needle Character to search for.
|
||||
* @param limit Max length you want to search for to limit yourself to.
|
||||
* @return Pointer to the character found, or NULL if not found.
|
||||
*/
|
||||
static inline char * stringFindNext(
|
||||
char *haystack,
|
||||
char needle,
|
||||
size_t limit
|
||||
) {
|
||||
char *p;
|
||||
|
||||
assertNotNull(haystack);
|
||||
assertTrue(limit > 0);
|
||||
|
||||
for(p = haystack; (size_t)(p - haystack) < limit; p++) {
|
||||
if(*p == needle) return p;
|
||||
assertFalse(*p == '\0');// We don't allow you to have a limit > strlen
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
Reference in New Issue
Block a user