1 //-----------------------------------------------------------------------------
5 // Filename : pluginterfaces/base/ustring.cpp
6 // Created by : Steinberg, 12/2005
7 // Description : UTF-16 String class
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 //-----------------------------------------------------------------------------
23 #pragma warning (disable : 4996) // deprecated functions
27 #include <CoreFoundation/CoreFoundation.h>
41 //------------------------------------------------------------------------
44 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
50 using Converter
= std::wstring_convert
<std::codecvt_utf8_utf16
<char16_t
>, char16_t
>;
52 //------------------------------------------------------------------------
53 Converter
& converter ()
55 static Converter instance
;
59 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
63 #endif // SMTG_OS_LINUX
65 //------------------------------------------------------------------------
66 /** Copy strings of different character width. */
67 //------------------------------------------------------------------------
68 template <class TDstChar
, class TSrcChar
>
69 void StringCopy (TDstChar
* dst
, int32 dstSize
, const TSrcChar
* src
, int32 srcSize
= -1)
71 int32 count
= dstSize
;
72 if (srcSize
>= 0 && srcSize
< dstSize
)
74 for (int32 i
= 0; i
< count
; i
++)
76 dst
[i
] = (TDstChar
)src
[i
];
83 //------------------------------------------------------------------------
84 /** Find length of null-terminated string, i.e. StringLength (L"ABC\0") => 3 */
85 //------------------------------------------------------------------------
86 template <class TSrcChar
>
87 int32
StringLength (const TSrcChar
* src
, int32 srcSize
= -1)
95 if (srcSize
> 0 && length
>= srcSize
)
101 //------------------------------------------------------------------------
103 //------------------------------------------------------------------------
104 int32
UString::getLength () const
106 return StringLength
<char16
> (thisBuffer
, thisSize
);
109 //------------------------------------------------------------------------
110 UString
& UString::assign (const char16
* src
, int32 srcSize
)
112 StringCopy
<char16
, char16
> (thisBuffer
, thisSize
, src
, srcSize
);
116 //------------------------------------------------------------------------
117 UString
& UString::append (const char16
* src
, int32 srcSize
)
119 int32 length
= getLength ();
120 StringCopy
<char16
, char16
> (thisBuffer
+ length
, thisSize
- length
, src
, srcSize
);
124 //------------------------------------------------------------------------
125 const UString
& UString::copyTo (char16
* dst
, int32 dstSize
) const
127 StringCopy
<char16
, char16
> (dst
, dstSize
, thisBuffer
, thisSize
);
131 //------------------------------------------------------------------------
132 UString
& UString::fromAscii (const char* src
, int32 srcSize
)
134 StringCopy
<char16
, char> (thisBuffer
, thisSize
, src
, srcSize
);
138 //------------------------------------------------------------------------
139 const UString
& UString::toAscii (char* dst
, int32 dstSize
) const
141 StringCopy
<char, char16
> (dst
, dstSize
, thisBuffer
, thisSize
);
145 //------------------------------------------------------------------------
146 bool UString::scanFloat (double& value
) const
149 return swscanf ((const wchar_t*)thisBuffer
, L
"%lf", &value
) != -1;
151 #elif TARGET_API_MAC_CARBON
152 CFStringRef cfStr
= CFStringCreateWithBytes (0, (const UInt8
*)thisBuffer
, getLength () * 2, kCFStringEncodingUTF16
, false);
155 value
= CFStringGetDoubleValue (cfStr
);
162 auto str
= converter ().to_bytes (thisBuffer
);
163 return sscanf (str
.data (), "%lf", &value
) == 1;
166 #warning Implement me
172 //------------------------------------------------------------------------
173 bool UString::printFloat (double value
, int32 precision
)
176 return swprintf ((wchar_t*)thisBuffer
, L
"%.*lf", precision
, value
) != -1;
179 CFStringRef cfStr
= CFStringCreateWithFormat (0, 0, CFSTR("%.*lf"), precision
, value
);
182 memset (thisBuffer
, 0, thisSize
);
183 CFRange range
= {0, CFStringGetLength (cfStr
)};
184 CFStringGetBytes (cfStr
, range
, kCFStringEncodingUTF16
, 0, false, (UInt8
*)thisBuffer
, thisSize
, 0);
190 auto utf8Buffer
= reinterpret_cast<char*> (thisBuffer
);
191 auto len
= snprintf (utf8Buffer
, thisSize
, "%.*lf", precision
, value
);
194 auto utf16Buffer
= reinterpret_cast<char16
*> (thisBuffer
);
195 utf16Buffer
[len
] = 0;
198 utf16Buffer
[len
] = utf8Buffer
[len
];
204 #warning Implement me
210 //------------------------------------------------------------------------
211 bool UString::scanInt (int64
& value
) const
214 return swscanf ((const wchar_t*)thisBuffer
, L
"%I64d", &value
) != -1;
217 CFStringRef cfStr
= CFStringCreateWithBytes (0, (const UInt8
*)thisBuffer
, getLength () * 2, kCFStringEncodingUTF16
, false);
220 value
= CFStringGetIntValue (cfStr
);
227 auto str
= converter ().to_bytes (thisBuffer
);
228 return sscanf (str
.data (), "%lld", &value
) == 1;
231 #warning Implement me
237 //------------------------------------------------------------------------
238 bool UString::printInt (int64 value
)
241 return swprintf ((wchar_t*)thisBuffer
, L
"%I64d", value
) != -1;
244 CFStringRef cfStr
= CFStringCreateWithFormat (0, 0, CFSTR("%lld"), value
);
247 memset (thisBuffer
, 0, thisSize
);
248 CFRange range
= {0, CFStringGetLength (cfStr
)};
249 CFStringGetBytes (cfStr
, range
, kCFStringEncodingUTF16
, 0, false, (UInt8
*)thisBuffer
, thisSize
, 0);
255 auto utf8Buffer
= reinterpret_cast<char*> (thisBuffer
);
256 auto len
= snprintf (utf8Buffer
, thisSize
, "%lld", value
);
259 auto utf16Buffer
= reinterpret_cast<char16
*> (thisBuffer
);
260 utf16Buffer
[len
] = 0;
263 utf16Buffer
[len
] = utf8Buffer
[len
];
270 #warning Implement me
275 } // namespace Steinberg