DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
TextEncoding.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include <vector>
4
5namespace DotNetDupe {
6 namespace System {
7 namespace Text {
8 // Static member initialization
9 UTF8EncodingPtr TextEncoding::s_utf8EncodingInstance(nullptr);
10
13 const char* raw = (const char*)s;
14 if (!raw) return Array<char>(0);
15
17 int length = s.GetLength();
18 Array<char> bytes(length);
19 for (int i = 0; i < length; i++) bytes[i] = raw[i];
20 return bytes;
21 }
22
25 if (bytes.GetLength() == 0) {
26 return String("");
27 }
28
30 return GetString(bytes.GetData(), bytes.GetLength());
31 }
32
33 String UTF8Encoding::GetString(const char* bytes, int byteCount) {
35 if (bytes == nullptr || byteCount == 0) {
36 return String("");
37 }
38
40 std::string tempString(bytes, byteCount);
41 return String(tempString.c_str());
42 }
43
46 if (s_utf8EncodingInstance.IsNull()) {
47 s_utf8EncodingInstance = SmartPointer<UTF8Encoding>::NewShared();
48 }
49
51 return s_utf8EncodingInstance.DynamicCast<Encoding>();
52 }
53 }
54 }
55}
Represents character encodings and code page conversions.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
T * GetData()
Gets a pointer to the contiguous internal element buffer.
Definition Array.h:146
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
SmartPointer< U > DynamicCast() const
Dynamically casts the managed pointer to another type U and returns a new SmartPointer sharing owners...
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
Represents a character encoding.
static EncodingPtr UTF8()
Gets an encoding for the UTF-8 format.
Array< char > GetBytes(const String &s) override
Encodes a set of characters into a sequence of UTF-8 bytes.
String GetString(const Array< char > &bytes) override
Decodes a sequence of UTF-8 bytes into a String.
SmartPointer< UTF8Encoding > UTF8EncodingPtr
SmartPointer alias for UTF8Encoding instances.
SmartPointer< Encoding > EncodingPtr
SmartPointer alias for Encoding instances.