summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/NeKit/KString.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-23 04:07:12 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-05-23 04:07:12 +0200
commitfc67c4af554189c941c811486a0b2b21aa3f54ea (patch)
treeddc7677c3bb1072c9b9fb85618b75c8ee172b377 /dev/kernel/NeKit/KString.h
parentfbd1f65a2cd30b3b4ed3da236398ddcfc437ac47 (diff)
feat!(kernel): Rename NewKit to NeKit.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/NeKit/KString.h')
-rw-r--r--dev/kernel/NeKit/KString.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/dev/kernel/NeKit/KString.h b/dev/kernel/NeKit/KString.h
new file mode 100644
index 00000000..16b09a78
--- /dev/null
+++ b/dev/kernel/NeKit/KString.h
@@ -0,0 +1,82 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+
+------------------------------------------- */
+
+#pragma once
+
+#include <CompilerKit/CompilerKit.h>
+#include <NeKit/Defines.h>
+#include <NeKit/ErrorOr.h>
+#include <NeKit/KernelPanic.h>
+#include <NeKit/Utils.h>
+
+#define kMinimumStringSize (8196U)
+
+namespace Kernel {
+/// @brief Kernel string class, not dynamic.
+class KString final {
+ public:
+ explicit KString() {
+ fDataSz = kMinimumStringSize;
+
+ fData = new Char[fDataSz];
+ MUST_PASS(fData);
+
+ rt_set_memory(fData, 0, fDataSz);
+ }
+
+ explicit KString(SizeT Sz) : fDataSz(Sz) {
+ MUST_PASS(Sz > 1);
+
+ fData = new Char[Sz];
+ MUST_PASS(fData);
+
+ rt_set_memory(fData, 0, Sz);
+ }
+
+ ~KString() {
+ if (fData) {
+ delete[] fData;
+ fData = nullptr;
+ }
+ }
+
+ NE_COPY_DEFAULT(KString)
+
+ Char* Data();
+ const Char* CData() const;
+ Size Length() const;
+
+ bool operator==(const Char* rhs) const;
+ bool operator!=(const Char* rhs) const;
+
+ bool operator==(const KString& rhs) const;
+ bool operator!=(const KString& rhs) const;
+
+ KString& operator+=(const Char* rhs);
+ KString& operator+=(const KString& rhs);
+
+ operator bool() { return fData; }
+
+ bool operator!() { return fData; }
+
+ private:
+ Char* fData{nullptr};
+ Size fDataSz{0};
+ Size fCur{0};
+
+ friend class KStringBuilder;
+};
+
+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);
+ static bool Equals(const WideChar* lhs, const WideChar* rhs);
+};
+} // namespace Kernel