diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-30 13:25:42 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-30 13:26:33 +0100 |
| commit | be73d82eff113e6a6723d6fb4bd80f56f0ef88ef (patch) | |
| tree | 93425f2d183c3ea7e5e1b50374ee6548c383ab78 /Private/CompilerKit/StdKit | |
| parent | 7c8afc0e15e54ae9e0f1a393bb52eed804d34edc (diff) | |
Compiler: Breaking changes, will work on C++ compiler from now on.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/CompilerKit/StdKit')
| -rw-r--r-- | Private/CompilerKit/StdKit/AE.hpp | 200 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/ErrorID.hpp | 2 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/ErrorOr.hpp | 63 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/PEF.hpp | 144 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/Ref.hpp | 129 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/String.cc | 302 | ||||
| -rw-r--r-- | Private/CompilerKit/StdKit/String.hpp | 141 |
7 files changed, 430 insertions, 551 deletions
diff --git a/Private/CompilerKit/StdKit/AE.hpp b/Private/CompilerKit/StdKit/AE.hpp index 77358fb..4144252 100644 --- a/Private/CompilerKit/StdKit/AE.hpp +++ b/Private/CompilerKit/StdKit/AE.hpp @@ -15,127 +15,115 @@ #define kAEMag1 'E' #define kAESymbolLen 64 -#define kAEPad 8 +#define kAEPad 8 #define kAEMagLen 2 #define kAEInvalidOpcode 0x00 // Advanced Executable File Format for MetroLink. // Reloctable by offset is the default strategy. -// You can also relocate at runtime but that's up to the operating system loader. - -namespace CompilerKit -{ - // @brief Advanced Executable Header - // One thing to keep in mind. - // This object format, is reloctable. - typedef struct AEHeader final - { - CharType fMagic[kAEMagLen]; - CharType fArch; - SizeType fCount; - CharType fSize; - SizeType fStartCode; - SizeType fCodeSize; - CharType fPad[kAEPad]; - } __attribute__((packed)) AEHeader, *AEHeaderPtr; - - // @brief Advanced Executable Record. - // Could be data, code or bss. - // fKind must be filled with PEF fields. - - typedef struct AERecordHeader final - { - CharType fName[kAESymbolLen]; - SizeType fKind; - SizeType fSize; - SizeType fFlags; - UIntPtr fOffset; - CharType fPad[kAEPad]; - } __attribute__((packed)) AERecordHeader, *AERecordHeaderPtr; - - enum - { - kKindRelocationByOffset = 0x23f, - kKindRelocationAtRuntime = 0x34f, - }; -} - +// You can also relocate at runtime but that's up to the operating system +// loader. + +namespace CompilerKit { +// @brief Advanced Executable Header +// One thing to keep in mind. +// This object format, is reloctable. +typedef struct AEHeader final { + CharType fMagic[kAEMagLen]; + CharType fArch; + SizeType fCount; + CharType fSize; + SizeType fStartCode; + SizeType fCodeSize; + CharType fPad[kAEPad]; +} __attribute__((packed)) AEHeader, *AEHeaderPtr; + +// @brief Advanced Executable Record. +// Could be data, code or bss. +// fKind must be filled with PEF fields. + +typedef struct AERecordHeader final { + CharType fName[kAESymbolLen]; + SizeType fKind; + SizeType fSize; + SizeType fFlags; + UIntPtr fOffset; + CharType fPad[kAEPad]; +} __attribute__((packed)) AERecordHeader, *AERecordHeaderPtr; + +enum { + kKindRelocationByOffset = 0x23f, + kKindRelocationAtRuntime = 0x34f, +}; +} // namespace CompilerKit // provide operator<< for AE -std::ofstream &operator<<(std::ofstream &fp, CompilerKit::AEHeader &container) -{ - fp.write((char *)&container, sizeof(CompilerKit::AEHeader)); +std::ofstream &operator<<(std::ofstream &fp, CompilerKit::AEHeader &container) { + fp.write((char *)&container, sizeof(CompilerKit::AEHeader)); - return fp; + return fp; } -std::ofstream &operator<<(std::ofstream &fp, CompilerKit::AERecordHeader &container) -{ - fp.write((char *)&container, sizeof(CompilerKit::AERecordHeader)); +std::ofstream &operator<<(std::ofstream &fp, + CompilerKit::AERecordHeader &container) { + fp.write((char *)&container, sizeof(CompilerKit::AERecordHeader)); - return fp; + return fp; } -std::ifstream &operator>>(std::ifstream& fp, CompilerKit::AEHeader& container) -{ - fp.read((char*)&container, sizeof(CompilerKit::AEHeader)); - return fp; +std::ifstream &operator>>(std::ifstream &fp, CompilerKit::AEHeader &container) { + fp.read((char *)&container, sizeof(CompilerKit::AEHeader)); + return fp; } -std::ifstream &operator>>(std::ifstream& fp, CompilerKit::AERecordHeader& container) -{ - fp.read((char*)&container, sizeof(CompilerKit::AERecordHeader)); - return fp; +std::ifstream &operator>>(std::ifstream &fp, + CompilerKit::AERecordHeader &container) { + fp.read((char *)&container, sizeof(CompilerKit::AERecordHeader)); + return fp; } -namespace CompilerKit::Utils -{ - /** - * @brief AE Reader protocol - * - */ - class AEReadableProtocol final - { - public: - std::ifstream FP; - - public: - explicit AEReadableProtocol() = default; - ~AEReadableProtocol() = default; - - CXXKIT_COPY_DELETE(AEReadableProtocol); - - /** - * @brief Read AE record - * - * @param raw the containing buffer - * @param sz it's size (without sizeof(AERecordHeader) added to it.) - * @return AERecordHeaderPtr - */ - AERecordHeaderPtr Read(char* raw, std::size_t sz) - { - if (!raw) - return nullptr; - - return this->_Read<AERecordHeader>(raw, sz * sizeof(AERecordHeader)); - } - - private: - /** - * @brief Implementation of Read for raw classes. - * - * @tparam TypeClass The class to read. - * @param raw the buffer - * @param sz the size - * @return TypeClass* the returning class. - */ - template <typename TypeClass> - TypeClass* _Read(char* raw, std::size_t sz) - { - FP.read(raw, std::streamsize(sz)); - return reinterpret_cast<TypeClass*>(raw); - } - - }; -} // namespace CompilerKit::Utils
\ No newline at end of file +namespace CompilerKit::Utils { +/** + * @brief AE Reader protocol + * + */ +class AEReadableProtocol final { + public: + std::ifstream FP; + + public: + explicit AEReadableProtocol() = default; + ~AEReadableProtocol() = default; + + CXXKIT_COPY_DELETE(AEReadableProtocol); + + /** + * @brief Read AE record + * + * @param raw the containing buffer + * @param sz it's size (without sizeof(AERecordHeader) added to it.) + * @return AERecordHeaderPtr + */ + AERecordHeaderPtr Read(char *raw, std::size_t sz) { + if (!raw) return nullptr; + + return this->_Read<AERecordHeader>(raw, sz * sizeof(AERecordHeader)); + } + + private: + /** + * @brief Implementation of Read for raw classes. + * + * @tparam TypeClass The class to read. + * @param raw the buffer + * @param sz the size + * @return TypeClass* the returning class. + */ + template <typename TypeClass> + TypeClass *_Read(char *raw, std::size_t sz) { + FP.read(raw, std::streamsize(sz)); + return reinterpret_cast<TypeClass *>(raw); + } +}; +} // namespace CompilerKit::Utils diff --git a/Private/CompilerKit/StdKit/ErrorID.hpp b/Private/CompilerKit/StdKit/ErrorID.hpp index e7bc7de..5577197 100644 --- a/Private/CompilerKit/StdKit/ErrorID.hpp +++ b/Private/CompilerKit/StdKit/ErrorID.hpp @@ -9,8 +9,8 @@ #pragma once -#include <StdKit/ErrorOr.hpp> #include <Defines.hpp> +#include <StdKit/ErrorOr.hpp> #define CXXKIT_EXEC_ERROR -30 #define CXXKIT_FILE_NOT_FOUND -31 diff --git a/Private/CompilerKit/StdKit/ErrorOr.hpp b/Private/CompilerKit/StdKit/ErrorOr.hpp index 97aeaa2..7da6135 100644 --- a/Private/CompilerKit/StdKit/ErrorOr.hpp +++ b/Private/CompilerKit/StdKit/ErrorOr.hpp @@ -12,47 +12,34 @@ #include <CompilerKit/Defines.hpp> #include <CompilerKit/StdKit/Ref.hpp> -namespace CompilerKit -{ +namespace CompilerKit { using ErrorT = UInt32; -template <typename T> -class ErrorOr final -{ - public: - ErrorOr() = default; - ~ErrorOr() = default; - - public: - explicit ErrorOr(Int32 err) - : mId(err) - {} - - explicit ErrorOr(nullPtr Null) - {} - - explicit ErrorOr(T Class) - : mRef(Class) - {} - - ErrorOr &operator=(const ErrorOr &) = default; - ErrorOr(const ErrorOr &) = default; - - Ref<T> Leak() - { - return mRef; - } - - operator bool() - { - return mRef; - } - - private: - Ref<T> mRef; - Int32 mId{0}; +template <typename T> +class ErrorOr final { + public: + ErrorOr() = default; + ~ErrorOr() = default; + + public: + explicit ErrorOr(Int32 err) : mId(err) {} + + explicit ErrorOr(nullPtr Null) {} + + explicit ErrorOr(T Class) : mRef(Class) {} + + ErrorOr &operator=(const ErrorOr &) = default; + ErrorOr(const ErrorOr &) = default; + + Ref<T> Leak() { return mRef; } + + operator bool() { return mRef; } + + private: + Ref<T> mRef; + Int32 mId{0}; }; using ErrorOrAny = ErrorOr<voidPtr>; -} // namespace CompilerKit +} // namespace CompilerKit diff --git a/Private/CompilerKit/StdKit/PEF.hpp b/Private/CompilerKit/StdKit/PEF.hpp index a26af9e..dbc98ca 100644 --- a/Private/CompilerKit/StdKit/PEF.hpp +++ b/Private/CompilerKit/StdKit/PEF.hpp @@ -14,7 +14,7 @@ // @file PEF.hpp // @brief Preferred Executable Format -#define kPefMagic "PEF" +#define kPefMagic "PEF" #define kPefMagicFat "FEP" #define kPefExt ".out" @@ -29,84 +29,78 @@ #define kPefBaseOrigin 0x1000 -namespace CompilerKit -{ - enum - { - kPefArchIntel86S = 100, - kPefArchAMD64, - kPefArchRISCV, - kPefArch64000, /* 64x0 RISC architecture. */ - kPefArch32000, - kPefArchInvalid = 0xFF, - }; - - enum - { - kPefKindExec = 1, /* .o/.pef/<none> */ - kPefKindSharedObject = 2, /* .lib */ - kPefKindObject = 4, /* .obj */ - kPefKindDwarf = 5, /* .dsym */ - }; - - /* PEF container */ - typedef struct PEFContainer final - { - CharType Magic[kPefMagicLen]; - UInt32 Linker; - UInt32 Version; - UInt32 Kind; - UInt32 Abi; - UInt32 Cpu; - UInt32 SubCpu; /* Cpu specific information */ - UIntPtr Start; /* Origin of code */ - SizeType HdrSz; /* Size of header */ - SizeType Count; /* container header count */ - } __attribute__((packed)) PEFContainer; - - /* First PEFCommandHeader starts after PEFContainer */ - /* Last container is __exec_end */ - - /* PEF executable section and commands. */ - - typedef struct PEFCommandHeader final - { - CharType Name[kPefNameLen]; /* container name */ - UInt32 Flags; /* container flags */ - UInt16 Kind; /* container kind */ - UIntPtr Offset; /* file offset */ - SizeType Size; /* file size */ - } __attribute__((packed)) PEFCommandHeader; - - enum - { - kPefCode = 0xC, - kPefData = 0xD, - kPefZero = 0xE, - kPefLinkerID = 0x1, - }; -} // namespace CompilerKit - -inline std::ofstream& operator<<(std::ofstream& fp, CompilerKit::PEFContainer& container) -{ - fp.write((char*)&container, sizeof(CompilerKit::PEFContainer)); - return fp; +namespace CompilerKit { +enum { + kPefArchIntel86S = 100, + kPefArchAMD64, + kPefArchRISCV, + kPefArch64000, /* 64x0 RISC architecture. */ + kPefArch32000, + kPefArchInvalid = 0xFF, +}; + +enum { + kPefKindExec = 1, /* .o/.pef/<none> */ + kPefKindSharedObject = 2, /* .lib */ + kPefKindObject = 4, /* .obj */ + kPefKindDwarf = 5, /* .dsym */ +}; + +/* PEF container */ +typedef struct PEFContainer final { + CharType Magic[kPefMagicLen]; + UInt32 Linker; + UInt32 Version; + UInt32 Kind; + UInt32 Abi; + UInt32 Cpu; + UInt32 SubCpu; /* Cpu specific information */ + UIntPtr Start; /* Origin of code */ + SizeType HdrSz; /* Size of header */ + SizeType Count; /* container header count */ +} __attribute__((packed)) PEFContainer; + +/* First PEFCommandHeader starts after PEFContainer */ +/* Last container is __exec_end */ + +/* PEF executable section and commands. */ + +typedef struct PEFCommandHeader final { + CharType Name[kPefNameLen]; /* container name */ + UInt32 Flags; /* container flags */ + UInt16 Kind; /* container kind */ + UIntPtr Offset; /* file offset */ + SizeType Size; /* file size */ +} __attribute__((packed)) PEFCommandHeader; + +enum { + kPefCode = 0xC, + kPefData = 0xD, + kPefZero = 0xE, + kPefLinkerID = 0x1, +}; +} // namespace CompilerKit + +inline std::ofstream& operator<<(std::ofstream& fp, + CompilerKit::PEFContainer& container) { + fp.write((char*)&container, sizeof(CompilerKit::PEFContainer)); + return fp; } -inline std::ofstream& operator<<(std::ofstream& fp, CompilerKit::PEFCommandHeader& container) -{ - fp.write((char*)&container, sizeof(CompilerKit::PEFCommandHeader)); - return fp; +inline std::ofstream& operator<<(std::ofstream& fp, + CompilerKit::PEFCommandHeader& container) { + fp.write((char*)&container, sizeof(CompilerKit::PEFCommandHeader)); + return fp; } -std::ifstream &operator>>(std::ifstream& fp, CompilerKit::PEFContainer& container) -{ - fp.read((char*)&container, sizeof(CompilerKit::PEFContainer)); - return fp; +std::ifstream& operator>>(std::ifstream& fp, + CompilerKit::PEFContainer& container) { + fp.read((char*)&container, sizeof(CompilerKit::PEFContainer)); + return fp; } -std::ifstream &operator>>(std::ifstream& fp, CompilerKit::PEFCommandHeader& container) -{ - fp.read((char*)&container, sizeof(CompilerKit::PEFCommandHeader)); - return fp; +std::ifstream& operator>>(std::ifstream& fp, + CompilerKit::PEFCommandHeader& container) { + fp.read((char*)&container, sizeof(CompilerKit::PEFCommandHeader)); + return fp; } diff --git a/Private/CompilerKit/StdKit/Ref.hpp b/Private/CompilerKit/StdKit/Ref.hpp index 9b79ca9..d7ef957 100644 --- a/Private/CompilerKit/StdKit/Ref.hpp +++ b/Private/CompilerKit/StdKit/Ref.hpp @@ -10,78 +10,57 @@ #pragma once -namespace CompilerKit -{ - // @author Amlal EL Mahrouss - // @brief Reference class, refers to a pointer of data in static memory. - template <typename T> - class Ref final - { - public: - explicit Ref() = default; - ~Ref() = default; - - public: - explicit Ref(T cls, const bool &strong = false) : m_Class(cls), m_Strong(strong) {} - - Ref& operator=(T ref) - { - m_Class = ref; - return *this; - } - - public: - T operator->() const - { - return m_Class; - } - - T &Leak() - { - return m_Class; - } - - T operator*() - { - return m_Class; - } - - bool IsStrong() const - { - return m_Strong; - } - - operator bool() - { - return m_Class; - } - - private: - T m_Class; - bool m_Strong{ false }; - - }; - - template <typename T> - class NonNullRef final - { - public: - NonNullRef() = delete; - NonNullRef(nullPtr) = delete; - - explicit NonNullRef(T *ref) : m_Ref(ref, true) {} - - Ref<T> &operator->() - { - MUST_PASS(m_Ref); - return m_Ref; - } - - NonNullRef &operator=(const NonNullRef<T> &ref) = delete; - NonNullRef(const NonNullRef<T> &ref) = default; - - private: - Ref<T> m_Ref{ nullptr }; - - }; -} // namespace CompilerKit +namespace CompilerKit { +// @author Amlal EL Mahrouss +// @brief Reference class, refers to a pointer of data in static memory. +template <typename T> +class Ref final { + public: + explicit Ref() = default; + ~Ref() = default; + + public: + explicit Ref(T cls, const bool &strong = false) + : m_Class(cls), m_Strong(strong) {} + + Ref &operator=(T ref) { + m_Class = ref; + return *this; + } + + public: + T operator->() const { return m_Class; } + + T &Leak() { return m_Class; } + + T operator*() { return m_Class; } + + bool IsStrong() const { return m_Strong; } + + operator bool() { return m_Class; } + + private: + T m_Class; + bool m_Strong{false}; +}; + +template <typename T> +class NonNullRef final { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + explicit NonNullRef(T *ref) : m_Ref(ref, true) {} + + Ref<T> &operator->() { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef &operator=(const NonNullRef<T> &ref) = delete; + NonNullRef(const NonNullRef<T> &ref) = default; + + private: + Ref<T> m_Ref{nullptr}; +}; +} // namespace CompilerKit diff --git a/Private/CompilerKit/StdKit/String.cc b/Private/CompilerKit/StdKit/String.cc index 519ccff..958638d 100644 --- a/Private/CompilerKit/StdKit/String.cc +++ b/Private/CompilerKit/StdKit/String.cc @@ -13,241 +13,187 @@ * @brief C++ String Manip API. * @version 0.2 * @date 2024-01-23 - * + * * @copyright Copyright (c) 2024 Mahrouss Logic - * + * */ #include "String.hpp" + #include <utility> -namespace CompilerKit -{ - CharType* StringView::Data() - { - return m_Data; - } +namespace CompilerKit { +CharType *StringView::Data() { return m_Data; } - const CharType* StringView::CData() const - { - return m_Data; - } +const CharType *StringView::CData() const { return m_Data; } - SizeType StringView::Length() const - { - return strlen(m_Data); - } +SizeType StringView::Length() const { return strlen(m_Data); } - bool StringView::operator==(const StringView &rhs) const - { - if (rhs.Length() != Length()) - return false; +bool StringView::operator==(const StringView &rhs) const { + if (rhs.Length() != Length()) return false; - for (SizeType index = 0; index < Length(); ++index) - { - if (rhs.m_Data[index] != m_Data[index]) - return false; - } + for (SizeType index = 0; index < Length(); ++index) { + if (rhs.m_Data[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator==(const CharType *rhs) const - { - if (string_length(rhs) != Length()) - return false; +bool StringView::operator==(const CharType *rhs) const { + if (string_length(rhs) != Length()) return false; - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != m_Data[index]) - return false; - } + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const StringView &rhs) const - { - if (rhs.Length() != Length()) - return false; +bool StringView::operator!=(const StringView &rhs) const { + if (rhs.Length() != Length()) return false; - for (SizeType index = 0; index < rhs.Length(); ++index) - { - if (rhs.m_Data[index] == m_Data[index]) - return false; - } + for (SizeType index = 0; index < rhs.Length(); ++index) { + if (rhs.m_Data[index] == m_Data[index]) return false; + } - return true; - } + return true; +} - bool StringView::operator!=(const CharType *rhs) const - { - if (string_length(rhs) != Length()) - return false; +bool StringView::operator!=(const CharType *rhs) const { + if (string_length(rhs) != Length()) return false; - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] == m_Data[index]) - return false; - } + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] == m_Data[index]) return false; + } - return true; - } - - StringView StringBuilder::Construct(const CharType *data) - { - if (!data || - *data == 0) - return StringView(0); + return true; +} - StringView view(strlen(data)); - view += data; - - return view; - } +StringView StringBuilder::Construct(const CharType *data) { + if (!data || *data == 0) return StringView(0); - const char* StringBuilder::FromInt(const char *fmt, int i) - { - if (!fmt) - return ("-1"); + StringView view(strlen(data)); + view += data; - char *ret = new char[8 + string_length(fmt)]; + return view; +} - if (!ret) - return ("-1"); +const char *StringBuilder::FromInt(const char *fmt, int i) { + if (!fmt) return ("-1"); - CharType result[8]; - if (!to_str(result, sizeof(int), i)) - { - delete[] ret; - return ("-1"); - } + char *ret = new char[8 + string_length(fmt)]; - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); + if (!ret) return ("-1"); - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') { - SizeType result_cnt = idx; + CharType result[8]; + if (!to_str(result, sizeof(int), i)) { + delete[] ret; + return ("-1"); + } - for (auto y_idx = idx; y_idx < res_len; ++y_idx) { - ret[result_cnt] = result[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); - break; - } + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = result[y_idx]; + ++result_cnt; + } - return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ + break; } - const char* StringBuilder::FromBool(const char *fmt, bool i) - { - if (!fmt) - return ("?"); + ret[idx] = fmt[idx]; + } - const char *boolean_expr = i ? "true" : "false"; - char *ret = new char[i ? 4 : 5 + string_length(fmt)]; + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ +} - if (!ret) - return ("?"); +const char *StringBuilder::FromBool(const char *fmt, bool i) { + if (!fmt) return ("?"); - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(boolean_expr); + const char *boolean_expr = i ? "true" : "false"; + char *ret = new char[i ? 4 : 5 + string_length(fmt)]; - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; + if (!ret) return ("?"); - for (auto y_idx = idx; y_idx < res_len; ++y_idx) - { - ret[result_cnt] = boolean_expr[y_idx]; - ++result_cnt; - } + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); - break; - } + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } - return ret; + break; } - bool StringBuilder::Equals(const char *lhs, const char *rhs) - { - if (string_length(rhs) != string_length(lhs)) - return false; + ret[idx] = fmt[idx]; + } - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != lhs[index]) - return false; - } + return ret; +} - return true; - } +bool StringBuilder::Equals(const char *lhs, const char *rhs) { + if (string_length(rhs) != string_length(lhs)) return false; - const char *StringBuilder::Format(const char *fmt, const char *fmt2) - { - if (!fmt || !fmt2) - return ("?"); + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } - char *ret = new char[string_length(fmt2) + string_length(fmt2)]; - if (!ret) - return ("?"); + return true; +} - for (SizeType idx = 0; idx < string_length(fmt); ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; +const char *StringBuilder::Format(const char *fmt, const char *fmt2) { + if (!fmt || !fmt2) return ("?"); - for (SizeType y_idx = 0; y_idx < string_length(fmt2); ++y_idx) - { - ret[result_cnt] = fmt2[y_idx]; - ++result_cnt; - } + char *ret = new char[string_length(fmt2) + string_length(fmt2)]; + if (!ret) return ("?"); - break; - } + for (SizeType idx = 0; idx < string_length(fmt); ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; - ret[idx] = fmt[idx]; - } + for (SizeType y_idx = 0; y_idx < string_length(fmt2); ++y_idx) { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } - return ret; + break; } - StringView &StringView::operator+=(const CharType *rhs) - { - if (strlen(rhs) > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } + ret[idx] = fmt[idx]; + } + return ret; +} - memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); - this->m_Cur += strlen(rhs); +StringView &StringView::operator+=(const CharType *rhs) { + if (strlen(rhs) > this->m_Sz) { + throw std::runtime_error("out_of_bounds: StringView"); + } - return *this; - } + memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); + this->m_Cur += strlen(rhs); - StringView &StringView::operator+=(const StringView &rhs) - { - if (rhs.m_Cur > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } + return *this; +} - memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); - this->m_Cur += strlen(rhs.CData()); +StringView &StringView::operator+=(const StringView &rhs) { + if (rhs.m_Cur > this->m_Sz) { + throw std::runtime_error("out_of_bounds: StringView"); + } - return *this; - } -} // namespace CompilerKit + memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); + this->m_Cur += strlen(rhs.CData()); + + return *this; +} +} // namespace CompilerKit diff --git a/Private/CompilerKit/StdKit/String.hpp b/Private/CompilerKit/StdKit/String.hpp index eada24d..1e4b34c 100644 --- a/Private/CompilerKit/StdKit/String.hpp +++ b/Private/CompilerKit/StdKit/String.hpp @@ -12,81 +12,66 @@ #include <CompilerKit/Defines.hpp> #include <CompilerKit/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; - } - } - - CXXKIT_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 +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; + } + } + + CXXKIT_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 |
