From 0b400af9519444deef4cc6ad2c43c30e2092ab4f Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 4 Jan 2025 09:56:12 -0600 Subject: Fix bug related to string copying. --- source/fud_string_view.cpp | 115 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 25 deletions(-) (limited to 'source/fud_string_view.cpp') diff --git a/source/fud_string_view.cpp b/source/fud_string_view.cpp index ba88ad4..5c5e623 100644 --- a/source/fud_string_view.cpp +++ b/source/fud_string_view.cpp @@ -133,6 +133,96 @@ FudStatus skipWhitespace(StringView& view, size_t& skipIndex) return FudStatus::Success; } +FudStatus fud_string_compare(StringView levo, StringView dextro, int& difference) +{ + if (anyAreNull(levo.m_data, dextro.m_data)) { + return FudStatus::NullPointer; + } + + int diff = 0; + size_t index = 0; + while (diff == 0 && index < levo.length() && index < dextro.length()) { + diff = levo.m_data[index] - dextro.m_data[index]; + index++; + } + + if (diff != 0 || levo.length() == dextro.length()) { + /* nothing to do */ + } else if (levo.length() > dextro.length()) { + diff = static_cast(levo.m_data[index]); + } else { + diff = -static_cast(dextro.m_data[index]); + } + + difference = diff; + return FudStatus::Success; +} + +bool StringView::operator==(const StringView& rhs) const noexcept +{ + if (m_length == rhs.m_length && m_data == rhs.m_data) { + return true; + } + int difference{}; + auto compareStatus = fud_string_compare(*this, rhs, difference); + if (compareStatus != FudStatus::Success) { + if (m_data != nullptr && rhs.m_data == nullptr) { + return false; + } + if (m_data == nullptr && rhs.m_data != nullptr) { + return false; + } + fudAssert(m_data == nullptr && rhs.m_data == nullptr); + if (m_length > rhs.m_length) { + return false; + } + fudAssert(m_length < rhs.m_length); + return false; + } + if (difference > 0) { + return false; + } + if (difference < 0) { + return false; + } + return true; + +} + +auto StringView::operator<=>(const StringView& rhs) const noexcept +{ + if (this == &rhs) { + return std::strong_ordering::equivalent; + } + if (m_length == rhs.m_length && m_data == rhs.m_data) + { + return std::strong_ordering::equivalent; + } + int difference{}; + auto compareStatus = fud_string_compare(*this, rhs, difference); + if (compareStatus != FudStatus::Success) { + if (m_data != nullptr && rhs.m_data == nullptr) { + return std::strong_ordering::greater; + } + if (m_data == nullptr && rhs.m_data != nullptr) { + return std::strong_ordering::less; + } + fudAssert(m_data == nullptr && rhs.m_data == nullptr); + if (m_length > rhs.m_length) { + return std::strong_ordering::greater; + } + fudAssert(m_length < rhs.m_length); + return std::strong_ordering::less; + } + if (difference > 0) { + return std::strong_ordering::greater; + } + if (difference < 0) { + return std::strong_ordering::less; + } + return std::strong_ordering::equivalent; +} + #if 0 FudStatus fud_string_truncate(ExtBasicString* source, ssize_t newLength) @@ -226,31 +316,6 @@ FudStatus fud_string_reverse_substring(ExtBasicString* source, StringView subStr return FudStatus::Success; } -FudStatus fud_string_compare(StringView levo, StringView dextro, int* difference) -{ - if (anyAreNull(difference, levo.data, dextro.data)) { - return FudStatus::NullPointer; - } - - int diff = 0; - size_t index = 0; - while (diff == 0 && index < levo.length && index < dextro.length) { - diff = levo.data[index] - dextro.data[index]; - index++; - } - - if (diff != 0 || levo.length == dextro.length) { - /* nothing to do */ - } else if (levo.length > dextro.length) { - diff = static_cast(levo.data[index]); - } else { - diff = -static_cast(dextro.data[index]); - } - - *difference = diff; - return FudStatus::Success; -} - FudStatus fud_string_chr(StringView extStringView, char character, size_t* index) { if (anyAreNull(extStringView.data, index)) { -- cgit v1.2.3