-
Notifications
You must be signed in to change notification settings - Fork 16
stringToInt feature implemented #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cstring.h
Outdated
| char stringCharToUpper(char c); | ||
|
|
||
| int stringCharToInt(char c); | ||
| int64_t stringCharToInt64(char c); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a kind of duplication of the previous function, because int can always be implicitly converted to int64_t
cstring.h
Outdated
| if(res == NULL) { | ||
| setError(ERR_NULL_POINTER); | ||
| } | ||
|
|
||
| if (isError()) { | ||
| return res; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if(res == NULL) { | |
| setError(ERR_NULL_POINTER); | |
| } | |
| if (isError()) { | |
| return res; | |
| } | |
| if (res == NULL) { | |
| setError(ERR_NULL_POINTER); | |
| return NULL; | |
| } |
cstring.h
Outdated
|
|
||
| char* str = stringConvertToCharArr(s); | ||
|
|
||
| if(str[0] == '-'){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Segfault when len = 0
- Please, check code style convention
- It is not necessary to allocate a C-string, better use
s.dataands.size
cstring.h
Outdated
| // check for integer overflow | ||
| if(val < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this how to prevent signed overflow
Now it is UB
cstring.h
Outdated
| for (; i < s.size; i++) { | ||
| int64_t digit = stringCharToInt(s.data[i]); | ||
|
|
||
| if (val * 10 > INT64_MAX - digit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything is good but this still can cause overflow val * 10. It'l be better to check smth like this:
val > (INT64_MAX - digit) / 10
be careful with rounding, it requires divisibility check
| int stringCharToInt(char c) { | ||
| if ('0' <= c && c <= '9') | ||
| return c - '0'; | ||
| setError(ERR_INVALID_NUMBER_REPR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If function can change error state, it have to clear error in the begin, to prevent smth like that:
// err = no_err
foo() // set err to smth, but result wasn't checked
...
//err = some_err
bar() // no error actually happened but flag wasn't cleaned
if (err == some_err) { // check error in bar() result
... // but goes here because err after foo()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although it is better to always check the error after each function call
cstring.h
Outdated
| if (val > (INT64_MAX - digit) / 10) { | ||
| setError(ERR_NUMBER_OVERFLOW); | ||
| return val; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such a nice code!
just change to >= and add test for stringToInt(INT64_MAX)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will probably overflow with INT64_MIN due to the asymmetric range but you may just leave TODO
Tnirpps
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
Function strinToInt that returns int64_t is implemented using efficient algorithm of converting string to int. For the sake of initial string data safety I used helper c-style string(But it can be implemented without any helper strings by traversing string data field). Also the sign of the number is considered. Error handling is implemented using this frameworks error handling tools. Also new error code was added for better debugging. As helper function was added function stringConvertToCharArr that converts TString type to c-style string type.