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.

114 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. StringPool::StringPool() noexcept {}
  21. StringPool::~StringPool() {}
  22. namespace StringPoolHelpers
  23. {
  24. template <class StringType>
  25. const String::CharPointerType 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).getCharPointer();
  36. }
  37. else
  38. {
  39. const String& startString = strings.getReference (start);
  40. if (startString == newString)
  41. return startString.getCharPointer();
  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).getCharPointer();
  49. }
  50. const int comp = strings.getReference (halfway).compare (newString);
  51. if (comp == 0)
  52. return strings.getReference (halfway).getCharPointer();
  53. else if (comp < 0)
  54. start = halfway;
  55. else
  56. end = halfway;
  57. }
  58. }
  59. }
  60. }
  61. const String::CharPointerType StringPool::getPooledString (const String& s)
  62. {
  63. if (s.isEmpty())
  64. return String::empty.getCharPointer();
  65. return StringPoolHelpers::getPooledStringFromArray (strings, s);
  66. }
  67. const String::CharPointerType StringPool::getPooledString (const char* const s)
  68. {
  69. if (s == nullptr || *s == 0)
  70. return String::empty.getCharPointer();
  71. return StringPoolHelpers::getPooledStringFromArray (strings, s);
  72. }
  73. const String::CharPointerType StringPool::getPooledString (const wchar_t* const s)
  74. {
  75. if (s == nullptr || *s == 0)
  76. return String::empty.getCharPointer();
  77. return StringPoolHelpers::getPooledStringFromArray (strings, s);
  78. }
  79. int StringPool::size() const noexcept
  80. {
  81. return strings.size();
  82. }
  83. const String::CharPointerType StringPool::operator[] (const int index) const noexcept
  84. {
  85. return strings [index].getCharPointer();
  86. }
  87. END_JUCE_NAMESPACE