summaryrefslogtreecommitdiff
path: root/source/fud_string.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-09-23 07:36:16 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-09-23 07:36:16 -0500
commit0b860bb5dd6d2007db605291d239a6a9d41f57d1 (patch)
treefab140e03a3665236503d1405de9d33ba58ccc4a /source/fud_string.cpp
parent7da829d48f9059c83ab9cada2c850621e8bbd3f3 (diff)
Installable library.
Diffstat (limited to 'source/fud_string.cpp')
-rw-r--r--source/fud_string.cpp148
1 files changed, 57 insertions, 91 deletions
diff --git a/source/fud_string.cpp b/source/fud_string.cpp
index d690aa9..cd35bdb 100644
--- a/source/fud_string.cpp
+++ b/source/fud_string.cpp
@@ -16,8 +16,7 @@
*/
#include "fud_string.hpp"
-
-#include <cassert>
+#include "fud_assert.hpp"
namespace fud {
@@ -56,16 +55,17 @@ String::String(const char* cString)
if (lenResult < 0 || lenResult >= SSIZE_MAX) {
m_length = 1;
m_capacity = 0;
- } else if (lenResult < SSO_BUF_SIZE) {
+ } else if (static_cast<size_t>(lenResult) < SSO_BUF_SIZE) {
m_length = static_cast<size_t>(lenResult);
- assert(copyMem(m_buffer.data(), m_buffer.size(), cString, m_length));
+ fudAssert(copyMem(m_buffer.data(), m_buffer.size(), cString, m_length) == FudStatus::Success);
+ fudAssert(nullTerminate() == FudStatus::Success);
} else {
m_length = static_cast<size_t>(lenResult);
m_capacity = m_length + 1;
m_data = static_cast<utf8*>(fudAlloc(m_capacity));
- assert(m_data != nullptr);
- assert(copyMem(m_buffer.data(), m_capacity, cString, m_length) == FudStatus::Success);
- assert(nullTerminate() == FudStatus::Success);
+ fudAssert(m_data != nullptr);
+ fudAssert(copyMem(m_buffer.data(), m_capacity, cString, m_length) == FudStatus::Success);
+ fudAssert(nullTerminate() == FudStatus::Success);
}
}
@@ -74,10 +74,10 @@ String::String(const String& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_cap
if (rhs.valid()) {
if (isLarge()) {
m_data = static_cast<utf8*>(fudAlloc(m_capacity));
- assert(m_data != nullptr);
+ fudAssert(m_data != nullptr);
}
- assert(copyMem(data(), m_capacity, rhs.data(), m_length) == FudStatus::Success);
- assert(nullTerminate() == FudStatus::Success);
+ fudAssert(copyMem(data(), m_capacity, rhs.data(), m_length) == FudStatus::Success);
+ fudAssert(nullTerminate() == FudStatus::Success);
}
}
@@ -87,8 +87,8 @@ String::String(String&& rhs) : m_length{rhs.m_length}, m_capacity{rhs.m_capacity
m_data = rhs.m_data;
rhs.m_data = nullptr;
} else {
- assert(copyMem(m_buffer.data(), m_buffer.size(), rhs.m_buffer.data(), m_length));
- assert(nullTerminate() == FudStatus::Success);
+ m_buffer = std::move(rhs.m_buffer);
+ fudAssert(nullTerminate() == FudStatus::Success);
}
}
@@ -107,10 +107,10 @@ String& String::operator=(const String& rhs)
if (rhs.valid()) {
if (isLarge()) {
m_data = static_cast<utf8*>(fudAlloc(m_capacity));
- assert(m_data != nullptr);
+ fudAssert(m_data != nullptr);
}
- assert(copyMem(data(), m_capacity, rhs.data(), m_length) == FudStatus::Success);
- assert(nullTerminate() == FudStatus::Success);
+ fudAssert(copyMem(data(), m_capacity, rhs.data(), m_length) == FudStatus::Success);
+ fudAssert(nullTerminate() == FudStatus::Success);
}
return *this;
}
@@ -122,8 +122,8 @@ String& String::operator=(String&& rhs) {
m_data = rhs.m_data;
rhs.m_data = nullptr;
} else {
- assert(copyMem(m_buffer.data(), m_buffer.size(), rhs.m_buffer.data(), m_length));
- assert(nullTerminate() == FudStatus::Success);
+ m_buffer = std::move(rhs.m_buffer);
+ fudAssert(nullTerminate() == FudStatus::Success);
}
return *this;
}
@@ -148,10 +148,10 @@ bool String::utf8Valid() const
return view.utf8Valid();
}
-FudStatus String::nullTerminate() const
+FudStatus String::nullTerminate()
{
if (m_length < m_capacity) {
- m_data[m_length] = '\0';
+ data()[m_length] = '\0';
return FudStatus::Success;
}
return FudStatus::StringInvalid;
@@ -266,7 +266,9 @@ String String::append(const String& rhs) const
auto* destPtr = output.data();
auto status = copyMem(destPtr, m_capacity, rhs.data(), rhs.length());
- assert(output.nullTerminate() == FudStatus::Success);
+ fudAssert(status == FudStatus::Success);
+ static_cast<void>(status);
+ fudAssert(output.nullTerminate() == FudStatus::Success);
return output;
}
@@ -333,44 +335,8 @@ Result<size_t, FudStatus> StringView::trimWhitespace()
}
#if 0
-FudStatus ext_string_copy(ExtBasicString* destination, const ExtBasicString* source)
-{
- if (anyAreNull(source, destination) || destination->m_data == nullptr) {
- return FudStatus::NullPointer;
- }
-
- /* TODO: ensure that destination and source aren't aliased, over the entire length. */
- if (source == destination || source->m_data == destination->m_data) {
- return ExtAliased;
- }
-
- if (!String{*source}.valid()) {
- return FudStatus::StringInvalid;
- }
-
- if (destination->m_size <= source->m_length) {
- return FudStatus::OperationInvalid;
- }
-
- auto status = ExtCopyMem(destination->m_data, destination->m_size, source->m_data, source->m_length);
- if (status == FudStatus::Success) {
- destination->m_length = source->m_length;
- status = StringBorrow{*destination}.nullTerminate();
- }
-
- return status;
-}
-FudStatus ext_string_catenate(ExtBasicString* destination, StringView source)
-{
- if (destination == nullptr || anyAreNull(destination->m_data, source.data)) {
- return FudStatus::NullPointer;
- }
-
- return StringBorrow{*destination}.catenate(source);
-}
-
-FudStatus ext_string_truncate(ExtBasicString* source, ssize_t newLength)
+FudStatus fud_string_truncate(ExtBasicString* source, ssize_t newLength)
{
if (source == nullptr) {
return FudStatus::NullPointer;
@@ -394,15 +360,15 @@ FudStatus ext_string_truncate(ExtBasicString* source, ssize_t newLength)
return wrapper.nullTerminate();
}
-FudStatus ext_string_reverse(ExtBasicString* source)
+FudStatus fud_string_reverse(ExtBasicString* source)
{
if (source == nullptr || source->m_data == nullptr) {
return FudStatus::NullPointer;
}
- return ext_string_reverse_substring(source, StringView{source->m_length, source->m_data});
+ return fud_string_reverse_substring(source, StringView{source->m_length, source->m_data});
}
-FudStatus ext_string_reverse_substring(ExtBasicString* source, StringView subString)
+FudStatus fud_string_reverse_substring(ExtBasicString* source, StringView subString)
{
auto dataOffset = subString.data - source->m_data;
if (dataOffset < 0 || static_cast<size_t>(dataOffset) > source->m_length) {
@@ -461,7 +427,7 @@ FudStatus ext_string_reverse_substring(ExtBasicString* source, StringView subStr
return FudStatus::Success;
}
-FudStatus ext_string_compare(StringView levo, StringView dextro, int* difference)
+FudStatus fud_string_compare(StringView levo, StringView dextro, int* difference)
{
if (anyAreNull(difference, levo.data, dextro.data)) {
return FudStatus::NullPointer;
@@ -486,7 +452,7 @@ FudStatus ext_string_compare(StringView levo, StringView dextro, int* difference
return FudStatus::Success;
}
-FudStatus ext_string_chr(StringView extStringView, char character, size_t* index)
+FudStatus fud_string_chr(StringView extStringView, char character, size_t* index)
{
if (anyAreNull(extStringView.data, index)) {
return FudStatus::NullPointer;
@@ -508,7 +474,7 @@ FudStatus ext_string_chr(StringView extStringView, char character, size_t* index
return ExtNotFound;
}
-FudStatus ext_string_unicode_chr(StringView extString, const ExtUtf8* unicode, size_t* index)
+FudStatus fud_string_unicode_chr(StringView extString, const ExtUtf8* unicode, size_t* index)
{
if (anyAreNull(extString.data, unicode, index)) {
return FudStatus::NullPointer;
@@ -548,7 +514,7 @@ FudStatus ext_string_unicode_chr(StringView extString, const ExtUtf8* unicode, s
namespace ext_lib {
-FudStatus ext_string_span_c_api(
+FudStatus fud_string_span_c_api(
const StringView& inputView,
const StringView& characterSetString,
StringView& result,
@@ -599,7 +565,7 @@ FudStatus ext_string_span_c_api(
return ExtNotFound;
}
-FudStatus ext_string_span_set(StringView inputView, const ExtUtf8Set* characterSet, StringView* stringView, bool inSet)
+FudStatus fud_string_span_set(StringView inputView, const ExtUtf8Set* characterSet, StringView* stringView, bool inSet)
{
if (anyAreNull(inputView.data, characterSet, stringView)) {
return FudStatus::NullPointer;
@@ -642,7 +608,7 @@ FudStatus ext_string_span_set(StringView inputView, const ExtUtf8Set* characterS
} // namespace ext_lib
-FudStatus ext_string_span(StringView extString, StringView characterSetString, StringView* result)
+FudStatus fud_string_span(StringView extString, StringView characterSetString, StringView* result)
{
if (result == nullptr) {
return FudStatus::NullPointer;
@@ -651,10 +617,10 @@ FudStatus ext_string_span(StringView extString, StringView characterSetString, S
const StringView inputView{extString};
const StringView characterSet{characterSetString};
- return ext_string_span_c_api(inputView, characterSet, *result, true);
+ return fud_string_span_c_api(inputView, characterSet, *result, true);
}
-FudStatus ext_string_c_span(StringView extString, StringView characterSetString, StringView* result)
+FudStatus fud_string_c_span(StringView extString, StringView characterSetString, StringView* result)
{
if (result == nullptr) {
return FudStatus::NullPointer;
@@ -663,20 +629,20 @@ FudStatus ext_string_c_span(StringView extString, StringView characterSetString,
const StringView inputView{extString};
const StringView characterSet{characterSetString};
- return ext_string_span_c_api(inputView, characterSet, *result, false);
+ return fud_string_span_c_api(inputView, characterSet, *result, false);
}
-FudStatus ext_string_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
+FudStatus fud_string_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
{
- return ext_lib::ext_string_span_set(extString, characterSet, stringView, true);
+ return ext_lib::fud_string_span_set(extString, characterSet, stringView, true);
}
-FudStatus ext_string_c_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
+FudStatus fud_string_c_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
{
- return ext_lib::ext_string_span_set(extString, characterSet, stringView, false);
+ return ext_lib::fud_string_span_set(extString, characterSet, stringView, false);
}
-FudStatus ext_string_find_substring(StringView haystack, StringView needle, StringView* stringView)
+FudStatus fud_string_find_substring(StringView haystack, StringView needle, StringView* stringView)
{
if (anyAreNull(haystack.data, needle.data, stringView)) {
return FudStatus::NullPointer;
@@ -688,7 +654,7 @@ FudStatus ext_string_find_substring(StringView haystack, StringView needle, Stri
if (needle.length == 1) {
size_t index = 0;
- auto chrFindStatus = ext_string_chr(haystack, static_cast<char>(needle.data[0]), &index);
+ auto chrFindStatus = fud_string_chr(haystack, static_cast<char>(needle.data[0]), &index);
if (chrFindStatus == FudStatus::Success) {
stringView->data = haystack.data + index;
stringView->length = 1;
@@ -702,7 +668,7 @@ FudStatus ext_string_find_substring(StringView haystack, StringView needle, Stri
lhs.data = haystack.data + haystackIdx;
lhs.length = haystack.length - haystackIdx;
size_t lhsIndex = 0;
- auto chrFindStatus = ext_string_chr(lhs, static_cast<char>(needle.data[0]), &lhsIndex);
+ auto chrFindStatus = fud_string_chr(lhs, static_cast<char>(needle.data[0]), &lhsIndex);
if (chrFindStatus != FudStatus::Success) {
return chrFindStatus;
}
@@ -715,7 +681,7 @@ FudStatus ext_string_find_substring(StringView haystack, StringView needle, Stri
lhs.length = needle.length;
int difference = -1;
- auto cmpStatus = ext_string_compare(lhs, needle, &difference);
+ auto cmpStatus = fud_string_compare(lhs, needle, &difference);
ExtDebugAssert(cmpStatus == FudStatus::Success);
if (difference == 0) {
stringView->data = lhs.data;
@@ -745,7 +711,7 @@ FudStatus skipWhitespace(StringView& view, size_t& skipIndex)
} // namespace ext_lib
-FudStatus ext_string_view_skip_whitespace(StringView* view)
+FudStatus fud_string_view_skip_whitespace(StringView* view)
{
if (view == nullptr) {
return FudStatus::NullPointer;
@@ -761,7 +727,7 @@ FudStatus ext_string_view_skip_whitespace(StringView* view)
return FudStatus::Success;
}
-FudStatus ext_string_view_trim_whitespace(StringView* view)
+FudStatus fud_string_view_trim_whitespace(StringView* view)
{
if (view == nullptr) {
return FudStatus::NullPointer;
@@ -1086,42 +1052,42 @@ FudStatus stringViewToSignedInteger(StringView input, T* number, uint8_t specifi
} // namespace impl
-FudStatus ext_string_to_uint8(StringView input, uint8_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_uint8(StringView input, uint8_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToUnsignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_uint16(StringView input, uint16_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_uint16(StringView input, uint16_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToUnsignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_uint32(StringView input, uint32_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_uint32(StringView input, uint32_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToUnsignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_uint64(StringView input, uint64_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_uint64(StringView input, uint64_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToUnsignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_int8(StringView input, int8_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_int8(StringView input, int8_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToSignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_int16(StringView input, int16_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_int16(StringView input, int16_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToSignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_int32(StringView input, int32_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_int32(StringView input, int32_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToSignedInteger(input, number, specifiedRadix, index);
}
-FudStatus ext_string_to_int64(StringView input, int64_t* number, uint8_t specifiedRadix, size_t* index)
+FudStatus fud_string_to_int64(StringView input, int64_t* number, uint8_t specifiedRadix, size_t* index)
{
return impl::stringViewToSignedInteger(input, number, specifiedRadix, index);
}
@@ -1247,7 +1213,7 @@ FudStatus stringViewToFloat(StringView input, T& number, size_t& index)
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;
}
@@ -1346,17 +1312,17 @@ FudStatus stringViewToFloat(StringView input, T* number, size_t* index)
} // namespace impl
-FudStatus ext_string_to_float(StringView input, float* number, size_t* index)
+FudStatus fud_string_to_float(StringView input, float* number, size_t* index)
{
return impl::stringViewToFloat(input, number, index);
}
-FudStatus ext_string_to_double(StringView input, double* number, size_t* index)
+FudStatus fud_string_to_double(StringView input, double* number, size_t* index)
{
return impl::stringViewToFloat(input, number, index);
}
-namespace ext_lib {
+namespace fud {
FudStatus StringView::toUint8(uint8_t& number, uint8_t specifiedRadix, size_t& strLen) const
{