mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-28 23:15:52 +00:00
Tidy up String conversions
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
Portions of this file are from JUCE.
|
||||
Copyright (c) 2013 - Raw Material Software Ltd.
|
||||
Please visit http://www.juce.com
|
||||
Please visit http://www.beast.com
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
@@ -131,8 +131,8 @@ public:
|
||||
if (start.getAddress() == nullptr || start.isEmpty())
|
||||
return getEmpty();
|
||||
|
||||
const size_t numBytes = (size_t)( reinterpret_cast<const char*> (end.getAddress())
|
||||
- reinterpret_cast<const char*> (start.getAddress()));
|
||||
const size_t numBytes = (size_t) (reinterpret_cast<const char*> (end.getAddress())
|
||||
- reinterpret_cast<const char*> (start.getAddress()));
|
||||
const CharPointerType dest (createUninitialisedBytes (numBytes + sizeof (CharType)));
|
||||
memcpy (dest.getAddress(), start, numBytes);
|
||||
dest.getAddress()[numBytes / sizeof (CharType)] = 0;
|
||||
@@ -358,88 +358,54 @@ String String::charToString (const beast_wchar character)
|
||||
//==============================================================================
|
||||
namespace NumberToStringConverters
|
||||
{
|
||||
template <typename Type>
|
||||
static inline char* printDigits (char* t, Type v) noexcept
|
||||
{
|
||||
*--t = 0;
|
||||
|
||||
do
|
||||
{
|
||||
*--t = '0' + (char) (v % 10);
|
||||
v /= 10;
|
||||
|
||||
} while (v > 0);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
// pass in a pointer to the END of a buffer..
|
||||
static char* numberToString (char* t, const int64 n) noexcept
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
*--t = 0;
|
||||
uint64 v = static_cast <uint64> (n);
|
||||
|
||||
do
|
||||
{
|
||||
*--t = (char) ('0' + (int) (v % 10));
|
||||
v /= 10;
|
||||
|
||||
}
|
||||
while (v > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
*--t = 0;
|
||||
uint64 v = ((uint64)(-(n + 1)) + 1);
|
||||
|
||||
do
|
||||
{
|
||||
*--t = (char) ('0' + (int) (v % 10));
|
||||
v /= 10;
|
||||
|
||||
}
|
||||
while (v > 0);
|
||||
|
||||
*--t = '-';
|
||||
}
|
||||
return printDigits (t, static_cast<uint64> (n));
|
||||
|
||||
// NB: this needs to be careful not to call -std::numeric_limits<int64>::min(),
|
||||
// which has undefined behaviour
|
||||
t = printDigits (t, static_cast<uint64> (-(n + 1)) + 1);
|
||||
*--t = '-';
|
||||
return t;
|
||||
}
|
||||
|
||||
static char* numberToString (char* t, uint64 v) noexcept
|
||||
{
|
||||
*--t = 0;
|
||||
|
||||
do
|
||||
{
|
||||
*--t = (char) ('0' + (int) (v % 10));
|
||||
v /= 10;
|
||||
|
||||
} while (v > 0);
|
||||
|
||||
return t;
|
||||
return printDigits (t, v);
|
||||
}
|
||||
|
||||
static char* numberToString (char* t, const int n) noexcept
|
||||
{
|
||||
if (n == (int) 0x80000000) // (would cause an overflow)
|
||||
return numberToString (t, (int64) n);
|
||||
|
||||
*--t = 0;
|
||||
int v = abs (n);
|
||||
|
||||
do
|
||||
{
|
||||
*--t = (char) ('0' + (v % 10));
|
||||
v /= 10;
|
||||
|
||||
} while (v > 0);
|
||||
|
||||
if (n < 0)
|
||||
*--t = '-';
|
||||
if (n > 0)
|
||||
return printDigits (t, static_cast<unsigned int> (n));
|
||||
|
||||
// NB: this needs to be careful not to call -std::numeric_limits<int>::min(),
|
||||
// which has undefined behaviour
|
||||
t = printDigits (t, static_cast<unsigned int> (-(n + 1)) + 1);
|
||||
*--t = '-';
|
||||
return t;
|
||||
}
|
||||
|
||||
static char* numberToString (char* t, unsigned int v) noexcept
|
||||
{
|
||||
*--t = 0;
|
||||
|
||||
do
|
||||
{
|
||||
*--t = (char) ('0' + (v % 10));
|
||||
v /= 10;
|
||||
|
||||
} while (v > 0);
|
||||
|
||||
return t;
|
||||
return printDigits (t, v);
|
||||
}
|
||||
|
||||
static char* doubleToString (char* buffer, const int numChars, double n, int numDecPlaces, size_t& len) noexcept
|
||||
@@ -492,7 +458,6 @@ namespace NumberToStringConverters
|
||||
char buffer [32];
|
||||
char* const end = buffer + numElementsInArray (buffer);
|
||||
char* const start = numberToString (end, number);
|
||||
|
||||
return StringHolder::createFromFixedLength (start, (size_t) (end - start - 1));
|
||||
}
|
||||
|
||||
@@ -2094,7 +2059,9 @@ String String::fromUTF8 (const char* const buffer, int bufferSizeBytes)
|
||||
class StringTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
StringTests() : UnitTest ("String", "beast") { }
|
||||
StringTests() : UnitTest ("String", "beast")
|
||||
{
|
||||
}
|
||||
|
||||
template <class CharPointerType>
|
||||
struct TestUTFConversion
|
||||
@@ -2236,6 +2203,10 @@ public:
|
||||
expect (String ((int64) -1234).getLargeIntValue() == -1234);
|
||||
expect (String (-1234.56).getDoubleValue() == -1234.56);
|
||||
expect (String (-1234.56f).getFloatValue() == -1234.56f);
|
||||
expect (String (std::numeric_limits<int>::max()).getIntValue() == std::numeric_limits<int>::max());
|
||||
expect (String (std::numeric_limits<int>::min()).getIntValue() == std::numeric_limits<int>::min());
|
||||
expect (String (std::numeric_limits<int64>::max()).getLargeIntValue() == std::numeric_limits<int64>::max());
|
||||
expect (String (std::numeric_limits<int64>::min()).getLargeIntValue() == std::numeric_limits<int64>::min());
|
||||
expect (("xyz" + s).getTrailingIntValue() == s.getIntValue());
|
||||
expect (s.getHexValue32() == 0x12345678);
|
||||
expect (s.getHexValue64() == (int64) 0x12345678);
|
||||
@@ -2418,4 +2389,5 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
static StringTests stringTests;
|
||||
static StringTests stringUnitTests;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user