/* * File: core/chunk_string.hpp * Purpose: String implementation for the SOCL C++ library. * Author: Amlal El Mahrouss (amlal@nekernel.org) * Copyright 2025, Amlal El Mahrouss */ #ifndef OCL_UTILITY_CHUNK_STRING_HPP #define OCL_UTILITY_CHUNK_STRING_HPP #include #include namespace ocl { template class basic_chunk_string; template struct basic_chunk_string final { private: std::unique_ptr> next_chunk_string_{}; basic_chunk_string* prev_chunk_string_{nullptr}; std::basic_string packed_chunks_{}; int64_t chunk_total{}; constexpr const static auto max_chunk_size = 4096; public: basic_chunk_string() = default; basic_chunk_string(const char_type* in) { this->operator+=(in); } basic_chunk_string(const std::basic_string& in) { this->operator+=(in); } ~basic_chunk_string() = default; basic_chunk_string& operator=(const basic_chunk_string&) = default; basic_chunk_string(const basic_chunk_string&) = default; basic_chunk_string& operator+=(const std::basic_string& in) { if (in.empty()) return *this; if (chunk_total > max_chunk_size) { next_chunk_string_ = std::make_unique>(); *next_chunk_string_ += in; next_chunk_string_->prev_chunk_string_ = this; return *next_chunk_string_; } packed_chunks_ += in; chunk_total += in.size(); return *this; } const std::basic_string& str() const noexcept { return packed_chunks_; } void print() noexcept { ocl::io::print(packed_chunks_); if (next_chunk_string_) this->next_chunk_string_->print(); } }; template inline void print(basic_chunk_string& fmt) noexcept { fmt.print(); } } // namespace ocl #endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP