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"
5#include "StringsExport.h"
6namespace SC
7{
8template <typename T>
9struct StringBuilderFor;
12
26struct SC_STRINGS_EXPORT StringBuilder
27{
28 // clang-format off
30 template <typename T> static StringBuilderFor<T> create(T& stringOrBuffer) noexcept { return {stringOrBuffer, StringBuilder::Clear}; }
31
33 template <typename T> static StringBuilderFor<T> createForAppendingTo(T& stringOrBuffer) noexcept { return {stringOrBuffer, StringBuilder::Append}; }
34
36 template <typename T, typename... Types>
37 [[nodiscard]] static bool format(T& buffer, StringView fmt, Types&&... args) { return StringBuilder::create(buffer).append(fmt, forward<Types>(args)...); }
38 // clang-format on
39
41 template <typename... Types>
42 [[nodiscard]] bool append(StringView fmt, Types&&... args)
43 {
44 // It's ok parsing format string '{' and '}' both for utf8 and ascii with StringIteratorASCII
45 // because on a valid UTF8 string, these chars are unambiguously recognizable
46 StringFormatOutput sfo(encoding, *buffer);
47 return StringFormat<StringIteratorASCII>::format(sfo, fmt, forward<Types>(args)...);
48 }
49
52 [[nodiscard]] bool append(StringView str);
53
61 [[nodiscard]] bool appendReplaceAll(StringView source, StringView occurrencesOf, StringView with);
62
64 enum class AppendHexCase
65 {
66 UpperCase,
67 LowerCase,
68 };
76 [[nodiscard]] bool appendHex(Span<const uint8_t> data, AppendHexCase casing);
77
78 protected:
79 enum Flags
80 {
82 Append
83 };
84 friend struct Path;
85
86 StringBuilder() = default;
87 StringBuilder(IGrowableBuffer& ibuffer, StringEncoding encoding, Flags flags) noexcept;
88 void initWithEncoding(IGrowableBuffer& bufferT, StringEncoding stringEncoding, Flags flags) noexcept;
89
90 IGrowableBuffer* buffer = nullptr;
91 StringEncoding encoding;
92};
93
96template <typename T>
98{
99 GrowableBuffer<T> growableBuffer;
100 StringView finalizedView;
101 StringBuilderFor(T& stringOrBuffer, Flags flags) noexcept : growableBuffer(stringOrBuffer)
102 {
103 initWithEncoding(growableBuffer, GrowableBuffer<T>::getEncodingFor(stringOrBuffer), flags);
104 }
105
106 ~StringBuilderFor() noexcept { finalize(); }
107
110 {
111 if (buffer)
112 {
113 growableBuffer.finalize();
114 finalizedView = {{buffer->data(), buffer->size()}, true, encoding};
115 buffer = nullptr;
116 }
117 return view();
118 }
119
122 [[nodiscard]] StringView view() noexcept
123 {
124 SC_ASSERT_RELEASE(buffer == nullptr);
125 return finalizedView;
126 }
127};
128
130
131} // namespace SC
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:48
Parse and compose filesystem paths for windows and posix.
Definition Path.h:17
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:98
StringView view() noexcept
Returns the resulting StringView after finalize.
Definition StringBuilder.h:122
StringView finalize() noexcept
Finalizes the StringBuilder, returning the resulting StringView.
Definition StringBuilder.h:109
Builds String out of a sequence of StringView or formatting through StringFormat.
Definition StringBuilder.h:27
Flags
Definition StringBuilder.h:80
@ Clear
Destination buffer will be cleared before pushing to it.
Definition StringBuilder.h:81
bool append(StringView str)
Appends StringView to destination buffer.
AppendHexCase
Option for StringBuilder::appendHex.
Definition StringBuilder.h:65
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:30
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:33
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:37
bool append(StringView fmt, Types &&... args)
Formats the given StringView against args, appending to destination contents.
Definition StringBuilder.h:42
Definition StringFormat.h:27
Formats String with a simple DSL embedded in the format string.
Definition StringFormat.h:84
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:47