DNA Calib 1.1
Project brief
NativeString.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#ifdef _WIN32
7#endif
8
9#include <pma/TypeDefs.h>
10
11#ifdef _MSC_VER
12 #pragma warning(push)
13 #pragma warning(disable : 4365 4987)
14#endif
15#include <clocale>
16#include <cstddef>
17#include <cstring>
18#include <cwchar>
19#ifdef _MSC_VER
20 #pragma warning(pop)
21#endif
22
23namespace trio {
24
25template<typename TCharacter>
27
28template<>
29struct StringConverter<char> {
30 using CharacterType = char;
32
33 static StringType from(const char* source, pma::MemoryResource* memRes) {
34 return StringType{source, memRes};
35 }
36
37};
38
39template<>
40struct StringConverter<wchar_t> {
41 using CharacterType = wchar_t;
43
44 static StringType from(const char* source, pma::MemoryResource* memRes) {
45 #ifdef _WIN32
46 const std::size_t length = std::strlen(source);
47 const int neededSize = MultiByteToWideChar(CP_UTF8, 0, source, static_cast<int>(length), nullptr, 0);
48 StringType result{static_cast<std::size_t>(neededSize), 0, memRes};
49 MultiByteToWideChar(CP_UTF8, 0, source, static_cast<int>(length), &result[0], neededSize);
50 return result;
51 #else
52 const char* current = std::setlocale(LC_ALL, nullptr);
53 std::setlocale(LC_ALL, "en_US.utf8"); // Any UTF-8 locale will work
54
55 std::mbstate_t state{};
56 const std::size_t neededSize = std::mbsrtowcs(nullptr, &source, 0, &state) + 1ul;
57 StringType result{neededSize, 0, memRes};
58 std::mbsrtowcs(&result[0], &source, result.size(), &state);
59
60 if (current != nullptr) {
61 std::setlocale(LC_ALL, current);
62 }
63
64 return result;
65 #endif
66 }
67
68};
69
70#if defined(_WIN32) && defined(UNICODE)
71 using NativeCharacter = wchar_t;
72#else
73 using NativeCharacter = char;
74#endif
75
78
79} // namespace trio
MemoryResource is an abstract class that allows the implementation of polymorphic allocators.
Definition: MemoryResource.h:17
std::basic_string< T, std::char_traits< T >, Allocator > String
Definition: include/pma/TypeDefs.h:26
std::enable_if< std::is_floating_point< T >::value, T >::type length(const vec< L, T > &v)
Definition: Computations.h:46
Definition: Concepts.h:10
char NativeCharacter
Definition: NativeString.h:73
pma::String< NativeCharacter > NativeString
Definition: NativeString.h:76
char CharacterType
Definition: NativeString.h:30
static StringType from(const char *source, pma::MemoryResource *memRes)
Definition: NativeString.h:33
pma::String< CharacterType > StringType
Definition: NativeString.h:31
wchar_t CharacterType
Definition: NativeString.h:41
static StringType from(const char *source, pma::MemoryResource *memRes)
Definition: NativeString.h:44
pma::String< CharacterType > StringType
Definition: NativeString.h:42
Definition: NativeString.h:26