diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-21 15:10:31 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-21 15:11:09 +0200 |
| commit | 23d52c3cb72d595c971f189363c6cf9b222581c7 (patch) | |
| tree | cc070f3fc9342d227e405ef78ae09ac1560800a7 /dev | |
| parent | 5966f5da72f63dcd6d6203366a996a0c1b9f16fb (diff) | |
feat: chunk_string: new work in progress string container.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/examples/cgi/cgi.cc | 2 | ||||
| -rw-r--r-- | dev/lib/core/includes.hpp | 1 | ||||
| -rw-r--r-- | dev/lib/utility/cgi_writer.hpp (renamed from dev/lib/utility/cgi.hpp) | 2 | ||||
| -rw-r--r-- | dev/lib/utility/chunk_string.hpp | 79 | ||||
| -rw-r--r-- | dev/tests/chunk_string/CMakeLists.txt | 26 | ||||
| -rw-r--r-- | dev/tests/chunk_string/chunk_test.cc | 30 | ||||
| -rw-r--r-- | dev/tests/network_basic/net_test.cc | 4 |
7 files changed, 140 insertions, 4 deletions
diff --git a/dev/examples/cgi/cgi.cc b/dev/examples/cgi/cgi.cc index 2cb8f3a..4872cb5 100644 --- a/dev/examples/cgi/cgi.cc +++ b/dev/examples/cgi/cgi.cc @@ -4,7 +4,7 @@ licensed under the MIT license */ -#include <lib/utility/cgi.hpp> +#include <lib/utility/cgi_writer.hpp> #include <fstream> #include <sstream> #include <string> diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index 6bddba9..4beec1c 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -11,6 +11,7 @@ #include <boost/core/nvp.hpp> #include <boost/core/demangle.hpp> #include <boost/core/null_deleter.hpp> +#include <boost/container/allocator.hpp> #include <filesystem> #include <utility> diff --git a/dev/lib/utility/cgi.hpp b/dev/lib/utility/cgi_writer.hpp index 57625b8..2c30adf 100644 --- a/dev/lib/utility/cgi.hpp +++ b/dev/lib/utility/cgi_writer.hpp @@ -1,5 +1,5 @@ /* - * File: cgi.hpp + * File: cgi_writer.hpp * Author: Amlal El Mahrouss, * Copyright 2023-2025, Amlal El Mahrouss. */ diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp new file mode 100644 index 0000000..ae52c39 --- /dev/null +++ b/dev/lib/utility/chunk_string.hpp @@ -0,0 +1,79 @@ +/* +* File: core/chunk_string.hpp + * Purpose: String implementation for the SOCL C++ library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#ifndef SOCL_UTILITY_CHUNK_STRING_HPP +#define SOCL_UTILITY_CHUNK_STRING_HPP + +#include <lib/core/includes.hpp> +#include <boost/container/flat_set.hpp> + +namespace snu +{ + template <typename char_type> + class basic_chunk_string; + + template <typename char_type> + struct basic_chunk_string final + { + private: + std::unique_ptr<basic_chunk_string<char_type>> next_chunk_string_{}; + basic_chunk_string<char_type>* prev_chunk_string_{nullptr}; + + std::basic_string<char_type> packed_chunks_{}; + int64_t chunk_total{}; + + constexpr const static auto max_chunk_size = 4096; + + public: + explicit basic_chunk_string() = default; + virtual ~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<char_type>& in) + { + if (in.empty()) + return *this; + + if (chunk_total > max_chunk_size) + { + next_chunk_string_ = std::make_unique<basic_chunk_string<char_type>>(); + *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<char_type>& str() noexcept + { + return packed_chunks_; + } + + inline void print() noexcept + { + snu::io::print(packed_chunks_); + + if (next_chunk_string_) + this->next_chunk_string_->print(); + } + }; + + template <typename char_type> + inline void print(basic_chunk_string<char_type>& fmt) noexcept + { + std::cout << fmt.print(); + } +} // namespace snu +#endif // ifndef SOCL_UTILITY_CHUNK_STRING_HPP
\ No newline at end of file diff --git a/dev/tests/chunk_string/CMakeLists.txt b/dev/tests/chunk_string/CMakeLists.txt new file mode 100644 index 0000000..defa761 --- /dev/null +++ b/dev/tests/chunk_string/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.27) +project(BasicChunkUsage LANGUAGES CXX) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable(BasicChunkUsage chunk_test.cc) +target_link_libraries(BasicChunkUsage gtest_main) + +set_property(TARGET BasicChunkUsage PROPERTY CXX_STANDARD 20) +target_include_directories(BasicChunkUsage PUBLIC ../../) + +# FIXME: Make a pkg-config for the boost library here! +target_include_directories(BasicChunkUsage PUBLIC /opt/homebrew/Cellar/boost/1.87.0/include) + +include(GoogleTest) +gtest_discover_tests(BasicChunkUsage) diff --git a/dev/tests/chunk_string/chunk_test.cc b/dev/tests/chunk_string/chunk_test.cc new file mode 100644 index 0000000..e93f11d --- /dev/null +++ b/dev/tests/chunk_string/chunk_test.cc @@ -0,0 +1,30 @@ +/* + * File: tests/net_test.cc + * Purpose: Network unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#include <lib/io/print.hpp> +#include <lib/tests/gtest.hpp> +#include <lib/utility/chunk_string.hpp> + +TEST(ChunkTest, BasicChunkUsage) +{ + const std::string test_string = "HELLO, WORLD!"; + const unsigned iterations = 1024000; + + auto start = std::chrono::high_resolution_clock::now(); + + snu::basic_chunk_string<char> optimized; + + for (unsigned i = 0; i < iterations; ++i) + { + optimized += test_string; + } + + auto end = std::chrono::high_resolution_clock::now(); + auto optimized_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); + + EXPECT_TRUE(optimized_time.count() < 200U); +} diff --git a/dev/tests/network_basic/net_test.cc b/dev/tests/network_basic/net_test.cc index 34f6ebe..f16fb06 100644 --- a/dev/tests/network_basic/net_test.cc +++ b/dev/tests/network_basic/net_test.cc @@ -5,7 +5,7 @@ * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. */ -#include <lib/net/network.hpp> +#include <lib/net/modem.hpp> #include <lib/io/print.hpp> #include <lib/tests/gtest.hpp> #include <cstring> @@ -18,5 +18,5 @@ TEST(NetworkTest, BasicNetworkUsage) EXPECT_TRUE(modem.is_valid()); std::basic_string<char> buf_dst = "HELLO, NET!"; - modem.transmit(buf_dst); + EXPECT_TRUE(modem.transmit(buf_dst)); } |
