summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/NewKit/KString.h
blob: 350623d743ead9acae95f0d951404aa382baa167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* -------------------------------------------

	Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#pragma once

#include <CompilerKit/CompilerKit.h>
#include <NewKit/Defines.h>
#include <NewKit/ErrorOr.h>
#include <NewKit/Utils.h>
#include <NewKit/KernelPanic.h>

#define cMinimumStringSize 8196

namespace Kernel
{
	/// @brief KString static string class.
	class KString final
	{
	public:
		explicit KString()
		{
			fDataSz = cMinimumStringSize;

			fData = new Char[fDataSz];
			MUST_PASS(fData);

			rt_set_memory(fData, 0, fDataSz);
		}

		explicit KString(const 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;
			}
		}

		ZKA_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 StringBuilder;
	};

	struct StringBuilder final
	{
		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 WideChar* lhs, const WideChar* rhs);
	};
} // namespace Kernel