Merge topic 'cmStringAlgorithms_ulong'

935fbe0b04 cmStringAlgorithms: Add cmStrToLong and cmStrToULong

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3681
This commit is contained in:
Kyle Edwards
2019-08-16 18:49:13 +00:00
committed by Kitware Robot
20 changed files with 105 additions and 74 deletions
+34
View File
@@ -4,6 +4,8 @@
#include <algorithm>
#include <cstdio>
#include <errno.h>
#include <stdlib.h>
std::string cmTrimWhitespace(cm::string_view str)
{
@@ -138,3 +140,35 @@ std::string cmCatViews(std::initializer_list<cm::string_view> views)
}
return result;
}
bool cmStrToLong(const char* str, long* value)
{
errno = 0;
char* endp;
*value = strtol(str, &endp, 10);
return (*endp == '\0') && (endp != str) && (errno == 0);
}
bool cmStrToLong(std::string const& str, long* value)
{
return cmStrToLong(str.c_str(), value);
}
bool cmStrToULong(const char* str, unsigned long* value)
{
errno = 0;
char* endp;
while (cmIsSpace(*str)) {
++str;
}
if (*str == '-') {
return false;
}
*value = strtoul(str, &endp, 10);
return (*endp == '\0') && (endp != str) && (errno == 0);
}
bool cmStrToULong(std::string const& str, unsigned long* value)
{
return cmStrToULong(str.c_str(), value);
}