Skip to content

Commit

Permalink
Core: Atoi, Atoi64
Browse files Browse the repository at this point in the history
  • Loading branch information
mirek-fidler committed May 3, 2023
1 parent 5e5819a commit 0cea273
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions autotest/Atoi/Atoi.cpp
Expand Up @@ -157,5 +157,15 @@ CONSOLE_APP_MAIN
DDUMP(CParser("123456123").ReadNumber64(8));
DDUMP(CParser("123456123").ReadNumber64(16));

DDUMP(Atoi(""));
DDUMP(Atoi("x"));
DDUMP(Atoi("123"));
DDUMP(Atoi("-123"));

DDUMP(Atoi64(""));
DDUMP(Atoi64("x"));
DDUMP(Atoi64("123"));
DDUMP(Atoi64("-123"));

CheckLogEtalon();
}
8 changes: 8 additions & 0 deletions autotest/Atoi/Etalon.log
Expand Up @@ -56,3 +56,11 @@ e = (1,1): number is too big
CParser("123456123").ReadNumber64() = 123456123
CParser("123456123").ReadNumber64(8) = 21912659
CParser("123456123").ReadNumber64(16) = 591749411
Atoi("") = 0
Atoi("x") = 0
Atoi("123") = 123
Atoi("-123") = -123
Atoi64("") = 0
Atoi64("x") = 0
Atoi64("123") = 123
Atoi64("-123") = -123
14 changes: 14 additions & 0 deletions uppsrc/Core/Convert.cpp
Expand Up @@ -133,6 +133,13 @@ int ScanInt(const char *ptr)
return ScanInt<char, byte, uint32, int, 10>(x, ptr, overflow) && !overflow ? x : Null;
}

int Atoi(const char *ptr)
{
int x;
bool overflow = false;
return ScanInt<char, byte, uint32, int, 10>(x, ptr, overflow) && !overflow ? x : 0;
}

int64 ScanInt64(const char *ptr, const char **endptr)
{
int64 x;
Expand All @@ -150,6 +157,13 @@ int64 ScanInt64(const char *ptr)
return ScanInt<char, byte, uint64, int64, 10>(x, ptr, overflow) && !overflow ? x : Null;
}

int64 Atoi64(const char *ptr)
{
int64 x;
bool overflow = false;
return ScanInt<char, byte, uint64, int64, 10>(x, ptr, overflow) && !overflow ? x : 0;
}

Value StrIntValue(const char *s)
{
if(s && *s) {
Expand Down
3 changes: 3 additions & 0 deletions uppsrc/Core/Convert.h
Expand Up @@ -17,6 +17,9 @@ int64 ScanInt64(const char *ptr, const char **endptr, int radix);
int64 ScanInt64(const char *ptr, const char **endptr);
int64 ScanInt64(const char *ptr);

int Atoi(const char *ptr);
int64 Atoi64(const char *ptr);

double ScanDouble(const char *ptr, const char **endptr, bool accept_comma);
double ScanDouble(const wchar *ptr, const wchar **endptr, bool accept_comma);
double ScanDouble(const char *ptr, const char **endptr);
Expand Down

0 comments on commit 0cea273

Please sign in to comment.