String.h
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user