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.

110 lines
3.5KB

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