Sane C++ Libraries
C++ Platform Abstraction Libraries
StringConverter.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringView.h"
5
6namespace SC
7{
8template <typename T>
9struct Vector;
10struct SC_COMPILER_EXPORT StringConverter;
11struct String;
12} // namespace SC
13
16
25{
28 {
31 };
32
39 [[nodiscard]] static bool convertEncodingToUTF8(StringView text, Vector<char>& buffer,
40 StringView* encodedText = nullptr,
41 NullTermination nullTerminate = AddZeroTerminator);
42
49 [[nodiscard]] static bool convertEncodingToUTF16(StringView text, Vector<char>& buffer,
50 StringView* encodedText = nullptr,
51 NullTermination nullTerminate = AddZeroTerminator);
52
60 [[nodiscard]] static bool convertEncodingTo(StringEncoding encoding, StringView text, Vector<char>& buffer,
61 StringView* encodedText = nullptr,
62 NullTermination nullTerminate = AddZeroTerminator);
63
65 enum Flags
66 {
69 };
70
75
80
86 [[nodiscard]] bool convertNullTerminateFastPath(StringView input, StringView& encodedText);
87
93 [[nodiscard]] bool appendNullTerminated(StringView input, bool popExistingNullTerminator = true);
94
95 private:
100 [[nodiscard]] static bool popNullTermIfNotEmpty(Vector<char>& stringData, StringEncoding encoding);
101
106 [[nodiscard]] static bool pushNullTerm(Vector<char>& stringData, StringEncoding encoding);
107
112 [[nodiscard]] static bool ensureZeroTermination(Vector<char>& data, StringEncoding encoding);
113
114 void internalClear();
115 // TODO: FileSystemIterator should just use a Vector<char>
116 friend struct FileSystemIterator;
117 template <int N>
118 friend struct SmallString;
119 friend struct StringBuilder;
120 friend struct FileDescriptor;
121
122 [[nodiscard]] bool setTextLengthInBytesIncludingTerminator(size_t newDataSize);
123 [[nodiscard]] static bool convertSameEncoding(StringView text, Vector<char>& buffer, StringView* encodedText,
124 NullTermination terminate);
125 static void eventuallyNullTerminate(Vector<char>& buffer, StringEncoding destinationEncoding,
126 StringView* encodedText, NullTermination terminate);
127
128 StringEncoding encoding;
129 Vector<char>& data;
130 // Appends the input string null terminated
131 [[nodiscard]] bool internalAppend(StringView input, StringView* encodedText);
132
133 // Fallbacks for platforms without an API to do the conversion out of the box (Linux)
134 [[nodiscard]] static bool convertUTF16LE_to_UTF8(const StringView sourceUtf16, Vector<char>& destination,
135 int& writtenCodeUnits);
136 [[nodiscard]] static bool convertUTF8_to_UTF16LE(const StringView sourceUtf8, Vector<char>& destination,
137 int& writtenCodeUnits);
138};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
StringEncoding
String Encoding (Ascii, Utf8, Utf16)
Definition: StringIterator.h:17
Wraps an OS File descriptor to read and write to and from it.
Definition: FileDescriptor.h:57
Iterates files and directories inside a given path.
Definition: FileSystemIterator.h:34
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
Builds String out of a sequence of StringView or formatting through StringFormat.
Definition: StringBuilder.h:16
Converts String to a different encoding (UTF8, UTF16).
Definition: StringConverter.h:25
bool convertNullTerminateFastPath(StringView input, StringView &encodedText)
Converts a given input StringView to null-terminated version.
static bool convertEncodingTo(StringEncoding encoding, StringView text, Vector< char > &buffer, StringView *encodedText=nullptr, NullTermination nullTerminate=AddZeroTerminator)
Converts text to (eventually null terminated) requested encoding.
NullTermination
Specifies if to add a null terminator.
Definition: StringConverter.h:28
@ 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.
Flags
Clearing flags used when initializing destination buffer.
Definition: StringConverter.h:66
@ Clear
Destination buffer will be cleared before pushing to it.
Definition: StringConverter.h:67
@ DoNotClear
Destination buffer will not be cleared before pushing to it.
Definition: StringConverter.h:68
StringConverter(Vector< char > &text, StringEncoding encoding)
Create a StringBuilder that will push to given Vector, with specific encoding.
bool appendNullTerminated(StringView input, bool popExistingNullTerminator=true)
Appends the given StringView and adds null-terminator.
StringConverter(String &text, Flags flags=DoNotClear)
Create a StringBuilder that will push to given String.
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51