-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
is_str_num() is not handling negative numbers #1109
Comments
Call it parse_int, make it have a bool return and optional int outparam?On Aug 7, 2024 2:08 PM, rtxa ***@***.***> wrote:
The is_str_num() function in string_stocks.inc currently does not handle negative numbers. The function only checks for the presence of digits in the string, without considering a leading negative sign which may be useful in some cases and that makes the function name misleading.
Updating is_str_num() may affect any existing code that relies on that behaviour, so maybe a new stock like is_str_num_ex() would be needed or a new optional parameter to allow negative numbers check. A note warning about this would be useful too.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Already exists str_to_num() but you can't rely on it because it returns 0 on failure, and the input string can be a 0, unless we check that the string first digit isn't a 0. |
His suggestion solves both problems simultaneously. Conceptually: stock bool:parse_int(szString[], &intVal = 0)
{
if( /* If string is number */ )
{
intVal = str_to_num(szString)
return true
}
return false
} |
The
is_str_num()
function instring_stocks.inc
currently does not handle negative numbers. The function only checks for the presence of digits in the string, without considering a leading negative sign which may be useful in some cases and that makes the function name misleading.Updating
is_str_num()
may affect any existing code that relies on that behaviour, so maybe a new stock likeis_str_num_ex()
would be needed or a new optional parameter to allow negative numbers check. A note warning about this would be useful too.Proposed fix
Test code:
Output:
The text was updated successfully, but these errors were encountered: