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
fstring.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------
2// Project : SDK Base
3// Version : 1.0
4//
5// Category : Helpers
6// Filename : base/source/fstring.h
7// Created by : Steinberg, 2008
8// Description : String class
9//
10//-----------------------------------------------------------------------------
11// LICENSE
12// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
13//-----------------------------------------------------------------------------
14// Redistribution and use in source and binary forms, with or without modification,
15// are permitted provided that the following conditions are met:
16//
17// * Redistributions of source code must retain the above copyright notice,
18// this list of conditions and the following disclaimer.
19// * Redistributions in binary form must reproduce the above copyright notice,
20// this list of conditions and the following disclaimer in the documentation
21// and/or other materials provided with the distribution.
22// * Neither the name of the Steinberg Media Technologies nor the names of its
23// contributors may be used to endorse or promote products derived from this
24// software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35// OF THE POSSIBILITY OF SUCH DAMAGE.
36//-----------------------------------------------------------------------------
37
38#pragma once
39
44
45#include "base/source/fobject.h"
46
47#include <cstdarg>
48
49namespace Steinberg {
50
51class FVariant;
52class String;
53
54#ifdef UNICODE
55static const bool kWideStringDefault = true;
56#else
57static const bool kWideStringDefault = false;
58#endif
59
60static const uint16 kBomUtf16 = 0xFEFF;
61static const char8* const kBomUtf8 = "\xEF\xBB\xBF";
62static const int32 kBomUtf8Length = 3;
63
64
78
86
87//------------------------------------------------------------------------
88// Helper functions to create hash codes from string data.
89//------------------------------------------------------------------------
90extern uint32 hashString8 (const char8* s, uint32 m);
91extern uint32 hashString16 (const char16* s, uint32 m);
92inline uint32 hashString (const tchar* s, uint32 m)
93{
94#ifdef UNICODE
95 return hashString16 (s, m);
96#else
97 return hashString8 (s, m);
98#endif
99}
100
101
102//-----------------------------------------------------------------------------
115//-----------------------------------------------------------------------------
117{
118public:
119//-----------------------------------------------------------------------------
120 ConstString (const char8* str, int32 length = -1);
121 ConstString (const char16* str, int32 length = -1);
122 ConstString (const ConstString& str, int32 offset = 0, int32 length = -1);
123 ConstString (const FVariant& var);
124 ConstString ();
125 virtual ~ConstString () {}
126
127 // access -----------------------------------------------------------------
128 virtual int32 length () const {return static_cast<int32> (len);}
129 inline bool isEmpty () const {return buffer == nullptr || len == 0;}
130
131 operator const char8* () const {return text8 ();}
132 operator const char16* () const {return text16 ();}
133 inline tchar operator[] (short idx) const {return getChar (static_cast<uint32> (idx));}
134 inline tchar operator[] (long idx) const {return getChar (static_cast<uint32> (idx));}
135 inline tchar operator[] (int idx) const {return getChar (static_cast<uint32> (idx));}
136 inline tchar operator[] (unsigned short idx) const {return getChar (idx);}
137 inline tchar operator[] (unsigned long idx) const {return getChar (static_cast<uint32> (idx));}
138 inline tchar operator[] (unsigned int idx) const {return getChar (idx);}
139
140 inline virtual const char8* text8 () const;
141 inline virtual const char16* text16 () const;
142 inline virtual const tchar* text () const;
143 inline virtual const void* ptr () const {return buffer;}
144
145 inline virtual char8 getChar8 (uint32 index) const;
146 inline virtual char16 getChar16 (uint32 index) const;
147 inline tchar getChar (uint32 index) const;
148 inline tchar getCharAt (uint32 index) const;
149
150 bool testChar8 (uint32 index, char8 c) const;
151 bool testChar16 (uint32 index, char16 c) const;
152 inline bool testChar (uint32 index, char8 c) const {return testChar8 (index, c);}
153 inline bool testChar (uint32 index, char16 c) const {return testChar16 (index, c);}
154
155 bool extract (String& result, uint32 idx, int32 n = -1) const;
156 int32 copyTo8 (char8* str, uint32 idx = 0, int32 n = -1) const;
157 int32 copyTo16 (char16* str, uint32 idx = 0, int32 n = -1) const;
158 int32 copyTo (tchar* str, uint32 idx = 0, int32 n = -1) const;
159 void copyTo (IStringResult* result) const;
160 void copyTo (IString& string) const;
161
162 inline uint32 hash (uint32 tsize) const
163 {
164 return isWide ? hashString16 (buffer16, tsize) : hashString8 (buffer8, tsize) ;
165 }
166 //-------------------------------------------------------------------------
167
168 // compare ----------------------------------------------------------------
174
175 int32 compareAt (uint32 index, const ConstString& str, int32 n = -1, CompareMode m = kCaseSensitive) const;
176 int32 compare (const ConstString& str, int32 n, CompareMode m = kCaseSensitive) const;
177 int32 compare (const ConstString& str, CompareMode m = kCaseSensitive) const;
178
179 int32 naturalCompare (const ConstString& str, CompareMode mode = kCaseSensitive) const;
180
181 bool startsWith (const ConstString& str, CompareMode m = kCaseSensitive) const;
182 bool endsWith (const ConstString& str, CompareMode m = kCaseSensitive) const;
183 bool contains (const ConstString& str, CompareMode m = kCaseSensitive) const;
184
185 // static methods
186 static bool isCharSpace (char8 character);
187 static bool isCharSpace (char16 character);
188 static bool isCharAlpha (char8 character);
189 static bool isCharAlpha (char16 character);
190 static bool isCharAlphaNum (char8 character);
191 static bool isCharAlphaNum (char16 character);
192 static bool isCharDigit (char8 character);
193 static bool isCharDigit (char16 character);
194 static bool isCharAscii (char8 character);
195 static bool isCharAscii (char16 character);
196 static bool isCharUpper (char8 character);
197 static bool isCharUpper (char16 character);
198 static bool isCharLower (char8 character);
199 static bool isCharLower (char16 character);
200 //-------------------------------------------------------------------------
201
204 inline int32 findFirst (const ConstString& str, int32 n = -1, CompareMode m = kCaseSensitive, int32 endIndex = -1) const {return findNext (0, str, n, m, endIndex);}
205 inline int32 findFirst (char8 c, CompareMode m = kCaseSensitive, int32 endIndex = -1) const {return findNext (0, c, m, endIndex);}
206 inline int32 findFirst (char16 c, CompareMode m = kCaseSensitive, int32 endIndex = -1) const {return findNext (0, c, m, endIndex);}
208
210 int32 findNext (int32 startIndex, const ConstString& str, int32 n = -1, CompareMode = kCaseSensitive, int32 endIndex = -1) const;
211 int32 findNext (int32 startIndex, char8 c, CompareMode = kCaseSensitive, int32 endIndex = -1) const;
212 int32 findNext (int32 startIndex, char16 c, CompareMode = kCaseSensitive, int32 endIndex = -1) const;
214
216 int32 findPrev (int32 startIndex, const ConstString& str, int32 n = -1, CompareMode = kCaseSensitive) const;
217 int32 findPrev (int32 startIndex, char8 c, CompareMode = kCaseSensitive) const;
218 int32 findPrev (int32 startIndex, char16 c, CompareMode = kCaseSensitive) const;
220
221 inline int32 findLast (const ConstString& str, int32 n = -1, CompareMode m = kCaseSensitive) const {return findPrev (-1, str, n, m);}
222 inline int32 findLast (char8 c, CompareMode m = kCaseSensitive) const {return findPrev (-1, c, m);}
223 inline int32 findLast (char16 c, CompareMode m = kCaseSensitive) const {return findPrev (-1, c, m);}
224
225 int32 countOccurences (char8 c, uint32 startIndex, CompareMode = kCaseSensitive) const;
226 int32 countOccurences (char16 c, uint32 startIndex, CompareMode = kCaseSensitive) const;
227 int32 getFirstDifferent (const ConstString& str, CompareMode = kCaseSensitive) const;
228 //-------------------------------------------------------------------------
229
230 // numbers ----------------------------------------------------------------
231 bool isDigit (uint32 index) const;
232 bool scanFloat (double& value, uint32 offset = 0, bool scanToEnd = true) const;
233 bool scanInt64 (int64& value, uint32 offset = 0, bool scanToEnd = true) const;
234 bool scanUInt64 (uint64& value, uint32 offset = 0, bool scanToEnd = true) const;
235 bool scanInt32 (int32& value, uint32 offset = 0, bool scanToEnd = true) const;
236 bool scanUInt32 (uint32& value, uint32 offset = 0, bool scanToEnd = true) const;
237 bool scanHex (uint8& value, uint32 offset = 0, bool scanToEnd = true) const;
238
239 int32 getTrailingNumberIndex (uint32 width = 0) const;
240 int64 getTrailingNumber (int64 fallback = 0) const;
241 int64 getNumber () const;
242
243 // static methods
244 static bool scanInt64_8 (const char8* text, int64& value, bool scanToEnd = true);
245 static bool scanInt64_16 (const char16* text, int64& value, bool scanToEnd = true);
246 static bool scanInt64 (const tchar* text, int64& value, bool scanToEnd = true);
247
248 static bool scanUInt64_8 (const char8* text, uint64& value, bool scanToEnd = true);
249 static bool scanUInt64_16 (const char16* text, uint64& value, bool scanToEnd = true);
250 static bool scanUInt64 (const tchar* text, uint64& value, bool scanToEnd = true);
251
252 static bool scanInt32_8 (const char8* text, int32& value, bool scanToEnd = true);
253 static bool scanInt32_16 (const char16* text, int32& value, bool scanToEnd = true);
254 static bool scanInt32 (const tchar* text, int32& value, bool scanToEnd = true);
255
256 static bool scanUInt32_8 (const char8* text, uint32& value, bool scanToEnd = true);
257 static bool scanUInt32_16 (const char16* text, uint32& value, bool scanToEnd = true);
258 static bool scanUInt32 (const tchar* text, uint32& value, bool scanToEnd = true);
259
260 static bool scanHex_8 (const char8* text, uint8& value, bool scanToEnd = true);
261 static bool scanHex_16 (const char16* text, uint8& value, bool scanToEnd = true);
262 static bool scanHex (const tchar* text, uint8& value, bool scanToEnd = true);
263 //-------------------------------------------------------------------------
264
265 // conversion -------------------------------------------------------------
266 void toVariant (FVariant& var) const;
267
268 static char8 toLower (char8 c);
269 static char8 toUpper (char8 c);
270 static char16 toLower (char16 c);
271 static char16 toUpper (char16 c);
272
273 static int32 multiByteToWideString (char16* dest, const char8* source, int32 wcharCount, uint32 sourceCodePage = kCP_Default);
274 static int32 wideStringToMultiByte (char8* dest, const char16* source, int32 char8Count, uint32 destCodePage = kCP_Default);
275
276 bool isWideString () const {return isWide != 0;}
277 bool isAsciiString () const;
278
279 bool isNormalized (UnicodeNormalization = kUnicodeNormC);
280
281#if SMTG_OS_WINDOWS
282 ConstString (const wchar_t* str, int32 length = -1) : ConstString (wscast (str), length) {}
283 operator const wchar_t* () const { return wscast (text16 ());}
284#endif
285
286#if SMTG_OS_MACOS
287 virtual void* toCFStringRef (uint32 encoding = 0xFFFF, bool mutableCFString = false) const;
288#endif
289//-------------------------------------------------------------------------
290
291//-----------------------------------------------------------------------------
292protected:
293
294 union
295 {
296 void* buffer;
297 char8* buffer8;
298 char16* buffer16;
299 };
300 uint32 len : 30;
301 uint32 isWide : 1;
302};
303
304//-----------------------------------------------------------------------------
311//-----------------------------------------------------------------------------
312class String : public ConstString
313{
314public:
315
316//-----------------------------------------------------------------------------
317 String ();
318 String (const char8* str, MBCodePage codepage, int32 n = -1, bool isTerminated = true);
319 String (const char8* str, int32 n = -1, bool isTerminated = true);
320 String (const char16* str, int32 n = -1, bool isTerminated = true);
321 String (const String& str, int32 n = -1);
322 String (const ConstString& str, int32 n = -1);
323 String (const FVariant& var);
324 String (IString* str);
325 ~String () SMTG_OVERRIDE;
326
327#if SMTG_CPP11_STDLIBSUPPORT
328 String (String&& str);
329 String& operator= (String&& str);
330#endif
331
332 // access------------------------------------------------------------------
333 void updateLength ();
334 const char8* text8 () const SMTG_OVERRIDE;
335 const char16* text16 () const SMTG_OVERRIDE;
336 char8 getChar8 (uint32 index) const SMTG_OVERRIDE;
337 char16 getChar16 (uint32 index) const SMTG_OVERRIDE;
338
339 bool setChar8 (uint32 index, char8 c);
340 bool setChar16 (uint32 index, char16 c);
341 inline bool setChar (uint32 index, char8 c) {return setChar8 (index, c);}
342 inline bool setChar (uint32 index, char16 c) {return setChar16 (index, c);}
343 //-------------------------------------------------------------------------
344
345 // assignment--------------------------------------------------------------
346 String& operator= (const char8* str) {return assign (str);}
347 String& operator= (const char16* str) {return assign (str);}
348 String& operator= (const ConstString& str) {return assign (str);}
349 String& operator= (const String& str) {return assign (str);}
350 String& operator= (char8 c) {return assign (c);}
351 String& operator= (char16 c) {return assign (c);}
352
353 String& assign (const ConstString& str, int32 n = -1);
354 String& assign (const char8* str, int32 n = -1, bool isTerminated = true);
355 String& assign (const char16* str, int32 n = -1, bool isTerminated = true);
356 String& assign (char8 c, int32 n = 1);
357 String& assign (char16 c, int32 n = 1);
358 //-------------------------------------------------------------------------
359
360 // concat------------------------------------------------------------------
361 String& append (const ConstString& str, int32 n = -1);
362 String& append (const char8* str, int32 n = -1);
363 String& append (const char16* str, int32 n = -1);
364 String& append (const char8 c, int32 n = 1);
365 String& append (const char16 c, int32 n = 1);
366
367 String& insertAt (uint32 idx, const ConstString& str, int32 n = -1);
368 String& insertAt (uint32 idx, const char8* str, int32 n = -1);
369 String& insertAt (uint32 idx, const char16* str, int32 n = -1);
370 String& insertAt (uint32 idx, char8 c) {char8 str[] = {c, 0}; return insertAt (idx, str, 1);}
371 String& insertAt (uint32 idx, char16 c) {char16 str[] = {c, 0}; return insertAt (idx, str, 1);}
372
373 String& operator+= (const String& str) {return append (str);}
374 String& operator+= (const ConstString& str) {return append (str);}
375 String& operator+= (const char8* str) {return append (str);}
376 String& operator+= (const char16* str) {return append (str);}
377 String& operator+= (const char8 c) {return append (c);}
378 String& operator+= (const char16 c) {return append (c);}
379 //-------------------------------------------------------------------------
380
381 // replace-----------------------------------------------------------------
382 String& replace (uint32 idx, int32 n1, const ConstString& str, int32 n2 = -1);
383 String& replace (uint32 idx, int32 n1, const char8* str, int32 n2 = -1);
384 String& replace (uint32 idx, int32 n1, const char16* str, int32 n2 = -1);
385
386 int32 replace (const char8* toReplace, const char8* toReplaceWith, bool all = false, CompareMode m = kCaseSensitive);
387 int32 replace (const char16* toReplace, const char16* toReplaceWith, bool all = false, CompareMode m = kCaseSensitive);
388
389 bool replaceChars8 (const char8* toReplace, char8 toReplaceBy);
390 bool replaceChars16 (const char16* toReplace, char16 toReplaceBy);
391 inline bool replaceChars8 (char8 toReplace, char8 toReplaceBy) {char8 str[] = {toReplace, 0}; return replaceChars8 (str, toReplaceBy);}
392 inline bool replaceChars16 (char16 toReplace, char16 toReplaceBy) {char16 str[] = {toReplace, 0}; return replaceChars16 (str, toReplaceBy);}
393 inline bool replaceChars (char8 toReplace, char8 toReplaceBy) {return replaceChars8 (toReplace, toReplaceBy);}
394 inline bool replaceChars (char16 toReplace, char16 toReplaceBy) {return replaceChars16 (toReplace, toReplaceBy);}
395 inline bool replaceChars (const char8* toReplace, char8 toReplaceBy) {return replaceChars8 (toReplace, toReplaceBy);}
396 inline bool replaceChars (const char16* toReplace, char16 toReplaceBy) {return replaceChars16 (toReplace, toReplaceBy);}
397 //-------------------------------------------------------------------------
398
399 // remove------------------------------------------------------------------
400 String& remove (uint32 index = 0, int32 n = -1);
401 enum CharGroup {kSpace, kNotAlphaNum, kNotAlpha};
402 bool trim (CharGroup mode = kSpace);
403 void removeChars (CharGroup mode = kSpace);
404 bool removeChars8 (const char8* which);
405 bool removeChars16 (const char16* which);
406 inline bool removeChars8 (const char8 which) {char8 str[] = {which, 0}; return removeChars8 (str); }
407 inline bool removeChars16 (const char16 which) {char16 str[] = {which, 0}; return removeChars16 (str); }
408 inline bool removeChars (const char8* which) {return removeChars8 (which);}
409 inline bool removeChars (const char16* which) {return removeChars16 (which);}
410 inline bool removeChars (const char8 which) {return removeChars8 (which);}
411 inline bool removeChars (const char16 which) {return removeChars16 (which);}
412 bool removeSubString (const ConstString& subString, bool allOccurences = true);
413 //-------------------------------------------------------------------------
414
415 // print-------------------------------------------------------------------
416 String& printf (const char8* format, ...);
417 String& printf (const char16* format, ...);
418 String& vprintf (const char8* format, va_list args);
419 String& vprintf (const char16* format, va_list args);
420 //-------------------------------------------------------------------------
421
422 // numbers-----------------------------------------------------------------
423 String& printInt64 (int64 value);
424
432 String& printFloat (double value, uint32 maxPrecision = 6);
435 bool incrementTrailingNumber (uint32 width = 2, tchar separator = STR (' '), uint32 minNumber = 1, bool applyOnlyFormat = false);
436 //-------------------------------------------------------------------------
437
438 // conversion--------------------------------------------------------------
439 bool fromVariant (const FVariant& var);
440 void toVariant (FVariant& var) const;
441 bool fromAttributes (IAttributes* a, IAttrID attrID);
442 bool toAttributes (IAttributes* a, IAttrID attrID);
443
444 void swapContent (String& s);
445 void take (String& str);
446 void take (void* _buffer, bool wide);
447 void* pass ();
448 void passToVariant (FVariant& var);
449
450 void toLower (uint32 index);
451 void toLower ();
452 void toUpper (uint32 index);
453 void toUpper ();
454
455 unsigned char* toPascalString (unsigned char* buf);
456 const String& fromPascalString (const unsigned char* buf);
457
458 bool toWideString (uint32 sourceCodePage = kCP_Default);
459 bool toMultiByte (uint32 destCodePage = kCP_Default);
460
461 void fromUTF8 (const char8* utf8String);
462 bool normalize (UnicodeNormalization = kUnicodeNormC);
463
464#if SMTG_OS_WINDOWS
465 String (const wchar_t* str, int32 length = -1, bool isTerminated = true) : String (wscast (str), length, isTerminated) {}
466 String& operator= (const wchar_t* str) {return String::operator= (wscast (str)); }
467#endif
468
469#if SMTG_OS_MACOS
470 virtual bool fromCFStringRef (const void*, uint32 encoding = 0xFFFF);
471#endif
472 //-------------------------------------------------------------------------
473
474 //-----------------------------------------------------------------------------
475protected:
476 bool resize (uint32 newSize, bool wide, bool fill = false);
477
478private:
479 bool _toWideString (const char8* src, int32 length, uint32 sourceCodePage = kCP_Default);
480 void tryFreeBuffer ();
481 bool checkToMultiByte (uint32 destCodePage = kCP_Default) const; // to remove debug code from inline - const_cast inside!!!
482};
483
484// String concatenation functions.
485inline String operator+ (const ConstString& s1, const ConstString& s2) {return String (s1).append (s2);}
486inline String operator+ (const ConstString& s1, const char8* s2) {return String (s1).append (s2);}
487inline String operator+ (const ConstString& s1, const char16* s2) {return String (s1).append (s2);}
488inline String operator+ (const char8* s1, const ConstString& s2) {return String (s1).append (s2);}
489inline String operator+ (const char16* s1, const ConstString& s2) {return String (s1).append (s2);}
490inline String operator+ (const ConstString& s1, const String& s2) {return String (s1).append (s2);}
491inline String operator+ (const String& s1, const ConstString& s2) {return String (s1).append (s2);}
492inline String operator+ (const String& s1, const String& s2) {return String (s1).append (s2);}
493inline String operator+ (const String& s1, const char8* s2) {return String (s1).append (s2);}
494inline String operator+ (const String& s1, const char16* s2) {return String (s1).append (s2);}
495inline String operator+ (const char8* s1, const String& s2) {return String (s1).append (s2);}
496inline String operator+ (const char16* s1, const String& s2) {return String (s1).append (s2);}
497
498//-----------------------------------------------------------------------------
499// ConstString
500//-----------------------------------------------------------------------------
501inline const tchar* ConstString::text () const
502{
503#ifdef UNICODE
504 return text16 ();
505#else
506 return text8 ();
507#endif
508}
509
510//-----------------------------------------------------------------------------
511inline const char8* ConstString::text8 () const
512{
513 return (!isWide && buffer8) ? buffer8: kEmptyString8;
514}
515
516//-----------------------------------------------------------------------------
517inline const char16* ConstString::text16 () const
518{
519 return (isWide && buffer16) ? buffer16 : kEmptyString16;
520}
521
522//-----------------------------------------------------------------------------
523inline char8 ConstString::getChar8 (uint32 index) const
524{
525 if (index < len && buffer8 && !isWide)
526 return buffer8[index];
527 return 0;
528}
529
530//-----------------------------------------------------------------------------
531inline char16 ConstString::getChar16 (uint32 index) const
532{
533 if (index < len && buffer16 && isWide)
534 return buffer16[index];
535 return 0;
536}
537
538//-----------------------------------------------------------------------------
539inline tchar ConstString::getChar (uint32 index) const
540{
541#ifdef UNICODE
542 return getChar16 (index);
543#else
544 return getChar8 (index);
545#endif
546}
547
548//-----------------------------------------------------------------------------
549inline tchar ConstString::getCharAt (uint32 index) const
550{
551#ifdef UNICODE
552 if (isWide)
553 return getChar16 (index);
554#endif
555
556 return static_cast<tchar> (getChar8 (index));
557}
558
559//-----------------------------------------------------------------------------
560inline int64 ConstString::getNumber () const
561{
562 int64 tmp = 0;
563 scanInt64 (tmp);
564 return tmp;
565}
566
567//-----------------------------------------------------------------------------
568inline bool ConstString::scanInt32_8 (const char8* text, int32& value, bool scanToEnd)
569{
570 int64 tmp;
571 if (scanInt64_8 (text, tmp, scanToEnd))
572 {
573 value = (int32)tmp;
574 return true;
575 }
576 return false;
577}
578
579//-----------------------------------------------------------------------------
580inline bool ConstString::scanInt32_16 (const char16* text, int32& value, bool scanToEnd)
581{
582 int64 tmp;
583 if (scanInt64_16 (text, tmp, scanToEnd))
584 {
585 value = (int32)tmp;
586 return true;
587 }
588 return false;
589}
590
591//-----------------------------------------------------------------------------
592inline bool ConstString::scanInt32 (const tchar* text, int32& value, bool scanToEnd)
593{
594 int64 tmp;
595 if (scanInt64 (text, tmp, scanToEnd))
596 {
597 value = (int32)tmp;
598 return true;
599 }
600 return false;
601}
602
603//-----------------------------------------------------------------------------
604inline bool ConstString::scanUInt32_8 (const char8* text, uint32& value, bool scanToEnd)
605{
606 uint64 tmp;
607 if (scanUInt64_8 (text, tmp, scanToEnd))
608 {
609 value = (uint32)tmp;
610 return true;
611 }
612 return false;
613}
614
615//-----------------------------------------------------------------------------
616inline bool ConstString::scanUInt32_16 (const char16* text, uint32& value, bool scanToEnd)
617{
618 uint64 tmp;
619 if (scanUInt64_16 (text, tmp, scanToEnd))
620 {
621 value = (uint32)tmp;
622 return true;
623 }
624 return false;
625}
626
627//-----------------------------------------------------------------------------
628inline bool ConstString::scanUInt32 (const tchar* text, uint32& value, bool scanToEnd)
629{
630 uint64 tmp;
631 if (scanUInt64 (text, tmp, scanToEnd))
632 {
633 value = (uint32)tmp;
634 return true;
635 }
636 return false;
637}
638
639//-----------------------------------------------------------------------------
640inline const char8* String::text8 () const
641{
642 if (isWide && !isEmpty ())
643 checkToMultiByte (); // this should be avoided, since it can lead to information loss
644
645 return ConstString::text8 ();
646}
647
648//-----------------------------------------------------------------------------
649inline const char16* String::text16 () const
650{
651 if (!isWide && !isEmpty ())
652 {
653 const_cast<String&> (*this).toWideString ();
654 }
655 return ConstString::text16 ();
656}
657
658//-----------------------------------------------------------------------------
659inline char8 String::getChar8 (uint32 index) const
660{
661 if (isWide && !isEmpty ())
662 checkToMultiByte (); // this should be avoided, since it can lead to information loss
663
664 return ConstString::getChar8 (index);
665}
666
667//-----------------------------------------------------------------------------
668inline char16 String::getChar16 (uint32 index) const
669{
670 if (!isWide && !isEmpty ())
671 {
672 const_cast<String&> (*this).toWideString ();
673 }
674 return ConstString::getChar16 (index);
675}
676
677//-----------------------------------------------------------------------------
678
679
680inline bool operator< (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) < 0) ? true : false;}
681inline bool operator<= (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) <= 0) ? true : false;}
682inline bool operator> (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) > 0) ? true : false;}
683inline bool operator>= (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) >= 0) ? true : false;}
684inline bool operator== (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) == 0) ? true : false;}
685inline bool operator!= (const ConstString& s1, const ConstString& s2) {return (s1.compare (s2) != 0) ? true : false;}
686
687inline bool operator< (const ConstString& s1, const char8* s2) {return (s1.compare (s2) < 0) ? true : false;}
688inline bool operator<= (const ConstString& s1, const char8* s2) {return (s1.compare (s2) <= 0) ? true : false;}
689inline bool operator> (const ConstString& s1, const char8* s2) {return (s1.compare (s2) > 0) ? true : false;}
690inline bool operator>= (const ConstString& s1, const char8* s2) {return (s1.compare (s2) >= 0) ? true : false;}
691inline bool operator== (const ConstString& s1, const char8* s2) {return (s1.compare (s2) == 0) ? true : false;}
692inline bool operator!= (const ConstString& s1, const char8* s2) {return (s1.compare (s2) != 0) ? true : false;}
693inline bool operator< (const char8* s1, const ConstString& s2) {return (s2.compare (s1) > 0) ? true : false;}
694inline bool operator<= (const char8* s1, const ConstString& s2) {return (s2.compare (s1) >= 0) ? true : false;}
695inline bool operator> (const char8* s1, const ConstString& s2) {return (s2.compare (s1) < 0) ? true : false;}
696inline bool operator>= (const char8* s1, const ConstString& s2) {return (s2.compare (s1) <= 0) ? true : false;}
697inline bool operator== (const char8* s1, const ConstString& s2) {return (s2.compare (s1) == 0) ? true : false;}
698inline bool operator!= (const char8* s1, const ConstString& s2) {return (s2.compare (s1) != 0) ? true : false;}
699
700inline bool operator< (const ConstString& s1, const char16* s2) {return (s1.compare (s2) < 0) ? true : false;}
701inline bool operator<= (const ConstString& s1, const char16* s2) {return (s1.compare (s2) <= 0) ? true : false;}
702inline bool operator> (const ConstString& s1, const char16* s2) {return (s1.compare (s2) > 0) ? true : false;}
703inline bool operator>= (const ConstString& s1, const char16* s2) {return (s1.compare (s2) >= 0) ? true : false;}
704inline bool operator== (const ConstString& s1, const char16* s2) {return (s1.compare (s2) == 0) ? true : false;}
705inline bool operator!= (const ConstString& s1, const char16* s2) {return (s1.compare (s2) != 0) ? true : false;}
706inline bool operator< (const char16* s1, const ConstString& s2) {return (s2.compare (s1) > 0) ? true : false;}
707inline bool operator<= (const char16* s1, const ConstString& s2) {return (s2.compare (s1) >= 0) ? true : false;}
708inline bool operator> (const char16* s1, const ConstString& s2) {return (s2.compare (s1) < 0) ? true : false;}
709inline bool operator>= (const char16* s1, const ConstString& s2) {return (s2.compare (s1) <= 0) ? true : false;}
710inline bool operator== (const char16* s1, const ConstString& s2) {return (s2.compare (s1) == 0) ? true : false;}
711inline bool operator!= (const char16* s1, const ConstString& s2) {return (s2.compare (s1) != 0) ? true : false;}
712
713// The following functions will only work with European Numbers!
714// (e.g. Arabic, Tibetan, and Khmer digits are not supported)
715extern int32 strnatcmp8 (const char8* s1, const char8* s2, bool caseSensitive = true);
716extern int32 strnatcmp16 (const char16* s1, const char16* s2, bool caseSensitive = true);
717inline int32 strnatcmp (const tchar* s1, const tchar* s2, bool caseSensitive = true)
718{
719#ifdef UNICODE
720 return strnatcmp16 (s1, s2, caseSensitive);
721#else
722 return strnatcmp8 (s1, s2, caseSensitive);
723#endif
724}
725
726//-----------------------------------------------------------------------------
733//-----------------------------------------------------------------------------
734class StringObject : public FObject, public String, public IStringResult, public IString
735{
736public:
737//-----------------------------------------------------------------------------
738 StringObject () {}
739 StringObject (const char16* str, int32 n = -1, bool isTerminated = true) : String (str, n, isTerminated) {}
740 StringObject (const char8* str, int32 n = -1, bool isTerminated = true) : String (str, n, isTerminated) {}
741 StringObject (const StringObject& str, int32 n = -1) : String (str, n) {}
742 StringObject (const String& str, int32 n = -1) : String (str, n) {}
743 StringObject (const FVariant& var) : String (var) {}
744
745 using String::operator=;
746
747 // IStringResult ----------------------------------------------------------
748 void PLUGIN_API setText (const char8* text) SMTG_OVERRIDE;
749 //-------------------------------------------------------------------------
750
751 // IString-----------------------------------------------------------------
752 void PLUGIN_API setText8 (const char8* text) SMTG_OVERRIDE;
753 void PLUGIN_API setText16 (const char16* text) SMTG_OVERRIDE;
754
755 const char8* PLUGIN_API getText8 () SMTG_OVERRIDE;
756 const char16* PLUGIN_API getText16 () SMTG_OVERRIDE;
757
758 void PLUGIN_API take (void* s, bool _isWide) SMTG_OVERRIDE;
759 bool PLUGIN_API isWideString () const SMTG_OVERRIDE;
760 //-------------------------------------------------------------------------
761
762 OBJ_METHODS (StringObject, FObject)
763 FUNKNOWN_METHODS2 (IStringResult, IString, FObject)
764};
765
766//------------------------------------------------------------------------
767} // namespace Steinberg
Invariant String.
Definition fstring.h:117
virtual const char8 * text8() const
Returns pointer to string of type char8.
Definition fstring.h:511
int32 getFirstDifferent(const ConstString &str, CompareMode=kCaseSensitive) const
Returns position of first different character.
Definition fstring.cpp:1270
int64 getTrailingNumber(int64 fallback=0) const
Returns result of scanInt64 or the fallback.
Definition fstring.cpp:1755
bool isAsciiString() const
Checks if all characters in string are in ascii range.
Definition fstring.cpp:1784
virtual const tchar * text() const
Returns pointer to string of type tchar.
Definition fstring.h:501
virtual char8 getChar8(uint32 index) const
Returns character of type char16 at 'index'.
Definition fstring.h:523
static bool scanUInt32_16(const char16 *text, uint32 &value, bool scanToEnd=true)
Converts string of type char16 to int32 value.
Definition fstring.h:616
static bool scanInt32_8(const char8 *text, int32 &value, bool scanToEnd=true)
Converts string of type char8 to int32 value.
Definition fstring.h:568
static bool isCharSpace(char8 character)
Returns true if character is a space.
Definition fstring.cpp:1618
static bool scanUInt64_16(const char16 *text, uint64 &value, bool scanToEnd=true)
Converts string of type char16 to uint64 value.
Definition fstring.cpp:1429
static int32 wideStringToMultiByte(char8 *dest, const char16 *source, int32 char8Count, uint32 destCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
Definition fstring.cpp:1889
@ kCaseSensitive
Comparison is done with regard to character's case.
Definition fstring.h:171
@ kCaseInsensitive
Comparison is done without regard to character's case.
Definition fstring.h:172
int32 findLast(const ConstString &str, int32 n=-1, CompareMode m=kCaseSensitive) const
Find last occurrence of n characters of str in this (n=-1: all)
Definition fstring.h:221
tchar getCharAt(uint32 index) const
Returns character of type tchar at 'index', no conversion!
Definition fstring.h:549
bool endsWith(const ConstString &str, CompareMode m=kCaseSensitive) const
Check if this ends with str.
Definition fstring.cpp:859
bool isDigit(uint32 index) const
Returns true if character at position is a digit.
Definition fstring.cpp:1721
static bool scanHex_16(const char16 *text, uint8 &value, bool scanToEnd=true)
Converts string of type char16 to hex/unit8 value.
Definition fstring.cpp:1479
tchar operator[](short idx) const
Returns character at 'idx'.
Definition fstring.h:133
bool testChar8(uint32 index, char8 c) const
Returns true if character is equal at position 'index'.
Definition fstring.cpp:505
bool scanInt32(int32 &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to int32 value starting at offset.
Definition fstring.cpp:1367
static bool scanHex_8(const char8 *text, uint8 &value, bool scanToEnd=true)
Converts string of type char8 to hex/unit8 value.
Definition fstring.cpp:1461
bool scanHex(uint8 &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to hex/uint8 value starting at offset.
Definition fstring.cpp:1356
bool scanUInt32(uint32 &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to uint32 value starting at offset.
Definition fstring.cpp:1378
bool extract(String &result, uint32 idx, int32 n=-1) const
Get n characters long substring starting at index (n=-1: until end)
Definition fstring.cpp:539
static char8 toLower(char8 c)
Converts to lower case.
Definition fstring.cpp:1590
static bool isCharAlphaNum(char8 character)
Returns true if character is an alphanumeric character.
Definition fstring.cpp:1661
int32 countOccurences(char8 c, uint32 startIndex, CompareMode=kCaseSensitive) const
Counts occurences of c within this starting at index.
Definition fstring.cpp:1214
bool isWideString() const
Returns true if string is wide.
Definition fstring.h:276
bool startsWith(const ConstString &str, CompareMode m=kCaseSensitive) const
Check if this starts with str.
Definition fstring.cpp:813
int32 getTrailingNumberIndex(uint32 width=0) const
Returns start index of trailing number.
Definition fstring.cpp:1732
bool scanUInt64(uint64 &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to uint64 value starting at offset.
Definition fstring.cpp:1345
bool isEmpty() const
Return true if string is empty.
Definition fstring.h:129
tchar getChar(uint32 index) const
Returns character of type tchar at 'index'.
Definition fstring.h:539
int64 getNumber() const
Returns result of scanInt64.
Definition fstring.h:560
static int32 multiByteToWideString(char16 *dest, const char8 *source, int32 wcharCount, uint32 sourceCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
Definition fstring.cpp:1823
bool contains(const ConstString &str, CompareMode m=kCaseSensitive) const
Check if this contains str
Definition fstring.cpp:905
virtual ~ConstString()
Destructor.
Definition fstring.h:125
static bool scanInt64_8(const char8 *text, int64 &value, bool scanToEnd=true)
Converts string of type char8 to int64 value.
Definition fstring.cpp:1389
static bool isCharAscii(char8 character)
Returns true if character is in ASCII range.
Definition fstring.cpp:1685
static bool scanInt64_16(const char16 *text, int64 &value, bool scanToEnd=true)
Converts string of type char16 to int64 value.
Definition fstring.cpp:1403
static bool isCharDigit(char8 character)
Returns true if character is a number.
Definition fstring.cpp:1673
static bool isCharAlpha(char8 character)
Returns true if character is an alphabetic character.
Definition fstring.cpp:1649
bool isNormalized(UnicodeNormalization=kUnicodeNormC)
On PC only kUnicodeNormC is working.
Definition fstring.cpp:1971
bool scanFloat(double &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to double value starting at offset.
Definition fstring.cpp:1501
bool scanInt64(int64 &value, uint32 offset=0, bool scanToEnd=true) const
Converts string to int64 value starting at offset.
Definition fstring.cpp:1334
virtual char16 getChar16(uint32 index) const
Returns character of type char8 at 'index'.
Definition fstring.h:531
static bool scanUInt64_8(const char8 *text, uint64 &value, bool scanToEnd=true)
Converts string of type char8 to uint64 value.
Definition fstring.cpp:1415
int32 compareAt(uint32 index, const ConstString &str, int32 n=-1, CompareMode m=kCaseSensitive) const
Compare n characters of str with n characters of this starting at index (return: see above)
Definition fstring.cpp:707
int32 compare(const ConstString &str, int32 n, CompareMode m=kCaseSensitive) const
Compare n characters of str with n characters of this (return: see above)
Definition fstring.cpp:659
static bool scanUInt32_8(const char8 *text, uint32 &value, bool scanToEnd=true)
Converts string of type char8 to int32 value.
Definition fstring.h:604
virtual const void * ptr() const
Returns pointer to string of type void.
Definition fstring.h:143
virtual const char16 * text16() const
Returns pointer to string of type char16.
Definition fstring.h:517
virtual int32 length() const
Return length of string.
Definition fstring.h:128
static char8 toUpper(char8 c)
Converts to upper case.
Definition fstring.cpp:1604
static bool scanInt32_16(const char16 *text, int32 &value, bool scanToEnd=true)
Converts string of type char16 to int32 value.
Definition fstring.h:580
Implements FUnknown and IDependent.
Definition fobject.h:84
A Value of variable type.
Definition fvariant.h:34
Interface to return an ascii string of variable size.
Interface to a string of variable size and encoding.
StringObject implements IStringResult and IString methods.
Definition fstring.h:735
const char8 *PLUGIN_API getText8() SMTG_OVERRIDE
Return ASCII string.
Definition fstring.cpp:3937
void PLUGIN_API take(void *s, bool _isWide) SMTG_OVERRIDE
!Do not use this method! Early implementations take the given pointer as internal string and this wil...
Definition fstring.cpp:3949
void PLUGIN_API setText8(const char8 *text) SMTG_OVERRIDE
Assign ASCII string.
Definition fstring.cpp:3925
const char16 *PLUGIN_API getText16() SMTG_OVERRIDE
Return unicode string.
Definition fstring.cpp:3943
void PLUGIN_API setText16(const char16 *text) SMTG_OVERRIDE
Assign unicode string.
Definition fstring.cpp:3931
bool PLUGIN_API isWideString() const SMTG_OVERRIDE
Returns true if the string is in unicode format, returns false if the string is ASCII.
Definition fstring.cpp:3955
String & printf(const char8 *format,...)
Print formatted data into string.
Definition fstring.cpp:3296
char8 getChar8(uint32 index) const SMTG_OVERRIDE
Returns character of type char16 at 'index'.
Definition fstring.h:659
String & assign(const ConstString &str, int32 n=-1)
Assign n characters of str (-1: all)
Definition fstring.cpp:2486
bool replaceChars8(const char8 *toReplace, char8 toReplaceBy)
Returns true when any replacement was done.
Definition fstring.cpp:2974
const char8 * text8() const SMTG_OVERRIDE
Returns pointer to string of type char8.
Definition fstring.h:640
void swapContent(String &s)
Swaps ownership of the strings pointed to.
Definition fstring.cpp:3606
void toUpper()
Upper case the string.
Definition fstring.cpp:3513
bool normalize(UnicodeNormalization=kUnicodeNormC)
On PC only kUnicodeNormC is working.
Definition fstring.cpp:2253
bool fromAttributes(IAttributes *a, IAttrID attrID)
Assigns string from FAttributes.
Definition fstring.cpp:3586
bool fromVariant(const FVariant &var)
Assigns string from FVariant.
Definition fstring.cpp:3546
bool removeChars8(const char8 *which)
Remove all occurrences of each char in 'which'.
Definition fstring.cpp:3248
String & insertAt(uint32 idx, const ConstString &str, int32 n=-1)
Insert n characters of str at position idx (n=-1: all)
Definition fstring.cpp:2729
bool removeChars16(const char16 *which)
Remove all occurrences of each char in 'which'.
Definition fstring.cpp:3272
const String & fromPascalString(const unsigned char *buf)
Pascal string conversion.
Definition fstring.cpp:3705
bool toWideString(uint32 sourceCodePage=kCP_Default)
Converts to wide string according to sourceCodePage.
Definition fstring.cpp:2121
void take(String &str)
Take ownership of the string of 'str'.
Definition fstring.cpp:3620
char16 getChar16(uint32 index) const SMTG_OVERRIDE
Returns character of type char8 at 'index'.
Definition fstring.h:668
unsigned char * toPascalString(unsigned char *buf)
Pascal string conversion.
Definition fstring.cpp:3677
String & append(const ConstString &str, int32 n=-1)
Append n characters of str to this (n=-1: all)
Definition fstring.cpp:2582
String & operator=(const char8 *str)
Assign from a string of type char8.
Definition fstring.h:346
const char16 * text16() const SMTG_OVERRIDE
Returns pointer to string of type char16.
Definition fstring.h:649
bool incrementTrailingNumber(uint32 width=2, tchar separator=STR(' '), uint32 minNumber=1, bool applyOnlyFormat=false)
Increment the trailing number if present else start with minNumber, width specifies the string width ...
Definition fstring.cpp:3395
String & remove(uint32 index=0, int32 n=-1)
Remove n characters from string starting at index (n=-1: until end)
Definition fstring.cpp:3035
void fromUTF8(const char8 *utf8String)
Assigns from UTF8 string.
Definition fstring.cpp:2246
void passToVariant(FVariant &var)
Pass ownership of buffer to Variant - sets Variant ownership.
Definition fstring.cpp:3649
bool trim(CharGroup mode=kSpace)
Trim lead/trail.
Definition fstring.cpp:3111
String & printFloat(double value, uint32 maxPrecision=6)
print a float into a string, trailing zeros will be trimmed
Definition fstring.cpp:3354
void updateLength()
Call this when the string is truncated outside (not recommended though)
Definition fstring.cpp:2112
void toLower()
Lower case the string.
Definition fstring.cpp:3468
String & replace(uint32 idx, int32 n1, const ConstString &str, int32 n2=-1)
Replace n1 characters of this (starting at idx) with n2 characters of str (n1,n2=-1: until end)
Definition fstring.cpp:2807
void removeChars(CharGroup mode=kSpace)
Removes all of group.
Definition fstring.cpp:3174
Basic Object implementing FUnknown.
@ kCP_MAC_ROMAN
Default Mac codepage.
Definition fstring.h:68
@ kCP_ShiftJIS
Shifted Japan Industrial Standard Encoding.
Definition fstring.h:73
@ kCP_US_ASCII
US-ASCII (7-bit).
Definition fstring.h:74
@ kCP_Default
Default ANSI codepage.
Definition fstring.h:76
@ kCP_MAC_CEE
Mac Central European Encoding.
Definition fstring.h:71
@ kCP_ANSI_WEL
West European Latin Encoding.
Definition fstring.h:70
@ kCP_ANSI
Default ANSI codepage.
Definition fstring.h:67
@ kCP_Utf8
UTF8 Encoding.
Definition fstring.h:72
UnicodeNormalization
Definition fstring.h:80
@ kUnicodeNormKD
Unicode normalization form KD, compatibility decomposition.
Definition fstring.h:84
@ kUnicodeNormKC
Unicode normalization form KC, compatibility composition.
Definition fstring.h:83
@ kUnicodeNormD
Unicode normalization Form D, canonical decomposition.
Definition fstring.h:82
@ kUnicodeNormC
Unicode normalization Form C, canonical composition.
Definition fstring.h:81
bool isTerminated()
Returns true when singleton instances were already released.
Definition fobject.cpp:219