summaryrefslogtreecommitdiffhomepage
path: root/vendor/toml++/impl/std_string.inl
blob: 9cbf9893b7a2030ba1ce42ca7a2f3daad529cbc8 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// # This file is a part of toml++ and is subject to the the terms of the MIT license.
// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
//  SPDX-License-Identifier: MIT
#pragma once

// # {{
#include "preprocessor.hpp"
#if !TOML_IMPLEMENTATION
#error This is an implementation-only header.
#endif
// # }}

#if TOML_WINDOWS
#include "std_string.hpp"
#ifndef _WINDOWS_
#if TOML_INCLUDE_WINDOWS_H
#include <Windows.h>
#else

extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(
    unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar,
    char* lpMultiByteStr, int cbMultiByte, const char* lpDefaultChar, int* lpUsedDefaultChar);

extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(
    unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte,
    wchar_t* lpWideCharStr, int cchWideChar);

#endif  // TOML_INCLUDE_WINDOWS_H
#endif  // _WINDOWS_
#include "header_start.hpp"

TOML_IMPL_NAMESPACE_START {
  TOML_EXTERNAL_LINKAGE
  std::string narrow(std::wstring_view str) {
    if (str.empty()) return {};

    std::string s;
    const auto  len = ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()),
                                            nullptr, 0, nullptr, nullptr);
    if (len) {
      s.resize(static_cast<size_t>(len));
      ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len,
                            nullptr, nullptr);
    }
    return s;
  }

  TOML_EXTERNAL_LINKAGE
  std::wstring widen(std::string_view str) {
    if (str.empty()) return {};

    std::wstring s;
    const auto   len =
        ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
    if (len) {
      s.resize(static_cast<size_t>(len));
      ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
    }
    return s;
  }

#if TOML_HAS_CHAR8

  TOML_EXTERNAL_LINKAGE
  std::wstring widen(std::u8string_view str) {
    if (str.empty()) return {};

    return widen(std::string_view{reinterpret_cast<const char*>(str.data()), str.length()});
  }

#endif  // TOML_HAS_CHAR8
}
TOML_IMPL_NAMESPACE_END;

#include "header_end.hpp"
#endif  // TOML_WINDOWS