Browse Source

Misc minor string optimisations.

tags/2021-05-28
jules 12 years ago
parent
commit
3a47baf9b9
7 changed files with 84 additions and 89 deletions
  1. +20
    -0
      modules/juce_core/text/juce_CharacterFunctions.h
  2. +16
    -44
      modules/juce_core/text/juce_String.cpp
  3. +0
    -1
      modules/juce_core/text/juce_String.h
  4. +20
    -21
      modules/juce_core/text/juce_StringPool.cpp
  5. +2
    -2
      modules/juce_graphics/colour/juce_Colour.cpp
  6. +3
    -16
      modules/juce_graphics/colour/juce_Colour.h
  7. +23
    -5
      modules/juce_gui_basics/components/juce_Component.cpp

+ 20
- 0
modules/juce_core/text/juce_CharacterFunctions.h View File

@@ -280,6 +280,26 @@ public:
return isNeg ? -v : v;
}
template <typename ResultType>
struct HexParser
{
template <typename CharPointerType>
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. */


+ 16
- 44
modules/juce_core/text/juce_String.cpp View File

@@ -1825,55 +1825,27 @@ double String::getDoubleValue() const noexcept
static const char hexDigits[] = "0123456789abcdef";
template <typename Type>
struct HexConverter
static String hexToString (Type v)
{
static String hexToString (Type v)
{
char buffer[32];
char* const end = buffer + 32;
char* t = end;
*--t = 0;
do
{
*--t = hexDigits [(int) (v & 15)];
v >>= 4;
} while (v != 0);
String::CharPointerType::CharType buffer[32];
String::CharPointerType::CharType* const end = buffer + numElementsInArray (buffer) - 1;
String::CharPointerType::CharType* t = end;
*t-- = 0;
return String (t, (size_t) (end - t) - 1);
}
static Type stringToHex (String::CharPointerType t) noexcept
do
{
Type result = 0;
*--t = hexDigits [(int) (v & 15)];
v >>= 4;
while (! t.isEmpty())
{
const int hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance());
} while (v != 0);
if (hexValue >= 0)
result = (result << 4) | hexValue;
}
return result;
}
};
String String::toHexString (const int number)
{
return HexConverter <unsigned int>::hexToString ((unsigned int) number);
return String (String::CharPointerType (t),
String::CharPointerType (end));
}
String String::toHexString (const int64 number)
{
return HexConverter <uint64>::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<int> ::stringToHex (text); }
int64 String::getHexValue64() const noexcept { return HexConverter<int64>::stringToHex (text); }
int String::getHexValue32() const noexcept { return CharacterFunctions::HexParser<int> ::parse (text); }
int64 String::getHexValue64() const noexcept { return CharacterFunctions::HexParser<int64>::parse (text); }
//==============================================================================
String String::createStringFromData (const void* const unknownData, const int size)


+ 0
- 1
modules/juce_core/text/juce_String.h View File

@@ -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;


+ 20
- 21
modules/juce_core/text/juce_StringPool.cpp View File

@@ -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;
const String& startString = strings.getReference (start);
if (halfway == start)
{
if (startString.compare (newString) < 0)
++start;
if (startString == newString)
return startString.getCharPointer();
strings.insert (start, newString);
return strings.getReference (start).getCharPointer();
}
const int halfway = (start + end) >> 1;
const int comp = strings.getReference (halfway).compare (newString);
if (halfway == start)
{
if (startString.compare (newString) < 0)
++start;
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;
}
}
}


+ 2
- 2
modules/juce_graphics/colour/juce_Colour.cpp View File

@@ -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<int>::parse (encodedColourString.text));
}
String Colour::toDisplayString (const bool includeAlphaValue) const


+ 3
- 16
modules/juce_graphics/colour/juce_Colour.h View File

@@ -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;


+ 23
- 5
modules/juce_gui_basics/components/juce_Component.cpp View File

@@ -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;
}
//==============================================================================


Loading…
Cancel
Save