The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_StringPool.h"
  21. //==============================================================================
  22. StringPool::StringPool() throw() {}
  23. StringPool::~StringPool() {}
  24. template <class StringType>
  25. static const juce_wchar* getPooledStringFromArray (Array<String>& strings, StringType newString)
  26. {
  27. int start = 0;
  28. int end = strings.size();
  29. for (;;)
  30. {
  31. if (start >= end)
  32. {
  33. jassert (start <= end);
  34. strings.insert (start, newString);
  35. return strings.getReference (start);
  36. }
  37. else
  38. {
  39. const String& startString = strings.getReference (start);
  40. if (startString == newString)
  41. return startString;
  42. const int halfway = (start + end) >> 1;
  43. if (halfway == start)
  44. {
  45. if (startString.compare (newString) < 0)
  46. ++start;
  47. strings.insert (start, newString);
  48. return strings.getReference (start);
  49. }
  50. const int comp = strings.getReference (halfway).compare (newString);
  51. if (comp == 0)
  52. return strings.getReference (halfway);
  53. else if (comp < 0)
  54. start = halfway;
  55. else
  56. end = halfway;
  57. }
  58. }
  59. }
  60. const juce_wchar* StringPool::getPooledString (const String& s)
  61. {
  62. if (s.isEmpty())
  63. return String::empty;
  64. return getPooledStringFromArray (strings, s);
  65. }
  66. const juce_wchar* StringPool::getPooledString (const char* const s)
  67. {
  68. if (s == 0 || *s == 0)
  69. return String::empty;
  70. return getPooledStringFromArray (strings, s);
  71. }
  72. const juce_wchar* StringPool::getPooledString (const juce_wchar* const s)
  73. {
  74. if (s == 0 || *s == 0)
  75. return String::empty;
  76. return getPooledStringFromArray (strings, s);
  77. }
  78. int StringPool::size() const throw()
  79. {
  80. return strings.size();
  81. }
  82. const juce_wchar* StringPool::operator[] (const int index) const throw()
  83. {
  84. return strings [index];
  85. }
  86. END_JUCE_NAMESPACE