Fix crash when host does not support midi-in; Add missing file
[juce-lv2.git] / juce / source / src / text / juce_StringPool.cpp
blob72d7631f0a84052ed828caf86ae1cc960e6ce619
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_StringPool.h"
33 //==============================================================================
34 StringPool::StringPool() noexcept {}
35 StringPool::~StringPool() {}
37 namespace StringPoolHelpers
39 template <class StringType>
40 const String::CharPointerType getPooledStringFromArray (Array<String>& strings, StringType newString)
42 int start = 0;
43 int end = strings.size();
45 for (;;)
47 if (start >= end)
49 jassert (start <= end);
50 strings.insert (start, newString);
51 return strings.getReference (start).getCharPointer();
53 else
55 const String& startString = strings.getReference (start);
57 if (startString == newString)
58 return startString.getCharPointer();
60 const int halfway = (start + end) >> 1;
62 if (halfway == start)
64 if (startString.compare (newString) < 0)
65 ++start;
67 strings.insert (start, newString);
68 return strings.getReference (start).getCharPointer();
71 const int comp = strings.getReference (halfway).compare (newString);
73 if (comp == 0)
74 return strings.getReference (halfway).getCharPointer();
75 else if (comp < 0)
76 start = halfway;
77 else
78 end = halfway;
84 const String::CharPointerType StringPool::getPooledString (const String& s)
86 if (s.isEmpty())
87 return String::empty.getCharPointer();
89 return StringPoolHelpers::getPooledStringFromArray (strings, s);
92 const String::CharPointerType StringPool::getPooledString (const char* const s)
94 if (s == nullptr || *s == 0)
95 return String::empty.getCharPointer();
97 return StringPoolHelpers::getPooledStringFromArray (strings, s);
100 const String::CharPointerType StringPool::getPooledString (const wchar_t* const s)
102 if (s == nullptr || *s == 0)
103 return String::empty.getCharPointer();
105 return StringPoolHelpers::getPooledStringFromArray (strings, s);
108 int StringPool::size() const noexcept
110 return strings.size();
113 const String::CharPointerType StringPool::operator[] (const int index) const noexcept
115 return strings [index].getCharPointer();
118 END_JUCE_NAMESPACE