diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-06-19 16:05:40 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-19 16:05:40 -0400 |
| commit | 442f8c6d3d42b892e3f13128bcb6487ff7508f0d (patch) | |
| tree | 79746a98390c49bba9639ee84999304731210a8b /source/core | |
| parent | 48ae5496516878768d7de241b9b7fbba91fbaa74 (diff) | |
Make extractLine return a bool. (#991)
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-string-util.cpp | 35 | ||||
| -rw-r--r-- | source/core/slang-string-util.h | 15 |
2 files changed, 23 insertions, 27 deletions
diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp index fa96e4435..95caf8319 100644 --- a/source/core/slang-string-util.cpp +++ b/source/core/slang-string-util.cpp @@ -199,7 +199,7 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string) return (fromChar == toChar || string.indexOf(fromChar) == Index(-1)) ? string : calcCharReplaced(string.getUnownedSlice(), fromChar, toChar); } -/* static */UnownedStringSlice StringUtil::extractLine(UnownedStringSlice& ioText) +/* static */bool StringUtil::extractLine(UnownedStringSlice& ioText, UnownedStringSlice& outLine) { char const*const begin = ioText.begin(); char const*const end = ioText.end(); @@ -207,7 +207,8 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string) // If we have hit the end then return the 'special' terminator if (begin == nullptr) { - return UnownedStringSlice(nullptr, nullptr); + outLine = UnownedStringSlice(nullptr, nullptr); + return false; } char const* cursor = begin; @@ -234,7 +235,8 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string) } ioText = UnownedStringSlice(cursor, end); - return UnownedStringSlice(begin, lineEnd); + outLine = UnownedStringSlice(begin, lineEnd); + return true; } default: break; @@ -247,39 +249,32 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string) // Could be empty, or the remaining line (without line end terminators of) SLANG_ASSERT(begin <= cursor); - return UnownedStringSlice(begin, cursor); + outLine = UnownedStringSlice(begin, cursor); + return true; } /* static */void StringUtil::calcLines(const UnownedStringSlice& textIn, List<UnownedStringSlice>& outLines) { outLines.clear(); - - UnownedStringSlice text(textIn); - while (true) + UnownedStringSlice text(textIn), line; + while (extractLine(text, line)) { - UnownedStringSlice line = extractLine(text); - if (line.begin() == nullptr) - { - return; - } outLines.add(line); } } /* static */bool StringUtil::areLinesEqual(const UnownedStringSlice& inA, const UnownedStringSlice& inB) { - UnownedStringSlice a(inA); - UnownedStringSlice b(inB); - + UnownedStringSlice a(inA), b(inB), lineA, lineB; + while (true) { - const UnownedStringSlice lineA = extractLine(a); - const UnownedStringSlice lineB = extractLine(b); + const auto hasLineA = extractLine(a, lineA); + const auto hasLineB = extractLine(b, lineB); - // If either has ended, they both must have ended - if (lineA.begin() == nullptr || lineB.begin() == nullptr) + if (!(hasLineA && hasLineB)) { - return lineA.begin() == lineB.begin(); + return hasLineA == hasLineB; } // The lines must be equal diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h index b9618c17f..74ec75dc6 100644 --- a/source/core/slang-string-util.h +++ b/source/core/slang-string-util.h @@ -78,15 +78,16 @@ struct StringUtil /// Create a blob from a string static ComPtr<ISlangBlob> createStringBlob(const String& string); - /// Returns a line extracted from the start of ioText. + /// Extracts a line and stores the remaining text in ioText, and the line in outLine. Returns true if has a line. /// - /// At the end of all the text a 'special' null UnownedStringSlice with a null 'begin' pointer is returned. - /// The slice passed in will be modified on output to contain the remaining text, starting at the beginning of the next line. - /// As en empty final line is still a line, the special null UnownedStringSlice is the last value ioText after the last valid line is returned. + /// As well as indicating end of text with the return value, at the end of all the text a 'special' null UnownedStringSlice with a null 'begin' + /// pointer is also returned as the outLine. + /// ioText will be modified to contain the remaining text, starting at the beginning of the next line. + /// As an empty final line is still a line, the special null UnownedStringSlice is the last value ioText after the last valid line is returned. /// /// NOTE! That behavior is as if line terminators (like \n) act as separators. Thus input of "\n" will return *two* lines - an empty line /// before and then after the \n. - static UnownedStringSlice extractLine(UnownedStringSlice& ioText); + static bool extractLine(UnownedStringSlice& ioText, UnownedStringSlice& outLine); /// Given text, splits into lines stored in outLines. NOTE! That lines is only valid as long as textIn remains valid static void calcLines(const UnownedStringSlice& textIn, List<UnownedStringSlice>& lines); @@ -105,7 +106,7 @@ public: const UnownedStringSlice* operator->() const { return &m_line; } Iterator& operator++() { - m_line = StringUtil::extractLine(m_remaining); + StringUtil::extractLine(m_remaining, m_line); return *this; } Iterator operator++(int) { Iterator rs = *this; operator++(); return rs; } @@ -122,7 +123,7 @@ public: UnownedStringSlice m_remaining; }; - Iterator begin() const { UnownedStringSlice remaining(m_text); UnownedStringSlice line = StringUtil::extractLine(remaining); return Iterator(line, remaining); } + Iterator begin() const { UnownedStringSlice remaining(m_text), line; StringUtil::extractLine(remaining, line); return Iterator(line, remaining); } Iterator end() const { UnownedStringSlice term(nullptr, nullptr); return Iterator(term, term); } /// Ctor |
