Browse Source

string to int: Ignore whitespaces at beginning

testing/bash_completion_debugging
Martchus 5 years ago
parent
commit
045c713cf5
  1. 7
      conversion/stringconversion.h
  2. 18
      tests/conversiontests.cpp

7
conversion/stringconversion.h

@ -366,6 +366,8 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value
{
auto i = string.begin();
auto end = string.end();
for (; i != end && *i == ' '; ++i)
;
if (i == end) {
return 0;
}
@ -440,6 +442,11 @@ IntegralType stringToNumber(const CharType *string, unsigned char base = 10)
if (!*string) {
return 0;
}
for (; *string && *string == ' '; ++string)
;
if (!*string) {
return 0;
}
const bool negative = (*string == '-');
if (negative) {
++string;

18
tests/conversiontests.cpp

@ -249,16 +249,18 @@ void ConversionTests::testStringConversions()
}
}
// stringToNumber() with leading zeroes and different types
int32 res = stringToNumber<int32, string>("01");
CPPUNIT_ASSERT_EQUAL(1, res);
res = stringToNumber<int32, wstring>(L"01");
CPPUNIT_ASSERT_EQUAL(1, res);
res = stringToNumber<int32, u16string>(u"01");
CPPUNIT_ASSERT_EQUAL(1, res);
// stringToNumber() with spaces at the beginning, leading zeroes and different types
CPPUNIT_ASSERT_EQUAL(1, stringToNumber<int32>("01"));
CPPUNIT_ASSERT_EQUAL(1, stringToNumber<int32>(L"01"s));
CPPUNIT_ASSERT_EQUAL(1, stringToNumber<int32>(u"01"s));
CPPUNIT_ASSERT_EQUAL(-23, stringToNumber<int32>(" - 023"s));
CPPUNIT_ASSERT_EQUAL(1u, stringToNumber<uint32>("01"));
CPPUNIT_ASSERT_EQUAL(1u, stringToNumber<uint32>(L"01"s));
CPPUNIT_ASSERT_EQUAL(1u, stringToNumber<uint32>(u"01"s));
CPPUNIT_ASSERT_EQUAL(23u, stringToNumber<uint32>(" 023"s));
// interpretIntegerAsString()
CPPUNIT_ASSERT(interpretIntegerAsString<uint32>(0x54455354) == "TEST");
CPPUNIT_ASSERT_EQUAL("TEST"s, interpretIntegerAsString<uint32>(0x54455354));
// splitString() / joinStrings()
string splitJoinTest = joinStrings(splitString<vector<string>>(",a,,ab,ABC,s"s, ","s, EmptyPartsTreat::Keep), " "s, false, "("s, ")"s);

Loading…
Cancel
Save