-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathutf_utils.h
More file actions
41 lines (34 loc) · 1.29 KB
/
Copy pathutf_utils.h
File metadata and controls
41 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once
#include <cstddef>
#include <cstring>
#include <simdutf.h>
#include <sql.h>
#include <sqlext.h>
#include <string>
inline std::string utf16LeToUtf8Alloc(const std::u16string& utf16) {
if (utf16.empty()) {
return {};
}
std::string utf8(utf16.size() * 3, '\0');
size_t n = simdutf::convert_utf16le_to_utf8_with_replacement(
utf16.data(), utf16.size(), utf8.data());
utf8.resize(n);
return utf8;
}
inline std::u16string dupeSqlWCharAsUtf16Le(const SQLWCHAR* value, size_t length) {
std::u16string utf16(length, u'\0');
static_assert(sizeof(SQLWCHAR) == sizeof(char16_t), "SQLWCHAR must be 16-bit");
if (length > 0) {
std::memcpy(utf16.data(), value, length * sizeof(SQLWCHAR));
}
return utf16;
}
inline SQLWCHAR* reinterpretU16stringAsSqlWChar(const std::u16string& utf16) {
static_assert(sizeof(std::u16string::value_type) == sizeof(SQLWCHAR),
"SQLWCHAR must have the same size as std::u16string::value_type");
static_assert(alignof(std::u16string::value_type) == alignof(SQLWCHAR),
"SQLWCHAR must have the same alignment as std::u16string::value_type");
return const_cast<SQLWCHAR*>(reinterpret_cast<const SQLWCHAR*>(utf16.c_str()));
}