String.h
This commit is contained in:
@@ -27,6 +27,14 @@ void memoryCopy(void *dest, const void *src, const size_t size) {
|
||||
assertNotNull(src, "Cannot copy from NULL memory.");
|
||||
assertTrue(size > 0, "Cannot copy 0 bytes of memory.");
|
||||
assertTrue(dest != src, "Cannot copy memory to itself.");
|
||||
|
||||
// Check for overlapping regions
|
||||
assertTrue(
|
||||
((uint8_t*)dest + size <= (uint8_t*)src) ||
|
||||
((uint8_t*)src + size <= (uint8_t*)dest),
|
||||
"Source and destination memory regions overlap."
|
||||
);
|
||||
|
||||
memcpy(dest, src, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,12 +51,6 @@ void stringTrim(char_t *str) {
|
||||
if(start != str) memmove(str, start, end - start + 2);
|
||||
}
|
||||
|
||||
char_t * stringToken(char_t *str, const char_t *delim) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(delim, "delim must not be NULL");
|
||||
return strtok(str, delim);
|
||||
}
|
||||
|
||||
char_t * stringFindLastChar(const char_t *str, const char_t c) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
char_t *last = NULL;
|
||||
@@ -106,12 +100,25 @@ bool_t stringToI32(const char_t *str, int32_t *out) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(out, "out must not be NULL");
|
||||
|
||||
// Empty string check
|
||||
if(str[0] == '\0') return false;
|
||||
|
||||
// Leading or trailing whitespace check
|
||||
if(stringIsWhitespace(str[0])) return false;
|
||||
if(stringIsWhitespace(str[strlen(str) - 1])) return false;
|
||||
|
||||
char_t *endptr;
|
||||
errno = 0;
|
||||
long int result = strtol(str, &endptr, 10);
|
||||
if(errno != 0 || *endptr != '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Within INT32 range
|
||||
if(result < INT32_MIN || result > INT32_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = (int32_t)result;
|
||||
return true;
|
||||
}
|
||||
@@ -120,6 +127,13 @@ bool_t stringToI64(const char_t *str, int64_t *out) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(out, "out must not be NULL");
|
||||
|
||||
// Empty string check
|
||||
if(str[0] == '\0') return false;
|
||||
|
||||
// Leading or trailing whitespace check
|
||||
if(stringIsWhitespace(str[0])) return false;
|
||||
if(stringIsWhitespace(str[strlen(str) - 1])) return false;
|
||||
|
||||
char_t *endptr;
|
||||
errno = 0;
|
||||
long long int result = strtoll(str, &endptr, 10);
|
||||
@@ -130,10 +144,38 @@ bool_t stringToI64(const char_t *str, int64_t *out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t stringToI16(const char_t *str, int16_t *out) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(out, "out must not be NULL");
|
||||
|
||||
// Empty string check
|
||||
if(str[0] == '\0') return false;
|
||||
|
||||
// Leading or trailing whitespace check
|
||||
if(stringIsWhitespace(str[0])) return false;
|
||||
if(stringIsWhitespace(str[strlen(str) - 1])) return false;
|
||||
|
||||
char_t *endptr;
|
||||
errno = 0;
|
||||
long int result = strtol(str, &endptr, 10);
|
||||
if(errno != 0 || *endptr != '\0' || result < INT16_MIN || result > INT16_MAX) {
|
||||
return false;
|
||||
}
|
||||
*out = (int16_t)result;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t stringToU16(const char_t *str, uint16_t *out) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(out, "out must not be NULL");
|
||||
|
||||
// Empty string check
|
||||
if(str[0] == '\0') return false;
|
||||
|
||||
// Leading or trailing whitespace check
|
||||
if(stringIsWhitespace(str[0])) return false;
|
||||
if(stringIsWhitespace(str[strlen(str) - 1])) return false;
|
||||
|
||||
char_t *endptr;
|
||||
errno = 0;
|
||||
unsigned long int result = strtoul(str, &endptr, 10);
|
||||
@@ -148,6 +190,13 @@ bool_t stringToF32(const char_t *str, float_t *out) {
|
||||
assertNotNull(str, "str must not be NULL");
|
||||
assertNotNull(out, "out must not be NULL");
|
||||
|
||||
// Empty string check
|
||||
if(str[0] == '\0') return false;
|
||||
|
||||
// Leading or trailing whitespace check
|
||||
if(stringIsWhitespace(str[0])) return false;
|
||||
if(stringIsWhitespace(str[strlen(str) - 1])) return false;
|
||||
|
||||
char_t *endptr;
|
||||
errno = 0;
|
||||
float_t result = strtof(str, &endptr);
|
||||
|
||||
@@ -53,17 +53,6 @@ int stringCompareInsensitive(const char_t *str1, const char_t *str2);
|
||||
*/
|
||||
void stringTrim(char_t *str);
|
||||
|
||||
/**
|
||||
* Gets the next token in a string using a delimiter.
|
||||
* e.g. input: "Hello, World, Happy Monday!" with stringToken(input, ",") will
|
||||
* return "Hello" then " World" then " Happy Monday!" on each subsequent call.
|
||||
*
|
||||
* @param str The string to split.
|
||||
* @param delim The delimiter to split by.
|
||||
* @return A pointer to the next token in the string.
|
||||
*/
|
||||
char_t * stringToken(char_t *str, const char_t *delim);
|
||||
|
||||
/**
|
||||
* Finds the last occurrence of a character in a string.
|
||||
*
|
||||
@@ -119,6 +108,15 @@ int32_t stringFormatVA(
|
||||
*/
|
||||
bool_t stringToI32(const char_t *str, int32_t *out);
|
||||
|
||||
/**
|
||||
* Converts a string to a signed 64-bit integer.
|
||||
*
|
||||
* @param str The string to convert.
|
||||
* @param out The output signed integer.
|
||||
* @return TRUE if the conversion was successful, FALSE otherwise.
|
||||
*/
|
||||
bool_t stringToI64(const char_t *str, int64_t *out);
|
||||
|
||||
/**
|
||||
* Converts a string to a signed 16-bit integer.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user