LMMS
Loading...
Searching...
No Matches
juce_StringPool.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27static const uint32 garbageCollectionInterval = 30000;
28
29
31
33{
34 StartEndString (String::CharPointerType s, String::CharPointerType e) noexcept : start (s), end (e) {}
35 operator String() const { return String (start, end); }
36
37 String::CharPointerType start, end;
38};
39
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()); }
42
43static int compareStrings (const StartEndString& string1, const String& string2) noexcept
44{
45 String::CharPointerType s1 (string1.start), s2 (string2.getCharPointer());
46
47 for (;;)
48 {
49 const int c1 = s1 < string1.end ? (int) s1.getAndAdvance() : 0;
50 const int c2 = (int) s2.getAndAdvance();
51 const int diff = c1 - c2;
52
53 if (diff != 0) return diff < 0 ? -1 : 1;
54 if (c1 == 0) break;
55 }
56
57 return 0;
58}
59
60template <typename NewStringType>
61static String addPooledString (Array<String>& strings, const NewStringType& newString)
62{
63 int start = 0;
64 int end = strings.size();
65
66 while (start < end)
67 {
68 const String& startString = strings.getReference (start);
69 const int startComp = compareStrings (newString, startString);
70
71 if (startComp == 0)
72 return startString;
73
74 const int halfway = (start + end) / 2;
75
76 if (halfway == start)
77 {
78 if (startComp > 0)
79 ++start;
80
81 break;
82 }
83
84 const String& halfwayString = strings.getReference (halfway);
85 const int halfwayComp = compareStrings (newString, halfwayString);
86
87 if (halfwayComp == 0)
88 return halfwayString;
89
90 if (halfwayComp > 0)
91 start = halfway;
92 else
93 end = halfway;
94 }
95
96 strings.insert (start, newString);
97 return strings.getReference (start);
98}
99
100String StringPool::getPooledString (const char* const newString)
101{
102 if (newString == nullptr || *newString == 0)
103 return {};
104
105 const ScopedLock sl (lock);
107 return addPooledString (strings, CharPointer_UTF8 (newString));
108}
109
110String StringPool::getPooledString (String::CharPointerType start, String::CharPointerType end)
111{
112 if (start.isEmpty() || start == end)
113 return {};
114
115 const ScopedLock sl (lock);
118}
119
121{
122 if (newString.isEmpty())
123 return {};
124
125 const ScopedLock sl (lock);
127 return addPooledString (strings, newString.text);
128}
129
131{
132 if (newString.isEmpty())
133 return {};
134
135 const ScopedLock sl (lock);
137 return addPooledString (strings, newString);
138}
139
146
148{
149 const ScopedLock sl (lock);
150
151 for (int i = strings.size(); --i >= 0;)
152 if (strings.getReference(i).getReferenceCount() == 1)
153 strings.remove (i);
154
156}
157
163
164} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
pool_t pool
Definition Util.cpp:167
Definition juce_Array.h:56
int size() const noexcept
Definition juce_Array.h:215
void insert(int indexToInsertAt, ParameterType newElement)
Definition juce_Array.h:462
ElementType & getReference(int index) noexcept
Definition juce_Array.h:267
Definition juce_CharPointer_UTF8.h:35
Definition juce_String.h:53
bool isEmpty() const noexcept
Definition juce_String.h:310
String getPooledString(const String &original)
Definition juce_StringPool.cpp:130
StringPool() noexcept
Definition juce_StringPool.cpp:30
CriticalSection lock
Definition juce_StringPool.h:79
Array< String > strings
Definition juce_StringPool.h:78
static StringPool & getGlobalPool() noexcept
Definition juce_StringPool.cpp:158
void garbageCollect()
Definition juce_StringPool.cpp:147
void garbageCollectIfNeeded()
Definition juce_StringPool.cpp:140
uint32 lastGarbageCollectionTime
Definition juce_StringPool.h:80
Definition juce_StringRef.h:62
String::CharPointerType text
Definition juce_StringRef.h:130
bool isEmpty() const noexcept
Definition juce_StringRef.h:101
static uint32 getApproximateMillisecondCounter() noexcept
Definition juce_Time.cpp:261
* e
Definition inflate.c:1404
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
static void c2(register WDL_FFT_COMPLEX *a)
Definition fft.c:270
virtual ASIOError start()=0
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
unsigned int uint32
Definition juce_MathsFunctions.h:45
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Definition juce_RangedDirectoryIterator.h:184
static const uint32 garbageCollectionInterval
Definition juce_StringPool.cpp:27
static String addPooledString(Array< String > &strings, const NewStringType &newString)
Definition juce_StringPool.cpp:61
static const int minNumberOfStringsForGarbageCollection
Definition juce_StringPool.cpp:26
static int compareStrings(const String &s1, const String &s2) noexcept
Definition juce_StringPool.cpp:40
static bool diff(const std::string fn1, const std::string fn2)
Definition playertest.cpp:161
Definition juce_StringPool.cpp:33
StartEndString(String::CharPointerType s, String::CharPointerType e) noexcept
Definition juce_StringPool.cpp:34
String::CharPointerType start
Definition juce_StringPool.cpp:37
String::CharPointerType end
Definition juce_StringPool.cpp:37
typedef int(UZ_EXP MsgFn)()