LeviLamina
Loading...
Searching...
No Matches
FixedString.h
1#pragma once
2
3#include <cstddef>
4#include <string>
5#include <string_view>
6
7#include "ll/api/base/SourceLocation.h"
8
9namespace ll {
10template <size_t N>
12 char buf[N + 1]{};
13
14 consteval FixedString() noexcept = default;
15 consteval FixedString(std::string_view s) noexcept { std::copy_n(s.data(), s.size(), buf); }
16 consteval FixedString(char const* s) noexcept { std::copy_n(s, N, buf); }
17
18 constexpr operator std::string_view() const noexcept { return buf; }
19
20 constexpr char const* c_str() const noexcept { return buf; }
21 constexpr std::string_view sv() const noexcept { return buf; }
22 constexpr std::string str() const noexcept { return buf; }
23
24 [[nodiscard]] constexpr char const& operator[](size_t idx) const noexcept { return buf[idx]; }
25 [[nodiscard]] constexpr char& operator[](size_t idx) noexcept { return buf[idx]; }
26
27 template <size_t Ny>
28 consteval auto operator+(FixedString<Ny> const& other) noexcept {
30 std::copy_n(buf, N, res.buf);
31 std::copy_n(other.buf, Ny, N + res.buf);
32 return res;
33 }
34};
35
36template <size_t N>
38 SourceLocation location;
39
40 consteval FixedStrWithLoc(std::string_view s, SourceLocation const& loc = SourceLocation::current()) noexcept
41 : FixedString<N>(s),
42 location(loc) {}
43 consteval FixedStrWithLoc(char const* s, SourceLocation const& loc = SourceLocation::current()) noexcept
44 : FixedString<N>(s),
45 location(loc) {}
46
47 constexpr SourceLocation const& loc() const noexcept { return location; }
48};
49
50template <size_t N>
51FixedString(char const (&)[N]) -> FixedString<N - 1>;
52template <size_t N>
53FixedStrWithLoc(char const (&)[N]) -> FixedStrWithLoc<N - 1>;
54
55} // namespace ll
Definition SourceLocation.h:10
Definition FixedString.h:37
Definition FixedString.h:11