Bash++
Bash++ compiler internal documentation
FixedString.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <string_view>
5#include <ostream>
6
7template<size_t N>
8struct FixedString {
9 std::array<char, N> data{};
10 size_t size = 0;
11
12 consteval void append(std::string_view str) {
13 for (char c : str) {
14 data[size++] = c;
15 }
16 }
17
18 consteval void append(char c, size_t count = 1) {
19 for (size_t i = 0; i < count; i++) {
20 data[size++] = c;
21 }
22 }
23
24 consteval std::string_view view() const {
25 return std::string_view(data.data(), size);
26 }
27
28 friend std::ostream& operator<<(std::ostream& os, const FixedString& fs) {
29 return os.write(fs.data.data(), fs.size);
30 }
31};
32
33// Helper function to figure a string's length at compile-time
34consteval size_t string_length(const char* str) {
35 size_t length = 0;
36 while (str && str[length] != '\0') {
37 length++;
38 }
39 return length;
40}
consteval size_t string_length(const char *str)
Definition FixedString.h:34
Definition FixedString.h:8
consteval void append(std::string_view str)
Definition FixedString.h:12
consteval std::string_view view() const
Definition FixedString.h:24
std::array< char, N > data
Definition FixedString.h:9
friend std::ostream & operator<<(std::ostream &os, const FixedString &fs)
Definition FixedString.h:28
size_t size
Definition FixedString.h:10
consteval void append(char c, size_t count=1)
Definition FixedString.h:18