summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/KString.cc
diff options
context:
space:
mode:
authorAmlal <amlal@nekernel.org>2025-04-25 13:08:33 +0200
committerAmlal <amlal@nekernel.org>2025-04-25 13:08:33 +0200
commitfb790b07aeba8e22e4190cf3e1834d11ecde6c96 (patch)
tree4cec7d1b321307b1d5935577631dae116a658a37 /dev/kernel/src/KString.cc
parent63a2d92c5dfe976175cda024ec01905d11b43738 (diff)
dev: better .clang-format, ran format command.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/src/KString.cc')
-rw-r--r--dev/kernel/src/KString.cc388
1 files changed, 169 insertions, 219 deletions
diff --git a/dev/kernel/src/KString.cc b/dev/kernel/src/KString.cc
index cb21e003..a13bb364 100644
--- a/dev/kernel/src/KString.cc
+++ b/dev/kernel/src/KString.cc
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
@@ -10,221 +10,171 @@
/// @file KString.cc
/// @brief Kernel String manipulation file.
-namespace Kernel
-{
- Char* KString::Data()
- {
- return this->fData;
- }
-
- const Char* KString::CData() const
- {
- return const_cast<const Char*>(this->fData);
- }
-
- Size KString::Length() const
- {
- return this->fDataSz;
- }
-
- bool KString::operator==(const KString& 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;
- }
-
- bool KString::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;
- }
-
- bool KString::operator!=(const KString& 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;
- }
-
- bool KString::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;
- }
-
- ErrorOr<KString> KStringBuilder::Construct(const Char* data)
- {
- if (!data || *data == 0)
- return {};
-
- KString* view = new KString(rt_string_len(data));
- (*view) += data;
-
- return ErrorOr<KString>(*view);
- }
-
- 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;
- }
-
- 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;
- }
-
- /// @note This is unsafe!!!
- bool KStringBuilder::Equals(const Utf16Char* lhs, const Utf16Char* rhs)
- {
- for (Size index = 0; index < wrt_string_len(rhs); ++index)
- {
- if (rhs[index] != lhs[index])
- 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;
- }
-
- return true;
- }
-
- 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;
- }
-
- 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
+namespace Kernel {
+Char* KString::Data() {
+ return this->fData;
+}
+
+const Char* KString::CData() const {
+ return const_cast<const Char*>(this->fData);
+}
+
+Size KString::Length() const {
+ return this->fDataSz;
+}
+
+bool KString::operator==(const KString& 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;
+}
+
+bool KString::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;
+}
+
+bool KString::operator!=(const KString& 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;
+}
+
+bool KString::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;
+}
+
+ErrorOr<KString> KStringBuilder::Construct(const Char* data) {
+ if (!data || *data == 0) return {};
+
+ KString* view = new KString(rt_string_len(data));
+ (*view) += data;
+
+ return ErrorOr<KString>(*view);
+}
+
+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;
+}
+
+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;
+}
+
+/// @note This is unsafe!!!
+bool KStringBuilder::Equals(const Utf16Char* lhs, const Utf16Char* rhs) {
+ for (Size index = 0; index < wrt_string_len(rhs); ++index) {
+ if (rhs[index] != lhs[index]) 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;
+ }
+
+ return true;
+}
+
+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;
+}
+
+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