diff options
author | Michi Henning <michi@zeroc.com> | 2002-07-08 06:48:37 +0000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2002-07-08 06:48:37 +0000 |
commit | 36c02756246482fa862ee6547ef223d1ab4e4d2a (patch) | |
tree | 559cead4f19cdf0cf27776a08e8b845198410892 /cpp | |
parent | Fixed Windows side of things for stringToInt64(). (diff) | |
download | ice-36c02756246482fa862ee6547ef223d1ab4e4d2a.tar.bz2 ice-36c02756246482fa862ee6547ef223d1ab4e4d2a.tar.xz ice-36c02756246482fa862ee6547ef223d1ab4e4d2a.zip |
Final tidy-up of stringToInt64().
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/IceUtil/InputUtil.h | 39 | ||||
-rw-r--r-- | cpp/src/IceUtil/InputUtil.cpp | 13 | ||||
-rw-r--r-- | cpp/test/IceUtil/inputUtil/Client.cpp | 3 |
3 files changed, 48 insertions, 7 deletions
diff --git a/cpp/include/IceUtil/InputUtil.h b/cpp/include/IceUtil/InputUtil.h index 9f9f47a80fb..442206c2e5f 100644 --- a/cpp/include/IceUtil/InputUtil.h +++ b/cpp/include/IceUtil/InputUtil.h @@ -30,7 +30,46 @@ namespace IceUtil # error "Unsupported operating system or platform!" #endif +// +// strToInt64() is drop-in replacement for UNIX strtoll() +// ICE_UTIL_API Int64 strToInt64(const char*, char**, int); + +// +// stringToInt64 converts a string into a signed 64-bit integer. +// +// bool stringToInt64(const std::string& stringToParse, Int64& result, std::string::size_type& pos); +// +// Semantics: +// +// - Ignore leading and trailing whitespace +// +// - If the string starts with '0', parse as octal +// +// - If the string starts with "0x" or "0X", parse as hexadecimal +// +// - Otherwise, parse as decimal +// +// - return value == true indicates a successful conversion and result contains the converted value +// +// - if pos == string::npos, there are no trailing non-whitespace characters following the converted string +// +// - if pos != string::npos, there are trailing non-whitespace characters following the converted string; +// pos indicates the first such character +// +// - return value == false indicates an unsuccessful conversion: +// +// - result == 0 indicates that no digits were available for conversion +// +// - result == INT64MIN or result == INT64MAX indicate underflow or overflow. +// +// - if pos == string::npos, the string did not contain trailing non-whitespace characters +// +// - if pos != string::npos, the string contained trailing non-whitespace characters following the +// digits and pos indicates the first such character. (Note that all digits up to the first +// non-digit, non-whitespace character are consumed, regardless of how far into the digit string +// an overflow is detected.) +// ICE_UTIL_API bool stringToInt64(const std::string&, Int64&, std::string::size_type&); } diff --git a/cpp/src/IceUtil/InputUtil.cpp b/cpp/src/IceUtil/InputUtil.cpp index 3070e7e72e8..551758c028a 100644 --- a/cpp/src/IceUtil/InputUtil.cpp +++ b/cpp/src/IceUtil/InputUtil.cpp @@ -97,26 +97,25 @@ static const char digitVal[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0' - '9' 100, 100, 100, 100, 100, 100, 100, // punctuation - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J' - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T' - 30, 31, 32, 33, 34, 35 // 'U' - 'Z' + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J' + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T' + 30, 31, 32, 33, 34, 35 // 'U' - 'Z' }; Int64 result = 0; bool overflow = false; -const __int64 limit = INT64MAX; while(*s && validDigits.find_first_of(toupper(*s)) != validDigits.npos) { if(!overflow) { int digit = digitVal[toupper(*s++) - '0']; assert(digit != 100); - if(result < limit / base) + if(result < INT64MAX / base) { result *= base; result += digit; } - else if((digit <= limit % base) || (sign == -1 && digit == limit % base + 1)) + else if((digit <= INT64MAX % base) || (sign == -1 && digit == INT64MAX % base + 1)) { result *= base; result += digit; @@ -170,7 +169,7 @@ stringToInt64(const string& stringToParse, Int64& result, string::size_type& pos ++j; } // j now points at last non-whitespace char - string nonWhite(i, j.base()); // nonWhite has leading and trailing whitespace stripped + string nonWhite(i, j.base()); // nonWhite has trailing whitespace stripped errno = 0; const char* startp = nonWhite.c_str(); diff --git a/cpp/test/IceUtil/inputUtil/Client.cpp b/cpp/test/IceUtil/inputUtil/Client.cpp index 28bc6534d15..40a5a09a5a9 100644 --- a/cpp/test/IceUtil/inputUtil/Client.cpp +++ b/cpp/test/IceUtil/inputUtil/Client.cpp @@ -94,6 +94,9 @@ main(int, char**) b = stringToInt64("-9223372036854775809Q", result, pos); test(!b && result == INT64MIN && pos == 20); + b = stringToInt64("-9223372036854775809999Q", result, pos); + test(!b && result == INT64MIN && pos == 23); + cout << "ok" << endl; return EXIT_SUCCESS; |