From c2776f660e00dd9c0ec4cc50369b2a9cf2ed3e70 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 2 Oct 2024 12:19:53 -0500 Subject: Fix error in string reserve. --- source/fud_string.cpp | 34 ++++++++++++++++++++++++---------- source/fud_string_view.cpp | 28 ++++++++++------------------ test/test_string.cpp | 10 +++++++++- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/source/fud_string.cpp b/source/fud_string.cpp index c1937be..10352ad 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -148,6 +148,10 @@ void String::cleanup() FudStatus String::resize(size_t newCapacity) { + if (!valid()) { + return FudStatus::StringInvalid; + } + if (m_length >= newCapacity) { return FudStatus::OperationInvalid; } @@ -164,16 +168,28 @@ FudStatus String::resize(size_t newCapacity) fudFree(m_data); m_data = nullptr; copyMem(m_buffer, temp); - + data()[m_length] = '\0'; return FudStatus::Success; } - auto* newData = fudRealloc(m_data, newCapacity); - if (newData == nullptr) { - return FudStatus::AllocFailure; + utf8* newData; + if (isLarge()) { + newData = static_cast(fudRealloc(m_data, newCapacity)); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + } else { + newData = static_cast(fudAlloc(newCapacity)); + if (newData == nullptr) { + return FudStatus::AllocFailure; + } + static_cast(copyMem(newData, newCapacity, m_buffer.data(), length())); } + m_capacity = newCapacity; - m_data = static_cast(newData); + m_data = newData; + data()[m_length] = '\0'; + fudAssert(valid()); return FudStatus::Success; } @@ -184,7 +200,7 @@ bool String::nullTerminated() const bool String::valid() const { - return nullTerminated() && m_length < m_capacity; + return nullTerminated(); } bool String::utf8Valid() const @@ -342,13 +358,11 @@ FudStatus String::append(StringView source) } const auto newLength = length() + source.length(); - if (newLength < length()) - { + if (newLength < length()) { return FudStatus::OperationInvalid; } const auto newSize = newLength + 1; - if (newSize < newLength) - { + if (newSize < newLength) { return FudStatus::OperationInvalid; } if (newSize >= m_capacity) { diff --git a/source/fud_string_view.cpp b/source/fud_string_view.cpp index 4795cf2..411f055 100644 --- a/source/fud_string_view.cpp +++ b/source/fud_string_view.cpp @@ -139,7 +139,7 @@ FudStatus fud_string_reverse_substring(ExtBasicString* source, StringView subStr size_t index = 0; auto* data = source->m_data + dataOffset; while (index < subString.length) { - if (ext_lib_char_is_ascii(static_cast(data[index]))) { + if (fud_char_is_ascii(static_cast(data[index]))) { index++; continue; } @@ -173,7 +173,7 @@ FudStatus fud_string_reverse_substring(ExtBasicString* source, StringView subStr index += utf8Size; } - ext_lib::DataView dataView{subString.length, data}; + DataView dataView{subString.length, data}; reverse(dataView); return FudStatus::Success; @@ -241,7 +241,7 @@ FudStatus fud_string_unicode_chr(StringView extString, const ExtUtf8* unicode, s const uint8_t* dataMem = unicode->data(); ExtDebugAssert(dataMem != nullptr); - std::array localData{}; + Array localData{}; auto copyStatus = ExtCopyMem(localData.data(), localData.size(), dataMem, charSize); ExtDebugAssert(copyStatus == FudStatus::Success); @@ -264,8 +264,6 @@ FudStatus fud_string_unicode_chr(StringView extString, const ExtUtf8* unicode, s return ExtNotFound; } -namespace ext_lib { - FudStatus fud_string_span_c_api( const StringView& inputView, const StringView& characterSetString, @@ -358,8 +356,6 @@ FudStatus fud_string_span_set(StringView inputView, const ExtUtf8Set* characterS return ExtNotFound; } -} // namespace ext_lib - FudStatus fud_string_span(StringView extString, StringView characterSetString, StringView* result) { if (result == nullptr) { @@ -386,12 +382,12 @@ FudStatus fud_string_c_span(StringView extString, StringView characterSetString, FudStatus fud_string_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView) { - return ext_lib::fud_string_span_set(extString, characterSet, stringView, true); + return fud_string_span_set(extString, characterSet, stringView, true); } FudStatus fud_string_c_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView) { - return ext_lib::fud_string_span_set(extString, characterSet, stringView, false); + return fud_string_span_set(extString, characterSet, stringView, false); } FudStatus fud_string_find_substring(StringView haystack, StringView needle, StringView* stringView) @@ -446,8 +442,6 @@ FudStatus fud_string_find_substring(StringView haystack, StringView needle, Stri return ExtNotFound; } -namespace ext_lib { - FudStatus skipWhitespace(StringView& view, size_t& skipIndex) { auto skipResult = view.skipWhitespace(); @@ -461,8 +455,6 @@ FudStatus skipWhitespace(StringView& view, size_t& skipIndex) return FudStatus::Success; } -} // namespace ext_lib - FudStatus fud_string_view_skip_whitespace(StringView* view) { if (view == nullptr) { @@ -496,7 +488,7 @@ FudStatus fud_string_view_trim_whitespace(StringView* view) } namespace impl { -constexpr ext_lib::Array AsciiLookup{ +constexpr Array AsciiLookup{ {-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -2, -2, -2, -2, -2, -2, -2, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, @@ -619,7 +611,7 @@ FudStatus stringViewToUnsignedInteger(StringView input, T& number, uint8_t speci StringView view{input}; size_t skipIndex = 0; - auto status = ext_lib::skipWhitespace(view, skipIndex); + auto status = skipWhitespace(view, skipIndex); if (status != FudStatus::Success) { return status; } @@ -742,7 +734,7 @@ FudStatus stringViewToSignedInteger(StringView input, T& number, uint8_t specifi StringView view{input}; size_t skipIndex = 0; - auto status = ext_lib::skipWhitespace(view, skipIndex); + auto status = skipWhitespace(view, skipIndex); if (status != FudStatus::Success) { return status; } @@ -850,8 +842,8 @@ template bool isNanOrInf(T& num, StringView& view, T& sign, size_t& digitIndex) { if (view.length >= 3) { - std::array letters{{view.data[0], view.data[1], view.data[2]}}; - ext_lib::mapMut(letters, ext_lib_char_to_lower); + Array letters{{view.data[0], view.data[1], view.data[2]}}; + mapMut(letters, fud_char_to_lower); if (letters[0] == 'i' && letters[1] == 'n' && letters[2] == 'f') { num = sign * std::numeric_limits::infinity(); digitIndex = 3; diff --git a/test/test_string.cpp b/test/test_string.cpp index d48112b..15646bd 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -56,10 +56,18 @@ TEST(FudString, BasicStringOps) TEST(FudString, HeapAlloc) { constexpr const char filenameLiteral[] = "Amazing Saga Volume 01/000.jpg"; - fud::String filename{filenameLiteral}; + String filename{filenameLiteral}; ASSERT_EQ(filename.length(), sizeof(filenameLiteral) - 1); } +TEST(FudString, Reserve) +{ + String testString{}; + ASSERT_TRUE(testString.valid()); + ASSERT_EQ(testString.reserve(256), FudStatus::Success); + ASSERT_TRUE(testString.valid()); +} + #if 0 TEST(FudString, FindSubstringCxx) { -- cgit v1.2.3