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.

112 lines
3.7KB

  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. StringPool::StringPool() noexcept {}
  19. StringPool::~StringPool() {}
  20. namespace StringPoolHelpers
  21. {
  22. template <class StringType>
  23. String::CharPointerType getPooledStringFromArray (Array<String>& strings,
  24. StringType newString,
  25. const CriticalSection& lock)
  26. {
  27. const ScopedLock sl (lock);
  28. int start = 0;
  29. int end = strings.size();
  30. for (;;)
  31. {
  32. if (start >= end)
  33. {
  34. jassert (start <= end);
  35. strings.insert (start, newString);
  36. return strings.getReference (start).getCharPointer();
  37. }
  38. else
  39. {
  40. const String& startString = strings.getReference (start);
  41. if (startString == newString)
  42. return startString.getCharPointer();
  43. const int halfway = (start + end) >> 1;
  44. if (halfway == start)
  45. {
  46. if (startString.compare (newString) < 0)
  47. ++start;
  48. strings.insert (start, newString);
  49. return strings.getReference (start).getCharPointer();
  50. }
  51. const int comp = strings.getReference (halfway).compare (newString);
  52. if (comp == 0)
  53. return strings.getReference (halfway).getCharPointer();
  54. else if (comp < 0)
  55. start = halfway;
  56. else
  57. end = halfway;
  58. }
  59. }
  60. }
  61. }
  62. String::CharPointerType StringPool::getPooledString (const String& s)
  63. {
  64. if (s.isEmpty())
  65. return String::empty.getCharPointer();
  66. return StringPoolHelpers::getPooledStringFromArray (strings, s, lock);
  67. }
  68. String::CharPointerType StringPool::getPooledString (const char* const s)
  69. {
  70. if (s == nullptr || *s == 0)
  71. return String::empty.getCharPointer();
  72. return StringPoolHelpers::getPooledStringFromArray (strings, s, lock);
  73. }
  74. String::CharPointerType StringPool::getPooledString (const wchar_t* const s)
  75. {
  76. if (s == nullptr || *s == 0)
  77. return String::empty.getCharPointer();
  78. return StringPoolHelpers::getPooledStringFromArray (strings, s, lock);
  79. }
  80. int StringPool::size() const noexcept
  81. {
  82. return strings.size();
  83. }
  84. String::CharPointerType StringPool::operator[] (const int index) const noexcept
  85. {
  86. return strings [index].getCharPointer();
  87. }