diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-23 21:06:27 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-23 21:06:27 -0500 |
| commit | 23040fad647634c08697451fc22ee2ca999629c8 (patch) | |
| tree | 72888f88c7728c82f3f6df1f4f70591de15eab36 /src/kernel/NeKit/KString.inl | |
| parent | e5cc7351f0577b54c528fb827a7c7e6306c3e843 (diff) | |
| parent | 83d870e58457a1d335a1d9b9966a6a1887cc297b (diff) | |
Merge pull request #81 from nekernel-org/dev
feat! breaking changes on kernel sources.
Diffstat (limited to 'src/kernel/NeKit/KString.inl')
| -rw-r--r-- | src/kernel/NeKit/KString.inl | 174 |
1 files changed, 174 insertions, 0 deletions
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 <typename CharKind> +inline void ort_string_append(CharKind* lhs, const CharKind* rhs, Int32 cur) { + SizeT sz_rhs = oe_string_len<CharKind>(rhs); + SizeT rhs_i = 0; + + for (; rhs_i < sz_rhs; ++rhs_i) { + lhs[rhs_i + cur] = rhs[rhs_i]; + } +} + +template <typename CharKind> +inline CharKind* KBasicString<CharKind>::Data() { + return this->fData; +} + +template <typename CharKind> +inline const CharKind* KBasicString<CharKind>::CData() const { + return const_cast<const CharKind*>(this->fData); +} + +template <typename CharKind> +inline SizeT KBasicString<CharKind>::Length() const { + return this->fDataSz; +} + +template <typename CharKind> +inline bool KBasicString<CharKind>::operator==(const KBasicString<CharKind>& 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 <typename CharKind> +inline bool KBasicString<CharKind>::operator==(const CharKind* rhs) const { + if (oe_string_len<CharKind>(rhs) != this->Length()) return false; + + for (Size index = 0; index < oe_string_len<CharKind>(rhs); ++index) { + if (rhs[index] != this->fData[index]) return false; + } + + return true; +} + +template <typename CharKind> +inline bool KBasicString<CharKind>::operator!=(const KBasicString<CharKind>& 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 <typename CharKind> +inline bool KBasicString<CharKind>::operator!=(const CharKind* rhs) const { + if (oe_string_len<CharKind>(rhs) != this->Length()) return false; + + for (Size index = 0; index < oe_string_len<CharKind>(rhs); ++index) { + if (rhs[index] == this->fData[index]) return false; + } + + return true; +} + +template <typename CharKind> +inline KBasicString<CharKind>& KBasicString<CharKind>::operator+=( + const KBasicString<CharKind>& rhs) { + if (oe_string_len<CharKind>(rhs.fData) > this->Length()) return *this; + + ort_string_append(this->fData, const_cast<CharKind*>(rhs.fData), this->fCur); + this->fCur += oe_string_len<CharKind>(const_cast<CharKind*>(rhs.fData)); + + return *this; +} + +template <typename CharKind> +inline KBasicString<CharKind>& KBasicString<CharKind>::operator+=(const CharKind* rhs) { + ort_string_append(this->fData, const_cast<CharKind*>(rhs), this->fCur); + this->fCur += oe_string_len<CharKind>(const_cast<CharKind*>(rhs)); + + return *this; +} + +template <typename CharKind> +inline ErrorOr<KBasicString<CharKind>> KStringBuilder::Construct(const CharKind* data) { + if (!data || *data == 0) return ErrorOr<KBasicString<CharKind>>(nullptr); + + KBasicString<CharKind>* view = new KBasicString<CharKind>(oe_string_len<CharKind>(data)); + (*view) += data; + + return ErrorOr<KBasicString<CharKind>>(*view); +} +template <typename CharKind> +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<CharKind>(boolean_expr) + oe_string_len<CharKind>(fmt)); + + if (!ret) return ("?"); + + const auto fmt_len = oe_string_len<CharKind>(fmt); + const auto res_len = oe_string_len<CharKind>(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 <typename CharKind> +inline bool KStringBuilder::Equals(const CharKind* lhs, const CharKind* rhs) { + if (oe_string_len<CharKind>(rhs) != oe_string_len<CharKind>(lhs)) return false; + + for (Size index = 0; index < oe_string_len<CharKind>(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } + + return true; +} +template <typename CharKind> +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<CharKind>(fmt2) + oe_string_len<CharKind>(fmt))); + + if (!ret) return ("?"); + + const auto len = oe_string_len<CharKind>(fmt); + + for (Size idx = 0; idx < len; ++idx) { + if (fmt[idx] == '%' && idx < oe_string_len<CharKind>(fmt) && fmt[idx] == 's') { + Size result_cnt = idx; + + for (Size y_idx = 0; y_idx < oe_string_len<CharKind>(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + } + + ret[idx] = fmt[idx]; + } + + return ret; +} +} // namespace Kernel |
