juce_StringRef.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_STRINGREF_H_INCLUDED
  22. #define JUCE_STRINGREF_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A simple class for holding temporary references to a string literal or String.
  26. Unlike a real String object, the StringRef does not allocate any memory or
  27. take ownership of the strings you give to it - it simply holds a reference to
  28. a string that has been allocated elsewhere.
  29. The main purpose of the class is to be used instead of a const String& as the type
  30. of function arguments where the caller may pass either a string literal or a String
  31. object. This means that when the called uses a string literal, there's no need
  32. for an temporary String object to be allocated, and this cuts down overheads
  33. substantially.
  34. Because the class is simply a wrapper around a pointer, you should always pass
  35. it by value, not by reference.
  36. @code
  37. void myStringFunction1 (const String&);
  38. void myStringFunction2 (StringRef);
  39. myStringFunction1 ("abc"); // Implicitly allocates a temporary String object.
  40. myStringFunction2 ("abc"); // Much faster, as no local allocations are needed.
  41. @endcode
  42. For examples of it in use, see the XmlElement or StringArray classes.
  43. Bear in mind that there are still many cases where it's better to use an argument
  44. which is a const String&. For example if the function stores the string or needs
  45. to internally create a String from the argument, then it's better for the original
  46. argument to already be a String.
  47. @see String
  48. */
  49. class JUCE_API StringRef
  50. {
  51. public:
  52. /** Creates a StringRef from a raw string literal.
  53. The StringRef object does NOT take ownership or copy this data, so you must
  54. ensure that the data does not change during the lifetime of the StringRef.
  55. Note that this pointer not be null!
  56. */
  57. StringRef (const char* stringLiteral) noexcept;
  58. /** Creates a StringRef from a raw char pointer.
  59. The StringRef object does NOT take ownership or copy this data, so you must
  60. ensure that the data does not change during the lifetime of the StringRef.
  61. */
  62. StringRef (String::CharPointerType stringLiteral) noexcept;
  63. /** Creates a StringRef from a String.
  64. The StringRef object does NOT take ownership or copy the data from the String,
  65. so you must ensure that the String is not modified or deleted during the lifetime
  66. of the StringRef.
  67. */
  68. StringRef (const String& string) noexcept;
  69. /** Creates a StringRef pointer to an empty string. */
  70. StringRef() noexcept;
  71. //==============================================================================
  72. /** Returns a raw pointer to the underlying string data. */
  73. operator const String::CharPointerType::CharType*() const noexcept { return text.getAddress(); }
  74. /** Returns a pointer to the underlying string data as a char pointer object. */
  75. operator String::CharPointerType() const noexcept { return text; }
  76. /** Returns true if the string is empty. */
  77. bool isEmpty() const noexcept { return text.isEmpty(); }
  78. /** Returns true if the string is not empty. */
  79. bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
  80. /** Returns the number of characters in the string. */
  81. int length() const noexcept { return (int) text.length(); }
  82. /** Retrieves a character by index. */
  83. juce_wchar operator[] (int index) const noexcept { return text[index]; }
  84. /** Compares this StringRef with a String. */
  85. bool operator== (const String& s) const noexcept { return text.compare (s.getCharPointer()) == 0; }
  86. /** Compares this StringRef with a String. */
  87. bool operator!= (const String& s) const noexcept { return text.compare (s.getCharPointer()) != 0; }
  88. /** Case-sensitive comparison of two StringRefs. */
  89. bool operator== (StringRef s) const noexcept { return text.compare (s.text) == 0; }
  90. /** Case-sensitive comparison of two StringRefs. */
  91. bool operator!= (StringRef s) const noexcept { return text.compare (s.text) != 0; }
  92. //==============================================================================
  93. /** The text that is referenced. */
  94. String::CharPointerType text;
  95. #if JUCE_STRING_UTF_TYPE != 8 && ! defined (DOXYGEN)
  96. // Sorry, non-UTF8 people, you're unable to take advantage of StringRef, because
  97. // you've chosen a character encoding that doesn't match C++ string literals.
  98. String stringCopy;
  99. #endif
  100. };
  101. //==============================================================================
  102. /** Case-sensitive comparison of two strings. */
  103. JUCE_API bool JUCE_CALLTYPE operator== (const String& string1, StringRef string2) noexcept;
  104. /** Case-sensitive comparison of two strings. */
  105. JUCE_API bool JUCE_CALLTYPE operator!= (const String& string1, StringRef string2) noexcept;
  106. inline String operator+ (String s1, StringRef s2) { return s1 += String (s2.text); }
  107. inline String operator+ (StringRef s1, const String& s2) { return String (s1.text) + s2; }
  108. inline String operator+ (const char* s1, StringRef s2) { return String (s1) + String (s2.text); }
  109. inline String operator+ (StringRef s1, const char* s2) { return String (s1.text) + String (s2); }
  110. #endif // JUCE_STRINGREF_H_INCLUDED