diff options
Diffstat (limited to 'Private/CompilerKit/StdKit/String.cc')
| -rw-r--r-- | Private/CompilerKit/StdKit/String.cc | 302 |
1 files changed, 124 insertions, 178 deletions
diff --git a/Private/CompilerKit/StdKit/String.cc b/Private/CompilerKit/StdKit/String.cc index 519ccff..958638d 100644 --- a/Private/CompilerKit/StdKit/String.cc +++ b/Private/CompilerKit/StdKit/String.cc @@ -13,241 +13,187 @@ * @brief C++ String Manip API. * @version 0.2 * @date 2024-01-23 - * + * * @copyright Copyright (c) 2024 Mahrouss Logic - * + * */ #include "String.hpp" + #include <utility> -namespace CompilerKit -{ - CharType* StringView::Data() - { - return m_Data; - } +namespace CompilerKit { +CharType *StringView::Data() { return m_Data; } - const CharType* StringView::CData() const - { - return m_Data; - } +const CharType *StringView::CData() const { return m_Data; } - SizeType StringView::Length() const - { - return strlen(m_Data); - } +SizeType StringView::Length() const { return strlen(m_Data); } - bool StringView::operator==(const StringView &rhs) const - { - if (rhs.Length() != Length()) - return false; +bool StringView::operator==(const StringView &rhs) const { + if (rhs.Length() != Length()) return false; - for (SizeType index = 0; index < Length(); ++index) - { - if (rhs.m_Data[index] != m_Data[index]) - return false; - } + for (SizeType index = 0; index < Length(); ++index) { + if (rhs.m_Data[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator==(const CharType *rhs) const - { - if (string_length(rhs) != Length()) - return false; +bool StringView::operator==(const CharType *rhs) const { + if (string_length(rhs) != Length()) return false; - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != m_Data[index]) - return false; - } + for (SizeType 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() != Length()) - return false; +bool StringView::operator!=(const StringView &rhs) const { + if (rhs.Length() != Length()) return false; - for (SizeType index = 0; index < rhs.Length(); ++index) - { - if (rhs.m_Data[index] == m_Data[index]) - return false; - } + for (SizeType index = 0; index < rhs.Length(); ++index) { + if (rhs.m_Data[index] == m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const CharType *rhs) const - { - if (string_length(rhs) != Length()) - return false; +bool StringView::operator!=(const CharType *rhs) const { + if (string_length(rhs) != Length()) return false; - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] == m_Data[index]) - return false; - } + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] == m_Data[index]) return false; + } - return true; - } - - StringView StringBuilder::Construct(const CharType *data) - { - if (!data || - *data == 0) - return StringView(0); + return true; +} - StringView view(strlen(data)); - view += data; - - return view; - } +StringView StringBuilder::Construct(const CharType *data) { + if (!data || *data == 0) return StringView(0); - const char* StringBuilder::FromInt(const char *fmt, int i) - { - if (!fmt) - return ("-1"); + StringView view(strlen(data)); + view += data; - char *ret = new char[8 + string_length(fmt)]; + return view; +} - if (!ret) - return ("-1"); +const char *StringBuilder::FromInt(const char *fmt, int i) { + if (!fmt) return ("-1"); - CharType result[8]; - if (!to_str(result, sizeof(int), i)) - { - delete[] ret; - return ("-1"); - } + char *ret = new char[8 + string_length(fmt)]; - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); + if (!ret) return ("-1"); - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeType result_cnt = idx; + CharType result[8]; + if (!to_str(result, sizeof(int), i)) { + delete[] ret; + 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 (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType 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 = new 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 = new char[i ? 4 : 5 + string_length(fmt)]; - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') - { - SizeType 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 (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType 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 (SizeType 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 ("?"); + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } - char *ret = new char[string_length(fmt2) + string_length(fmt2)]; - if (!ret) - return ("?"); + return true; +} - for (SizeType idx = 0; idx < string_length(fmt); ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; +const char *StringBuilder::Format(const char *fmt, const char *fmt2) { + if (!fmt || !fmt2) return ("?"); - for (SizeType y_idx = 0; y_idx < string_length(fmt2); ++y_idx) - { - ret[result_cnt] = fmt2[y_idx]; - ++result_cnt; - } + char *ret = new char[string_length(fmt2) + string_length(fmt2)]; + if (!ret) return ("?"); - break; - } + for (SizeType idx = 0; idx < string_length(fmt); ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (SizeType y_idx = 0; y_idx < string_length(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } - return ret; + break; } - StringView &StringView::operator+=(const CharType *rhs) - { - if (strlen(rhs) > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } + ret[idx] = fmt[idx]; + } + return ret; +} - memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); - this->m_Cur += strlen(rhs); +StringView &StringView::operator+=(const CharType *rhs) { + if (strlen(rhs) > this->m_Sz) { + throw std::runtime_error("out_of_bounds: StringView"); + } - return *this; - } + memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); + this->m_Cur += strlen(rhs); - StringView &StringView::operator+=(const StringView &rhs) - { - if (rhs.m_Cur > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } + return *this; +} - memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); - this->m_Cur += strlen(rhs.CData()); +StringView &StringView::operator+=(const StringView &rhs) { + if (rhs.m_Cur > this->m_Sz) { + throw std::runtime_error("out_of_bounds: StringView"); + } - return *this; - } -} // namespace CompilerKit + memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); + this->m_Cur += strlen(rhs.CData()); + + return *this; +} +} // namespace CompilerKit |
