diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
| commit | 5339d016c07bf717ee388f4feb73544087324af0 (patch) | |
| tree | 94be6f67ed626091f24aee24ec3b3be03d01e4e7 /NewKit/String.hpp | |
git: port from mercurial repo.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'NewKit/String.hpp')
| -rw-r--r-- | NewKit/String.hpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/NewKit/String.hpp b/NewKit/String.hpp new file mode 100644 index 00000000..a1a1ad44 --- /dev/null +++ b/NewKit/String.hpp @@ -0,0 +1,80 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/ErrorOr.hpp> +#include <NewKit/Defines.hpp> +#include <NewKit/Panic.hpp> + +namespace hCore +{ + class StringView final + { + public: + explicit StringView() = default; + + explicit StringView(Size Sz) : m_Sz(Sz) + { + MUST_PASS(Sz > 1); + m_Data = new Char[Sz]; + MUST_PASS(m_Data); + } + + ~StringView() + { + if (m_Data) + delete[] m_Data; + } + + StringView &operator=(const StringView &) = default; + StringView(const StringView &) = default; + + Char *Data(); + const Char *CData(); + Size Length() const; + + bool operator==(const Char *rhs) const; + bool operator!=(const Char *rhs) const; + + bool operator==(const StringView &rhs) const; + bool operator!=(const StringView &rhs) const; + + StringView &operator+=(const Char *rhs); + StringView &operator+=(const StringView &rhs); + + operator bool() + { + return m_Data; + } + + bool operator!() + { + return m_Data; + } + + private: + Char *m_Data{nullptr}; + Size m_Sz{0}; + Size m_Cur{0}; + + friend class StringBuilder; + + }; + + struct StringBuilder final + { + static ErrorOr<StringView> Construct(const Char *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 hCore |
