GMP 0.3.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1// ___ __ __ ___
2// / __| \/ | _ \ GMP(Generative Metaprogramming)
3// | (_ | |\/| | _/ version 0.3.0
4// \___|_| |_|_| https://github.com/lkimuk/gmp
5//
6// SPDX-FileCopyrightText: 2026 Miles Li <https://www.cppmore.com/>
7// SPDX-License-Identifier: MIT
8//
9// This file is part of the GMP (Generative Metaprogramming) library.
10// Full project source: https://github.com/lkimuk/gmp
11
12#ifndef GMP_META_FIXED_STRING_HPP_
13#define GMP_META_FIXED_STRING_HPP_
14
15#include <algorithm>
16#include <string_view>
17
18#include "utility.hpp"
19
20namespace gmp {
21
35template<std::size_t N>
37 using size_type = std::size_t;
38 static constexpr size_type npos = size_type(-1);
39
45 [[maybe_unused]] constexpr fixed_string(const char (&str)[N + 1]) {
46 std::copy_n(str, N + 1, data_);
47 }
48
54 [[maybe_unused]] constexpr fixed_string(std::string_view sv) {
55 std::copy_n(sv.data(), N, data_);
56 data_[N] = '\0';
57 }
58
60
66 [[nodiscard]] consteval char operator[](size_type pos) const noexcept {
67 static_assert(pos <= N, "fixed_string: index out of range");
68 return data_[pos];
69 }
70
76 consteval const char* data() const noexcept { return data_; }
77
83 constexpr auto to_string_view() const noexcept { return std::string_view(data_, N); }
84
90 constexpr operator char const*() const noexcept { return data_; }
91
93
98 consteval size_type size() const noexcept { return N; }
99
105 consteval size_type length() const noexcept { return N; }
106
112 consteval bool empty() const noexcept { return N == 0; }
113
115
117
124 consteval size_type find(const char* str, size_type pos = 0) const noexcept {
125 auto sv = this->to_string_view();
126 auto found_pos = sv.find(str, pos);
127 return found_pos;
128 }
129
139 template<auto Pos, auto Count = fixed_string::npos>
141 constexpr std::size_t actual_count = (Count == fixed_string<N>::npos) ?
142 (N - Pos) : Count;
143 static_assert(Pos <= N, "fixed_string::substr: position out of range");
144 static_assert(Pos + actual_count <= N, "fixed_string::substr: count out of range");
145
146 char buffer[actual_count + 1]{};
147 std::copy_n(data_ + Pos, actual_count, buffer);
148 buffer[actual_count] = '\0';
150 }
151
152 friend consteval auto operator<=>(fixed_string const&, fixed_string const&) = default;
153
154 char data_[N + 1];
155};
156
157template<std::size_t N>
163fixed_string(char const(&)[N]) -> fixed_string<N - 1>;
164
174template<std::size_t N1, std::size_t N2>
176 char result[N1 + N2 + 1]{};
177 std::copy_n(s1.data(), N1, result);
178 std::copy_n(s2.data(), N2 + 1, result + N1);
180}
181
188template<fixed_string s>
189consteval auto operator""_fs() {
190 return s;
191}
192
195namespace detail {
196
197template<fixed_string Value, auto String>
198consteval auto remove_all_impl(constant_arg_t<String>) {
199 constexpr auto pos = String.find(Value.data());
200 if constexpr (pos == decltype(String)::npos) {
201 return String;
202 } else {
203 constexpr auto after = String.substr(constant_arg<0>, constant_arg<pos>);
204 if constexpr (pos + Value.size() < String.size()) {
205 constexpr auto res = after + String.substr(constant_arg<pos + Value.size()>);
207 } else {
208 return after;
209 }
210 }
211}
212
213} // namespace detail
214
230 constexpr auto after = detail::remove_all_impl<FirstValue>(constant_arg<String>);
231 if constexpr (sizeof...(RestValues) > 0) {
233 } else {
234 return after;
235 }
236}
237
240} // namespace gmp
241
242#endif // GMP_META_FIXED_STRING_HPP_
consteval fixed_string< N1+N2 > operator+(fixed_string< N1 > const &s1, fixed_string< N2 > const &s2)
Concatenate two fixed strings at compile time.
fixed_string(char const(&)[N]) -> fixed_string< N - 1 >
Deduce fixed_string length from a string literal.
consteval auto remove_all(constant_arg_t< String >)
Remove all occurrences of one or more compile-time substrings.
consteval auto enum_values()
Get all enumerator values of an enumeration type at compile-time.
Definition meta.hpp:155
constexpr constant_arg_t< V > constant_arg
A ready-to-use constant_arg_t<V> object for a compile-time value.
Definition utility.hpp:40
Definition lock.hpp:21
Wrap a compile-time value as a distinct function argument type.
Definition utility.hpp:30
A compile-time string type with fixed length and constexpr operations.
consteval auto substr(constant_arg_t< Pos >, constant_arg_t< Count >=constant_arg< fixed_string::npos >) const noexcept
Extract a compile-time substring.
friend consteval auto operator<=>(fixed_string const &, fixed_string const &)=default
constexpr auto to_string_view() const noexcept
Convert the fixed string to std::string_view.
consteval const char * data() const noexcept
Get a pointer to the underlying null-terminated character data.
static constexpr size_type npos
consteval size_type length() const noexcept
Get the number of stored characters.
consteval size_type find(const char *str, size_type pos=0) const noexcept
Modifiers.
consteval bool empty() const noexcept
Check whether the fixed string is empty.
constexpr fixed_string(std::string_view sv)
Construct a fixed string from a string view of matching length.
consteval char operator[](size_type pos) const noexcept
Element access.
consteval size_type size() const noexcept
Capacity.
constexpr fixed_string(const char(&str)[N+1])
Construct a fixed string from a string literal.
std::size_t size_type