Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
StringBuilder.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringFormat.h"
5namespace SC
6{
9
17struct SC_COMPILER_EXPORT StringBuilder
18{
20 enum Flags
21 {
23 DoNotClear
24 };
29 template <typename T>
30 StringBuilder(T& buffer, StringEncoding encoding, Flags flags = DoNotClear)
31 {
32 GrowableBuffer<T>& bufferT = bufferStorage.reinterpret_as<GrowableBuffer<T>>();
33 placementNew(bufferT, buffer);
34 initWithEncoding(bufferT, encoding, flags);
35 }
36
40 template <typename T>
41 StringBuilder(T& string, Flags flags = DoNotClear)
42 {
43 GrowableBuffer<T>& bufferT = bufferStorage.reinterpret_as<GrowableBuffer<T>>();
44 placementNew(bufferT, string);
45 initWithEncoding(bufferT, string.getEncoding(), flags);
46 }
47
48 StringBuilder(IGrowableBuffer& bufferT, StringEncoding encoding, Flags flags);
49
51
54
57 [[nodiscard]] StringView view();
58
71 template <typename T, typename... Types>
72 [[nodiscard]] static bool format(T& buffer, StringView fmt, Types&&... args);
73
78 template <typename T>
79 [[nodiscard]] static bool format(T& buffer, StringView text);
80
95 template <typename... Types>
96 [[nodiscard]] bool append(StringView fmt, Types&&... args);
97
101 [[nodiscard]] bool append(StringView str);
102
111 [[nodiscard]] bool appendReplaceAll(StringView source, StringView occurrencesOf, StringView with);
112
114 enum class AppendHexCase
115 {
116 UpperCase,
117 LowerCase,
118 };
126 [[nodiscard]] bool appendHex(Span<const uint8_t> data, AppendHexCase casing);
127
128 private:
129 void initWithEncoding(IGrowableBuffer& bufferT, StringEncoding stringEncoding, Flags flags);
130 void clear();
131
132 IGrowableBuffer* buffer = nullptr;
133 bool destroyBuffer = true;
134
135 AlignedStorage<6 * sizeof(void*)> bufferStorage;
136
137 StringEncoding encoding;
138 StringView finalizedView;
139};
141
142} // namespace SC
143
144//-----------------------------------------------------------------------------------------------------------------------
145// Implementations Details
146//-----------------------------------------------------------------------------------------------------------------------
147template <typename T>
149{
150 StringBuilder sb(buffer, Clear);
151 const bool res = sb.append(fmt);
152 sb.finalize();
153 return res;
154}
155
156template <typename T, typename... Types>
157inline bool SC::StringBuilder::format(T& buffer, StringView fmt, Types&&... args)
158{
159 StringBuilder sb(buffer, Clear);
160 const bool res = sb.append(fmt, forward<Types>(args)...);
161 sb.finalize();
162 return res;
163}
164
165template <typename... Types>
166inline bool SC::StringBuilder::append(StringView fmt, Types&&... args)
167{
168 SC_TRY(fmt.getEncoding() != StringEncoding::Utf16); // UTF16 format strings are not supported
169 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
170 // because on a valid UTF8 string, these chars are unambiguously recognizable
171 StringFormatOutput sfo(encoding, *buffer);
173}
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:260
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition Result.h:48
A buffer of bytes with given alignment.
Definition AlignedStorage.h:29
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Builds String out of a sequence of StringView or formatting through StringFormat.
Definition StringBuilder.h:18
StringBuilder(T &buffer, StringEncoding encoding, Flags flags=DoNotClear)
Create a StringBuilder that will push to given Buffer, with specific encoding.
Definition StringBuilder.h:30
Flags
Clearing flags used when initializing destination buffer.
Definition StringBuilder.h:21
@ Clear
Destination buffer will be cleared before pushing to it.
Definition StringBuilder.h:22
bool append(StringView str)
Appends StringView to destination buffer.
AppendHexCase
Option for StringBuilder::appendHex.
Definition StringBuilder.h:115
bool appendHex(Span< const uint8_t > data, AppendHexCase casing)
Appends given binary data escaping it as hexadecimal ASCII characters.
StringView view()
Obtains view after finalize has been previously called.
bool appendReplaceAll(StringView source, StringView occurrencesOf, StringView with)
Appends source to destination buffer, replacing occurrencesOf StringView with StringView with
StringBuilder(T &string, Flags flags=DoNotClear)
Create a StringBuilder that will push to given Buffer, with specific encoding.
Definition StringBuilder.h:41
static bool format(T &buffer, StringView fmt, Types &&... args)
Uses StringFormat to format the given StringView against args, replacing destination contents.
Definition StringBuilder.h:157
bool append(StringView fmt, Types &&... args)
Uses StringFormat to format the given StringView against args, appending to destination contents.
Definition StringBuilder.h:166
StringView finalize()
Finalizes building the string and returns a StringView with the contents.
Definition StringFormat.h:26
static bool format(StringFormatOutput &data, StringView fmt, Types &&... args)
Formats fmt StringView using simple DSL where {} are replaced with args.
Definition StringFormat.h:224
constexpr StringEncoding getEncoding() const
Get encoding of this StringView.
Definition StringSpan.h:98
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:46