summaryrefslogtreecommitdiffhomepage
path: root/Comm/StdKit/String.hpp
diff options
context:
space:
mode:
authorAmlal EL Mahrouss <amlal@softwarelabs.com>2024-06-09 19:57:20 +0200
committerAmlal EL Mahrouss <amlal@softwarelabs.com>2024-06-09 19:57:20 +0200
commit0aebad569eef2989e32d9825ce52708371685ff9 (patch)
tree29faa339ef63d65f02cbb8c549dd6f5f047a4f68 /Comm/StdKit/String.hpp
parent54ace477f90b5bd17ddd7c5d11c2e319f19555bd (diff)
MHR-21: Finish C++ compiler.
Signed-off-by: Amlal EL Mahrouss <amlal@softwarelabs.com>
Diffstat (limited to 'Comm/StdKit/String.hpp')
-rw-r--r--Comm/StdKit/String.hpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/Comm/StdKit/String.hpp b/Comm/StdKit/String.hpp
new file mode 100644
index 0000000..c6589cc
--- /dev/null
+++ b/Comm/StdKit/String.hpp
@@ -0,0 +1,90 @@
+/*
+ * ========================================================
+ *
+ * CompilerKit
+ * Copyright SoftwareLabs, all rights reserved.
+ *
+ * ========================================================
+ */
+
+#pragma once
+
+#include <Comm/Defines.hpp>
+#include <Comm/StdKit/ErrorOr.hpp>
+
+namespace CompilerKit
+{
+ /**
+ * @brief StringView class, contains a C string and manages it.
+ * @note No need to manage it it's getting deleted by default.
+ */
+
+ class StringView final
+ {
+ public:
+ explicit StringView() = delete;
+
+ explicit StringView(SizeType Sz) noexcept
+ : m_Sz(Sz)
+ {
+ m_Data = new char[Sz];
+ assert(m_Data);
+ }
+
+ ~StringView() noexcept
+ {
+ if (m_Data)
+ {
+ memset(m_Data, 0, m_Sz);
+ delete[] m_Data;
+
+ m_Data = nullptr;
+ }
+ }
+
+ MPCC_COPY_DEFAULT(StringView);
+
+ CharType* Data();
+ const CharType* CData() const;
+ SizeType Length() const;
+
+ bool operator==(const CharType* rhs) const;
+ bool operator!=(const CharType* rhs) const;
+
+ bool operator==(const StringView& rhs) const;
+ bool operator!=(const StringView& rhs) const;
+
+ StringView& operator+=(const CharType* rhs);
+ StringView& operator+=(const StringView& rhs);
+
+ operator bool()
+ {
+ return m_Data && m_Data[0] != 0;
+ }
+
+ bool operator!()
+ {
+ return !m_Data || m_Data[0] == 0;
+ }
+
+ private:
+ char* m_Data{nullptr};
+ SizeType m_Sz{0};
+ SizeType m_Cur{0};
+
+ friend class StringBuilder;
+ };
+
+ /**
+ * @brief StringBuilder class
+ * @note These results shall call delete[] after they're used.
+ */
+ struct StringBuilder final
+ {
+ static StringView Construct(const CharType* data);
+ static const char* FromInt(const char* fmt, int n);
+ 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);
+ };
+} // namespace CompilerKit