-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.hpp
More file actions
104 lines (89 loc) · 3.42 KB
/
Copy pathstring.hpp
File metadata and controls
104 lines (89 loc) · 3.42 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @file string.hpp
* @author wh0crypt (wh0crypt@proton.me)
* @brief String class representing a formal string (sequence of symbols) over an alphabet.
* @version 0.1
* @date 2026-06-27
*
* @copyright Copyright (c) 2026 wh0crypt. Licensed under the MIT License.
*/
#ifndef STRING_HPP
#define STRING_HPP
#include "alphabet.hpp"
#include "symbol.hpp"
#include <string>
#include <string_view>
#include <vector>
/**
* @class String
* @brief Represents a formal string (w) consisting of a finite sequence of Symbols.
*
* This class encapsulates a collection of symbols, providing validation tools
* against formal alphabets and standard iterator access for processing.
*/
class String
{
public:
/**
* @brief Default constructor. Initializes an empty string with no symbols.
*/
String() noexcept = default;
/**
* @brief Constructs a single-symbol string.
* @param symbol The initial Symbol to wrap.
*/
explicit String(const Symbol &symbol) noexcept(false) { this->symbols_.emplace_back(symbol); }
/**
* @brief Constructs a formal String from a raw string view.
* @param str The raw character sequence.
*/
explicit String(std::string_view str) noexcept(false);
/**
* @brief Constructs a String by taking ownership of an existing vector of symbols.
*
* Uses pass-by-value and std::move to prevent redundant overhead dynamic allocations.
*
* @param vec The source vector of symbols.
*/
explicit String(std::vector<Symbol> vec) noexcept(false) : symbols_(std::move(vec)) {}
/**
* @brief Returns the total number of symbols contained in the string.
* @return std::size_t The length of the formal string.
*/
[[nodiscard]] std::size_t length() const noexcept { return this->symbols_.size(); }
/**
* @brief Reconstructs a standard std::string representation from the underlying symbols.
* @return std::string A standard string concatenation of all wrapped symbols.
*/
[[nodiscard]] std::string get_str() const noexcept(false);
/**
* @brief Returns an iterator to the beginning of the symbol sequence.
*/
[[nodiscard]] auto begin() const noexcept { return this->symbols_.begin(); }
/**
* @brief Returns an iterator to the end of the symbol sequence.
*/
[[nodiscard]] auto end() const noexcept { return this->symbols_.end(); }
/**
* @brief Validates if all symbols within this string belong to a specific formal Alphabet.
*
* Uses C++20 ranges to perform a clean and optimized all-of predicate check.
*
* @param alphabet The formal Alphabet descriptor to validate against.
* @return true If every single symbol is registered in the alphabet, false otherwise.
*/
[[nodiscard]] bool is_valid_for(const Alphabet &alphabet) const noexcept;
/**
* @brief Defaulted three-way comparison operator (C++20 spaceship operator).
*
* Instructs the compiler to automatically generate standard relational
* operators (<, <=, >, >=, ==, !=) based on the underlying symbols_ member.
*
* @param other The other String instance to compare against.
* @return auto A strong ordering comparison category (std::strong_ordering).
*/
[[nodiscard]] auto operator<=>(const String &other) const noexcept = default;
private:
std::vector<Symbol> symbols_; ///< The finite sequence of symbols forming the string.
};
#endif // STRING_HPP