Skip to content
LeviMC
English
AutoLightDark
LeviLamina
Loading...
Searching...
No Matches
FixedString.h
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <string>
6#include <string_view>
7
8namespace ll {
9template <size_t N>
10struct FixedString {
11 char buf[N + 1]{};
12
13 consteval FixedString() noexcept = default;
14 consteval FixedString(std::string_view s) noexcept { std::copy_n(s.data(), std::min(s.size(), N), buf); }
15 consteval FixedString(char const* s) noexcept : FixedString(std::string_view{s}) {}
16
17 constexpr operator std::string_view() const noexcept { return buf; }
18
19 constexpr char const* c_str() const noexcept { return buf; }
20 constexpr std::string_view sv() const noexcept { return buf; }
21 constexpr std::string str() const noexcept { return buf; }
22
23 [[nodiscard]] constexpr char const& operator[](size_t idx) const noexcept { return buf[idx]; }
24 [[nodiscard]] constexpr char& operator[](size_t idx) noexcept { return buf[idx]; }
25 [[nodiscard]] constexpr char const* data() const { return buf; }
26 [[nodiscard]] constexpr char* data() { return buf; }
27
28 constexpr size_t size() const { return N; }
29
30
31 template <size_t Ny>
32 consteval auto operator+(FixedString<Ny> const& other) noexcept {
33 FixedString<N + Ny> res{};
34 std::copy_n(buf, N, res.buf);
35 std::copy_n(other.buf, Ny, N + res.buf);
36 return res;
37 }
38};
39
40template <size_t N>
41FixedString(char const (&)[N]) -> FixedString<N - 1>;
42
43} // namespace ll
Definition FixedString.h:10