cmStringAlgorithms: Add cmStrToLong and cmStrToULong

This adds the following functions to cmStringAlgorithms:
- `cmStrToLong`: moved from `cmSystemTools::StringToLong`
- `cmStrToULong`: moved from `cmSystemTools::StringToULong`

Overloads of the given functions for `std::string` are added as well.
This commit is contained in:
Sebastian Holtermann
2019-08-10 13:37:34 +02:00
parent 32a7605e69
commit 935fbe0b04
20 changed files with 105 additions and 74 deletions
+2 -2
View File
@@ -10,6 +10,7 @@
#include "cmDuration.h" #include "cmDuration.h"
#include "cmListFileCache.h" #include "cmListFileCache.h"
#include "cmRange.h" #include "cmRange.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmWorkingDirectory.h" #include "cmWorkingDirectory.h"
@@ -110,8 +111,7 @@ void cmCTestMultiProcessHandler::SetTestLoad(unsigned long load)
std::string fake_load_value; std::string fake_load_value;
if (cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING", if (cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING",
fake_load_value)) { fake_load_value)) {
if (!cmSystemTools::StringToULong(fake_load_value.c_str(), if (!cmStrToULong(fake_load_value, &this->FakeLoadForTesting)) {
&this->FakeLoadForTesting)) {
cmSystemTools::Error("Failed to parse fake load value: " + cmSystemTools::Error("Failed to parse fake load value: " +
fake_load_value); fake_load_value);
} }
+2 -3
View File
@@ -529,8 +529,7 @@ int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file,
auto retryDelay = std::chrono::seconds(0); auto retryDelay = std::chrono::seconds(0);
if (!retryDelayString.empty()) { if (!retryDelayString.empty()) {
unsigned long retryDelayValue = 0; unsigned long retryDelayValue = 0;
if (!cmSystemTools::StringToULong(retryDelayString.c_str(), if (!cmStrToULong(retryDelayString, &retryDelayValue)) {
&retryDelayValue)) {
cmCTestLog(this->CTest, WARNING, cmCTestLog(this->CTest, WARNING,
"Invalid value for 'RETRY_DELAY' : " << retryDelayString "Invalid value for 'RETRY_DELAY' : " << retryDelayString
<< std::endl); << std::endl);
@@ -540,7 +539,7 @@ int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file,
} }
unsigned long retryCount = 0; unsigned long retryCount = 0;
if (!retryCountString.empty()) { if (!retryCountString.empty()) {
if (!cmSystemTools::StringToULong(retryCountString.c_str(), &retryCount)) { if (!cmStrToULong(retryCountString, &retryCount)) {
cmCTestLog(this->CTest, WARNING, cmCTestLog(this->CTest, WARNING,
"Invalid value for 'RETRY_DELAY' : " << retryCountString "Invalid value for 'RETRY_DELAY' : " << retryCountString
<< std::endl); << std::endl);
+3 -4
View File
@@ -7,7 +7,7 @@
#include "cmCTestTestHandler.h" #include "cmCTestTestHandler.h"
#include "cmDuration.h" #include "cmDuration.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmSystemTools.h" #include "cmStringAlgorithms.h"
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
@@ -110,15 +110,14 @@ cmCTestGenericHandler* cmCTestTestCommand::InitializeHandler()
unsigned long testLoad; unsigned long testLoad;
const char* ctestTestLoad = this->Makefile->GetDefinition("CTEST_TEST_LOAD"); const char* ctestTestLoad = this->Makefile->GetDefinition("CTEST_TEST_LOAD");
if (this->Values[ctt_TEST_LOAD] && *this->Values[ctt_TEST_LOAD]) { if (this->Values[ctt_TEST_LOAD] && *this->Values[ctt_TEST_LOAD]) {
if (!cmSystemTools::StringToULong(this->Values[ctt_TEST_LOAD], if (!cmStrToULong(this->Values[ctt_TEST_LOAD], &testLoad)) {
&testLoad)) {
testLoad = 0; testLoad = 0;
cmCTestLog(this->CTest, WARNING, cmCTestLog(this->CTest, WARNING,
"Invalid value for 'TEST_LOAD' : " "Invalid value for 'TEST_LOAD' : "
<< this->Values[ctt_TEST_LOAD] << std::endl); << this->Values[ctt_TEST_LOAD] << std::endl);
} }
} else if (ctestTestLoad && *ctestTestLoad) { } else if (ctestTestLoad && *ctestTestLoad) {
if (!cmSystemTools::StringToULong(ctestTestLoad, &testLoad)) { if (!cmStrToULong(ctestTestLoad, &testLoad)) {
testLoad = 0; testLoad = 0;
cmCTestLog(this->CTest, WARNING, cmCTestLog(this->CTest, WARNING,
"Invalid value for 'CTEST_TEST_LOAD' : " << ctestTestLoad "Invalid value for 'CTEST_TEST_LOAD' : " << ctestTestLoad
+1 -2
View File
@@ -2193,8 +2193,7 @@ bool cmCTestTestHandler::SetTestsProperties(
cmListFileContext fc; cmListFileContext fc;
fc.FilePath = triples[i - 3]; fc.FilePath = triples[i - 3];
long line = 0; long line = 0;
if (!cmSystemTools::StringToLong(triples[i - 2].c_str(), if (!cmStrToLong(triples[i - 2], &line)) {
&line)) {
line = 0; line = 0;
} }
fc.Line = line; fc.Line = line;
+5 -5
View File
@@ -754,7 +754,7 @@ bool cmCTest::UpdateCTestConfiguration()
std::string const& testLoad = this->GetCTestConfiguration("TestLoad"); std::string const& testLoad = this->GetCTestConfiguration("TestLoad");
if (!testLoad.empty()) { if (!testLoad.empty()) {
unsigned long load; unsigned long load;
if (cmSystemTools::StringToULong(testLoad.c_str(), &load)) { if (cmStrToULong(testLoad, &load)) {
this->SetTestLoad(load); this->SetTestLoad(load);
} else { } else {
cmCTestLog(this, WARNING, cmCTestLog(this, WARNING,
@@ -1854,7 +1854,7 @@ bool cmCTest::HandleCommandLineArguments(size_t& i,
} }
i++; i++;
long repeat = 1; long repeat = 1;
if (!cmSystemTools::StringToLong(args[i].c_str(), &repeat)) { if (!cmStrToLong(args[i], &repeat)) {
errormsg = errormsg =
"'--repeat-until-fail' given non-integer value '" + args[i] + "'"; "'--repeat-until-fail' given non-integer value '" + args[i] + "'";
return false; return false;
@@ -1868,7 +1868,7 @@ bool cmCTest::HandleCommandLineArguments(size_t& i,
if (this->CheckArgument(arg, "--test-load") && i < args.size() - 1) { if (this->CheckArgument(arg, "--test-load") && i < args.size() - 1) {
i++; i++;
unsigned long load; unsigned long load;
if (cmSystemTools::StringToULong(args[i].c_str(), &load)) { if (cmStrToULong(args[i], &load)) {
this->SetTestLoad(load); this->SetTestLoad(load);
} else { } else {
cmCTestLog(this, WARNING, cmCTestLog(this, WARNING,
@@ -1942,7 +1942,7 @@ bool cmCTest::HandleCommandLineArguments(size_t& i,
i < args.size() - 1) { i < args.size() - 1) {
i++; i++;
long outputSize; long outputSize;
if (cmSystemTools::StringToLong(args[i].c_str(), &outputSize)) { if (cmStrToLong(args[i], &outputSize)) {
this->Impl->TestHandler.SetTestOutputSizePassed(int(outputSize)); this->Impl->TestHandler.SetTestOutputSizePassed(int(outputSize));
} else { } else {
cmCTestLog(this, WARNING, cmCTestLog(this, WARNING,
@@ -1954,7 +1954,7 @@ bool cmCTest::HandleCommandLineArguments(size_t& i,
i < args.size() - 1) { i < args.size() - 1) {
i++; i++;
long outputSize; long outputSize;
if (cmSystemTools::StringToLong(args[i].c_str(), &outputSize)) { if (cmStrToLong(args[i], &outputSize)) {
this->Impl->TestHandler.SetTestOutputSizeFailed(int(outputSize)); this->Impl->TestHandler.SetTestOutputSizeFailed(int(outputSize));
} else { } else {
cmCTestLog(this, WARNING, cmCTestLog(this, WARNING,
+1 -2
View File
@@ -2357,8 +2357,7 @@ bool HandleLockCommand(std::vector<std::string> const& args,
return false; return false;
} }
long scanned; long scanned;
if (!cmSystemTools::StringToLong(args[i].c_str(), &scanned) || if (!cmStrToLong(args[i], &scanned) || scanned < 0) {
scanned < 0) {
std::ostringstream e; std::ostringstream e;
e << "TIMEOUT value \"" << args[i] << "\" is not an unsigned integer."; e << "TIMEOUT value \"" << args[i] << "\" is not an unsigned integer.";
status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str()); status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, e.str());
+1 -1
View File
@@ -560,7 +560,7 @@ void cmGlobalNinjaGenerator::CheckNinjaFeatures()
if (pos != std::string::npos) { if (pos != std::string::npos) {
const char* fv = &this->NinjaVersion[pos + k_DYNDEP_.size()]; const char* fv = &this->NinjaVersion[pos + k_DYNDEP_.size()];
unsigned long dyndep = 0; unsigned long dyndep = 0;
cmSystemTools::StringToULong(fv, &dyndep); cmStrToULong(fv, &dyndep);
if (dyndep == 1) { if (dyndep == 1) {
this->NinjaSupportsDyndeps = true; this->NinjaSupportsDyndeps = true;
} }
+2 -2
View File
@@ -131,7 +131,7 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
} }
parseFromArgV = true; parseFromArgV = true;
argIter++; // move past PARSE_ARGV argIter++; // move past PARSE_ARGV
if (!cmSystemTools::StringToULong(argIter->c_str(), &argvStart)) { if (!cmStrToULong(*argIter, &argvStart)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"PARSE_ARGV index '" + *argIter + "PARSE_ARGV index '" + *argIter +
"' is not an unsigned integer"); "' is not an unsigned integer");
@@ -185,7 +185,7 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
// in the PARSE_ARGV move read the arguments from ARGC and ARGV# // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
std::string argc = this->Makefile->GetSafeDefinition("ARGC"); std::string argc = this->Makefile->GetSafeDefinition("ARGC");
unsigned long count; unsigned long count;
if (!cmSystemTools::StringToULong(argc.c_str(), &count)) { if (!cmStrToULong(argc, &count)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"PARSE_ARGV called with ARGC='" + argc + "PARSE_ARGV called with ARGC='" + argc +
"' that is not an unsigned integer"); "' that is not an unsigned integer");
+1 -1
View File
@@ -34,7 +34,7 @@ static bool stringToId(const char* input, cmPolicies::PolicyID& pid)
} }
} }
long id; long id;
if (!cmSystemTools::StringToLong(input + 3, &id)) { if (!cmStrToLong(input + 3, &id)) {
return false; return false;
} }
if (id >= cmPolicies::CMPCOUNT) { if (id >= cmPolicies::CMPCOUNT) {
+2 -2
View File
@@ -243,7 +243,7 @@ bool cmQtAutoGenInitializer::InitCustomTargets()
this->Verbosity = makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE"); this->Verbosity = makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
if (!this->Verbosity.empty()) { if (!this->Verbosity.empty()) {
unsigned long iVerb = 0; unsigned long iVerb = 0;
if (!cmSystemTools::StringToULong(this->Verbosity.c_str(), &iVerb)) { if (!cmStrToULong(this->Verbosity, &iVerb)) {
// Non numeric verbosity // Non numeric verbosity
this->Verbosity = cmSystemTools::IsOn(this->Verbosity) ? "1" : "0"; this->Verbosity = cmSystemTools::IsOn(this->Verbosity) ? "1" : "0";
} }
@@ -1523,7 +1523,7 @@ void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
static unsigned int CharPtrToUInt(const char* const input) static unsigned int CharPtrToUInt(const char* const input)
{ {
unsigned long tmp = 0; unsigned long tmp = 0;
if (input != nullptr && cmSystemTools::StringToULong(input, &tmp)) { if (input != nullptr && cmStrToULong(input, &tmp)) {
return static_cast<unsigned int>(tmp); return static_cast<unsigned int>(tmp);
} }
return 0; return 0;
+2 -2
View File
@@ -22,7 +22,7 @@ cmQtAutoGenerator::Logger::Logger()
std::string verbose; std::string verbose;
if (cmSystemTools::GetEnv("VERBOSE", verbose) && !verbose.empty()) { if (cmSystemTools::GetEnv("VERBOSE", verbose) && !verbose.empty()) {
unsigned long iVerbose = 0; unsigned long iVerbose = 0;
if (cmSystemTools::StringToULong(verbose.c_str(), &iVerbose)) { if (cmStrToULong(verbose, &iVerbose)) {
SetVerbosity(static_cast<unsigned int>(iVerbose)); SetVerbosity(static_cast<unsigned int>(iVerbose));
} else { } else {
// Non numeric verbosity // Non numeric verbosity
@@ -46,7 +46,7 @@ cmQtAutoGenerator::Logger::~Logger() = default;
void cmQtAutoGenerator::Logger::RaiseVerbosity(std::string const& value) void cmQtAutoGenerator::Logger::RaiseVerbosity(std::string const& value)
{ {
unsigned long verbosity = 0; unsigned long verbosity = 0;
if (cmSystemTools::StringToULong(value.c_str(), &verbosity)) { if (cmStrToULong(value, &verbosity)) {
if (this->Verbosity_ < verbosity) { if (this->Verbosity_ < verbosity) {
this->Verbosity_ = static_cast<unsigned int>(verbosity); this->Verbosity_ = static_cast<unsigned int>(verbosity);
} }
+2 -3
View File
@@ -1582,7 +1582,7 @@ bool cmQtAutoMocUic::Init(cmMakefile* makefile)
BaseConst_.MultiConfig = InfoGetBool("AM_MULTI_CONFIG"); BaseConst_.MultiConfig = InfoGetBool("AM_MULTI_CONFIG");
{ {
unsigned long num = 1; unsigned long num = 1;
if (cmSystemTools::StringToULong(InfoGet("AM_PARALLEL").c_str(), &num)) { if (cmStrToULong(InfoGet("AM_PARALLEL"), &num)) {
num = std::max<unsigned long>(num, 1); num = std::max<unsigned long>(num, 1);
num = std::min<unsigned long>(num, ParallelMax); num = std::min<unsigned long>(num, ParallelMax);
} }
@@ -1630,8 +1630,7 @@ bool cmQtAutoMocUic::Init(cmMakefile* makefile)
// - Qt environment // - Qt environment
{ {
unsigned long qtv = BaseConst_.QtVersionMajor; unsigned long qtv = BaseConst_.QtVersionMajor;
if (cmSystemTools::StringToULong(InfoGet("AM_QT_VERSION_MAJOR").c_str(), if (cmStrToULong(InfoGet("AM_QT_VERSION_MAJOR"), &qtv)) {
&qtv)) {
BaseConst_.QtVersionMajor = static_cast<unsigned int>(qtv); BaseConst_.QtVersionMajor = static_cast<unsigned int>(qtv);
} }
} }
+34
View File
@@ -4,6 +4,8 @@
#include <algorithm> #include <algorithm>
#include <cstdio> #include <cstdio>
#include <errno.h>
#include <stdlib.h>
std::string cmTrimWhitespace(cm::string_view str) std::string cmTrimWhitespace(cm::string_view str)
{ {
@@ -124,3 +126,35 @@ std::string cmCatViews(std::initializer_list<cm::string_view> views)
} }
return result; 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);
}
+9
View File
@@ -190,4 +190,13 @@ inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
} }
} }
/** Converts a string to long. Expects that the whole string is an integer. */
bool cmStrToLong(const char* str, long* value);
bool cmStrToLong(std::string const& str, long* value);
/** Converts a string to unsigned long. Expects that the whole string is an
* integer */
bool cmStrToULong(const char* str, unsigned long* value);
bool cmStrToULong(std::string const& str, unsigned long* value);
#endif #endif
+1 -1
View File
@@ -736,7 +736,7 @@ bool cmStringCommand::HandleRepeatCommand(std::vector<std::string> const& args)
} }
unsigned long times; unsigned long times;
if (!cmSystemTools::StringToULong(args[ArgPos::TIMES].c_str(), &times)) { if (!cmStrToULong(args[ArgPos::TIMES], &times)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"repeat count is not a positive number."); "repeat count is not a positive number.");
return true; return true;
-22
View File
@@ -2848,28 +2848,6 @@ bool cmSystemTools::RepeatedRemoveDirectory(const std::string& dir)
return false; return false;
} }
bool cmSystemTools::StringToLong(const char* str, long* value)
{
errno = 0;
char* endp;
*value = strtol(str, &endp, 10);
return (*endp == '\0') && (endp != str) && (errno == 0);
}
bool cmSystemTools::StringToULong(const char* str, unsigned long* value)
{
errno = 0;
char* endp;
while (isspace(*str)) {
++str;
}
if (*str == '-') {
return false;
}
*value = strtoul(str, &endp, 10);
return (*endp == '\0') && (endp != str) && (errno == 0);
}
std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes) std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes)
{ {
std::string out; std::string out;
-4
View File
@@ -500,10 +500,6 @@ public:
/** Remove a directory; repeat a few times in case of locked files. */ /** Remove a directory; repeat a few times in case of locked files. */
static bool RepeatedRemoveDirectory(const std::string& dir); static bool RepeatedRemoveDirectory(const std::string& dir);
/** Convert string to long. Expected that the whole string is an integer */
static bool StringToLong(const char* str, long* value);
static bool StringToULong(const char* str, unsigned long* value);
/** Encode a string as a URL. */ /** Encode a string as a URL. */
static std::string EncodeURL(std::string const& in, static std::string EncodeURL(std::string const& in,
bool escapeSlashes = true); bool escapeSlashes = true);
+2 -2
View File
@@ -321,7 +321,7 @@ int extract_job_number(int& index, char const* current, char const* next,
unsigned long numJobs = 0; unsigned long numJobs = 0;
if (jobString.empty()) { if (jobString.empty()) {
jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL; jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL;
} else if (cmSystemTools::StringToULong(jobString.c_str(), &numJobs)) { } else if (cmStrToULong(jobString, &numJobs)) {
if (numJobs == 0) { if (numJobs == 0) {
std::cerr std::cerr
<< "The <jobs> value requires a positive integer argument.\n\n"; << "The <jobs> value requires a positive integer argument.\n\n";
@@ -439,7 +439,7 @@ int do_build(int ac, char const* const* av)
jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL; jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL;
} else { } else {
unsigned long numJobs = 0; unsigned long numJobs = 0;
if (cmSystemTools::StringToULong(parallel.c_str(), &numJobs)) { if (cmStrToULong(parallel, &numJobs)) {
if (numJobs == 0) { if (numJobs == 0) {
std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable " std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable "
"requires a positive integer argument.\n\n"; "requires a positive integer argument.\n\n";
+35
View File
@@ -167,5 +167,40 @@ int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not"); assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not");
} }
// ----------------------------------------------------------------------
// Test cmStrToLong
{
long value;
assert_ok(cmStrToLong("1", &value) && value == 1,
"cmStrToLong parses a positive decimal integer.");
assert_ok(cmStrToLong(" 1", &value) && value == 1,
"cmStrToLong parses a decimal integer after whitespace.");
assert_ok(cmStrToLong("-1", &value) && value == -1,
"cmStrToLong parses a negative decimal integer.");
assert_ok(
cmStrToLong(" -1", &value) && value == -1,
"cmStrToLong parses a negative decimal integer after whitespace.");
assert_ok(!cmStrToLong("1x", &value),
"cmStrToLong rejects trailing content.");
}
// ----------------------------------------------------------------------
// Test cmStrToULong
{
unsigned long value;
assert_ok(cmStrToULong("1", &value) && value == 1,
"cmStrToULong parses a decimal integer.");
assert_ok(cmStrToULong(" 1", &value) && value == 1,
"cmStrToULong parses a decimal integer after whitespace.");
assert_ok(!cmStrToULong("-1", &value),
"cmStrToULong rejects a negative number.");
assert_ok(!cmStrToULong(" -1", &value),
"cmStrToULong rejects a negative number after whitespace.");
assert_ok(!cmStrToULong("1x", &value),
"cmStrToULong rejects trailing content.");
}
return failed; return failed;
} }
-16
View File
@@ -94,21 +94,5 @@ int testSystemTools(int /*unused*/, char* /*unused*/ [])
cmPassed("cmSystemTools::strverscmp working"); cmPassed("cmSystemTools::strverscmp working");
} }
// ----------------------------------------------------------------------
// Test cmSystemTools::StringToULong
{
unsigned long value;
cmAssert(cmSystemTools::StringToULong("1", &value) && value == 1,
"StringToULong parses a decimal integer.");
cmAssert(cmSystemTools::StringToULong(" 1", &value) && value == 1,
"StringToULong parses a decimal integer after whitespace.");
cmAssert(!cmSystemTools::StringToULong("-1", &value),
"StringToULong rejects a negative number.");
cmAssert(!cmSystemTools::StringToULong(" -1", &value),
"StringToULong rejects a negative number after whitespace.");
cmAssert(!cmSystemTools::StringToULong("1x", &value),
"StringToULong rejects trailing content.");
}
return failed; return failed;
} }