diff options
Diffstat (limited to 'Private/Source/String.cxx')
| -rw-r--r-- | Private/Source/String.cxx | 309 |
1 files changed, 132 insertions, 177 deletions
diff --git a/Private/Source/String.cxx b/Private/Source/String.cxx index 3ad2a0cd..b4f95f6c 100644 --- a/Private/Source/String.cxx +++ b/Private/Source/String.cxx @@ -10,230 +10,185 @@ #include <NewKit/String.hpp> #include <NewKit/Utils.hpp> -namespace hCore -{ - Char* StringView::Data() { return m_Data; } +namespace hCore { +Char *StringView::Data() { return m_Data; } - const Char* StringView::CData() { return m_Data; } +const Char *StringView::CData() { return m_Data; } - Size StringView::Length() const { return string_length(m_Data); } +Size StringView::Length() const { return string_length(m_Data); } - bool StringView::operator==(const StringView &rhs) const - { - if (rhs.Length() != this->Length()) - return false; +bool StringView::operator==(const StringView &rhs) const { + if (rhs.Length() != this->Length()) return false; - for (Size index = 0; index < this->Length(); ++index) - { - if (rhs.m_Data[index] != m_Data[index]) - return false; - } + for (Size index = 0; index < this->Length(); ++index) { + if (rhs.m_Data[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator==(const Char *rhs) const - { - if (string_length(rhs) != this->Length()) - return false; +bool StringView::operator==(const Char *rhs) const { + if (string_length(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != m_Data[index]) - return false; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const StringView &rhs) const - { - if (rhs.Length() != this->Length()) - return false; +bool StringView::operator!=(const StringView &rhs) const { + if (rhs.Length() != this->Length()) return false; - for (Size index = 0; index < rhs.Length(); ++index) - { - if (rhs.m_Data[index] == m_Data[index]) - return false; - } + for (Size index = 0; index < rhs.Length(); ++index) { + if (rhs.m_Data[index] == m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const Char *rhs) const - { - if (string_length(rhs) != this->Length()) - return false; +bool StringView::operator!=(const Char *rhs) const { + if (string_length(rhs) != this->Length()) return false; - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] == m_Data[index]) - return false; - } - - return true; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] == m_Data[index]) return false; + } - ErrorOr<StringView> StringBuilder::Construct(const Char *data) - { - if (!data || - *data == 0) - return {}; + return true; +} - StringView view(string_length(data)); - - rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char*>(data)), reinterpret_cast<voidPtr>(view.Data()), - view.Length()); +ErrorOr<StringView> StringBuilder::Construct(const Char *data) { + if (!data || *data == 0) return {}; - return ErrorOr<StringView>(view); - } + StringView view(string_length(data)); - const char* StringBuilder::FromInt(const char *fmt, int i) - { - if (!fmt) - return ("-1"); + rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char *>(data)), + reinterpret_cast<voidPtr>(view.Data()), view.Length()); - char* ret = (char*)__alloca(sizeof(char) * 8 + string_length(fmt)); + return ErrorOr<StringView>(view); +} - if (!ret) - return ("-1"); +const char *StringBuilder::FromInt(const char *fmt, int i) { + if (!fmt) return ("-1"); - Char result[8]; + char *ret = (char *)__alloca(sizeof(char) * 8 + string_length(fmt)); - if (!to_str(result, sizeof(int), i)) - { - return ("-1"); - } + if (!ret) return ("-1"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); + Char result[8]; - for (Size idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeT result_cnt = idx; + if (!to_str(result, sizeof(int), i)) { + return ("-1"); + } - for (auto y_idx = idx; y_idx < res_len; ++y_idx) { - ret[result_cnt] = result[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); - break; - } + for (Size idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeT result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = result[y_idx]; + ++result_cnt; + } - return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ + break; } - const char *StringBuilder::FromBool(const char *fmt, bool i) - { - if (!fmt) - return ("?"); + ret[idx] = fmt[idx]; + } - const char* boolean_expr = i ? "true" : "false"; - char* ret = (char*)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt)); + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ +} - if (!ret) - return ("?"); +const char *StringBuilder::FromBool(const char *fmt, bool i) { + if (!fmt) return ("?"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(boolean_expr); + const char *boolean_expr = i ? "true" : "false"; + char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + string_length(fmt)); - for (Size idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeT result_cnt = idx; + if (!ret) return ("?"); - for (auto y_idx = idx; y_idx < res_len; ++y_idx) - { - ret[result_cnt] = boolean_expr[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); - break; - } + for (Size idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeT result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } - return ret; + break; } - bool StringBuilder::Equals(const char *lhs, const char *rhs) - { - if (string_length(rhs) != string_length(lhs)) - return false; + ret[idx] = fmt[idx]; + } - for (Size index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != lhs[index]) - return false; - } + return ret; +} - return true; - } +bool StringBuilder::Equals(const char *lhs, const char *rhs) { + if (string_length(rhs) != string_length(lhs)) return false; - const char *StringBuilder::Format(const char *fmt, const char *fmt2) - { - if (!fmt || !fmt2) - return ("?"); - - char* ret = (char*)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2)); - - if (!ret) - return ("?"); - - for (Size idx = 0; idx < string_length(fmt); ++idx) - { - if (fmt[idx] == '%') { - Size result_cnt = idx; - for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) - { - ret[result_cnt] = fmt2[y_idx]; - ++result_cnt; - } - - break; - } - - ret[idx] = fmt[idx]; - } - - return ret; - } + for (Size index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } - static void string_append(char *lhs, char *rhs, int cur) - { - if (lhs && rhs && - cur < string_length(lhs)) - { - SizeT sz_rhs = string_length(rhs); + return true; +} - rt_copy_memory(rhs, lhs + cur, sz_rhs); - } - } +const char *StringBuilder::Format(const char *fmt, const char *fmt2) { + if (!fmt || !fmt2) return ("?"); + + char *ret = + (char *)alloca(sizeof(char) * string_length(fmt2) + string_length(fmt2)); - StringView& StringView::operator+=(const Char *rhs) - { - if (string_length(rhs) > string_length(this->m_Data)) - return *this; + if (!ret) return ("?"); - string_append(this->m_Data, const_cast<char*>(rhs), this->m_Cur); - this->m_Cur += string_length(rhs); - - return *this; + for (Size idx = 0; idx < string_length(fmt); ++idx) { + if (fmt[idx] == '%') { + Size result_cnt = idx; + for (Size y_idx = 0; y_idx < string_length(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + + break; } - StringView& StringView::operator+=(const StringView &rhs) - { - if (string_length(rhs.m_Data) > string_length(this->m_Data)) - return *this; + ret[idx] = fmt[idx]; + } - string_append(this->m_Data, const_cast<char*>(rhs.m_Data), this->m_Cur); - this->m_Cur += string_length(const_cast<char*>(rhs.m_Data)); + return ret; +} - return *this; - } -} // namespace hCore +static void string_append(char *lhs, char *rhs, int cur) { + if (lhs && rhs && cur < string_length(lhs)) { + SizeT sz_rhs = string_length(rhs); + + rt_copy_memory(rhs, lhs + cur, sz_rhs); + } +} + +StringView &StringView::operator+=(const Char *rhs) { + if (string_length(rhs) > string_length(this->m_Data)) return *this; + + string_append(this->m_Data, const_cast<char *>(rhs), this->m_Cur); + this->m_Cur += string_length(rhs); + + return *this; +} + +StringView &StringView::operator+=(const StringView &rhs) { + if (string_length(rhs.m_Data) > string_length(this->m_Data)) return *this; + + string_append(this->m_Data, const_cast<char *>(rhs.m_Data), this->m_Cur); + this->m_Cur += string_length(const_cast<char *>(rhs.m_Data)); + + return *this; +} +} // namespace hCore |
