JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
stringconvert.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : VST SDK
3//
4// Category : Helpers
5// Filename : public.sdk/source/vst/utility/stringconvert.cpp
6// Created by : Steinberg, 11/2014
7// Description : c++11 unicode string convert functions
8//
9//-----------------------------------------------------------------------------
10// LICENSE
11// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
12//-----------------------------------------------------------------------------
13// Redistribution and use in source and binary forms, with or without modification,
14// are permitted provided that the following conditions are met:
15//
16// * Redistributions of source code must retain the above copyright notice,
17// this list of conditions and the following disclaimer.
18// * Redistributions in binary form must reproduce the above copyright notice,
19// this list of conditions and the following disclaimer in the documentation
20// and/or other materials provided with the distribution.
21// * Neither the name of the Steinberg Media Technologies nor the names of its
22// contributors may be used to endorse or promote products derived from this
23// software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34// OF THE POSSIBILITY OF SUCH DAMAGE.
35//-----------------------------------------------------------------------------
36
37#include "stringconvert.h"
38#include <codecvt>
39#include <locale>
40#include <istream>
41
42//------------------------------------------------------------------------
43namespace VST3 {
44namespace StringConvert {
45
46//------------------------------------------------------------------------
47namespace {
48
49#if defined(_MSC_VER) && _MSC_VER >= 1900
50#define USE_WCHAR_AS_UTF16TYPE
51using UTF16Type = wchar_t;
52#else
53using UTF16Type = char16_t;
54#endif
55
57
58//------------------------------------------------------------------------
59Converter& converter ()
60{
61 static Converter conv;
62 return conv;
63}
64
65//------------------------------------------------------------------------
66} // anonymous
67
68//------------------------------------------------------------------------
70{
71#if defined(USE_WCHAR_AS_UTF16TYPE)
72 auto wstr = converter ().from_bytes (utf8Str);
73 return {wstr.data (), wstr.data () + wstr.size ()};
74#else
75 return converter ().from_bytes (utf8Str);
76#endif
77}
78
79//------------------------------------------------------------------------
81{
82 return convert (utf8Str, str, 128);
83}
84
85//------------------------------------------------------------------------
86bool convert (const std::string& utf8Str, Steinberg::Vst::TChar* str, uint32_t maxCharacters)
87{
88 auto ucs2 = convert (utf8Str);
89 if (ucs2.length () < maxCharacters)
90 {
91 ucs2.copy (reinterpret_cast<char16_t*> (str), ucs2.length ());
92 str[ucs2.length ()] = 0;
93 return true;
94 }
95 return false;
96}
97
98//------------------------------------------------------------------------
100{
101 return converter ().to_bytes (reinterpret_cast<const UTF16Type*> (str));
102}
103
104//------------------------------------------------------------------------
105std::string convert (const Steinberg::Vst::TChar* str, uint32_t max)
106{
107 std::string result;
108 if (str)
109 {
110 Steinberg::Vst::TChar tmp[2] {};
111 for (uint32_t i = 0; i < max; ++i, ++str)
112 {
113 tmp[0] = *str;
114 if (tmp[0] == 0)
115 break;
116 result += convert (tmp);
117 }
118 }
119 return result;
120}
121
122//------------------------------------------------------------------------
124{
125 return converter ().to_bytes (reinterpret_cast<const UTF16Type*> (str.data ()),
126 reinterpret_cast<const UTF16Type*> (str.data () + str.size ()));
127}
128
129//------------------------------------------------------------------------
130std::string convert (const char* str, uint32_t max)
131{
132 std::string result;
133 if (str)
134 {
135 result.reserve (max);
136 for (uint32_t i = 0; i < max; ++i, ++str)
137 {
138 if (*str == 0)
139 break;
140 result += *str;
141 }
142 }
143 return result;
144}
145
146//------------------------------------------------------------------------
147} // String
148} // VST3
T data(T... args)
TChar String128[128]
128 character UTF-16 string
Definition vsttypes.h:69
char16 TChar
UTF-16 character.
Definition vsttypes.h:68
T reserve(T... args)
T size(T... args)
typedef uint32_t
std::u16string convert(const std::string &utf8Str)
convert an UTF-8 string to an UTF-16 string