// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/ne-foss-org/nekernel /// @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>(-kErrorInvalidData); 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