diff --git a/cstring.h b/cstring.h index f99bd4c..fc47e7f 100644 --- a/cstring.h +++ b/cstring.h @@ -6,7 +6,9 @@ #include #include #include - +#include +#include +#include typedef enum EErrorCode { ERR_NO_ERROR, @@ -17,6 +19,7 @@ typedef enum EErrorCode { ERR_INVALID_STATE, ERR_NULL_POINTER, ERR_NUMBER_OVERFLOW, + ERR_INVALID_NUMBER_REPR, ERR_NAN } EErrorCode; @@ -41,6 +44,8 @@ bool stringCharIsAlphanum(char c); char stringCharToLower(char c); char stringCharToUpper(char c); +int stringCharToInt(char c); + bool stringStartWith(TString s, TString pref); bool stringStartWithCharArr(TString s, char *pref); bool stringEndWith(TString s, TString pref); @@ -61,6 +66,7 @@ int stringCompare(TString s1, TString s2); int64_t stringFindFirst(TString s, TString pattern); int64_t stringFindFirstCharArr(TString s, const char *pattern); +int64_t stringToInt(TString s); TString stringRand(size_t size); TString stringInit(size_t capacity); @@ -76,6 +82,7 @@ TString stringJoinCharArr(TString s1, TString s2, const char *delim); TString stringArrJoin(const TString *s, size_t count, TString delim); TString stringArrJoinCharArr(const TString *s, size_t count, const char *delim); +char* stringConvertToCharArr(const TString s); void stringScan(TString *s); void stringPrint(TString s); void stringDebug(TString s); @@ -218,7 +225,7 @@ void stringIncreaseCap(TString *s) { s->capacity = newCap; } -// import +// import bool stringCharIsDigit(char c) { return ('0' <= c && c <= '9'); @@ -241,6 +248,14 @@ char stringCharToUpper(char c) { return c; } +int stringCharToInt(char c) { + clearError(); + if ('0' <= c && c <= '9') + return c - '0'; + setError(ERR_INVALID_NUMBER_REPR); + return -1; +} + bool stringStartWith(TString s, TString pref) { if (s.size < pref.size) return false; return stringCompSubstr(s.data, 0, pref.size, pref.data, @@ -361,6 +376,39 @@ int64_t stringFindFirstCharArr(TString s, const char *pattern) { return -1; } +int64_t stringToInt(TString s) { + clearError(); + + int64_t sign = 1; + int i = 0; + + if (s.data[0] == '-') { + if (s.size == 1) { + setError(ERR_INVALID_NUMBER_REPR); + return 0; + } + sign = -1; + i++; + } + + int64_t val = 0; + for (; i < s.size; i++) { + int64_t digit = stringCharToInt(s.data[i]); + + // TODO: check for INT64_MIN overflow + if (val >= (INT64_MAX - digit) / 10) { + setError(ERR_NUMBER_OVERFLOW); + return val; + } + else { + val = val * 10 + digit; + } + } + val = val * sign; + + return val; +} + TString stringInit(size_t capacity) { TString s = {0}; s.data = (char *)malloc(sizeof(char) * capacity); @@ -604,6 +652,25 @@ TString stringSubstring(TString s, size_t pos, size_t len) { return res; } +char* stringConvertToCharArr(const TString s) { + if (s.data == NULL) + return NULL; + + clearError(); + char *res = malloc(s.size + 1); + + if(res == NULL) { + setError(ERR_NULL_POINTER); + return NULL; + } + + for (size_t i = 0; i < s.size; ++i) { + res[i] = s.data[i]; + } + res[s.size] = '\0'; + return res; +} + void stringScan(TString *s) { if (s == NULL) { setError(ERR_NULL_POINTER); diff --git a/tests/main.c b/tests/main.c index 92621be..835af3c 100644 --- a/tests/main.c +++ b/tests/main.c @@ -467,6 +467,22 @@ void test_stringRemove() { printGreen("test_stringRemove\n"); } +void test_stringToInt() { + TString s1 = stringInitWithCharArr("0"); + TString s2 = stringInitWithCharArr("-2132456"); + TString s3 = stringInitWithCharArr("18446744073709551615"); + + assertEq(stringToInt(s1), 0); + assertEq(stringToInt(s2), -2132456); + assertEq(stringToInt(s3), 18446744073709551615LL); + + stringDestroy(&s1); + stringDestroy(&s2); + stringDestroy(&s3); + + printGreen("test_stringToInt\n"); +} + void test_stringToDouble() { TString s1 = stringInitWithCharArr("0"); TString s2 = stringInitWithCharArr("12.34"); @@ -511,5 +527,6 @@ int main() { test_stringIsPalindrome(); test_stringPad(); test_stringRemove(); + test_stringToInt(); return 0; }