1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <sal/config.h>
14 #include <string_view>
19 // Helpers for safe conversion between wchar_t and char16_t in MSVC
21 static_assert(sizeof(char16_t
) == sizeof(wchar_t),
22 "These helper functions are only applicable to implementations with 16-bit wchar_t");
24 // While other implementations define wchar_t as 32-bit integral value, and mostly use
25 // char-based UTF-8 string APIs, in MSVC wchar_t is (non-conformant) 16-bit, and Unicode
26 // support is implemented by Unicode-aware WinAPI functions taking UTF-16 LE strings,
27 // and also stdlib functions taking those strings.
29 // In LibreOffice, internal string representation is also UTF-16 with system endianness
30 // (sal_Unicode that is typedef for char16_t); so it is an important implementation concept
31 // to treat internal strings as directly usable by WinAPI/stdlib functions and vice versa.
32 // Also, it's important to use safe conversion between unrelated underlying C++ types
33 // used for MSVC and LibreOffice string storage without plain reinterpret_cast that brings
34 // risks of masking errors like casting char buffers to wchar_t/char16_t.
36 // Use these helpers for wchar_t (WSTR, WCHAR, OLESTR etc) to char16_t (sal_Unicode) string
37 // conversions instead of reinterpret-cast in Windows-specific code.
38 inline wchar_t* toW(char16_t
* p
) { return reinterpret_cast<wchar_t*>(p
); }
39 inline wchar_t const* toW(char16_t
const* p
) { return reinterpret_cast<wchar_t const*>(p
); }
40 inline char16_t
* toU(wchar_t* p
) { return reinterpret_cast<char16_t
*>(p
); }
41 inline char16_t
const* toU(wchar_t const* p
) { return reinterpret_cast<char16_t
const*>(p
); }
43 inline std::u16string_view
toU(std::wstring_view v
) { return { toU(v
.data()), v
.size() }; }
44 inline std::wstring_view
toW(std::u16string_view v
) { return { toW(v
.data()), v
.size() }; }
48 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */