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.

159 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A simple class for holding temporary references to a string literal or String.
  22. Unlike a real String object, the StringRef does not allocate any memory or
  23. take ownership of the strings you give to it - it simply holds a reference to
  24. a string that has been allocated elsewhere.
  25. The main purpose of the class is to be used instead of a const String& as the type
  26. of function arguments where the caller may pass either a string literal or a String
  27. object. This means that when the called uses a string literal, there's no need
  28. for an temporary String object to be allocated, and this cuts down overheads
  29. substantially.
  30. Because the class is simply a wrapper around a pointer, you should always pass
  31. it by value, not by reference.
  32. @code
  33. void myStringFunction1 (const String&);
  34. void myStringFunction2 (StringRef);
  35. myStringFunction1 ("abc"); // Implicitly allocates a temporary String object.
  36. myStringFunction2 ("abc"); // Much faster, as no local allocations are needed.
  37. @endcode
  38. For examples of it in use, see the XmlElement or StringArray classes.
  39. Bear in mind that there are still many cases where it's better to use an argument
  40. which is a const String&. For example if the function stores the string or needs
  41. to internally create a String from the argument, then it's better for the original
  42. argument to already be a String.
  43. @see String
  44. @tags{Core}
  45. */
  46. class JUCE_API StringRef final
  47. {
  48. public:
  49. /** Creates a StringRef from a raw string literal.
  50. The StringRef object does NOT take ownership or copy this data, so you must
  51. ensure that the data does not change during the lifetime of the StringRef.
  52. Note that this pointer cannot be null!
  53. */
  54. StringRef (const char* stringLiteral) noexcept;
  55. /** Creates a StringRef from a raw char pointer.
  56. The StringRef object does NOT take ownership or copy this data, so you must
  57. ensure that the data does not change during the lifetime of the StringRef.
  58. */
  59. StringRef (String::CharPointerType stringLiteral) noexcept;
  60. /** Creates a StringRef from a String.
  61. The StringRef object does NOT take ownership or copy the data from the String,
  62. so you must ensure that the String is not modified or deleted during the lifetime
  63. of the StringRef.
  64. */
  65. StringRef (const String& string) noexcept;
  66. /** Creates a StringRef from a String.
  67. The StringRef object does NOT take ownership or copy the data from the std::string,
  68. so you must ensure that the source string object is not modified or deleted during
  69. the lifetime of the StringRef.
  70. */
  71. StringRef (const std::string& string);
  72. /** Creates a StringRef pointer to an empty string. */
  73. StringRef() noexcept;
  74. //==============================================================================
  75. /** Returns a raw pointer to the underlying string data. */
  76. operator const String::CharPointerType::CharType*() const noexcept { return text.getAddress(); }
  77. /** Returns a pointer to the underlying string data as a char pointer object. */
  78. operator String::CharPointerType() const noexcept { return text; }
  79. /** Returns true if the string is empty. */
  80. bool isEmpty() const noexcept { return text.isEmpty(); }
  81. /** Returns true if the string is not empty. */
  82. bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
  83. /** Returns the number of characters in the string. */
  84. int length() const noexcept { return (int) text.length(); }
  85. /** Retrieves a character by index. */
  86. juce_wchar operator[] (int index) const noexcept { return text[index]; }
  87. /** Compares this StringRef with a String. */
  88. bool operator== (const String& s) const noexcept { return text.compare (s.getCharPointer()) == 0; }
  89. /** Compares this StringRef with a String. */
  90. bool operator!= (const String& s) const noexcept { return text.compare (s.getCharPointer()) != 0; }
  91. /** Compares this StringRef with a String. */
  92. bool operator< (const String& s) const noexcept { return text.compare (s.getCharPointer()) < 0; }
  93. /** Compares this StringRef with a String. */
  94. bool operator<= (const String& s) const noexcept { return text.compare (s.getCharPointer()) <= 0; }
  95. /** Compares this StringRef with a String. */
  96. bool operator> (const String& s) const noexcept { return text.compare (s.getCharPointer()) > 0; }
  97. /** Compares this StringRef with a String. */
  98. bool operator>= (const String& s) const noexcept { return text.compare (s.getCharPointer()) >= 0; }
  99. /** Case-sensitive comparison of two StringRefs. */
  100. bool operator== (StringRef s) const noexcept { return text.compare (s.text) == 0; }
  101. /** Case-sensitive comparison of two StringRefs. */
  102. bool operator!= (StringRef s) const noexcept { return text.compare (s.text) != 0; }
  103. //==============================================================================
  104. /** The text that is referenced. */
  105. String::CharPointerType text;
  106. #if JUCE_STRING_UTF_TYPE != 8 && ! defined (DOXYGEN)
  107. // Sorry, non-UTF8 people, you're unable to take advantage of StringRef, because
  108. // you've chosen a character encoding that doesn't match C++ string literals.
  109. String stringCopy;
  110. #endif
  111. };
  112. //==============================================================================
  113. /** Case-sensitive comparison of two strings. */
  114. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, StringRef string2) noexcept;
  115. /** Case-sensitive comparison of two strings. */
  116. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, StringRef string2) noexcept;
  117. /** Case-sensitive comparison of two strings. */
  118. JUCE_API bool JUCE_CALLTYPE operator< (const String& string1, StringRef string2) noexcept;
  119. /** Case-sensitive comparison of two strings. */
  120. JUCE_API bool JUCE_CALLTYPE operator<= (const String& string1, StringRef string2) noexcept;
  121. /** Case-sensitive comparison of two strings. */
  122. JUCE_API bool JUCE_CALLTYPE operator> (const String& string1, StringRef string2) noexcept;
  123. /** Case-sensitive comparison of two strings. */
  124. JUCE_API bool JUCE_CALLTYPE operator>= (const String& string1, StringRef string2) noexcept;
  125. inline String operator+ (String s1, StringRef s2) { return s1 += String (s2.text); }
  126. inline String operator+ (StringRef s1, const String& s2) { return String (s1.text) + s2; }
  127. inline String operator+ (const char* s1, StringRef s2) { return String (s1) + String (s2.text); }
  128. inline String operator+ (StringRef s1, const char* s2) { return String (s1.text) + String (s2); }
  129. } // namespace juce