From 337d36ee5f1f8376f3dcc863270bbe92fd7f8ca7 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 25 Mar 2025 20:34:13 +0100 Subject: fix(linker): A critical fix regarding the output being repetadely written on the file. refactor(NFC): Rename String.h to StringView.h Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/AAL/AssemblyInterface.h | 2 +- dev/LibCompiler/NFC/String.h | 90 ----------- dev/LibCompiler/NFC/StringView.h | 90 +++++++++++ dev/LibCompiler/src/DynamicLinkerPEF.cc | 7 +- dev/LibCompiler/src/String.cc | 256 -------------------------------- dev/LibCompiler/src/StringView.cc | 256 ++++++++++++++++++++++++++++++++ 6 files changed, 350 insertions(+), 351 deletions(-) delete mode 100644 dev/LibCompiler/NFC/String.h create mode 100644 dev/LibCompiler/NFC/StringView.h delete mode 100644 dev/LibCompiler/src/String.cc create mode 100644 dev/LibCompiler/src/StringView.cc (limited to 'dev/LibCompiler') diff --git a/dev/LibCompiler/AAL/AssemblyInterface.h b/dev/LibCompiler/AAL/AssemblyInterface.h index 8e46455..7e02e4f 100644 --- a/dev/LibCompiler/AAL/AssemblyInterface.h +++ b/dev/LibCompiler/AAL/AssemblyInterface.h @@ -8,7 +8,7 @@ #include #include -#include +#include #define ASSEMBLY_INTERFACE : public LibCompiler::AssemblyInterface diff --git a/dev/LibCompiler/NFC/String.h b/dev/LibCompiler/NFC/String.h deleted file mode 100644 index 51fb8b8..0000000 --- a/dev/LibCompiler/NFC/String.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * ======================================================== - * - * LibCompiler - * Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include -#include - -namespace LibCompiler -{ - /** - * @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 CharType[Sz]; - assert(m_Data); - } - - ~StringView() noexcept - { - if (m_Data) - { - memset(m_Data, 0, m_Sz); - delete[] m_Data; - - m_Data = nullptr; - } - } - - LIBCOMPILER_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: - CharType* 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 LibCompiler diff --git a/dev/LibCompiler/NFC/StringView.h b/dev/LibCompiler/NFC/StringView.h new file mode 100644 index 0000000..51fb8b8 --- /dev/null +++ b/dev/LibCompiler/NFC/StringView.h @@ -0,0 +1,90 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include +#include + +namespace LibCompiler +{ + /** + * @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 CharType[Sz]; + assert(m_Data); + } + + ~StringView() noexcept + { + if (m_Data) + { + memset(m_Data, 0, m_Sz); + delete[] m_Data; + + m_Data = nullptr; + } + } + + LIBCOMPILER_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: + CharType* 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 LibCompiler diff --git a/dev/LibCompiler/src/DynamicLinkerPEF.cc b/dev/LibCompiler/src/DynamicLinkerPEF.cc index 4a7bb49..924f587 100644 --- a/dev/LibCompiler/src/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/DynamicLinkerPEF.cc @@ -411,10 +411,9 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) reader_protocol.FP.seekg(std::streamsize(ae_header.fStartCode)); reader_protocol.FP.read(bytes.data(), std::streamsize(ae_header.fCodeSize)); - for (auto& byte : bytes) - { - kObjectBytes.push_back({.mBlob = bytes, .mOffset = ae_header.fStartCode}); - } + kObjectBytes.push_back({.mBlob = bytes, .mOffset = ae_header.fStartCode}); + + // Blob was written, close fp. reader_protocol.FP.close(); diff --git a/dev/LibCompiler/src/String.cc b/dev/LibCompiler/src/String.cc deleted file mode 100644 index 1ecbd7a..0000000 --- a/dev/LibCompiler/src/String.cc +++ /dev/null @@ -1,256 +0,0 @@ -/* - * ======================================================== - * - * LibCompiler - * Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved. - * - * ======================================================== - */ - -/** - * @file String.cxx - * @author Amlal (amlal@el-mahrouss-logic.com) - * @brief C++ string manipulation API. - * @version 0.2 - * @date 2024-01-23 - * - * @copyright Copyright (c) Amlal EL Mahrouss - * - */ - -#include - -namespace LibCompiler -{ - CharType* StringView::Data() - { - return m_Data; - } - - const CharType* StringView::CData() const - { - return m_Data; - } - - SizeType StringView::Length() const - { - return strlen(m_Data); - } - - 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; - } - - return true; - } - - 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; - } - - return true; - } - - 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; - } - - return true; - } - - 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; - } - - return true; - } - - StringView StringBuilder::Construct(const CharType* data) - { - if (!data || *data == 0) - return StringView(0); - - StringView view(strlen(data)); - view += data; - - return view; - } - - const char* StringBuilder::FromInt(const char* fmt, int i) - { - if (!fmt) - return ("-1"); - - auto ret_len = 8 + string_length(fmt); - char* ret = new char[ret_len]; - - if (!ret) - return ("-1"); - - memset(ret, 0, ret_len); - - CharType result[sizeof(int64_t)]; - - if (!to_str(result, sizeof(int64_t), i)) - { - delete[] ret; - return ("-1"); - } - - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(result); - - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; - - for (auto y_idx = 0; y_idx < res_len; ++y_idx) - { - ret[y_idx] = result[result_cnt]; - ++result_cnt; - } - - break; - } - - ret[idx] = fmt[idx]; - } - - return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ - } - - const char* StringBuilder::FromBool(const char* fmt, bool i) - { - if (!fmt) - return ("?"); - - const char* boolean_expr = i ? "true" : "false"; - char* ret = new char[i ? 4 : 5 + string_length(fmt)]; - - if (!ret) - return ("?"); - - const auto fmt_len = string_length(fmt); - const auto res_len = string_length(boolean_expr); - - for (SizeType idx = 0; idx < fmt_len; ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; - - for (auto y_idx = idx; y_idx < res_len; ++y_idx) - { - ret[result_cnt] = boolean_expr[y_idx]; - ++result_cnt; - } - - break; - } - - ret[idx] = fmt[idx]; - } - - return ret; - } - - bool StringBuilder::Equals(const char* lhs, const char* rhs) - { - if (string_length(rhs) != string_length(lhs)) - return false; - - for (SizeType index = 0; index < string_length(rhs); ++index) - { - if (rhs[index] != lhs[index]) - return false; - } - - return true; - } - - const char* StringBuilder::Format(const char* fmt, const char* fmtRight) - { - if (!fmt || !fmtRight) - return ("?"); - - char* ret = new char[string_length(fmtRight) + string_length(fmtRight)]; - if (!ret) - return ("?"); - - for (SizeType idx = 0; idx < string_length(fmt); ++idx) - { - if (fmt[idx] == '%') - { - SizeType result_cnt = idx; - - for (SizeType y_idx = 0; y_idx < string_length(fmtRight); ++y_idx) - { - ret[result_cnt] = fmtRight[y_idx]; - ++result_cnt; - } - - break; - } - - ret[idx] = fmt[idx]; - } - - return ret; - } - - StringView& StringView::operator+=(const CharType* rhs) - { - if (strlen(rhs) > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } - - memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); - this->m_Cur += strlen(rhs); - - return *this; - } - - StringView& StringView::operator+=(const StringView& rhs) - { - if (rhs.m_Cur > this->m_Sz) - { - throw std::runtime_error("out_of_bounds: StringView"); - } - - memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); - this->m_Cur += strlen(rhs.CData()); - - return *this; - } -} // namespace LibCompiler diff --git a/dev/LibCompiler/src/StringView.cc b/dev/LibCompiler/src/StringView.cc new file mode 100644 index 0000000..186d475 --- /dev/null +++ b/dev/LibCompiler/src/StringView.cc @@ -0,0 +1,256 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved. + * + * ======================================================== + */ + +/** + * @file String.cxx + * @author Amlal (amlal@el-mahrouss-logic.com) + * @brief C++ string manipulation API. + * @version 0.2 + * @date 2024-01-23 + * + * @copyright Copyright (c) Amlal EL Mahrouss + * + */ + +#include + +namespace LibCompiler +{ + CharType* StringView::Data() + { + return m_Data; + } + + const CharType* StringView::CData() const + { + return m_Data; + } + + SizeType StringView::Length() const + { + return strlen(m_Data); + } + + 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; + } + + return true; + } + + 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; + } + + return true; + } + + 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; + } + + return true; + } + + 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; + } + + return true; + } + + StringView StringBuilder::Construct(const CharType* data) + { + if (!data || *data == 0) + return StringView(0); + + StringView view(strlen(data)); + view += data; + + return view; + } + + const char* StringBuilder::FromInt(const char* fmt, int i) + { + if (!fmt) + return ("-1"); + + auto ret_len = 8 + string_length(fmt); + char* ret = new char[ret_len]; + + if (!ret) + return ("-1"); + + memset(ret, 0, ret_len); + + CharType result[sizeof(int64_t)]; + + if (!to_str(result, sizeof(int64_t), i)) + { + delete[] ret; + return ("-1"); + } + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); + + for (SizeType idx = 0; idx < fmt_len; ++idx) + { + if (fmt[idx] == '%') + { + SizeType result_cnt = idx; + + for (auto y_idx = 0; y_idx < res_len; ++y_idx) + { + ret[y_idx] = result[result_cnt]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ + } + + const char* StringBuilder::FromBool(const char* fmt, bool i) + { + if (!fmt) + return ("?"); + + const char* boolean_expr = i ? "true" : "false"; + char* ret = new char[i ? 4 : 5 + string_length(fmt)]; + + if (!ret) + return ("?"); + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); + + for (SizeType idx = 0; idx < fmt_len; ++idx) + { + if (fmt[idx] == '%') + { + SizeType result_cnt = idx; + + for (auto y_idx = idx; y_idx < res_len; ++y_idx) + { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; + } + + bool StringBuilder::Equals(const char* lhs, const char* rhs) + { + if (string_length(rhs) != string_length(lhs)) + return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) + { + if (rhs[index] != lhs[index]) + return false; + } + + return true; + } + + const char* StringBuilder::Format(const char* fmt, const char* fmtRight) + { + if (!fmt || !fmtRight) + return ("?"); + + char* ret = new char[string_length(fmtRight) + string_length(fmtRight)]; + if (!ret) + return ("?"); + + for (SizeType idx = 0; idx < string_length(fmt); ++idx) + { + if (fmt[idx] == '%') + { + SizeType result_cnt = idx; + + for (SizeType y_idx = 0; y_idx < string_length(fmtRight); ++y_idx) + { + ret[result_cnt] = fmtRight[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; + } + + StringView& StringView::operator+=(const CharType* rhs) + { + if (strlen(rhs) > this->m_Sz) + { + throw std::runtime_error("out_of_bounds: StringView"); + } + + memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); + this->m_Cur += strlen(rhs); + + return *this; + } + + StringView& StringView::operator+=(const StringView& rhs) + { + if (rhs.m_Cur > this->m_Sz) + { + throw std::runtime_error("out_of_bounds: StringView"); + } + + memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); + this->m_Cur += strlen(rhs.CData()); + + return *this; + } +} // namespace LibCompiler -- cgit v1.2.3