summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/utility/chunk_string.hpp
blob: 79a388166bbe126e7e0d0599515eea17a59072e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* 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 <lib/core/includes.hpp>
#include <boost/container/flat_set.hpp>

namespace ocl
{
	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:
		basic_chunk_string() = default;

		basic_chunk_string(const char_type* in)
		{
			this->operator+=(in);
		}

		basic_chunk_string(const std::basic_string<char_type>& 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<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() const noexcept
		{
			return packed_chunks_;
		}

		void print() noexcept
		{
			ocl::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
	{
		fmt.print();
	}
} // namespace ocl
#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP