26 static const int minNumberOfStringsForGarbageCollection = 300;
27 static const uint32 garbageCollectionInterval = 30000;
41 static int compareStrings (
const String& s1,
const String& s2) noexcept {
return s1.
compare (s2); }
50 const int c1 = s1 < string1.end ? (int) s1.
getAndAdvance() : 0;
51 const int c2 = (int) s2.getAndAdvance();
52 const int diff = c1 - c2;
54 if (diff != 0)
return diff < 0 ? -1 : 1;
61 template <
typename NewStringType>
65 int end = strings.
size();
70 const int startComp = compareStrings (newString, startString);
75 const int halfway = (start + end) / 2;
86 const int halfwayComp = compareStrings (newString, halfwayString);
97 strings.
insert (start, newString);
103 if (newString ==
nullptr || *newString == 0)
107 garbageCollectIfNeeded();
113 if (start.
isEmpty() || start == end)
117 garbageCollectIfNeeded();
127 garbageCollectIfNeeded();
128 return addPooledString (strings, newString.
text);
137 garbageCollectIfNeeded();
138 return addPooledString (strings, newString);
141 void StringPool::garbageCollectIfNeeded()
143 if (strings.
size() > minNumberOfStringsForGarbageCollection
152 for (
int i = strings.
size(); --i >= 0;)
void garbageCollect()
Scans the pool, and removes any strings that are unreferenced.
A simple class for holding temporary references to a string literal or String.
static StringPool & getGlobalPool() noexcept
Returns a shared global pool which is used for things like Identifiers, XML parsing.
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
bool isEmpty() const noexcept
Returns true if this pointer is pointing to a null character.
static uint32 getApproximateMillisecondCounter() noexcept
Less-accurate but faster version of getMillisecondCounter().
StringPool() noexcept
Creates an empty pool.
void insert(int indexToInsertAt, ParameterType newElement)
Inserts a new element into the array at a given position.
bool isEmpty() const noexcept
Returns true if the string is empty.
A StringPool holds a set of shared strings, which reduces storage overheads and improves comparison s...
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
bool isEmpty() const noexcept
Returns true if the string contains no characters.
String::CharPointerType text
The text that is referenced.
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in...
int compare(const CharPointer other) const noexcept
Compares this string with another one.
String getPooledString(const String &original)
Returns a pointer to a shared copy of the string that is passed in.
int compare(const String &other) const noexcept
Case-sensitive comparison with another string.
Automatically locks and unlocks a mutex object.
void remove(int indexToRemove)
Removes an element from the array.
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...