26static const int minNumberOfStringsForGarbageCollection = 300;
27static const uint32 garbageCollectionInterval = 30000;
34 StartEndString (String::CharPointerType s, String::CharPointerType e) noexcept : start (s), end (e) {}
35 operator String()
const {
return String (start, end); }
37 String::CharPointerType start, end;
40static int compareStrings (
const String& s1,
const String& s2)
noexcept {
return s1.compare (s2); }
41static int compareStrings (
CharPointer_UTF8 s1,
const String& s2)
noexcept {
return s1.compare (s2.getCharPointer()); }
43static int compareStrings (
const StartEndString& string1,
const String& string2)
noexcept
45 String::CharPointerType s1 (string1.start), s2 (string2.getCharPointer());
49 const int c1 = s1 < string1.end ? (int) s1.getAndAdvance() : 0;
50 const int c2 = (int) s2.getAndAdvance();
51 const int diff = c1 - c2;
53 if (diff != 0)
return diff < 0 ? -1 : 1;
60template <
typename NewStringType>
64 int end = strings.size();
68 const String& startString = strings.getReference (start);
69 const int startComp = compareStrings (newString, startString);
74 const int halfway = (start + end) / 2;
84 const String& halfwayString = strings.getReference (halfway);
85 const int halfwayComp = compareStrings (newString, halfwayString);
96 strings.insert (start, newString);
97 return strings.getReference (start);
102 if (newString ==
nullptr || *newString == 0)
105 const ScopedLock sl (lock);
106 garbageCollectIfNeeded();
112 if (start.isEmpty() || start == end)
115 const ScopedLock sl (lock);
116 garbageCollectIfNeeded();
117 return addPooledString (strings, StartEndString (start, end));
125 const ScopedLock sl (lock);
126 garbageCollectIfNeeded();
127 return addPooledString (strings, newString.
text);
135 const ScopedLock sl (lock);
136 garbageCollectIfNeeded();
137 return addPooledString (strings, newString);
140void StringPool::garbageCollectIfNeeded()
142 if (strings.size() > minNumberOfStringsForGarbageCollection
149 const ScopedLock sl (lock);
151 for (
int i = strings.size(); --i >= 0;)
152 if (strings.getReference (i).getReferenceCount() == 1)
String getPooledString(const String &original)
static StringPool & getGlobalPool() noexcept
String::CharPointerType text
bool isEmpty() const noexcept
bool isEmpty() const noexcept
static uint32 getApproximateMillisecondCounter() noexcept