Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::StringBuilder Struct Reference

Builds String out of a sequence of StringView or formatting through StringFormat. More...

#include <StringBuilder.h>

Inheritance diagram for SC::StringBuilder:
SC::StringBuilderFor< T >

Public Types

enum class  AppendHexCase {
  UpperCase ,
  LowerCase
}
 Option for StringBuilder::appendHex. More...
 

Public Member Functions

template<typename... Types>
bool append (StringView fmt, Types &&... args)
 Formats the given StringView against args, appending to destination contents.
 
bool append (StringView str)
 Appends StringView to destination buffer.
 
bool appendReplaceAll (StringView source, StringView occurrencesOf, StringView with)
 Appends source to destination buffer, replacing occurrencesOf StringView with StringView with
 
bool appendHex (Span< const uint8_t > data, AppendHexCase casing)
 Appends given binary data escaping it as hexadecimal ASCII characters.
 

Static Public Member Functions

template<typename T >
static StringBuilderFor< T > create (T &stringOrBuffer) noexcept
 Creates a StringBuilder for the given string or buffer, replacing its current contents.
 
template<typename T >
static StringBuilderFor< T > createForAppendingTo (T &stringOrBuffer) noexcept
 Creates a StringBuilder for the given string or buffer, appending to its current contents.
 
template<typename T , typename... Types>
static bool format (T &buffer, StringView fmt, Types &&... args)
 Helper to format a StringView against args, replacing destination contents, in a single function call.
 

Protected Types

enum  Flags {
  Clear ,
  Append
}
 

Protected Member Functions

 StringBuilder (IGrowableBuffer &ibuffer, StringEncoding encoding, Flags flags) noexcept
 
void initWithEncoding (IGrowableBuffer &bufferT, StringEncoding stringEncoding, Flags flags) noexcept
 

Protected Attributes

IGrowableBuffer * buffer = nullptr
 
StringEncoding encoding
 

Friends

struct Path
 

Detailed Description

Builds String out of a sequence of StringView or formatting through StringFormat.

The output can be a SC::Buffer or a SC::SmallBuffer (see Foundation) One can do a:

StringBuilder::format example:

String buffer(StringEncoding::Ascii); // or SmallString<N> / Buffer
SC_TEST_EXPECT(StringBuilder::format(buffer, "[{1}-{0}]", "Storia", "Bella"));
SC_TEST_EXPECT(buffer.view() == "[Bella-Storia]");

StringBuilder::create example:

String buffer(StringEncoding::Ascii); // or SmallString<N> / Buffer
auto builder = StringBuilder::create(buffer); // or StringBuilder::createAppend(buffer)
SC_TEST_EXPECT(builder.append("Salve"));
SC_TEST_EXPECT(builder.append(" {1} {0}!!!", "tutti", "a"));
SC_ASSERT_RELEASE(builder.finalize() == "Salve a tutti!!!");

Member Enumeration Documentation

◆ AppendHexCase

◆ Flags

enum SC::StringBuilder::Flags
protected
Enumerator
Clear 

Destination buffer will be cleared before pushing to it.

Append 

Destination buffer will not be cleared before pushing to it.

Member Function Documentation

◆ append() [1/2]

template<typename... Types>
bool SC::StringBuilder::append ( StringView fmt,
Types &&... args )
inlinenodiscard

Formats the given StringView against args, appending to destination contents.

◆ append() [2/2]

bool SC::StringBuilder::append ( StringView str)
nodiscard

Appends StringView to destination buffer.

Parameters
strStringView to append to destination buffer

◆ appendHex()

bool SC::StringBuilder::appendHex ( Span< const uint8_t > data,
AppendHexCase casing )
nodiscard

Appends given binary data escaping it as hexadecimal ASCII characters.

Parameters
dataBinary data to append to destination buffer
casingSpecifies if it should be appended using upper case or lower case
Returns
true if append succeeded

Example:

uint8_t bytes[4] = {0x12, 0x34, 0x56, 0x78};
String buffer;
auto builder = StringBuilder::create(buffer);
SC_TEST_EXPECT(builder.appendHex({bytes, sizeof(bytes)}, StringBuilder::AppendHexCase::UpperCase));
SC_TEST_EXPECT(builder.finalize() == "12345678");

◆ appendReplaceAll()

bool SC::StringBuilder::appendReplaceAll ( StringView source,
StringView occurrencesOf,
StringView with )
nodiscard

Appends source to destination buffer, replacing occurrencesOf StringView with StringView with

Parameters
sourceThe StringView to be appended
occurrencesOfThe StringView to be searched inside source
withThe replacement StringView to be written in destination buffer

Example:

String buffer(StringEncoding::Ascii);
{
auto builder = StringBuilder::create(buffer);
SC_TEST_EXPECT(builder.appendReplaceAll("123 456 123 10", "123", "1234"));
SC_TEST_EXPECT(builder.finalize() == "1234 456 1234 10");
}
buffer = String();
{
auto builder = StringBuilder::create(buffer);
SC_TEST_EXPECT(builder.appendReplaceAll("088123", "123", "1"));
SC_TEST_EXPECT(builder.finalize() == "0881");
}

◆ create()

template<typename T >
static StringBuilderFor< T > SC::StringBuilder::create ( T & stringOrBuffer)
inlinestaticnoexcept

Creates a StringBuilder for the given string or buffer, replacing its current contents.

◆ createForAppendingTo()

template<typename T >
static StringBuilderFor< T > SC::StringBuilder::createForAppendingTo ( T & stringOrBuffer)
inlinestaticnoexcept

Creates a StringBuilder for the given string or buffer, appending to its current contents.

◆ format()

template<typename T , typename... Types>
static bool SC::StringBuilder::format ( T & buffer,
StringView fmt,
Types &&... args )
inlinestaticnodiscard

Helper to format a StringView against args, replacing destination contents, in a single function call.


The documentation for this struct was generated from the following file: