Azure C++ Utils 1.5.2+3
Azure REST API Helpers for Modern C++
C:/Users/maas/source/repos/siddiqsoft/azure-cpp-utils/src/conversion-utils.hpp
Go to the documentation of this file.
1/*
2 azure-cpp-utils : Azure REST API Utilities for Modern C++
3
4 BSD 3-Clause License
5
6 Copyright (c) 2021, Siddiq Software LLC
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 1. Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 3. Neither the name of the copyright holder nor the names of its
20 contributors may be used to endorse or promote products derived from
21 this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 SERVICES; LOSS OF USE, d_, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#pragma once
36
37#ifndef CONVERSION_UTILS_HPP
38#define CONVERSION_UTILS_HPP
39
40
41#include <chrono>
42#include <string>
43#include <ranges>
44#include <concepts>
45#include <format>
46
47#if defined(_WIN64) || defined(WIN64) || defined(WIN32) || defined(_WIN32)
48#include <Windows.h>
49#endif
50
51
53namespace siddiqsoft
54{
57 {
61 static std::string asciiFromWide(const std::wstring& src)
62 {
63 if (src.empty()) return {};
64
65 if (auto destSize =
66 WideCharToMultiByte(CP_ACP, 0, src.c_str(), static_cast<int>(src.length()), nullptr, 0, nullptr, nullptr);
67 destSize > 0)
68 {
69 // Allocate appropriate buffer +1 for null-char
70 std::vector<char> destBuffer(static_cast<size_t>(destSize) + 1);
71 destSize = WideCharToMultiByte(CP_ACP,
72 0,
73 src.c_str(),
74 static_cast<int>(src.length()),
75 destBuffer.data(),
76 static_cast<DWORD>(destSize),
77 nullptr,
78 nullptr);
79 return {destBuffer.data(), static_cast<size_t>(destSize)};
80 }
81
82 // Fall-through error -> empty string
83 return {};
84 }
85
89 static std::string utf8FromWide(const std::wstring& src)
90 {
91 if (src.empty()) return {};
92
93 if (auto destSize =
94 WideCharToMultiByte(CP_UTF8, 0, src.c_str(), static_cast<int>(src.length()), nullptr, 0, nullptr, nullptr);
95 destSize > 0)
96 {
97 // Allocate appropriate buffer +1 for null-char
98 std::vector<char> destBuffer(static_cast<size_t>(destSize) + 1);
99 destSize = WideCharToMultiByte(CP_UTF8,
100 0,
101 src.c_str(),
102 static_cast<int>(src.length()),
103 destBuffer.data(),
104 static_cast<DWORD>(destSize),
105 nullptr,
106 nullptr);
107 return {destBuffer.data(), static_cast<size_t>(destSize)};
108 }
109
110 // Fall-through error -> empty string
111 return {};
112 }
113
117 static std::wstring wideFromUtf8(const std::string& src)
118 {
119 if (src.empty()) return {};
120
121 if (auto destSize = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), static_cast<int>(src.length()), nullptr, 0);
122 destSize > 0) {
123 // Allocate appropriate buffer +1 for null-char
124 std::vector<wchar_t> destBuffer(static_cast<size_t>(destSize) + 1);
125 destSize = MultiByteToWideChar(
126 CP_UTF8, 0, src.c_str(), static_cast<int>(src.length()), destBuffer.data(), static_cast<DWORD>(destSize));
127 return {destBuffer.data(), static_cast<size_t>(destSize)};
128 }
129
130 // Fall-through error -> empty string
131 return {};
132 }
133
134
138 static std::wstring wideFromAscii(const std::string& src)
139 {
140 if (src.empty()) return {};
141
142 if (auto destSize = MultiByteToWideChar(CP_ACP, 0, src.c_str(), static_cast<int>(src.length()), nullptr, 0);
143 destSize > 0) {
144 // Allocate appropriate buffer +1 for null-char
145 std::vector<wchar_t> destBuffer(static_cast<size_t>(destSize) + 1);
146 destSize = MultiByteToWideChar(
147 CP_ACP, 0, src.c_str(), static_cast<int>(src.length()), destBuffer.data(), static_cast<DWORD>(destSize));
148 return {destBuffer.data(), static_cast<size_t>(destSize)};
149 }
150
151 // Fall-through error -> empty string
152 return {};
153 }
154 };
155} // namespace siddiqsoft
156
157#endif // !AZURECPPUTILS_HPP
SiddiqSoft.
Conversion Functions for ascii to wide, utf8 to wide and vice-versa.
static std::wstring wideFromAscii(const std::string &src)
Given an ascii encoded string returns a utf-16 in std::wstring.
static std::wstring wideFromUtf8(const std::string &src)
Given a utf-8 encoded string returns a utf-16 in std::wstring.
static std::string utf8FromWide(const std::wstring &src)
Convert given wide string to utf8 encoded string.
static std::string asciiFromWide(const std::wstring &src)
Convert given wide string to ascii encoded string.