NeBuild dev
Loading...
Searching...
No Matches
std_string.inl
Go to the documentation of this file.
1// # This file is a part of toml++ and is subject to the the terms of the MIT license.
2// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7// # {{
8#include "preprocessor.hpp"
9#if !TOML_IMPLEMENTATION
10#error This is an implementation-only header.
11#endif
12// # }}
13
14#if TOML_WINDOWS
15#include "std_string.hpp"
16#ifndef _WINDOWS_
17#if TOML_INCLUDE_WINDOWS_H
18#include <Windows.h>
19#else
20
21extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(
22 unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar,
23 char* lpMultiByteStr, int cbMultiByte, const char* lpDefaultChar, int* lpUsedDefaultChar);
24
25extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(
26 unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte,
27 wchar_t* lpWideCharStr, int cchWideChar);
28
29#endif // TOML_INCLUDE_WINDOWS_H
30#endif // _WINDOWS_
31#include "header_start.hpp"
32
35 std::string narrow(std::wstring_view str) {
36 if (str.empty()) return {};
37
38 std::string s;
39 const auto len = ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()),
40 nullptr, 0, nullptr, nullptr);
41 if (len) {
42 s.resize(static_cast<size_t>(len));
43 ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len,
44 nullptr, nullptr);
45 }
46 return s;
47 }
48
50 std::wstring widen(std::string_view str) {
51 if (str.empty()) return {};
52
53 std::wstring s;
54 const auto len =
55 ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
56 if (len) {
57 s.resize(static_cast<size_t>(len));
58 ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
59 }
60 return s;
61 }
62
63#if TOML_HAS_CHAR8
64
66 std::wstring widen(std::u8string_view str) {
67 if (str.empty()) return {};
68
69 return widen(std::string_view{reinterpret_cast<const char*>(str.data()), str.length()});
70 }
71
72#endif // TOML_HAS_CHAR8
73}
75
76#include "header_end.hpp"
77#endif // TOML_WINDOWS
#define TOML_EXTERNAL_LINKAGE
Definition preprocessor.hpp:1339
#define TOML_IMPL_NAMESPACE_END
Definition preprocessor.hpp:1334
#define TOML_IMPL_NAMESPACE_START
Definition preprocessor.hpp:1333