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