1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

String: Provide range-for comaptibility

This commit is contained in:
reuk 2020-04-07 10:28:16 +01:00
parent 2ca69e8f70
commit 175644e8c1
3 changed files with 58 additions and 18 deletions

View file

@ -36,44 +36,44 @@ class CharPointer_UTF8 final
public:
using CharType = char;
inline explicit CharPointer_UTF8 (const CharType* rawPointer) noexcept
explicit CharPointer_UTF8 (const CharType* rawPointer) noexcept
: data (const_cast<CharType*> (rawPointer))
{
}
inline CharPointer_UTF8 (const CharPointer_UTF8& other) = default;
CharPointer_UTF8 (const CharPointer_UTF8& other) = default;
inline CharPointer_UTF8 operator= (CharPointer_UTF8 other) noexcept
CharPointer_UTF8 operator= (CharPointer_UTF8 other) noexcept
{
data = other.data;
return *this;
}
inline CharPointer_UTF8 operator= (const CharType* text) noexcept
CharPointer_UTF8 operator= (const CharType* text) noexcept
{
data = const_cast<CharType*> (text);
return *this;
}
/** This is a pointer comparison, it doesn't compare the actual text. */
inline bool operator== (CharPointer_UTF8 other) const noexcept { return data == other.data; }
inline bool operator!= (CharPointer_UTF8 other) const noexcept { return data != other.data; }
inline bool operator<= (CharPointer_UTF8 other) const noexcept { return data <= other.data; }
inline bool operator< (CharPointer_UTF8 other) const noexcept { return data < other.data; }
inline bool operator>= (CharPointer_UTF8 other) const noexcept { return data >= other.data; }
inline bool operator> (CharPointer_UTF8 other) const noexcept { return data > other.data; }
bool operator== (CharPointer_UTF8 other) const noexcept { return data == other.data; }
bool operator!= (CharPointer_UTF8 other) const noexcept { return data != other.data; }
bool operator<= (CharPointer_UTF8 other) const noexcept { return data <= other.data; }
bool operator< (CharPointer_UTF8 other) const noexcept { return data < other.data; }
bool operator>= (CharPointer_UTF8 other) const noexcept { return data >= other.data; }
bool operator> (CharPointer_UTF8 other) const noexcept { return data > other.data; }
/** Returns the address that this pointer is pointing to. */
inline CharType* getAddress() const noexcept { return data; }
CharType* getAddress() const noexcept { return data; }
/** Returns the address that this pointer is pointing to. */
inline operator const CharType*() const noexcept { return data; }
operator const CharType*() const noexcept { return data; }
/** Returns true if this pointer is pointing to a null character. */
inline bool isEmpty() const noexcept { return *data == 0; }
bool isEmpty() const noexcept { return *data == 0; }
/** Returns true if this pointer is not pointing to a null character. */
inline bool isNotEmpty() const noexcept { return *data != 0; }
bool isNotEmpty() const noexcept { return *data != 0; }
/** Returns the unicode character that this pointer is pointing to. */
juce_wchar operator*() const noexcept
@ -348,7 +348,7 @@ public:
}
/** Writes a null character to this string (leaving the pointer's position unchanged). */
inline void writeNull() const noexcept
void writeNull() const noexcept
{
*data = 0;
}

View file

@ -2935,6 +2935,17 @@ public:
expectEquals (serialiseDouble (-test.first), "-" + test.second);
}
}
{
beginTest ("Loops");
String str (CharPointer_UTF8 ("\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf"));
std::vector<juce_wchar> parts { 175, 92, 95, 40, 12484, 41, 95, 47, 175 };
size_t index = 0;
for (auto c : str)
expectEquals (c, parts[index++]);
}
}
};

View file

@ -307,13 +307,13 @@ public:
Note that there's also an isNotEmpty() method to help write readable code.
@see containsNonWhitespaceChars()
*/
inline bool isEmpty() const noexcept { return text.isEmpty(); }
bool isEmpty() const noexcept { return text.isEmpty(); }
/** Returns true if the string contains at least one character.
Note that there's also an isEmpty() method to help write readable code.
@see containsNonWhitespaceChars()
*/
inline bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
/** Resets this string to be empty. */
void clear() noexcept;
@ -919,6 +919,35 @@ public:
template <typename... Args>
static String formatted (const String& formatStr, Args... args) { return formattedRaw (formatStr.toRawUTF8(), args...); }
/** Returns an iterator pointing at the beginning of the string. */
CharPointerType begin() const { return getCharPointer(); }
/** Returns an iterator pointing at the terminating null of the string.
Note that this has to find the terminating null before returning it, so prefer to
call this once before looping and then reuse the result, rather than calling 'end()'
each time through the loop.
@code
String str = ...;
// BEST
for (auto c : str)
DBG (c);
// GOOD
for (auto ptr = str.begin(), end = str.end(); ptr != end; ++ptr)
DBG (*ptr);
std::for_each (str.begin(), str.end(), [] (juce_wchar c) { DBG (c); });
// BAD
for (auto ptr = str.begin(); ptr != str.end(); ++ptr)
DBG (*ptr);
@endcode
*/
CharPointerType end() const { return begin().findTerminatingNull(); }
//==============================================================================
// Numeric conversions..
@ -1209,7 +1238,7 @@ public:
that is returned must not be stored anywhere, as it can be deleted whenever the
string changes.
*/
inline CharPointerType getCharPointer() const noexcept { return text; }
CharPointerType getCharPointer() const noexcept { return text; }
/** Returns a pointer to a UTF-8 version of this string.