mirror of
https://github.com/Kitware/CMake.git
synced 2025-12-31 10:50:16 -06:00
cmStringAlgorithms: Add functions to parse strings to long long integers
This commit is contained in:
@@ -250,6 +250,38 @@ bool cmStrToULong(std::string const& str, unsigned long* value)
|
||||
return cmStrToULong(str.c_str(), value);
|
||||
}
|
||||
|
||||
bool cmStrToLongLong(const char* str, long long* value)
|
||||
{
|
||||
errno = 0;
|
||||
char* endp;
|
||||
*value = strtoll(str, &endp, 10);
|
||||
return (*endp == '\0') && (endp != str) && (errno == 0);
|
||||
}
|
||||
|
||||
bool cmStrToLongLong(std::string const& str, long long* value)
|
||||
{
|
||||
return cmStrToLongLong(str.c_str(), value);
|
||||
}
|
||||
|
||||
bool cmStrToULongLong(const char* str, unsigned long long* value)
|
||||
{
|
||||
errno = 0;
|
||||
char* endp;
|
||||
while (cmIsSpace(*str)) {
|
||||
++str;
|
||||
}
|
||||
if (*str == '-') {
|
||||
return false;
|
||||
}
|
||||
*value = strtoull(str, &endp, 10);
|
||||
return (*endp == '\0') && (endp != str) && (errno == 0);
|
||||
}
|
||||
|
||||
bool cmStrToULongLong(std::string const& str, unsigned long long* value)
|
||||
{
|
||||
return cmStrToULongLong(str.c_str(), value);
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
std::size_t getJoinedLength(Range const& rng, cm::string_view separator)
|
||||
{
|
||||
|
||||
@@ -303,3 +303,13 @@ bool cmStrToLong(std::string const& str, long* value);
|
||||
* integer */
|
||||
bool cmStrToULong(const char* str, unsigned long* value);
|
||||
bool cmStrToULong(std::string const& str, unsigned long* value);
|
||||
|
||||
/** Converts a string to long long. Expects that the whole string
|
||||
* is an integer */
|
||||
bool cmStrToLongLong(const char* str, long long* value);
|
||||
bool cmStrToLongLong(std::string const& str, long long* value);
|
||||
|
||||
/** Converts a string to unsigned long long. Expects that the whole string
|
||||
* is an integer */
|
||||
bool cmStrToULongLong(const char* str, unsigned long long* value);
|
||||
bool cmStrToULongLong(std::string const& str, unsigned long long* value);
|
||||
|
||||
@@ -226,6 +226,41 @@ int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
|
||||
"cmStrToULong rejects trailing content.");
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test cmStrToLongLong
|
||||
{
|
||||
long long value;
|
||||
assert_ok(cmStrToLongLong("1", &value) && value == 1,
|
||||
"cmStrToLongLong parses a positive decimal integer.");
|
||||
assert_ok(cmStrToLongLong(" 1", &value) && value == 1,
|
||||
"cmStrToLongLong parses a decimal integer after whitespace.");
|
||||
|
||||
assert_ok(cmStrToLongLong("-1", &value) && value == -1,
|
||||
"cmStrToLongLong parses a negative decimal integer.");
|
||||
assert_ok(
|
||||
cmStrToLongLong(" -1", &value) && value == -1,
|
||||
"cmStrToLongLong parses a negative decimal integer after whitespace.");
|
||||
|
||||
assert_ok(!cmStrToLongLong("1x", &value),
|
||||
"cmStrToLongLong rejects trailing content.");
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test cmStrToULongLong
|
||||
{
|
||||
unsigned long long value;
|
||||
assert_ok(cmStrToULongLong("1", &value) && value == 1,
|
||||
"cmStrToULongLong parses a decimal integer.");
|
||||
assert_ok(cmStrToULongLong(" 1", &value) && value == 1,
|
||||
"cmStrToULongLong parses a decimal integer after whitespace.");
|
||||
assert_ok(!cmStrToULongLong("-1", &value),
|
||||
"cmStrToULongLong rejects a negative number.");
|
||||
assert_ok(!cmStrToULongLong(" -1", &value),
|
||||
"cmStrToULongLong rejects a negative number after whitespace.");
|
||||
assert_ok(!cmStrToULongLong("1x", &value),
|
||||
"cmStrToULongLong rejects trailing content.");
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test cmStrLen
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user