From 08ff5f02b16ca731e45e302599bdb074a9049b8c Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 29 Jul 2013 05:02:19 -0700 Subject: [PATCH] Tidy up String conversions --- modules/beast_core/text/beast_String.cpp | 108 +++++++++-------------- 1 file changed, 40 insertions(+), 68 deletions(-) diff --git a/modules/beast_core/text/beast_String.cpp b/modules/beast_core/text/beast_String.cpp index f55bb510d4..6d010ab40e 100644 --- a/modules/beast_core/text/beast_String.cpp +++ b/modules/beast_core/text/beast_String.cpp @@ -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 (end.getAddress()) - - reinterpret_cast (start.getAddress())); + const size_t numBytes = (size_t) (reinterpret_cast (end.getAddress()) + - reinterpret_cast (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 + 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 (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 (n)); + // NB: this needs to be careful not to call -std::numeric_limits::min(), + // which has undefined behaviour + t = printDigits (t, static_cast (-(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 (n)); + // NB: this needs to be careful not to call -std::numeric_limits::min(), + // which has undefined behaviour + t = printDigits (t, static_cast (-(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 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::max()).getIntValue() == std::numeric_limits::max()); + expect (String (std::numeric_limits::min()).getIntValue() == std::numeric_limits::min()); + expect (String (std::numeric_limits::max()).getLargeIntValue() == std::numeric_limits::max()); + expect (String (std::numeric_limits::min()).getLargeIntValue() == std::numeric_limits::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; +