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.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. StringPool::StringPool() noexcept {}
  22. StringPool::~StringPool() {}
  23. namespace StringPoolHelpers
  24. {
  25. template <class StringType>
  26. String::CharPointerType getPooledStringFromArray (Array<String>& strings,
  27. StringType newString,
  28. const CriticalSection& lock)
  29. {
  30. const ScopedLock sl (lock);
  31. int start = 0;
  32. int end = strings.size();
  33. for (;;)
  34. {
  35. if (start >= end)
  36. {
  37. jassert (start <= end);
  38. strings.insert (start, newString);
  39. return strings.getReference (start).getCharPointer();
  40. }
  41. const String& startString = strings.getReference (start);
  42. if (startString == newString)
  43. return startString.getCharPointer();
  44. const int halfway = (start + end) >> 1;
  45. if (halfway == start)
  46. {
  47. if (startString.compare (newString) < 0)
  48. ++start;
  49. strings.insert (start, newString);
  50. return strings.getReference (start).getCharPointer();
  51. }
  52. const int comp = strings.getReference (halfway).compare (newString);
  53. if (comp == 0)
  54. return strings.getReference (halfway).getCharPointer();
  55. if (comp < 0)
  56. start = halfway;
  57. else
  58. end = halfway;
  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. }