From c313a55f831333fa654a8f861e6660f103595f46 Mon Sep 17 00:00:00 2001 From: jules Date: Sun, 23 Jul 2017 09:39:00 +0100 Subject: [PATCH] Bit of minor modernisation in CharacterFunctions --- .../text/juce_CharacterFunctions.cpp | 9 ++- .../juce_core/text/juce_CharacterFunctions.h | 60 ++++++++++--------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/modules/juce_core/text/juce_CharacterFunctions.cpp b/modules/juce_core/text/juce_CharacterFunctions.cpp index bb75ff170a..cd81957d95 100644 --- a/modules/juce_core/text/juce_CharacterFunctions.cpp +++ b/modules/juce_core/text/juce_CharacterFunctions.cpp @@ -114,15 +114,18 @@ bool CharacterFunctions::isPrintable (const juce_wchar character) noexcept int CharacterFunctions::getHexDigitValue (const juce_wchar digit) noexcept { - unsigned int d = (unsigned int) digit - '0'; + auto d = (unsigned int) (digit - '0'); + if (d < (unsigned int) 10) return (int) d; d += (unsigned int) ('0' - 'a'); + if (d < (unsigned int) 6) return (int) d + 10; d += (unsigned int) ('a' - 'A'); + if (d < (unsigned int) 6) return (int) d + 10; @@ -138,19 +141,23 @@ double CharacterFunctions::mulexp10 (const double value, int exponent) noexcept return 0; const bool negative = (exponent < 0); + if (negative) exponent = -exponent; double result = 1.0, power = 10.0; + for (int bit = 1; exponent != 0; bit <<= 1) { if ((exponent & bit) != 0) { exponent ^= bit; result *= power; + if (exponent == 0) break; } + power *= power; } diff --git a/modules/juce_core/text/juce_CharacterFunctions.h b/modules/juce_core/text/juce_CharacterFunctions.h index 024640a798..68263efd8c 100644 --- a/modules/juce_core/text/juce_CharacterFunctions.h +++ b/modules/juce_core/text/juce_CharacterFunctions.h @@ -135,7 +135,7 @@ public: bool decimalPointFound = false; text = text.findEndOfWhitespace(); - juce_wchar c = *text; + auto c = *text; switch (c) { @@ -165,7 +165,7 @@ public: int digit = (int) text.getAndAdvance() - '0'; if (numSigFigs >= maxSignificantDigits - || ((numSigFigs == 0 && (! decimalPointFound)) && digit == 0)) + || ((numSigFigs == 0 && (! decimalPointFound)) && digit == 0)) continue; *currentCharacter++ = '0' + (char) digit; @@ -184,6 +184,7 @@ public: } c = *text; + if ((c == 'e' || c == 'E') && numSigFigs > 0) { *currentCharacter++ = 'e'; @@ -201,7 +202,7 @@ public: if (currentCharacter == &buffer[bufferSize - 1]) return std::numeric_limits::quiet_NaN(); - int digit = (int) text.getAndAdvance() - '0'; + auto digit = (int) text.getAndAdvance() - '0'; if (digit != 0 || exponentMagnitude != 0) { @@ -230,15 +231,15 @@ public: static IntType getIntValue (const CharPointerType text) noexcept { IntType v = 0; - CharPointerType s (text.findEndOfWhitespace()); - + auto s = text.findEndOfWhitespace(); const bool isNeg = *s == '-'; + if (isNeg) ++s; for (;;) { - const juce_wchar c = s.getAndAdvance(); + auto c = s.getAndAdvance(); if (c >= '0' && c <= '9') v = v * 10 + (IntType) (c - '0'); @@ -259,7 +260,7 @@ public: while (! t.isEmpty()) { - const int hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance()); + auto hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance()); if (hexValue >= 0) result = (result << 4) | hexValue; @@ -311,16 +312,16 @@ public: template static size_t copyWithDestByteLimit (DestCharPointerType& dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept { - typename DestCharPointerType::CharType const* const startAddress = dest.getAddress(); - ssize_t maxBytes = (ssize_t) maxBytesToWrite; + auto startAddress = dest.getAddress(); + auto maxBytes = (ssize_t) maxBytesToWrite; maxBytes -= sizeof (typename DestCharPointerType::CharType); // (allow for a terminating null) for (;;) { - const juce_wchar c = src.getAndAdvance(); - const size_t bytesNeeded = DestCharPointerType::getBytesRequiredFor (c); - + auto c = src.getAndAdvance(); + auto bytesNeeded = DestCharPointerType::getBytesRequiredFor (c); maxBytes -= bytesNeeded; + if (c == 0 || maxBytes < 0) break; @@ -340,7 +341,8 @@ public: { while (--maxChars > 0) { - const juce_wchar c = src.getAndAdvance(); + auto c = src.getAndAdvance(); + if (c == 0) break; @@ -353,7 +355,7 @@ public: /** Compares two characters. */ static inline int compare (juce_wchar char1, juce_wchar char2) noexcept { - if (int diff = static_cast (char1) - static_cast (char2)) + if (auto diff = static_cast (char1) - static_cast (char2)) return diff < 0 ? -1 : 1; return 0; @@ -365,9 +367,9 @@ public: { for (;;) { - const juce_wchar c1 = s1.getAndAdvance(); + auto c1 = s1.getAndAdvance(); - if (int diff = compare (c1, s2.getAndAdvance())) + if (auto diff = compare (c1, s2.getAndAdvance())) return diff; if (c1 == 0) @@ -383,9 +385,9 @@ public: { while (--maxChars >= 0) { - const juce_wchar c1 = s1.getAndAdvance(); + auto c1 = s1.getAndAdvance(); - if (int diff = compare (c1, s2.getAndAdvance())) + if (auto diff = compare (c1, s2.getAndAdvance())) return diff; if (c1 == 0) @@ -407,9 +409,9 @@ public: { for (;;) { - const juce_wchar c1 = s1.getAndAdvance(); + auto c1 = s1.getAndAdvance(); - if (int diff = compareIgnoreCase (c1, s2.getAndAdvance())) + if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance())) return diff; if (c1 == 0) @@ -425,9 +427,9 @@ public: { while (--maxChars >= 0) { - const juce_wchar c1 = s1.getAndAdvance(); + auto c1 = s1.getAndAdvance(); - if (int diff = compareIgnoreCase (c1, s2.getAndAdvance())) + if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance())) return diff; if (c1 == 0) @@ -444,7 +446,7 @@ public: static int indexOf (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept { int index = 0; - const int substringLength = (int) substringToLookFor.length(); + auto substringLength = (int) substringToLookFor.length(); for (;;) { @@ -465,7 +467,7 @@ public: template static CharPointerType1 find (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept { - const int substringLength = (int) substringToLookFor.length(); + auto substringLength = (int) substringToLookFor.length(); while (textToSearch.compareUpTo (substringToLookFor, substringLength) != 0 && ! textToSearch.isEmpty()) @@ -483,7 +485,7 @@ public: { for (;; ++textToSearch) { - const juce_wchar c = *textToSearch; + auto c = *textToSearch; if (c == charToLookFor || c == 0) break; @@ -500,7 +502,7 @@ public: static int indexOfIgnoreCase (CharPointerType1 haystack, const CharPointerType2 needle) noexcept { int index = 0; - const int needleLength = (int) needle.length(); + auto needleLength = (int) needle.length(); for (;;) { @@ -572,13 +574,13 @@ public: the breakCharacters string. */ template - static Type findEndOfToken (Type text, const BreakType breakCharacters, const Type quoteCharacters) + static Type findEndOfToken (Type text, BreakType breakCharacters, Type quoteCharacters) { juce_wchar currentQuoteChar = 0; while (! text.isEmpty()) { - const juce_wchar c = text.getAndAdvance(); + auto c = text.getAndAdvance(); if (currentQuoteChar == 0 && breakCharacters.indexOf (c) >= 0) { @@ -599,5 +601,5 @@ public: } private: - static double mulexp10 (const double value, int exponent) noexcept; + static double mulexp10 (double value, int exponent) noexcept; };