summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/kernel/CFKit/Property.h4
-rw-r--r--dev/kernel/FSKit/IndexableProperty.h2
-rw-r--r--dev/kernel/NeKit/Json.h10
-rw-r--r--dev/kernel/NeKit/KString.h28
-rw-r--r--dev/kernel/NeKit/KString.inl (renamed from dev/kernel/src/KString.cc)101
-rw-r--r--dev/kernel/src/Property.cc4
-rw-r--r--dev/kernel/src/UtfUtils.cc2
7 files changed, 79 insertions, 72 deletions
diff --git a/dev/kernel/CFKit/Property.h b/dev/kernel/CFKit/Property.h
index 9d35dc60..e872896d 100644
--- a/dev/kernel/CFKit/Property.h
+++ b/dev/kernel/CFKit/Property.h
@@ -32,9 +32,9 @@ class Property {
Property& operator=(const Property&) = default;
Property(const Property&) = default;
- BOOL StringEquals(KString& name);
+ BOOL StringEquals(BasicKString<>& name);
PropertyId& GetValue();
- KString& GetKey();
+ BasicKString<>& GetKey();
private:
KString fName{kMaxPropLen};
diff --git a/dev/kernel/FSKit/IndexableProperty.h b/dev/kernel/FSKit/IndexableProperty.h
index 3f2c42ac..8be6d7c3 100644
--- a/dev/kernel/FSKit/IndexableProperty.h
+++ b/dev/kernel/FSKit/IndexableProperty.h
@@ -25,7 +25,7 @@ namespace Indexer {
class IndexableProperty final : public Property {
public:
explicit IndexableProperty() : Property() {
- Kernel::KString strProp(kMaxPropLen);
+ Kernel::BasicKString<> strProp(kMaxPropLen);
strProp += "/prop/indexable";
this->GetKey() = strProp;
diff --git a/dev/kernel/NeKit/Json.h b/dev/kernel/NeKit/Json.h
index 2b2c9c04..ebd4acf9 100644
--- a/dev/kernel/NeKit/Json.h
+++ b/dev/kernel/NeKit/Json.h
@@ -26,7 +26,7 @@ class Json final {
public:
explicit Json() {
auto len = kJSONMaxLen;
- KString key = KString(len);
+ BasicKString<> key = KString(len);
key += kJSONNullObj;
this->AsKey() = key;
@@ -43,17 +43,17 @@ class Json final {
private:
Bool fUndefined; // is this instance undefined?
- KString fKey;
- KString fValue;
+ 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..35bdce97 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,8 @@ class KString final {
friend class KStringBuilder;
};
+using KString = BasicKString<>;
+
class KStringBuilder final {
public:
static ErrorOr<KString> Construct(const Char* data);
@@ -77,6 +82,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/src/KString.cc b/dev/kernel/NeKit/KString.inl
index f5732280..a0dd3623 100644
--- a/dev/kernel/src/KString.cc
+++ b/dev/kernel/NeKit/KString.inl
@@ -4,26 +4,38 @@
------------------------------------------- */
-#include <NeKit/KString.h>
#include <NeKit/Utils.h>
-/// @file KString.cc
+/// @file BasicKString<>.cc
/// @brief Kernel String manipulation file.
namespace Kernel {
-Char* KString::Data() {
+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;
}
-const Char* KString::CData() const {
+template<>
+inline const Char* BasicKString<>::CData() const {
return const_cast<const Char*>(this->fData);
}
-Size KString::Length() const {
+template<>
+inline SizeT BasicKString<>::Length() const {
return this->fDataSz;
}
-bool KString::operator==(const KString& rhs) const {
+template<>
+inline bool BasicKString<>::operator==(const BasicKString<>& rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < this->Length(); ++index) {
@@ -33,7 +45,8 @@ bool KString::operator==(const KString& rhs) const {
return true;
}
-bool KString::operator==(const Char* rhs) const {
+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) {
@@ -43,7 +56,8 @@ bool KString::operator==(const Char* rhs) const {
return true;
}
-bool KString::operator!=(const KString& rhs) const {
+template<>
+inline bool BasicKString<>::operator!=(const BasicKString<>& rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < rhs.Length(); ++index) {
@@ -53,7 +67,8 @@ bool KString::operator!=(const KString& rhs) const {
return true;
}
-bool KString::operator!=(const Char* rhs) const {
+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) {
@@ -63,16 +78,34 @@ bool KString::operator!=(const Char* rhs) const {
return true;
}
-ErrorOr<KString> KStringBuilder::Construct(const Char* data) {
- if (!data || *data == 0) return ErrorOr<KString>(new KString(0));
+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));
- KString* view = new KString(rt_string_len(data));
+ 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<KString>(*view);
+ return ErrorOr<BasicKString<>>(*view);
}
-const Char* KStringBuilder::FromBool(const Char* fmt, bool i) {
+inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) {
if (!fmt) return ("?");
const Char* boolean_expr = i ? "YES" : "NO";
@@ -101,7 +134,7 @@ const Char* KStringBuilder::FromBool(const Char* fmt, bool i) {
return ret;
}
-bool KStringBuilder::Equals(const Char* lhs, const Char* rhs) {
+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) {
@@ -111,16 +144,9 @@ bool KStringBuilder::Equals(const Char* lhs, const Char* rhs) {
return true;
}
-/// @note This is unsafe!!!
-bool KStringBuilder::Equals(const Utf8Char* lhs, const Utf8Char* rhs) {
- for (Size index = 0; index < urt_string_len(rhs); ++index) {
- if (rhs[index] != lhs[index]) return false;
- }
+inline bool KStringBuilder::Equals(const Utf8Char* lhs, const Utf8Char* rhs) {
+ if (urt_string_len(rhs) != urt_string_len(lhs)) return false;
- return true;
-}
-
-bool KStringBuilder::Equals(const WideChar* lhs, const WideChar* rhs) {
for (Size index = 0; rhs[index] != 0; ++index) {
if (rhs[index] != lhs[index]) return false;
}
@@ -128,7 +154,7 @@ bool KStringBuilder::Equals(const WideChar* lhs, const WideChar* rhs) {
return true;
}
-const Char* KStringBuilder::Format(const Char* fmt, const Char* fmt2) {
+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)));
@@ -152,29 +178,4 @@ const Char* KStringBuilder::Format(const Char* fmt, const Char* fmt2) {
return ret;
}
-
-STATIC 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];
- }
-}
-
-KString& KString::operator+=(const Char* rhs) {
- rt_string_append(this->fData, rhs, this->fCur);
- this->fCur += rt_string_len(rhs);
-
- return *this;
-}
-
-KString& KString::operator+=(const KString& 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;
-}
} // namespace Kernel
diff --git a/dev/kernel/src/Property.cc b/dev/kernel/src/Property.cc
index 62aa6ef2..581da501 100644
--- a/dev/kernel/src/Property.cc
+++ b/dev/kernel/src/Property.cc
@@ -21,14 +21,14 @@ Property::Property() = default;
/// @brief Check if property's name equals to name.
/// @param name string to check.
/***********************************************************************************/
-Bool Property::StringEquals(KString& name) {
+Bool Property::StringEquals(BasicKString<>& name) {
return this->fName && this->fName == name;
}
/***********************************************************************************/
/// @brief Gets the key (name) of property.
/***********************************************************************************/
-KString& Property::GetKey() {
+BasicKString<>& Property::GetKey() {
return this->fName;
}
diff --git a/dev/kernel/src/UtfUtils.cc b/dev/kernel/src/UtfUtils.cc
index a5c03b85..907632ad 100644
--- a/dev/kernel/src/UtfUtils.cc
+++ b/dev/kernel/src/UtfUtils.cc
@@ -27,7 +27,7 @@ Void urt_set_memory(const voidPtr src, UInt32 dst, Size len) {
}
}
-Int32 rt_string_cmp(const Utf8Char* src, const Utf8Char* cmp, Size size) {
+Int32 urt_string_cmp(const Utf8Char* src, const Utf8Char* cmp, Size size) {
Int32 counter = 0;
for (Size index = 0; index < size; ++index) {