Audio plugin host https://kx.studio/carla
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.

137 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_STRINGREF_H_INCLUDED
  24. #define JUCE_STRINGREF_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. A simple class for holding temporary references to a string literal or String.
  28. Unlike a real String object, the StringRef does not allocate any memory or
  29. take ownership of the strings you give to it - it simply holds a reference to
  30. a string that has been allocated elsewhere.
  31. The main purpose of the class is to be used instead of a const String& as the type
  32. of function arguments where the caller may pass either a string literal or a String
  33. object. This means that when the called uses a string literal, there's no need
  34. for an temporary String object to be allocated, and this cuts down overheads
  35. substantially.
  36. Because the class is simply a wrapper around a pointer, you should always pass
  37. it by value, not by reference.
  38. @code
  39. void myStringFunction1 (const String&);
  40. void myStringFunction2 (StringRef);
  41. myStringFunction1 ("abc"); // Implicitly allocates a temporary String object.
  42. myStringFunction2 ("abc"); // Much faster, as no local allocations are needed.
  43. @endcode
  44. For examples of it in use, see the XmlElement or StringArray classes.
  45. Bear in mind that there are still many cases where it's better to use an argument
  46. which is a const String&. For example if the function stores the string or needs
  47. to internally create a String from the argument, then it's better for the original
  48. argument to already be a String.
  49. @see String
  50. */
  51. class JUCE_API StringRef
  52. {
  53. public:
  54. /** Creates a StringRef from a raw string literal.
  55. The StringRef object does NOT take ownership or copy this data, so you must
  56. ensure that the data does not change during the lifetime of the StringRef.
  57. Note that this pointer not be null!
  58. */
  59. StringRef (const char* stringLiteral) noexcept;
  60. /** Creates a StringRef from a raw char pointer.
  61. The StringRef object does NOT take ownership or copy this data, so you must
  62. ensure that the data does not change during the lifetime of the StringRef.
  63. */
  64. StringRef (String::CharPointerType stringLiteral) noexcept;
  65. /** Creates a StringRef from a String.
  66. The StringRef object does NOT take ownership or copy the data from the String,
  67. so you must ensure that the String is not modified or deleted during the lifetime
  68. of the StringRef.
  69. */
  70. StringRef (const String& string) noexcept;
  71. /** Creates a StringRef pointer to an empty string. */
  72. StringRef() noexcept;
  73. //==============================================================================
  74. /** Returns a raw pointer to the underlying string data. */
  75. operator const String::CharPointerType::CharType*() const noexcept { return text.getAddress(); }
  76. /** Returns a pointer to the underlying string data as a char pointer object. */
  77. operator String::CharPointerType() const noexcept { return text; }
  78. /** Returns true if the string is empty. */
  79. bool isEmpty() const noexcept { return text.isEmpty(); }
  80. /** Returns true if the string is not empty. */
  81. bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
  82. /** Returns the number of characters in the string. */
  83. int length() const noexcept { return (int) text.length(); }
  84. /** Retrieves a character by index. */
  85. juce_wchar operator[] (int index) const noexcept { return text[index]; }
  86. /** Compares this StringRef with a String. */
  87. bool operator== (const String& s) const noexcept { return text.compare (s.getCharPointer()) == 0; }
  88. /** Compares this StringRef with a String. */
  89. bool operator!= (const String& s) const noexcept { return text.compare (s.getCharPointer()) != 0; }
  90. /** Case-sensitive comparison of two StringRefs. */
  91. bool operator== (StringRef s) const noexcept { return text.compare (s.text) == 0; }
  92. /** Case-sensitive comparison of two StringRefs. */
  93. bool operator!= (StringRef s) const noexcept { return text.compare (s.text) != 0; }
  94. //==============================================================================
  95. /** The text that is referenced. */
  96. String::CharPointerType text;
  97. };
  98. //==============================================================================
  99. /** Case-sensitive comparison of two strings. */
  100. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, StringRef string2) noexcept;
  101. /** Case-sensitive comparison of two strings. */
  102. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, StringRef string2) noexcept;
  103. inline String operator+ (String s1, StringRef s2) { return s1 += String (s2.text); }
  104. inline String operator+ (StringRef s1, const String& s2) { return String (s1.text) + s2; }
  105. inline String operator+ (const char* s1, StringRef s2) { return String (s1) + String (s2.text); }
  106. inline String operator+ (StringRef s1, const char* s2) { return String (s1.text) + String (s2); }
  107. #endif // JUCE_STRINGREF_H_INCLUDED