diff options
Diffstat (limited to 'dev/kernel/NeKit')
| -rw-r--r-- | dev/kernel/NeKit/Json.h | 14 | ||||
| -rw-r--r-- | dev/kernel/NeKit/KString.h | 29 | ||||
| -rw-r--r-- | dev/kernel/NeKit/KString.inl | 181 | ||||
| -rw-r--r-- | dev/kernel/NeKit/Macros.h | 2 | ||||
| -rw-r--r-- | dev/kernel/NeKit/MutableArray.h | 20 | ||||
| -rw-r--r-- | dev/kernel/NeKit/OwnPtr.h | 3 | ||||
| -rw-r--r-- | dev/kernel/NeKit/Utils.h | 2 |
7 files changed, 219 insertions, 32 deletions
diff --git a/dev/kernel/NeKit/Json.h b/dev/kernel/NeKit/Json.h index 2b2c9c04..24357dd7 100644 --- a/dev/kernel/NeKit/Json.h +++ b/dev/kernel/NeKit/Json.h @@ -25,8 +25,8 @@ namespace Kernel { class Json final { public: explicit Json() { - auto len = kJSONMaxLen; - KString key = KString(len); + auto len = kJSONMaxLen; + BasicKString<> key = KString(len); key += kJSONNullObj; this->AsKey() = key; @@ -42,18 +42,18 @@ class Json final { Bool& IsUndefined() { return fUndefined; } private: - Bool fUndefined; // is this instance undefined? - KString fKey; - KString fValue; + Bool fUndefined; // is this instance undefined? + BasicKString<> fKey; + BasicKString<> fValue; public: /// @brief returns the key of the json /// @return the key as string view. - KString& AsKey() { return fKey; } + BasicKString<>& AsKey() { return fKey; } /// @brief returns the value of the json. /// @return the key as string view. - KString& AsValue() { return fValue; } + BasicKString<>& AsValue() { return fValue; } static Json kNull; }; diff --git a/dev/kernel/NeKit/KString.h b/dev/kernel/NeKit/KString.h index 16b09a78..bbe49f8e 100644 --- a/dev/kernel/NeKit/KString.h +++ b/dev/kernel/NeKit/KString.h @@ -16,10 +16,11 @@ namespace Kernel { /// @brief Kernel string class, not dynamic. -class KString final { +template <SizeT MinSz = kMinimumStringSize> +class BasicKString final { public: - explicit KString() { - fDataSz = kMinimumStringSize; + explicit BasicKString() { + fDataSz = MinSz; fData = new Char[fDataSz]; MUST_PASS(fData); @@ -27,7 +28,7 @@ class KString final { rt_set_memory(fData, 0, fDataSz); } - explicit KString(SizeT Sz) : fDataSz(Sz) { + explicit BasicKString(SizeT Sz) : fDataSz(Sz) { MUST_PASS(Sz > 1); fData = new Char[Sz]; @@ -36,14 +37,14 @@ class KString final { rt_set_memory(fData, 0, Sz); } - ~KString() { + ~BasicKString() { if (fData) { delete[] fData; fData = nullptr; } } - NE_COPY_DEFAULT(KString) + NE_COPY_DEFAULT(BasicKString) Char* Data(); const Char* CData() const; @@ -52,11 +53,13 @@ class KString final { bool operator==(const Char* rhs) const; bool operator!=(const Char* rhs) const; - bool operator==(const KString& rhs) const; - bool operator!=(const KString& rhs) const; + bool operator==(const BasicKString<>& rhs) const; + bool operator!=(const BasicKString<>& rhs) const; - KString& operator+=(const Char* rhs); - KString& operator+=(const KString& rhs); + BasicKString<>& operator+=(const Char* rhs); + BasicKString<>& operator+=(const BasicKString<>& rhs); + + operator const char*() { return fData; } operator bool() { return fData; } @@ -70,6 +73,9 @@ class KString final { friend class KStringBuilder; }; +using KString = BasicKString<>; +using KStringOr = ErrorOr<KString>; + class KStringBuilder final { public: static ErrorOr<KString> Construct(const Char* data); @@ -77,6 +83,7 @@ class KStringBuilder final { static const Char* Format(const Char* fmt, const Char* from); static bool Equals(const Char* lhs, const Char* rhs); static bool Equals(const Utf8Char* lhs, const Utf8Char* rhs); - static bool Equals(const WideChar* lhs, const WideChar* rhs); }; } // namespace Kernel + +#include <NeKit/KString.inl> diff --git a/dev/kernel/NeKit/KString.inl b/dev/kernel/NeKit/KString.inl new file mode 100644 index 00000000..1faefb08 --- /dev/null +++ b/dev/kernel/NeKit/KString.inl @@ -0,0 +1,181 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include <NeKit/Utils.h> + +/// @file BasicKString<>.cc +/// @brief Kernel String manipulation file. + +namespace Kernel { +inline void rt_string_append(Char* lhs, const Char* rhs, Int32 cur) { + SizeT sz_rhs = rt_string_len(rhs); + SizeT rhs_i = 0; + + for (; rhs_i < sz_rhs; ++rhs_i) { + lhs[rhs_i + cur] = rhs[rhs_i]; + } +} + +template <> +inline Char* BasicKString<>::Data() { + return this->fData; +} + +template <> +inline const Char* BasicKString<>::CData() const { + return const_cast<const Char*>(this->fData); +} + +template <> +inline SizeT BasicKString<>::Length() const { + return this->fDataSz; +} + +template <> +inline bool BasicKString<>::operator==(const BasicKString<>& 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 BasicKString<>::operator==(const Char* rhs) const { + if (rt_string_len(rhs) != this->Length()) return false; + + for (Size index = 0; index < rt_string_len(rhs); ++index) { + if (rhs[index] != this->fData[index]) return false; + } + + return true; +} + +template <> +inline bool BasicKString<>::operator!=(const BasicKString<>& 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 BasicKString<>::operator!=(const Char* rhs) const { + if (rt_string_len(rhs) != this->Length()) return false; + + for (Size index = 0; index < rt_string_len(rhs); ++index) { + if (rhs[index] == this->fData[index]) return false; + } + + return true; +} + +template <> +inline BasicKString<>& BasicKString<>::operator+=(const BasicKString<>& rhs) { + if (rt_string_len(rhs.fData) > this->Length()) return *this; + + rt_string_append(this->fData, const_cast<Char*>(rhs.fData), this->fCur); + this->fCur += rt_string_len(const_cast<Char*>(rhs.fData)); + + return *this; +} + +template <> +inline BasicKString<>& BasicKString<>::operator+=(const Char* rhs) { + rt_string_append(this->fData, const_cast<Char*>(rhs), this->fCur); + this->fCur += rt_string_len(const_cast<Char*>(rhs)); + + return *this; +} + +inline ErrorOr<BasicKString<>> KStringBuilder::Construct(const Char* data) { + if (!data || *data == 0) return ErrorOr<BasicKString<>>(new BasicKString<>(0)); + + BasicKString<>* view = new BasicKString<>(rt_string_len(data)); + (*view) += data; + + return ErrorOr<BasicKString<>>(*view); +} + +inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) { + if (!fmt) return ("?"); + + const Char* boolean_expr = i ? "YES" : "NO"; + Char* ret = (Char*) RTL_ALLOCA(rt_string_len(boolean_expr) + rt_string_len(fmt)); + + if (!ret) return ("?"); + + const auto fmt_len = rt_string_len(fmt); + const auto res_len = rt_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; +} + +inline bool KStringBuilder::Equals(const Char* lhs, const Char* rhs) { + if (rt_string_len(rhs) != rt_string_len(lhs)) return false; + + for (Size index = 0; index < rt_string_len(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } + + return true; +} + +inline bool KStringBuilder::Equals(const Utf8Char* lhs, const Utf8Char* rhs) { + if (urt_string_len(rhs) != urt_string_len(lhs)) return false; + + for (Size index = 0; rhs[index] != 0; ++index) { + if (rhs[index] != lhs[index]) return false; + } + + return true; +} + +inline const Char* KStringBuilder::Format(const Char* fmt, const Char* fmt2) { + if (!fmt || !fmt2) return ("?"); + + Char* ret = (Char*) RTL_ALLOCA(sizeof(char) * (rt_string_len(fmt2) + rt_string_len(fmt))); + + if (!ret) return ("?"); + + const auto len = rt_string_len(fmt); + + for (Size idx = 0; idx < len; ++idx) { + if (fmt[idx] == '%' && idx < rt_string_len(fmt) && fmt[idx] == 's') { + Size result_cnt = idx; + + for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + } + + ret[idx] = fmt[idx]; + } + + return ret; +} +} // namespace Kernel diff --git a/dev/kernel/NeKit/Macros.h b/dev/kernel/NeKit/Macros.h index e80e2e47..b46ffaa8 100644 --- a/dev/kernel/NeKit/Macros.h +++ b/dev/kernel/NeKit/Macros.h @@ -16,7 +16,7 @@ #endif #ifndef kib_cast -#define kib_cast(X) (Kernel::UInt64)((X) * 1024) +#define kib_cast(X) (Kernel::UInt64)((X) *1024) #endif #ifndef MIB diff --git a/dev/kernel/NeKit/MutableArray.h b/dev/kernel/NeKit/MutableArray.h index 8dee6e03..02c8dc2d 100644 --- a/dev/kernel/NeKit/MutableArray.h +++ b/dev/kernel/NeKit/MutableArray.h @@ -9,21 +9,21 @@ #include <NeKit/Array.h> #include <NeKit/Defines.h> -#define TRY_FIND_NODE(NAME, NODE) \ +#define RTL_TRY_FIND_NODE(NAME, NODE) \ auto* NAME = NODE; \ while (NAME) { \ if (NAME->fIndex == Index) return NAME->fVal; \ NAME = NAME->fNext; \ } -#define TRY_FIND_NODE2(NAME, NODE) \ +#define RTL_TRY_FIND_NODE2(NAME, NODE) \ auto* NAME = NODE; \ while (NAME) { \ if (NAME->fIndex == Index) return Ref<T>{NAME->fVal}; \ NAME = NAME->fNext; \ } -#define TRY_REMOVE_NODE(NODE) \ +#define RTL_TRY_REMOVE_NODE(NODE) \ if (NODE && NODE->fIndex == Index) { \ NODE->fUsed = false; \ NODE->fIndex = 0; \ @@ -31,7 +31,7 @@ return true; \ } -// FIXME: this is a shitty algorithm, which is consumer hungry. +// FIXME: this is a shitty algorithm, because it is memory heavy. // Remove and occurences of that, and remove that class. namespace Kernel { template <typename T> @@ -81,8 +81,8 @@ class NullableMutableArray { public: T operator[](SizeT Index) const { - TRY_FIND_NODE(first, fFirstNode); - TRY_FIND_NODE(last, fLastNode); + RTL_TRY_FIND_NODE(first, fFirstNode); + RTL_TRY_FIND_NODE(last, fLastNode); return _PlaceHolderValue; } @@ -91,8 +91,8 @@ class NullableMutableArray { public: Boolean Remove(SizeT Index) { - TRY_REMOVE_NODE(fFirstNode); - TRY_REMOVE_NODE(fLastNode); + RTL_TRY_REMOVE_NODE(fFirstNode); + RTL_TRY_REMOVE_NODE(fLastNode); return false; } @@ -172,8 +172,8 @@ class MutableArray : public NullableMutableArray<voidPtr, nullptr> { public: Ref<T> operator[](SizeT Index) const { - TRY_FIND_NODE2(first, fFirstNode); - TRY_FIND_NODE2(last, fLastNode); + RTL_TRY_FIND_NODE2(first, fFirstNode); + RTL_TRY_FIND_NODE2(last, fLastNode); return {}; } diff --git a/dev/kernel/NeKit/OwnPtr.h b/dev/kernel/NeKit/OwnPtr.h index f5ff4b54..22435118 100644 --- a/dev/kernel/NeKit/OwnPtr.h +++ b/dev/kernel/NeKit/OwnPtr.h @@ -50,7 +50,7 @@ class OwnPtr final { Ref<T> AsRef() { return Ref<T>(fCls); } - operator bool() { return fCls; } + operator bool() { return fCls; } bool operator!() { return !fCls; } private: @@ -61,7 +61,6 @@ template <typename T, typename... Args> inline OwnPtr<T> mm_make_own_ptr(Args... args) { OwnPtr<T> ret; ret.template New<Args...>(forward(args)...); - MUST_PASS(ret); return ret; } diff --git a/dev/kernel/NeKit/Utils.h b/dev/kernel/NeKit/Utils.h index 11566008..a7576e77 100644 --- a/dev/kernel/NeKit/Utils.h +++ b/dev/kernel/NeKit/Utils.h @@ -34,7 +34,7 @@ voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size); /// UNICODE API -Int urt_string_cmp(const Char* src, const Char* cmp, Size len); +Int urt_string_cmp(const Utf8Char* src, const Utf8Char* cmp, Size len); Void urt_set_memory(const voidPtr src, UInt32 dst, Size len); Int urt_copy_memory(const voidPtr src, voidPtr dst, Size len); Size urt_string_len(const Utf8Char* str); |
