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{
7template <typename T>
8struct StringBuilderFor;
11
25struct SC_COMPILER_EXPORT StringBuilder
26{
27 // clang-format off
29 template <typename T> static StringBuilderFor<T> create(T& stringOrBuffer) noexcept { return {stringOrBuffer, StringBuilder::Clear}; }
30
32 template <typename T> static StringBuilderFor<T> createForAppendingTo(T& stringOrBuffer) noexcept { return {stringOrBuffer, StringBuilder::Append}; }
33
35 template <typename T, typename... Types>
36 [[nodiscard]] static bool format(T& buffer, StringView fmt, Types&&... args) { return StringBuilder::create(buffer).append(fmt, forward<Types>(args)...); }
37 // clang-format on
38
40 template <typename... Types>
41 [[nodiscard]] bool append(StringView fmt, Types&&... args)
42 {
43 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
44 // because on a valid UTF8 string, these chars are unambiguously recognizable
45 StringFormatOutput sfo(encoding, *buffer);
46 return StringFormat<StringIteratorASCII>::format(sfo, fmt, forward<Types>(args)...);
47 }
48
51 [[nodiscard]] bool append(StringView str);
52
60 [[nodiscard]] bool appendReplaceAll(StringView source, StringView occurrencesOf, StringView with);
61
63 enum class AppendHexCase
64 {
65 UpperCase,
66 LowerCase,
67 };
75 [[nodiscard]] bool appendHex(Span<const uint8_t> data, AppendHexCase casing);
76
77 protected:
78 enum Flags
79 {
81 Append
82 };
83 friend struct Path;
84
85 StringBuilder() = default;
86 StringBuilder(IGrowableBuffer& ibuffer, StringEncoding encoding, Flags flags) noexcept;
87 void initWithEncoding(IGrowableBuffer& bufferT, StringEncoding stringEncoding, Flags flags) noexcept;
88
89 IGrowableBuffer* buffer = nullptr;
90 StringEncoding encoding;
91};
92
95template <typename T>
97{
98 GrowableBuffer<T> growableBuffer;
99 StringView finalizedView;
100 StringBuilderFor(T& stringOrBuffer, Flags flags) noexcept : growableBuffer(stringOrBuffer)
101 {
102 initWithEncoding(growableBuffer, GrowableBuffer<T>::getEncodingFor(stringOrBuffer), flags);
103 }
104
105 ~StringBuilderFor() noexcept { finalize(); }
106
109 {
110 if (buffer)
111 {
112 growableBuffer.finalize();
113 finalizedView = {{buffer->data(), buffer->size()}, true, encoding};
114 buffer = nullptr;
115 }
116 return view();
117 }
118
121 [[nodiscard]] StringView view() noexcept
122 {
123 SC_ASSERT_RELEASE(buffer == nullptr);
124 return finalizedView;
125 }
126};
127
129
130} // namespace SC
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:42
Parse and compose filesystem paths for windows and posix.
Definition Path.h:16
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
StringBuilder tied to a specific type, created through StringBuilder::create or StringBuilder::create...
Definition StringBuilder.h:97
StringView view() noexcept
Returns the resulting StringView after finalize.
Definition StringBuilder.h:121
StringView finalize() noexcept
Finalizes the StringBuilder, returning the resulting StringView.
Definition StringBuilder.h:108
Builds String out of a sequence of StringView or formatting through StringFormat.
Definition StringBuilder.h:26
Flags
Definition StringBuilder.h:79
@ Clear
Destination buffer will be cleared before pushing to it.
Definition StringBuilder.h:80
bool append(StringView str)
Appends StringView to destination buffer.
AppendHexCase
Option for StringBuilder::appendHex.
Definition StringBuilder.h:64
bool appendHex(Span< const uint8_t > data, AppendHexCase casing)
Appends given binary data escaping it as hexadecimal ASCII characters.
static StringBuilderFor< T > create(T &stringOrBuffer) noexcept
Creates a StringBuilder for the given string or buffer, replacing its current contents.
Definition StringBuilder.h:29
bool appendReplaceAll(StringView source, StringView occurrencesOf, StringView with)
Appends source to destination buffer, replacing occurrencesOf StringView with StringView with
static StringBuilderFor< T > createForAppendingTo(T &stringOrBuffer) noexcept
Creates a StringBuilder for the given string or buffer, appending to its current contents.
Definition StringBuilder.h:32
static bool format(T &buffer, StringView fmt, Types &&... args)
Helper to format a StringView against args, replacing destination contents, in a single function call...
Definition StringBuilder.h:36
bool append(StringView fmt, Types &&... args)
Formats the given StringView against args, appending to destination contents.
Definition StringBuilder.h:41
Definition StringFormat.h:26
Formats String with a simple DSL embedded in the format string.
Definition StringFormat.h:81
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:46