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
ustring.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : SDK Core
3//
4// Category : Helpers
5// Filename : pluginterfaces/base/ustring.cpp
6// Created by : Steinberg, 12/2005
7// Description : UTF-16 String class
8//
9//-----------------------------------------------------------------------------
10// This file is part of a Steinberg SDK. It is subject to the license terms
11// in the LICENSE file found in the top-level directory of this distribution
12// and at www.steinberg.net/sdklicenses.
13// No part of the SDK, including this file, may be copied, modified, propagated,
14// or distributed except according to the terms contained in the LICENSE file.
15//-----------------------------------------------------------------------------
16
17#include "ustring.h"
18
19#if SMTG_OS_WINDOWS
20#include <cstdio>
21
22#ifdef _MSC_VER
23#pragma warning (disable : 4996) // deprecated functions
24#endif
25
26#elif SMTG_OS_MACOS
27#include <CoreFoundation/CoreFoundation.h>
28
29#elif SMTG_OS_LINUX
30#include <cstring>
31#include <string>
32#include <codecvt>
33#include <sstream>
34#include <locale>
35
36#include <wctype.h>
37#include <wchar.h>
38
39#endif
40
41//------------------------------------------------------------------------
42namespace Steinberg {
43
44//------------------------------------------------------------------------
45#if SMTG_OS_LINUX
46
47//------------------------------------------------------------------------
48namespace {
49
51
52//------------------------------------------------------------------------
53Converter& converter ()
54{
55 static Converter instance;
56 return instance;
57}
58
59//------------------------------------------------------------------------
60} // anonymous
61
62//------------------------------------------------------------------------
63#endif // SMTG_OS_LINUX
64
65//------------------------------------------------------------------------
67//------------------------------------------------------------------------
68template <class TDstChar, class TSrcChar>
69void StringCopy (TDstChar* dst, int32 dstSize, const TSrcChar* src, int32 srcSize = -1)
70{
71 int32 count = dstSize;
72 if (srcSize >= 0 && srcSize < dstSize)
73 count = srcSize;
74 for (int32 i = 0; i < count; i++)
75 {
76 dst[i] = (TDstChar)src[i];
77 if (src[i] == 0)
78 break;
79 }
80 dst[dstSize - 1] = 0;
81}
82
83//------------------------------------------------------------------------
85//------------------------------------------------------------------------
86template <class TSrcChar>
87int32 StringLength (const TSrcChar* src, int32 srcSize = -1)
88{
89 if (srcSize == 0)
90 return 0;
91 int32 length = 0;
92 while (src[length])
93 {
94 length++;
95 if (srcSize > 0 && length >= srcSize)
96 break;
97 }
98 return length;
99}
100
101//------------------------------------------------------------------------
102// UString
103//------------------------------------------------------------------------
104int32 UString::getLength () const
105{
106 return StringLength<char16> (thisBuffer, thisSize);
107}
108
109//------------------------------------------------------------------------
110UString& UString::assign (const char16* src, int32 srcSize)
111{
112 StringCopy<char16, char16> (thisBuffer, thisSize, src, srcSize);
113 return *this;
114}
115
116//------------------------------------------------------------------------
117UString& UString::append (const char16* src, int32 srcSize)
118{
119 int32 length = getLength ();
120 StringCopy<char16, char16> (thisBuffer + length, thisSize - length, src, srcSize);
121 return *this;
122}
123
124//------------------------------------------------------------------------
125const UString& UString::copyTo (char16* dst, int32 dstSize) const
126{
127 StringCopy<char16, char16> (dst, dstSize, thisBuffer, thisSize);
128 return *this;
129}
130
131//------------------------------------------------------------------------
132UString& UString::fromAscii (const char* src, int32 srcSize)
133{
134 StringCopy<char16, char> (thisBuffer, thisSize, src, srcSize);
135 return *this;
136}
137
138//------------------------------------------------------------------------
139const UString& UString::toAscii (char* dst, int32 dstSize) const
140{
141 StringCopy<char, char16> (dst, dstSize, thisBuffer, thisSize);
142 return *this;
143}
144
145//------------------------------------------------------------------------
146bool UString::scanFloat (double& value) const
147{
148#if SMTG_OS_WINDOWS
149 return swscanf ((const wchar_t*)thisBuffer, L"%lf", &value) != -1;
150
151#elif TARGET_API_MAC_CARBON
152 CFStringRef cfStr = CFStringCreateWithBytes (0, (const UInt8 *)thisBuffer, getLength () * 2, kCFStringEncodingUTF16, false);
153 if (cfStr)
154 {
155 value = CFStringGetDoubleValue (cfStr);
156 CFRelease (cfStr);
157 return true;
158 }
159 return false;
160
161#elif SMTG_OS_LINUX
162 auto str = converter ().to_bytes (thisBuffer);
163 return sscanf (str.data (), "%lf", &value) == 1;
164
165#else
166#warning Implement me
167 // implement me!
168 return false;
169#endif
170}
171
172//------------------------------------------------------------------------
173bool UString::printFloat (double value, int32 precision)
174{
175#if SMTG_OS_WINDOWS
176 return swprintf ((wchar_t*)thisBuffer, L"%.*lf", precision, value) != -1;
177#elif SMTG_OS_MACOS
178 bool result = false;
179 CFStringRef cfStr = CFStringCreateWithFormat (0, 0, CFSTR("%.*lf"), precision, value);
180 if (cfStr)
181 {
182 memset (thisBuffer, 0, thisSize);
183 CFRange range = {0, CFStringGetLength (cfStr)};
184 CFStringGetBytes (cfStr, range, kCFStringEncodingUTF16, 0, false, (UInt8*)thisBuffer, thisSize, 0);
185 CFRelease (cfStr);
186 return true;
187 }
188 return result;
189#elif SMTG_OS_LINUX
190 auto utf8Buffer = reinterpret_cast<char*> (thisBuffer);
191 auto len = snprintf (utf8Buffer, thisSize, "%.*lf", precision, value);
192 if (len > 0)
193 {
194 auto utf16Buffer = reinterpret_cast<char16*> (thisBuffer);
195 utf16Buffer[len] = 0;
196 while (--len >= 0)
197 {
198 utf16Buffer[len] = utf8Buffer[len];
199 }
200 return true;
201 }
202 return false;
203#else
204#warning Implement me
205 // implement me!
206 return false;
207#endif
208}
209
210//------------------------------------------------------------------------
211bool UString::scanInt (int64& value) const
212{
213#if SMTG_OS_WINDOWS
214 return swscanf ((const wchar_t*)thisBuffer, L"%I64d", &value) != -1;
215
216#elif SMTG_OS_MACOS
217 CFStringRef cfStr = CFStringCreateWithBytes (0, (const UInt8 *)thisBuffer, getLength () * 2, kCFStringEncodingUTF16, false);
218 if (cfStr)
219 {
220 value = CFStringGetIntValue (cfStr);
221 CFRelease (cfStr);
222 return true;
223 }
224 return false;
225
226#elif SMTG_OS_LINUX
227 auto str = converter ().to_bytes (thisBuffer);
228 return sscanf (str.data (), "%lld", &value) == 1;
229
230#else
231#warning Implement me
232 // implement me!
233 return false;
234#endif
235}
236
237//------------------------------------------------------------------------
238bool UString::printInt (int64 value)
239{
240#if SMTG_OS_WINDOWS
241 return swprintf ((wchar_t*)thisBuffer, L"%I64d", value) != -1;
242
243#elif SMTG_OS_MACOS
244 CFStringRef cfStr = CFStringCreateWithFormat (0, 0, CFSTR("%lld"), value);
245 if (cfStr)
246 {
247 memset (thisBuffer, 0, thisSize);
248 CFRange range = {0, CFStringGetLength (cfStr)};
249 CFStringGetBytes (cfStr, range, kCFStringEncodingUTF16, 0, false, (UInt8*)thisBuffer, thisSize, 0);
250 CFRelease (cfStr);
251 return true;
252 }
253 return false;
254#elif SMTG_OS_LINUX
255 auto utf8Buffer = reinterpret_cast<char*> (thisBuffer);
256 auto len = snprintf (utf8Buffer, thisSize, "%lld", value);
257 if (len > 0)
258 {
259 auto utf16Buffer = reinterpret_cast<char16*> (thisBuffer);
260 utf16Buffer[len] = 0;
261 while (--len >= 0)
262 {
263 utf16Buffer[len] = utf8Buffer[len];
264 }
265 return true;
266 }
267 return false;
268
269#else
270#warning Implement me
271 // implement me!
272 return false;
273#endif
274}
275} // namespace Steinberg
UTF-16 string class without buffer management.
Definition ustring.h:29
bool printInt(int64 value)
Print integer to string.
Definition ustring.cpp:238
bool scanInt(int64 &value) const
Scan integer from string.
Definition ustring.cpp:211
UString & append(const char16 *src, int32 srcSize=-1)
Append UTF-16 buffer (srcSize is in code unit (count of char16)).
Definition ustring.cpp:117
const UString & toAscii(char *dst, int32 dstSize) const
Copy to ASCII string.
Definition ustring.cpp:139
int32 getLength() const
Returns length of string (in code unit).
Definition ustring.cpp:104
bool scanFloat(double &value) const
Scan float from string.
Definition ustring.cpp:146
const UString & copyTo(char16 *dst, int32 dstSize) const
Copy to UTF-16 buffer (dstSize is in code unit (count of char16)).
Definition ustring.cpp:125
UString & fromAscii(const char *src, int32 srcSize=-1)
Copy from ASCII string (srcSize is in code unit (count of char16)).
Definition ustring.cpp:132
UString & assign(const char16 *src, int32 srcSize=-1)
Copy from UTF-16 buffer (srcSize is in code unit (count of char16)).
Definition ustring.cpp:110
int32 thisSize
size in code unit (not in byte!)
Definition ustring.h:74
bool printFloat(double value, int32 precision=4)
Print float to string.
Definition ustring.cpp:173
snprintf
sscanf
swprintf
swscanf
memset
int32 StringLength(const TSrcChar *src, int32 srcSize=-1)
Find length of null-terminated string, i.e.
Definition ustring.cpp:87
void StringCopy(TDstChar *dst, int32 dstSize, const TSrcChar *src, int32 srcSize=-1)
Copy strings of different character width.
Definition ustring.cpp:69