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 [[nodiscard]] constexpr char const* data() const { return buf; }
27 [[nodiscard]] constexpr char* data() { return buf; }
28
29 constexpr size_t size() const { return N; }
30
31
32 template <size_t Ny>
33 consteval auto operator+(FixedString<Ny> const& other) noexcept {
35 std::copy_n(buf, N, res.buf);
36 std::copy_n(other.buf, Ny, N + res.buf);
37 return res;
38 }
39};
40
41template <size_t N>
43 SourceLocation location;
44
45 consteval FixedStrWithLoc(std::string_view s, SourceLocation const& loc = SourceLocation::current()) noexcept
46 : FixedString<N>(s),
47 location(loc) {}
48 consteval FixedStrWithLoc(char const* s, SourceLocation const& loc = SourceLocation::current()) noexcept
49 : FixedString<N>(s),
50 location(loc) {}
51
52 constexpr SourceLocation const& loc() const noexcept { return location; }
53};
54
55template <size_t N>
56FixedString(char const (&)[N]) -> FixedString<N - 1>;
57template <size_t N>
58FixedStrWithLoc(char const (&)[N]) -> FixedStrWithLoc<N - 1>;
59
60} // namespace ll
Definition SourceLocation.h:10
Definition FixedString.h:42
Definition FixedString.h:11