From 83d870e58457a1d335a1d9b9966a6a1887cc297b Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 24 Nov 2025 03:02:43 +0100 Subject: feat! breaking changes on kernel sources. Signed-off-by: Amlal El Mahrouss --- src/kernel/NeKit/KString.inl | 174 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/kernel/NeKit/KString.inl (limited to 'src/kernel/NeKit/KString.inl') diff --git a/src/kernel/NeKit/KString.inl b/src/kernel/NeKit/KString.inl new file mode 100644 index 00000000..3a73e90f --- /dev/null +++ b/src/kernel/NeKit/KString.inl @@ -0,0 +1,174 @@ +/* ======================================== + + Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + +======================================== */ + +/// @file KBasicString.inl +/// @brief Kernel String manipulation file. + +namespace Kernel { +template +inline void ort_string_append(CharKind* lhs, const CharKind* rhs, Int32 cur) { + SizeT sz_rhs = oe_string_len(rhs); + SizeT rhs_i = 0; + + for (; rhs_i < sz_rhs; ++rhs_i) { + lhs[rhs_i + cur] = rhs[rhs_i]; + } +} + +template +inline CharKind* KBasicString::Data() { + return this->fData; +} + +template +inline const CharKind* KBasicString::CData() const { + return const_cast(this->fData); +} + +template +inline SizeT KBasicString::Length() const { + return this->fDataSz; +} + +template +inline bool KBasicString::operator==(const KBasicString& rhs) const { + if (rhs.Length() != this->Length()) return false; + + for (Size index = 0; index < this->Length(); ++index) { + if (rhs.fData[index] != this->fData[index]) return false; + } + + return true; +} + +template +inline bool KBasicString::operator==(const CharKind* rhs) const { + if (oe_string_len(rhs) != this->Length()) return false; + + for (Size index = 0; index < oe_string_len(rhs); ++index) { + if (rhs[index] != this->fData[index]) return false; + } + + return true; +} + +template +inline bool KBasicString::operator!=(const KBasicString& rhs) const { + if (rhs.Length() != this->Length()) return false; + + for (Size index = 0; index < rhs.Length(); ++index) { + if (rhs.fData[index] == this->fData[index]) return false; + } + + return true; +} + +template +inline bool KBasicString::operator!=(const CharKind* rhs) const { + if (oe_string_len(rhs) != this->Length()) return false; + + for (Size index = 0; index < oe_string_len(rhs); ++index) { + if (rhs[index] == this->fData[index]) return false; + } + + return true; +} + +template +inline KBasicString& KBasicString::operator+=( + const KBasicString& rhs) { + if (oe_string_len(rhs.fData) > this->Length()) return *this; + + ort_string_append(this->fData, const_cast(rhs.fData), this->fCur); + this->fCur += oe_string_len(const_cast(rhs.fData)); + + return *this; +} + +template +inline KBasicString& KBasicString::operator+=(const CharKind* rhs) { + ort_string_append(this->fData, const_cast(rhs), this->fCur); + this->fCur += oe_string_len(const_cast(rhs)); + + return *this; +} + +template +inline ErrorOr> KStringBuilder::Construct(const CharKind* data) { + if (!data || *data == 0) return ErrorOr>(nullptr); + + KBasicString* view = new KBasicString(oe_string_len(data)); + (*view) += data; + + return ErrorOr>(*view); +} +template +inline const CharKind* KStringBuilder::FromBool(const CharKind* fmt, bool i) { + if (!fmt) return ("?"); + + const CharKind* boolean_expr = i ? "YES" : "NO"; + CharKind* ret = + (CharKind*) RTL_ALLOCA(oe_string_len(boolean_expr) + oe_string_len(fmt)); + + if (!ret) return ("?"); + + const auto fmt_len = oe_string_len(fmt); + const auto res_len = oe_string_len(boolean_expr); + + for (Size idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeT result_cnt = idx; + + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; +} +template +inline bool KStringBuilder::Equals(const CharKind* lhs, const CharKind* rhs) { + if (oe_string_len(rhs) != oe_string_len(lhs)) return false; + + for (Size index = 0; index < oe_string_len(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } + + return true; +} +template +inline const CharKind* KStringBuilder::Format(const CharKind* fmt, const CharKind* fmt2) { + if (!fmt || !fmt2) return ("?"); + + CharKind* ret = (CharKind*) RTL_ALLOCA( + sizeof(char) * (oe_string_len(fmt2) + oe_string_len(fmt))); + + if (!ret) return ("?"); + + const auto len = oe_string_len(fmt); + + for (Size idx = 0; idx < len; ++idx) { + if (fmt[idx] == '%' && idx < oe_string_len(fmt) && fmt[idx] == 's') { + Size result_cnt = idx; + + for (Size y_idx = 0; y_idx < oe_string_len(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + } + + ret[idx] = fmt[idx]; + } + + return ret; +} +} // namespace Kernel -- cgit v1.2.3