diff --git a/modules/juce_core/text/juce_CharacterFunctions.h b/modules/juce_core/text/juce_CharacterFunctions.h index d2cdc1208c..bf206a9366 100644 --- a/modules/juce_core/text/juce_CharacterFunctions.h +++ b/modules/juce_core/text/juce_CharacterFunctions.h @@ -280,6 +280,26 @@ public: return isNeg ? -v : v; } + template + struct HexParser + { + template + static ResultType parse (CharPointerType t) noexcept + { + ResultType result = 0; + + while (! t.isEmpty()) + { + const int hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance()); + + if (hexValue >= 0) + result = (result << 4) | hexValue; + } + + return result; + } + }; + //============================================================================== /** Counts the number of characters in a given string, stopping if the count exceeds a specified limit. */ diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index d08ca8f883..a2a53ecd8a 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -1825,55 +1825,27 @@ double String::getDoubleValue() const noexcept static const char hexDigits[] = "0123456789abcdef"; template -struct HexConverter +static String hexToString (Type v) { - static String hexToString (Type v) + String::CharPointerType::CharType buffer[32]; + String::CharPointerType::CharType* const end = buffer + numElementsInArray (buffer) - 1; + String::CharPointerType::CharType* t = end; + *t-- = 0; + + do { - char buffer[32]; - char* const end = buffer + 32; - char* t = end; - *--t = 0; + *--t = hexDigits [(int) (v & 15)]; + v >>= 4; - do - { - *--t = hexDigits [(int) (v & 15)]; - v >>= 4; + } while (v != 0); - } while (v != 0); - - return String (t, (size_t) (end - t) - 1); - } - - static Type stringToHex (String::CharPointerType t) noexcept - { - Type result = 0; - - while (! t.isEmpty()) - { - const int hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance()); - - if (hexValue >= 0) - result = (result << 4) | hexValue; - } - - return result; - } -}; - -String String::toHexString (const int number) -{ - return HexConverter ::hexToString ((unsigned int) number); + return String (String::CharPointerType (t), + String::CharPointerType (end)); } -String String::toHexString (const int64 number) -{ - return HexConverter ::hexToString ((uint64) number); -} - -String String::toHexString (const short number) -{ - return toHexString ((int) (unsigned short) number); -} +String String::toHexString (int number) { return hexToString ((unsigned int) number); } +String String::toHexString (int64 number) { return hexToString ((uint64) number); } +String String::toHexString (short number) { return toHexString ((int) (unsigned short) number); } String String::toHexString (const void* const d, const int size, const int groupSize) { @@ -1903,8 +1875,8 @@ String String::toHexString (const void* const d, const int size, const int group return s; } -int String::getHexValue32() const noexcept { return HexConverter ::stringToHex (text); } -int64 String::getHexValue64() const noexcept { return HexConverter::stringToHex (text); } +int String::getHexValue32() const noexcept { return CharacterFunctions::HexParser ::parse (text); } +int64 String::getHexValue64() const noexcept { return CharacterFunctions::HexParser::parse (text); } //============================================================================== String String::createStringFromData (const void* const unknownData, const int size) diff --git a/modules/juce_core/text/juce_String.h b/modules/juce_core/text/juce_String.h index 6cc9bde8cc..b368058094 100644 --- a/modules/juce_core/text/juce_String.h +++ b/modules/juce_core/text/juce_String.h @@ -950,7 +950,6 @@ public: int getIntValue() const noexcept; /** Reads the value of the string as a decimal number (up to 64 bits in size). - @returns the value of the string as a 64 bit signed base-10 integer. */ int64 getLargeIntValue() const noexcept; diff --git a/modules/juce_core/text/juce_StringPool.cpp b/modules/juce_core/text/juce_StringPool.cpp index 672b3025a2..88ea9a0ffb 100644 --- a/modules/juce_core/text/juce_StringPool.cpp +++ b/modules/juce_core/text/juce_StringPool.cpp @@ -48,33 +48,32 @@ namespace StringPoolHelpers strings.insert (start, newString); return strings.getReference (start).getCharPointer(); } - else + + const String& startString = strings.getReference (start); + + if (startString == newString) + return startString.getCharPointer(); + + const int halfway = (start + end) >> 1; + + if (halfway == start) { - const String& startString = strings.getReference (start); + if (startString.compare (newString) < 0) + ++start; - if (startString == newString) - return startString.getCharPointer(); - - const int halfway = (start + end) >> 1; - - if (halfway == start) - { - if (startString.compare (newString) < 0) - ++start; - - strings.insert (start, newString); - return strings.getReference (start).getCharPointer(); - } - - const int comp = strings.getReference (halfway).compare (newString); - - if (comp == 0) - return strings.getReference (halfway).getCharPointer(); - else if (comp < 0) - start = halfway; - else - end = halfway; + strings.insert (start, newString); + return strings.getReference (start).getCharPointer(); } + + const int comp = strings.getReference (halfway).compare (newString); + + if (comp == 0) + return strings.getReference (halfway).getCharPointer(); + + if (comp < 0) + start = halfway; + else + end = halfway; } } } diff --git a/modules/juce_graphics/colour/juce_Colour.cpp b/modules/juce_graphics/colour/juce_Colour.cpp index dcf9eeb633..d2095ccbea 100644 --- a/modules/juce_graphics/colour/juce_Colour.cpp +++ b/modules/juce_graphics/colour/juce_Colour.cpp @@ -437,9 +437,9 @@ String Colour::toString() const return String::toHexString ((int) argb.getARGB()); } -Colour Colour::fromString (const String& encodedColourString) +Colour Colour::fromString (StringRef encodedColourString) { - return Colour ((uint32) encodedColourString.getHexValue32()); + return Colour ((uint32) CharacterFunctions::HexParser::parse (encodedColourString.text)); } String Colour::toDisplayString (const bool includeAlphaValue) const diff --git a/modules/juce_graphics/colour/juce_Colour.h b/modules/juce_graphics/colour/juce_Colour.h index 6e08e0c7bd..3e2b222b79 100644 --- a/modules/juce_graphics/colour/juce_Colour.h +++ b/modules/juce_graphics/colour/juce_Colour.h @@ -139,37 +139,31 @@ public: //============================================================================== /** Returns the red component of this colour. - @returns a value between 0x00 and 0xff. */ uint8 getRed() const noexcept { return argb.getRed(); } /** Returns the green component of this colour. - @returns a value between 0x00 and 0xff. */ uint8 getGreen() const noexcept { return argb.getGreen(); } /** Returns the blue component of this colour. - @returns a value between 0x00 and 0xff. */ uint8 getBlue() const noexcept { return argb.getBlue(); } /** Returns the red component of this colour as a floating point value. - @returns a value between 0.0 and 1.0 */ float getFloatRed() const noexcept; /** Returns the green component of this colour as a floating point value. - @returns a value between 0.0 and 1.0 */ float getFloatGreen() const noexcept; /** Returns the blue component of this colour as a floating point value. - @returns a value between 0.0 and 1.0 */ float getFloatBlue() const noexcept; @@ -217,21 +211,17 @@ public: Colour withAlpha (float newAlpha) const noexcept; /** Returns a colour that's the same colour as this one, but with a modified alpha value. - The new colour's alpha will be this object's alpha multiplied by the value passed-in. */ Colour withMultipliedAlpha (float alphaMultiplier) const noexcept; //============================================================================== /** Returns a colour that is the result of alpha-compositing a new colour over this one. - - If the foreground colour is semi-transparent, it is blended onto this colour - accordingly. + If the foreground colour is semi-transparent, it is blended onto this colour accordingly. */ Colour overlaidWith (Colour foregroundColour) const noexcept; /** Returns a colour that lies somewhere between this one and another. - If amountOfOther is zero, the result is 100% this colour, if amountOfOther is 1.0, the result is 100% of the other colour. */ @@ -339,21 +329,18 @@ public: //============================================================================== /** Returns an opaque shade of grey. - @param brightness the level of grey to return - 0 is black, 1.0 is white */ static Colour greyLevel (float brightness) noexcept; //============================================================================== /** Returns a stringified version of this colour. - The string can be turned back into a colour using the fromString() method. */ String toString() const; - /** Reads the colour from a string that was created with toString(). - */ - static Colour fromString (const String& encodedColourString); + /** Reads the colour from a string that was created with toString(). */ + static Colour fromString (StringRef encodedColourString); /** Returns the colour as a hex string in the form RRGGBB or AARRGGBB. */ String toDisplayString (bool includeAlphaValue) const; diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index ec9ca53823..18255f00c5 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -184,12 +184,30 @@ struct Component::ComponentHelpers } #endif - static Identifier getColourPropertyId (const int colourId) + static Identifier getColourPropertyId (int colourId) { - String s; - s.preallocateBytes (32); - s << "jcclr_" << String::toHexString (colourId); - return s; + char reversedHex[32]; + char* t = reversedHex; + + for (unsigned int v = (unsigned int) colourId;;) + { + *t++ = "0123456789abcdef" [(int) (v & 15)]; + v >>= 4; + + if (v == 0) + break; + } + + char destBuffer[32]; + char* dest = destBuffer; + memcpy (dest, "jcclr_", 6); + dest += 6; + + while (t > reversedHex) + *dest++ = *--t; + + *dest++ = 0; + return destBuffer; } //==============================================================================