summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/kernel/NeKit/KString.h45
-rw-r--r--dev/kernel/NeKit/KString.inl116
-rw-r--r--dev/kernel/NeKit/Utils.h12
-rw-r--r--dev/kernel/src/Network/IPAddress.cc4
4 files changed, 91 insertions, 86 deletions
diff --git a/dev/kernel/NeKit/KString.h b/dev/kernel/NeKit/KString.h
index bbe49f8e..ec883a2f 100644
--- a/dev/kernel/NeKit/KString.h
+++ b/dev/kernel/NeKit/KString.h
@@ -16,13 +16,13 @@
namespace Kernel {
/// @brief Kernel string class, not dynamic.
-template <SizeT MinSz = kMinimumStringSize>
+template <typename CharKind = Char>
class BasicKString final {
public:
explicit BasicKString() {
- fDataSz = MinSz;
+ fDataSz = kMinimumStringSize;
- fData = new Char[fDataSz];
+ fData = new CharKind[fDataSz];
MUST_PASS(fData);
rt_set_memory(fData, 0, fDataSz);
@@ -31,7 +31,7 @@ class BasicKString final {
explicit BasicKString(SizeT Sz) : fDataSz(Sz) {
MUST_PASS(Sz > 1);
- fData = new Char[Sz];
+ fData = new CharKind[Sz];
MUST_PASS(fData);
rt_set_memory(fData, 0, Sz);
@@ -46,18 +46,18 @@ class BasicKString final {
NE_COPY_DEFAULT(BasicKString)
- Char* Data();
- const Char* CData() const;
- Size Length() const;
+ CharKind* Data();
+ const CharKind* CData() const;
+ Size Length() const;
- bool operator==(const Char* rhs) const;
- bool operator!=(const Char* rhs) const;
+ bool operator==(const CharKind* rhs) const;
+ bool operator!=(const CharKind* rhs) const;
- bool operator==(const BasicKString<>& rhs) const;
- bool operator!=(const BasicKString<>& rhs) const;
+ bool operator==(const BasicKString<CharKind>& rhs) const;
+ bool operator!=(const BasicKString<CharKind>& rhs) const;
- BasicKString<>& operator+=(const Char* rhs);
- BasicKString<>& operator+=(const BasicKString<>& rhs);
+ BasicKString<CharKind>& operator+=(const CharKind* rhs);
+ BasicKString<CharKind>& operator+=(const BasicKString<CharKind>& rhs);
operator const char*() { return fData; }
@@ -66,9 +66,9 @@ class BasicKString final {
bool operator!() { return fData; }
private:
- Char* fData{nullptr};
- Size fDataSz{0};
- Size fCur{0};
+ CharKind* fData{nullptr};
+ Size fDataSz{0};
+ Size fCur{0};
friend class KStringBuilder;
};
@@ -78,11 +78,14 @@ using KStringOr = ErrorOr<KString>;
class KStringBuilder final {
public:
- static ErrorOr<KString> Construct(const Char* data);
- static const Char* FromBool(const Char* fmt, bool n);
- 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);
+ template <typename CharKind = Char>
+ static ErrorOr<BasicKString<CharKind>> Construct(const CharKind* data);
+ template <typename CharKind = Char>
+ static const CharKind* FromBool(const CharKind* fmt, bool n);
+ template <typename CharKind = Char>
+ static const CharKind* Format(const CharKind* fmt, const CharKind* from);
+ template <typename CharKind = Char>
+ static bool Equals(const CharKind* lhs, const CharKind* rhs);
};
} // namespace Kernel
diff --git a/dev/kernel/NeKit/KString.inl b/dev/kernel/NeKit/KString.inl
index 1faefb08..4bb48415 100644
--- a/dev/kernel/NeKit/KString.inl
+++ b/dev/kernel/NeKit/KString.inl
@@ -4,14 +4,13 @@
------------------------------------------- */
-#include <NeKit/Utils.h>
-
-/// @file BasicKString<>.cc
+/// @file BasicKString.inl
/// @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);
+template <typename CharKind>
+inline void ort_string_append(CharKind* lhs, const CharKind* rhs, Int32 cur) {
+ SizeT sz_rhs = ort_string_len<CharKind>(rhs);
SizeT rhs_i = 0;
for (; rhs_i < sz_rhs; ++rhs_i) {
@@ -19,23 +18,23 @@ inline void rt_string_append(Char* lhs, const Char* rhs, Int32 cur) {
}
}
-template <>
-inline Char* BasicKString<>::Data() {
+template <typename CharKind>
+inline CharKind* BasicKString<CharKind>::Data() {
return this->fData;
}
-template <>
-inline const Char* BasicKString<>::CData() const {
- return const_cast<const Char*>(this->fData);
+template <typename CharKind>
+inline const CharKind* BasicKString<CharKind>::CData() const {
+ return const_cast<const CharKind*>(this->fData);
}
-template <>
-inline SizeT BasicKString<>::Length() const {
+template <typename CharKind>
+inline SizeT BasicKString<CharKind>::Length() const {
return this->fDataSz;
}
-template <>
-inline bool BasicKString<>::operator==(const BasicKString<>& rhs) const {
+template <typename CharKind>
+inline bool BasicKString<CharKind>::operator==(const BasicKString<CharKind>& rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < this->Length(); ++index) {
@@ -45,19 +44,19 @@ inline bool BasicKString<>::operator==(const BasicKString<>& rhs) const {
return true;
}
-template <>
-inline bool BasicKString<>::operator==(const Char* rhs) const {
- if (rt_string_len(rhs) != this->Length()) return false;
+template <typename CharKind>
+inline bool BasicKString<CharKind>::operator==(const CharKind* rhs) const {
+ if (ort_string_len<CharKind>(rhs) != this->Length()) return false;
- for (Size index = 0; index < rt_string_len(rhs); ++index) {
+ for (Size index = 0; index < ort_string_len<CharKind>(rhs); ++index) {
if (rhs[index] != this->fData[index]) return false;
}
return true;
}
-template <>
-inline bool BasicKString<>::operator!=(const BasicKString<>& rhs) const {
+template <typename CharKind>
+inline bool BasicKString<CharKind>::operator!=(const BasicKString<CharKind>& rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < rhs.Length(); ++index) {
@@ -67,54 +66,55 @@ inline bool BasicKString<>::operator!=(const BasicKString<>& rhs) const {
return true;
}
-template <>
-inline bool BasicKString<>::operator!=(const Char* rhs) const {
- if (rt_string_len(rhs) != this->Length()) return false;
+template <typename CharKind>
+inline bool BasicKString<CharKind>::operator!=(const CharKind* rhs) const {
+ if (ort_string_len<CharKind>(rhs) != this->Length()) return false;
- for (Size index = 0; index < rt_string_len(rhs); ++index) {
+ for (Size index = 0; index < ort_string_len<CharKind>(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;
+template <typename CharKind>
+inline BasicKString<CharKind>& BasicKString<CharKind>::operator+=(const BasicKString<CharKind>& rhs) {
+ if (ort_string_len<CharKind>(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));
+ ort_string_append(this->fData, const_cast<CharKind*>(rhs.fData), this->fCur);
+ this->fCur += ort_string_len<CharKind>(const_cast<CharKind*>(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));
+template <typename CharKind>
+inline BasicKString<CharKind>& BasicKString<CharKind>::operator+=(const CharKind* rhs) {
+ ort_string_append(this->fData, const_cast<CharKind*>(rhs), this->fCur);
+ this->fCur += ort_string_len<CharKind>(const_cast<CharKind*>(rhs));
return *this;
}
-inline ErrorOr<BasicKString<>> KStringBuilder::Construct(const Char* data) {
- if (!data || *data == 0) return ErrorOr<BasicKString<>>(new BasicKString<>(0));
+template <typename CharKind>
+inline ErrorOr<BasicKString<CharKind>> KStringBuilder::Construct(const CharKind* data) {
+ if (!data || *data == 0) return ErrorOr<BasicKString<CharKind>>(nullptr);
- BasicKString<>* view = new BasicKString<>(rt_string_len(data));
+ BasicKString<CharKind>* view = new BasicKString<CharKind>(ort_string_len<CharKind>(data));
(*view) += data;
- return ErrorOr<BasicKString<>>(*view);
+ return ErrorOr<BasicKString<CharKind>>(*view);
}
-
-inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) {
+template <typename CharKind>
+inline const CharKind* KStringBuilder::FromBool(const CharKind* 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));
+ const CharKind* boolean_expr = i ? "YES" : "NO";
+ CharKind* ret = (CharKind*) RTL_ALLOCA(ort_string_len<CharKind>(boolean_expr) + ort_string_len<CharKind>(fmt));
if (!ret) return ("?");
- const auto fmt_len = rt_string_len(fmt);
- const auto res_len = rt_string_len(boolean_expr);
+ const auto fmt_len = ort_string_len<CharKind>(fmt);
+ const auto res_len = ort_string_len<CharKind>(boolean_expr);
for (Size idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
@@ -133,41 +133,31 @@ inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) {
return ret;
}
+template <typename CharKind>
+inline bool KStringBuilder::Equals(const CharKind* lhs, const CharKind* rhs) {
+ if (ort_string_len<CharKind>(rhs) != ort_string_len<CharKind>(lhs)) return false;
-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) {
+ for (Size index = 0; index < ort_string_len<CharKind>(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) {
+template <typename CharKind>
+inline const CharKind* KStringBuilder::Format(const CharKind* fmt, const CharKind* fmt2) {
if (!fmt || !fmt2) return ("?");
- Char* ret = (Char*) RTL_ALLOCA(sizeof(char) * (rt_string_len(fmt2) + rt_string_len(fmt)));
+ CharKind* ret = (CharKind*) RTL_ALLOCA(sizeof(char) * (ort_string_len<CharKind>(fmt2) + ort_string_len<CharKind>(fmt)));
if (!ret) return ("?");
- const auto len = rt_string_len(fmt);
+ const auto len = ort_string_len<CharKind>(fmt);
for (Size idx = 0; idx < len; ++idx) {
- if (fmt[idx] == '%' && idx < rt_string_len(fmt) && fmt[idx] == 's') {
+ if (fmt[idx] == '%' && idx < ort_string_len<CharKind>(fmt) && fmt[idx] == 's') {
Size result_cnt = idx;
- for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) {
+ for (Size y_idx = 0; y_idx < ort_string_len<CharKind>(fmt2); ++y_idx) {
ret[result_cnt] = fmt2[y_idx];
++result_cnt;
}
diff --git a/dev/kernel/NeKit/Utils.h b/dev/kernel/NeKit/Utils.h
index a7576e77..ee18d04c 100644
--- a/dev/kernel/NeKit/Utils.h
+++ b/dev/kernel/NeKit/Utils.h
@@ -38,4 +38,16 @@ 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);
+
+/// OpenTemplate UTILS API
+
+template <typename CharType = Char>
+inline SizeT ort_string_len(const CharType* str) {
+ if (!str) return 0;
+
+ SizeT len{0};
+
+ while (str[len] != 0) ++len;
+ return len;
+}
} // namespace Kernel
diff --git a/dev/kernel/src/Network/IPAddress.cc b/dev/kernel/src/Network/IPAddress.cc
index b02eae08..bc46292b 100644
--- a/dev/kernel/src/Network/IPAddress.cc
+++ b/dev/kernel/src/Network/IPAddress.cc
@@ -73,14 +73,14 @@ bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) {
/// @todo
ErrorOr<KString> IPFactory::ToKString(Ref<RawIPAddress6>& ipv6) {
NE_UNUSED(ipv6);
- auto str = KStringBuilder::Construct(0);
+ auto str = KStringBuilder::Construct("");
return str;
}
/// @todo
ErrorOr<KString> IPFactory::ToKString(Ref<RawIPAddress>& ipv4) {
NE_UNUSED(ipv4);
- auto str = KStringBuilder::Construct(0);
+ auto str = KStringBuilder::Construct("");
return str;
}