From 64b9366e8fdfe6504595329d4e20a3cea6eb661f Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 17 Feb 2021 13:01:01 +0000 Subject: [PATCH] CharacterFunctions: Add new function to move pointer past whitespace --- examples/Assets/WavefrontObjParser.h | 4 ++-- .../Utility/Helpers/jucer_MiscUtilities.cpp | 4 ++-- .../Utility/Helpers/jucer_TranslationHelpers.h | 4 ++-- .../littlefoot/juce_LittleFootCompiler.h | 2 +- .../juce_core/javascript/juce_Javascript.cpp | 2 +- modules/juce_core/maths/juce_Expression.cpp | 10 +++++----- modules/juce_core/text/juce_CharPointer_ASCII.h | 3 +++ modules/juce_core/text/juce_CharPointer_UTF16.h | 3 +++ modules/juce_core/text/juce_CharPointer_UTF32.h | 3 +++ modules/juce_core/text/juce_CharPointer_UTF8.h | 3 +++ .../juce_core/text/juce_CharacterFunctions.h | 17 ++++++++++++++--- modules/juce_core/xml/juce_XmlDocument.cpp | 2 +- modules/juce_graphics/geometry/juce_Path.cpp | 2 +- .../drawables/juce_SVGParser.cpp | 4 ++-- .../positioning/juce_RelativePoint.cpp | 2 +- .../positioning/juce_RelativeRectangle.cpp | 2 +- .../misc/juce_LiveConstantEditor.cpp | 2 +- 17 files changed, 46 insertions(+), 23 deletions(-) diff --git a/examples/Assets/WavefrontObjParser.h b/examples/Assets/WavefrontObjParser.h index 72c1db4415..396c9c7a6b 100644 --- a/examples/Assets/WavefrontObjParser.h +++ b/examples/Assets/WavefrontObjParser.h @@ -143,7 +143,7 @@ private: static float parseFloat (String::CharPointerType& t) { - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); return (float) CharacterFunctions::readDoubleValue (t); } @@ -211,7 +211,7 @@ private: { TripleIndex i; - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); i.vertexIndex = t.getIntValue32() - 1; t = findEndOfFaceToken (t); diff --git a/extras/Projucer/Source/Utility/Helpers/jucer_MiscUtilities.cpp b/extras/Projucer/Source/Utility/Helpers/jucer_MiscUtilities.cpp index 599ce4499d..65862a7407 100644 --- a/extras/Projucer/Source/Utility/Helpers/jucer_MiscUtilities.cpp +++ b/extras/Projucer/Source/Utility/Helpers/jucer_MiscUtilities.cpp @@ -123,12 +123,12 @@ StringPairArray parsePreprocessorDefs (const String& text) while (! s.isEmpty()) { String token, value; - s = s.findEndOfWhitespace(); + s.incrementToEndOfWhitespace(); while ((! s.isEmpty()) && *s != '=' && ! s.isWhitespace()) token << s.getAndAdvance(); - s = s.findEndOfWhitespace(); + s.incrementToEndOfWhitespace(); if (*s == '=') { diff --git a/extras/Projucer/Source/Utility/Helpers/jucer_TranslationHelpers.h b/extras/Projucer/Source/Utility/Helpers/jucer_TranslationHelpers.h index 3f64d7865a..8325ca96ad 100644 --- a/extras/Projucer/Source/Utility/Helpers/jucer_TranslationHelpers.h +++ b/extras/Projucer/Source/Utility/Helpers/jucer_TranslationHelpers.h @@ -48,7 +48,7 @@ struct TranslationHelpers break; p += 5; - p = p.findEndOfWhitespace(); + p.incrementToEndOfWhitespace(); if (*p == '(') { @@ -63,7 +63,7 @@ struct TranslationHelpers static void parseStringLiteral (String::CharPointerType& p, MemoryOutputStream& out) noexcept { - p = p.findEndOfWhitespace(); + p.incrementToEndOfWhitespace(); if (p.getAndAdvance() == '"') { diff --git a/modules/juce_blocks_basics/littlefoot/juce_LittleFootCompiler.h b/modules/juce_blocks_basics/littlefoot/juce_LittleFootCompiler.h index 764aaf8526..d47187452e 100644 --- a/modules/juce_blocks_basics/littlefoot/juce_LittleFootCompiler.h +++ b/modules/juce_blocks_basics/littlefoot/juce_LittleFootCompiler.h @@ -272,7 +272,7 @@ private: { for (;;) { - p = p.findEndOfWhitespace(); + p.incrementToEndOfWhitespace(); if (*p == '/') { diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 9fe380c468..587299909b 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -940,7 +940,7 @@ struct JavascriptEngine::RootObject : public DynamicObject { for (;;) { - p = p.findEndOfWhitespace(); + p.incrementToEndOfWhitespace(); if (*p == '/') { diff --git a/modules/juce_core/maths/juce_Expression.cpp b/modules/juce_core/maths/juce_Expression.cpp index edbb074356..211860d89f 100644 --- a/modules/juce_core/maths/juce_Expression.cpp +++ b/modules/juce_core/maths/juce_Expression.cpp @@ -699,7 +699,7 @@ struct Expression::Helpers bool readOperator (const char* ops, char* const opType = nullptr) noexcept { - text = text.findEndOfWhitespace(); + text.incrementToEndOfWhitespace(); while (*ops != 0) { @@ -719,7 +719,7 @@ struct Expression::Helpers bool readIdentifier (String& identifier) noexcept { - text = text.findEndOfWhitespace(); + text.incrementToEndOfWhitespace(); auto t = text; int numChars = 0; @@ -747,21 +747,21 @@ struct Expression::Helpers Term* readNumber() noexcept { - text = text.findEndOfWhitespace(); + text.incrementToEndOfWhitespace(); auto t = text; bool isResolutionTarget = (*t == '@'); if (isResolutionTarget) { ++t; - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); text = t; } if (*t == '-') { ++t; - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); } if (isDecimalDigit (*t) || (*t == '.' && isDecimalDigit (t[1]))) diff --git a/modules/juce_core/text/juce_CharPointer_ASCII.h b/modules/juce_core/text/juce_CharPointer_ASCII.h index 597d6f2f82..d84b19d5b7 100644 --- a/modules/juce_core/text/juce_CharPointer_ASCII.h +++ b/modules/juce_core/text/juce_CharPointer_ASCII.h @@ -350,6 +350,9 @@ public: /** Returns the first non-whitespace character in the string. */ CharPointer_ASCII findEndOfWhitespace() const noexcept { return CharacterFunctions::findEndOfWhitespace (*this); } + /** Move this pointer to the first non-whitespace character in the string. */ + void incrementToEndOfWhitespace() noexcept { CharacterFunctions::incrementToEndOfWhitespace (*this); } + /** Returns true if the given unicode character can be represented in this encoding. */ static bool canRepresent (juce_wchar character) noexcept { diff --git a/modules/juce_core/text/juce_CharPointer_UTF16.h b/modules/juce_core/text/juce_CharPointer_UTF16.h index 62cfce5481..5dcf6b8629 100644 --- a/modules/juce_core/text/juce_CharPointer_UTF16.h +++ b/modules/juce_core/text/juce_CharPointer_UTF16.h @@ -426,6 +426,9 @@ public: /** Returns the first non-whitespace character in the string. */ CharPointer_UTF16 findEndOfWhitespace() const noexcept { return CharacterFunctions::findEndOfWhitespace (*this); } + /** Move this pointer to the first non-whitespace character in the string. */ + void incrementToEndOfWhitespace() noexcept { CharacterFunctions::incrementToEndOfWhitespace (*this); } + /** Returns true if the given unicode character can be represented in this encoding. */ static bool canRepresent (juce_wchar character) noexcept { diff --git a/modules/juce_core/text/juce_CharPointer_UTF32.h b/modules/juce_core/text/juce_CharPointer_UTF32.h index da66024bf6..4dcbbd8686 100644 --- a/modules/juce_core/text/juce_CharPointer_UTF32.h +++ b/modules/juce_core/text/juce_CharPointer_UTF32.h @@ -341,6 +341,9 @@ public: /** Returns the first non-whitespace character in the string. */ CharPointer_UTF32 findEndOfWhitespace() const noexcept { return CharacterFunctions::findEndOfWhitespace (*this); } + /** Move this pointer to the first non-whitespace character in the string. */ + void incrementToEndOfWhitespace() noexcept { CharacterFunctions::incrementToEndOfWhitespace (*this); } + /** Returns true if the given unicode character can be represented in this encoding. */ static bool canRepresent (juce_wchar character) noexcept { diff --git a/modules/juce_core/text/juce_CharPointer_UTF8.h b/modules/juce_core/text/juce_CharPointer_UTF8.h index 5aa42c424f..117d021bda 100644 --- a/modules/juce_core/text/juce_CharPointer_UTF8.h +++ b/modules/juce_core/text/juce_CharPointer_UTF8.h @@ -483,6 +483,9 @@ public: /** Returns the first non-whitespace character in the string. */ CharPointer_UTF8 findEndOfWhitespace() const noexcept { return CharacterFunctions::findEndOfWhitespace (*this); } + /** Move this pointer to the first non-whitespace character in the string. */ + void incrementToEndOfWhitespace() noexcept { CharacterFunctions::incrementToEndOfWhitespace (*this); } + /** Returns true if the given unicode character can be represented in this encoding. */ static bool canRepresent (juce_wchar character) noexcept { diff --git a/modules/juce_core/text/juce_CharacterFunctions.h b/modules/juce_core/text/juce_CharacterFunctions.h index fad54c93d9..cafe918e52 100644 --- a/modules/juce_core/text/juce_CharacterFunctions.h +++ b/modules/juce_core/text/juce_CharacterFunctions.h @@ -794,6 +794,19 @@ public: return -1; } + /** Increments a pointer until it points to the first non-whitespace character + in a string. + + If the string contains only whitespace, the pointer will point to the + string's null terminator. + */ + template + static void incrementToEndOfWhitespace (Type& text) noexcept + { + while (text.isWhitespace()) + ++text; + } + /** Returns a pointer to the first non-whitespace character in a string. If the string contains only whitespace, this will return a pointer to its null terminator. @@ -801,9 +814,7 @@ public: template static Type findEndOfWhitespace (Type text) noexcept { - while (text.isWhitespace()) - ++text; - + incrementToEndOfWhitespace (text); return text; } diff --git a/modules/juce_core/xml/juce_XmlDocument.cpp b/modules/juce_core/xml/juce_XmlDocument.cpp index eb472c3eb2..69d2e796b6 100644 --- a/modules/juce_core/xml/juce_XmlDocument.cpp +++ b/modules/juce_core/xml/juce_XmlDocument.cpp @@ -291,7 +291,7 @@ void XmlDocument::skipNextWhiteSpace() { for (;;) { - input = input.findEndOfWhitespace(); + input.incrementToEndOfWhitespace(); if (input.isEmpty()) { diff --git a/modules/juce_graphics/geometry/juce_Path.cpp b/modules/juce_graphics/geometry/juce_Path.cpp index eb7d04efd6..851e65c587 100644 --- a/modules/juce_graphics/geometry/juce_Path.cpp +++ b/modules/juce_graphics/geometry/juce_Path.cpp @@ -37,7 +37,7 @@ namespace PathHelpers static String nextToken (String::CharPointerType& t) { - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); auto start = t; size_t numChars = 0; diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index d7ea9d45c8..caaacbfede 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -415,7 +415,7 @@ public: case 'z': path.closeSubPath(); last = last2 = subpathStart; - d = d.findEndOfWhitespace(); + d.incrementToEndOfWhitespace(); currentCommand = 'M'; break; @@ -755,7 +755,7 @@ private: dashLengths.add (value); - t = t.findEndOfWhitespace(); + t.incrementToEndOfWhitespace(); if (*t == ',') ++t; diff --git a/modules/juce_gui_basics/positioning/juce_RelativePoint.cpp b/modules/juce_gui_basics/positioning/juce_RelativePoint.cpp index 1fa8f4f7de..90f99473ca 100644 --- a/modules/juce_gui_basics/positioning/juce_RelativePoint.cpp +++ b/modules/juce_gui_basics/positioning/juce_RelativePoint.cpp @@ -30,7 +30,7 @@ namespace RelativePointHelpers { inline void skipComma (String::CharPointerType& s) { - s = s.findEndOfWhitespace(); + s.incrementToEndOfWhitespace(); if (*s == ',') ++s; diff --git a/modules/juce_gui_basics/positioning/juce_RelativeRectangle.cpp b/modules/juce_gui_basics/positioning/juce_RelativeRectangle.cpp index ac482ed666..9012bafe48 100644 --- a/modules/juce_gui_basics/positioning/juce_RelativeRectangle.cpp +++ b/modules/juce_gui_basics/positioning/juce_RelativeRectangle.cpp @@ -30,7 +30,7 @@ namespace RelativeRectangleHelpers { inline void skipComma (String::CharPointerType& s) { - s = s.findEndOfWhitespace(); + s.incrementToEndOfWhitespace(); if (*s == ',') ++s; diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp index ce6ea14ffc..4b422facbe 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp @@ -213,7 +213,7 @@ void LivePropertyEditorBase::findOriginalValueInCode() } p += (int) (sizeof ("JUCE_LIVE_CONSTANT") - 1); - p = p.findEndOfWhitespace(); + p.incrementToEndOfWhitespace(); if (! CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")).isEmpty()) {