Fix conversion of 0 to string

Return "0" instead of an empty string
fix/windows/stop_console
Martchus 6 years ago
parent f626bdf9c7
commit 787d541974
  1. 10
      conversion/stringbuilder.h
  2. 10
      conversion/stringconversion.h
  3. 5
      tests/conversiontests.cpp

@ -81,9 +81,10 @@ template <class StringType, typename IntegralType, Traits::EnableIf<Traits::Not<
void append(StringType &target, IntegralType number, typename StringType::value_type base = 10)
{
const auto start = target.begin() + target.size();
for(; number; number /= base) {
do {
target.insert(start, digitToChar<typename StringType::value_type>(number % base));
}
number /= base;
} while(number);
}
template <class StringType, typename IntegralType, Traits::EnableIf<Traits::Not<std::is_same<typename StringType::value_type, IntegralType> >, std::is_integral<IntegralType>, std::is_signed<IntegralType> >...>
@ -94,9 +95,10 @@ void append(StringType &target, IntegralType number, typename StringType::value_
number = -number;
}
const auto start = target.begin() + target.size();
for(; number; number /= base) {
do {
target.insert(start, digitToChar<typename StringType::value_type>(number % base));
}
number /= base;
} while(number);
}
template<class StringType, class Tuple, std::size_t N>

@ -258,9 +258,10 @@ StringType numberToString(IntegralType number, typename StringType::value_type b
for(auto n = number; n; n /= base, ++resSize);
StringType res;
res.reserve(resSize);
for(; number; number /= base) {
do {
res.insert(res.begin(), digitToChar<typename StringType::value_type>(number % base));
}
number /= base;
} while(number);
return res;
}
@ -283,9 +284,10 @@ StringType numberToString(IntegralType number, typename StringType::value_type b
for(auto n = number; n; n /= base, ++resSize);
StringType res;
res.reserve(resSize);
for(; number; number /= base) {
do {
res.insert(res.begin(), digitToChar<typename StringType::value_type>(number % base));
}
number /= base;
} while(number);
if(negative) {
res.insert(res.begin(), '-');
}

@ -231,7 +231,9 @@ void ConversionTests::testStringEncodingConversions()
*/
void ConversionTests::testStringConversions()
{
// stringToNumber() / numberToString() with random numbers
// stringToNumber() / numberToString() with zero and random numbers
CPPUNIT_ASSERT_EQUAL(string("0"), numberToString<unsigned int>(0));
CPPUNIT_ASSERT_EQUAL(string("0"), numberToString<signed int>(0));
uniform_int_distribution<int64> randomDistSigned(numeric_limits<int64>::min());
uniform_int_distribution<uint64> randomDistUnsigned(0);
for(byte b = 1; b < 100; ++b) {
@ -310,6 +312,7 @@ void ConversionTests::testStringBuilder()
const tuple<const char *, string, int, const char *> tuple("string1", "string2", 1234, "string3");
CPPUNIT_ASSERT_EQUAL(string("string1string21234string3"), tupleToString(tuple));
CPPUNIT_ASSERT_EQUAL(string("foobarfoo2bar2"), tupleToString(string("foo") % "bar" % string("foo2") % "bar2"));
CPPUNIT_ASSERT_EQUAL(string("v2.3.0"), argsToString("v2.", 3, '.', 0));
// construction of string-tuple and final conversion to string works
CPPUNIT_ASSERT_EQUAL_MESSAGE("result can be passed to any function taking a std::string"s, "123456789"s, functionTakingString("12" % string("34") % '5' % 67 + "89"));

Loading…
Cancel
Save