Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::StringConverter Struct Reference

Converts String to a different encoding (UTF8, UTF16). More...

#include <StringConverter.h>

Public Types

enum  NullTermination {
  AddZeroTerminator ,
  DoNotAddZeroTerminator
}
 Specifies if to add a null terminator. More...
 
enum  Flags {
  Clear ,
  DoNotClear
}
 Clearing flags used when initializing destination buffer. More...
 

Public Member Functions

 StringConverter (String &text, Flags flags=DoNotClear)
 Create a StringBuilder that will push to given String. More...
 
 StringConverter (Vector< char > &text, StringEncoding encoding)
 Create a StringBuilder that will push to given Vector, with specific encoding. More...
 
bool convertNullTerminateFastPath (StringView input, StringView &encodedText)
 Converts a given input StringView to null-terminated version. More...
 
bool appendNullTerminated (StringView input, bool popExistingNullTerminator=true)
 Appends the given StringView and adds null-terminator. More...
 

Static Public Member Functions

static bool convertEncodingToUTF8 (StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
 Converts text to (eventually null terminated) UTF8 encoding. More...
 
static bool convertEncodingToUTF16 (StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
 Converts text to (eventually null terminated) UTF16 encoding. More...
 
static bool convertEncodingTo (StringEncoding encoding, StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
 Converts text to (eventually null terminated) requested encoding. More...
 

Friends

struct FileSystemIterator
 
template<int N>
struct SmallString
 
struct StringBuilder
 
struct FileDescriptor
 

Detailed Description

Converts String to a different encoding (UTF8, UTF16).

SC::StringConverter converts strings between different UTF encodings and can add null-terminator if requested. When the SC::StringView is already null-terminated, the class just forwards the original SC::StringView.

Example:

const char utf8String1[] = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E"; // "日本語" in UTF-8
const char utf16String1[] = "\xE5\x65\x2C\x67\x9E\x8a"; // "日本語" in UTF-16LE
SmallVector<char, 255> buffer;
StringView input, output, expected;
input = StringView({utf8String1, sizeof(utf8String1) - 1}, false, StringEncoding::Utf8);
expected = StringView({utf16String1, sizeof(utf16String1) - 1}, false, StringEncoding::Utf16);
buffer.clear();
SC_TEST_EXPECT(output == expected);
input = StringView({utf16String1, sizeof(utf16String1) - 1}, false, StringEncoding::Utf16);
expected = StringView({utf8String1, sizeof(utf8String1) - 1}, false, StringEncoding::Utf8);
buffer.clear();
SC_TEST_EXPECT(output == expected);
@ Utf8
Encoding is UTF8.
@ Utf16
Encoding is UTF16-LE.
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
@ DoNotAddZeroTerminator
A null terminator will NOT be added at the end of the String.
Definition: StringConverter.h:30
@ AddZeroTerminator
A null terminator will be added at the end of the String.
Definition: StringConverter.h:29
static bool convertEncodingToUTF8(StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
Converts text to (eventually null terminated) UTF8 encoding.
static bool convertEncodingToUTF16(StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
Converts text to (eventually null terminated) UTF16 encoding.

Member Enumeration Documentation

◆ Flags

Clearing flags used when initializing destination buffer.

Enumerator
Clear 

Destination buffer will be cleared before pushing to it.

DoNotClear 

Destination buffer will not be cleared before pushing to it.

◆ NullTermination

Specifies if to add a null terminator.

Enumerator
AddZeroTerminator 

A null terminator will be added at the end of the String.

DoNotAddZeroTerminator 

A null terminator will NOT be added at the end of the String.

Constructor & Destructor Documentation

◆ StringConverter() [1/2]

SC::StringConverter::StringConverter ( String text,
Flags  flags = DoNotClear 
)

Create a StringBuilder that will push to given String.

Parameters
textDestination buffer where code points will be pushed
flagsSpecifies if destination buffer must be emptied or not before pushing

◆ StringConverter() [2/2]

SC::StringConverter::StringConverter ( Vector< char > &  text,
StringEncoding  encoding 
)

Create a StringBuilder that will push to given Vector, with specific encoding.

Parameters
textDestination buffer where code points will be pushed
encodingThe encoding to be used

Member Function Documentation

◆ appendNullTerminated()

bool SC::StringConverter::appendNullTerminated ( StringView  input,
bool  popExistingNullTerminator = true 
)

Appends the given StringView and adds null-terminator.

If existing null-terminator was already last inserted code point, it will be removed before appending input.

Parameters
inputThe StringView to be appended
popExistingNullTerminatorIf true, removes existing null terminator before adding the new one
Returns
true if the StringView has been successfully appended

◆ convertEncodingTo()

static bool SC::StringConverter::convertEncodingTo ( StringEncoding  encoding,
StringView  text,
Vector< char > &  buffer,
StringView encodedText = nullptr,
NullTermination  nullTerminate = AddZeroTerminator 
)
static

Converts text to (eventually null terminated) requested encoding.

Uses the passed in buffer if necessary.

Parameters
encodingThe requested destination encoding to convert to
textThe StringView to be converted
bufferThe destination buffer that will be eventually used
encodedTextIf specified, a StringView containing the encoded text will be returned
nullTerminateSpecifies if the StringView will need to be null terminated or not
Returns
true if the conversion succeeds

◆ convertEncodingToUTF16()

static bool SC::StringConverter::convertEncodingToUTF16 ( StringView  text,
Vector< char > &  buffer,
StringView encodedText = nullptr,
NullTermination  nullTerminate = AddZeroTerminator 
)
static

Converts text to (eventually null terminated) UTF16 encoding.

Uses the passed in buffer if necessary.

Parameters
textThe StringView to be converted
bufferThe destination buffer that will be eventually used
encodedTextIf specified, a StringView containing the encoded text will be returned
nullTerminateSpecifies if the StringView will need to be null terminated or not
Returns
true if the conversion succeeds

◆ convertEncodingToUTF8()

static bool SC::StringConverter::convertEncodingToUTF8 ( StringView  text,
Vector< char > &  buffer,
StringView encodedText = nullptr,
NullTermination  nullTerminate = AddZeroTerminator 
)
static

Converts text to (eventually null terminated) UTF8 encoding.

Uses the passed in buffer if necessary.

Parameters
textThe StringView to be converted
bufferThe destination buffer that will be eventually used
encodedTextIf specified, a StringView containing the encoded text will be returned
nullTerminateSpecifies if the StringView will need to be null terminated or not
Returns
true if the conversion succeeds

◆ convertNullTerminateFastPath()

bool SC::StringConverter::convertNullTerminateFastPath ( StringView  input,
StringView encodedText 
)

Converts a given input StringView to null-terminated version.

Uses supplied buffer in constructor if an actual conversion is needed.

Parameters
inputThe StringView to be converted
encodedTextThe converted output StringView
Returns
true if the conversion succeeded

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