openMSX
TemporaryString.hh
Go to the documentation of this file.
1#ifndef TEMPORARYSTRING_HH
2#define TEMPORARYSTRING_HH
3
4#include "StringStorage.hh"
5#include "zstring_view.hh"
6#include <array>
7#include <concepts>
8#include <iostream>
9#include <string_view>
10
11
27public:
28 static constexpr size_t BUFSIZE = 127;
29
30 explicit TemporaryString()
31 : n(0)
32 {
33 ptr = buffer.data();
34 buffer[0] = '\0';
35 }
36
37 explicit TemporaryString(const char* s)
38 : n(std::char_traits<char>::length(s)), ptr(s) {}
40 : n(s.size()), ptr(s.data()) {}
41
42 explicit TemporaryString(size_t n_, std::invocable<char*> auto fillOp)
43 : n(n_)
44 {
45 char* p = [&] {
46 if (n <= BUFSIZE) {
47 return buffer.data();
48 } else {
49 owner = allocate_string_storage(n + 1);
50 return owner.get();
51 }
52 }();
53 ptr = p;
54 fillOp(p);
55 p[n] = '\0';
56 }
61 ~TemporaryString() = default;
62
63 [[nodiscard]] auto size() const { return n; }
64 [[nodiscard]] auto empty() const { return n == 0; }
65
66 [[nodiscard]] const char* data() const { return ptr; }
67 [[nodiscard]] const char* c_str() const { return ptr; }
68
69 [[nodiscard]] std::string_view view() const { return {ptr, n}; }
70 [[nodiscard]] operator std::string_view() const { return {ptr, n}; }
71 [[nodiscard]] operator zstring_view() const { return {ptr, n}; }
72
73 [[nodiscard]] bool starts_with(std::string_view s) const { return view().starts_with(s); }
74 [[nodiscard]] bool starts_with(char c) const { return view().starts_with(c); }
75 [[nodiscard]] bool starts_with(const char* s) const { return view().starts_with(s); }
76 [[nodiscard]] bool ends_with(std::string_view s) const { return view().ends_with(s); }
77 [[nodiscard]] bool ends_with(char c) const { return view().ends_with(c); }
78 [[nodiscard]] bool ends_with(const char* s) const { return view().ends_with(s); }
79
80private:
81 size_t n;
82 const char* ptr;
83 StringStorage owner;
84 std::array<char, BUFSIZE + 1> buffer;
85};
86
87inline std::ostream& operator<<(std::ostream& os, const TemporaryString& str)
88{
89 os << std::string_view(str);
90 return os;
91}
92
93#endif
std::unique_ptr< char, FreeStringStorage > StringStorage
StringStorage allocate_string_storage(size_t size)
Allocate a 'StringStorage' large enough for 'size' characters.
std::ostream & operator<<(std::ostream &os, const TemporaryString &str)
TemporaryString.
static constexpr size_t BUFSIZE
TemporaryString & operator=(TemporaryString &&)=delete
const char * c_str() const
bool starts_with(char c) const
bool starts_with(const char *s) const
std::string_view view() const
TemporaryString(TemporaryString &&)=delete
TemporaryString(zstring_view s)
bool ends_with(std::string_view s) const
const char * data() const
bool starts_with(std::string_view s) const
bool ends_with(const char *s) const
TemporaryString(const TemporaryString &)=delete
auto empty() const
bool ends_with(char c) const
TemporaryString & operator=(const TemporaryString &)=delete
auto size() const
TemporaryString(const char *s)
TemporaryString(size_t n_, std::invocable< char * > auto fillOp)
~TemporaryString()=default
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
STL namespace.