10 #include "platform/iplatformstring.h" 20 template<
typename BaseIterator>
24 using CodePoint = value_type;
38 CodePoint operator* ()
const noexcept;
40 BaseIterator base ()
const noexcept {
return it; }
44 static constexpr uint8_t kFirstBitMask = 128u;
45 static constexpr uint8_t kSecondBitMask = 64u;
46 static constexpr uint8_t kThirdBitMask = 32u;
47 static constexpr uint8_t kFourthBitMask = 16u;
48 static constexpr uint8_t kFifthBitMask = 8u;
59 using StringType = std::string;
60 using SizeType = StringType::size_type;
70 UTF8String& operator= (
const StringType& other);
72 UTF8String& operator= (StringType&& str) noexcept;
73 UTF8String& operator= (UTF8StringPtr str) { assign (str);
return *
this; }
75 SizeType length ()
const noexcept {
return string.length (); }
76 bool empty ()
const noexcept {
return string.empty (); }
78 void copy (UTF8StringBuffer dst, SizeType dstSize)
const noexcept;
83 bool operator== (UTF8StringPtr str)
const noexcept;
84 bool operator!= (UTF8StringPtr str)
const noexcept;
85 bool operator== (
const UTF8String& str)
const noexcept;
86 bool operator!= (
const UTF8String& str)
const noexcept;
87 bool operator== (
const StringType& str)
const noexcept;
88 bool operator!= (
const StringType& str)
const noexcept;
93 void assign (UTF8StringPtr str);
94 void clear () noexcept;
96 const UTF8StringPtr data ()
const noexcept {
return string.data (); }
97 operator const UTF8StringPtr ()
const noexcept {
return data (); }
98 const StringType& getString ()
const noexcept {
return string; }
99 IPlatformString* getPlatformString ()
const noexcept;
101 explicit operator bool ()
const =
delete;
108 inline bool operator== (
const UTF8String::StringType& lhs,
const UTF8String& rhs) noexcept {
return lhs == rhs.getString (); }
109 inline bool operator!= (
const UTF8String::StringType& lhs,
const UTF8String& rhs) noexcept {
return lhs != rhs.getString (); }
111 inline UTF8String operator+ (UTF8StringPtr lhs,
const UTF8String& rhs) {
return UTF8String (lhs) += rhs; }
115 inline UTF8String toString (
const T& value)
117 return UTF8String (std::to_string (value));
125 bool isSpace (char32_t character) noexcept;
130 using CharTestFunc = std::function<bool (char32_t)>;
131 TrimOptions (CharTestFunc&& func = [] (char32_t c) {
return isSpace (c); }) : test (std::move (func)) {}
133 TrimOptions& left () { setBit (flags, Flags::kLeft,
true);
return *
this; }
134 TrimOptions& right () { setBit (flags, Flags::kRight,
true);
return *
this; }
136 bool trimLeft ()
const {
return hasBit (flags, Flags::kLeft); }
137 bool trimRight ()
const {
return hasBit (flags, Flags::kRight); }
139 bool operator() (char32_t c)
const {
return !test (c); }
142 enum Flags : uint8_t {
153 #if VSTGUI_ENABLE_DEPRECATED_METHODS 157 UTF8StringBuffer newWithString (UTF8StringPtr
string);
159 void free (UTF8StringBuffer buffer);
178 UTF8StringView (
const std::string&
string) : str (
string.data ()), byteCount (
string.size () + 1) {}
192 bool contains (
const UTF8StringPtr subString,
bool ignoreCase =
false)
const;
201 double toDouble (uint32_t precision = 8)
const;
204 float toFloat (uint32_t precision = 8)
const;
212 bool operator== (
const UTF8StringPtr otherString)
const;
213 bool operator!= (
const UTF8StringPtr otherString)
const;
215 operator const UTF8StringPtr ()
const;
227 : startPos ((uint8_t*)utf8Str)
229 , strLen (std::strlen (utf8Str))
235 : startPos ((uint8_t*)utf8Str)
243 : startPos ((uint8_t*)stdStr.c_str ())
245 , strLen (stdStr.size ())
254 if (currentPos == back ())
256 else if (*currentPos <= 0x7F)
260 uint8_t characterLength = getByteLength ();
262 currentPos += characterLength;
275 if (currentPos < front ())
277 currentPos = begin ();
282 if (*currentPos <= 0x7f || (*currentPos >= 0xC0 && *currentPos <= 0xFD))
289 uint8_t getByteLength ()
const 291 if (currentPos && currentPos != back ())
293 if (*currentPos <= 0x7F)
297 if (*currentPos >= 0xC0 && *currentPos <= 0xFD)
299 if ((*currentPos & 0xF8) == 0xF8)
301 else if ((*currentPos & 0xF0) == 0xF0)
303 else if ((*currentPos & 0xE0) == 0xE0)
305 else if ((*currentPos & 0xC0) == 0xC0)
313 uint8_t* begin () { currentPos = startPos;
return currentPos;}
314 uint8_t* end () { currentPos = startPos + strLen;
return currentPos; }
316 const uint8_t* front ()
const {
return startPos; }
317 const uint8_t* back ()
const {
return startPos + strLen; }
319 const uint8_t* operator++() {
return next (); }
320 const uint8_t* operator--() {
return previous (); }
321 bool operator==(uint8_t i) {
if (currentPos)
return *currentPos == i;
return false; }
322 operator uint8_t* ()
const {
return (uint8_t*)currentPos; }
333 inline UTF8StringView::UTF8StringView (
const UTF8StringView& other) noexcept
339 inline UTF8StringView& UTF8StringView::operator= (
const UTF8StringView& other) noexcept
343 byteCount = makeOptional (*other.byteCount);
355 while (it != it.back ())
367 byteCount = makeOptional<size_t> (str ? std::strlen (str) + 1 : 0);
376 if (!str || !subString)
380 auto foundIt = std::search (
381 it.begin (), it.end (), subIt.begin (), subIt.end (),
382 [] (uint8_t c1, uint8_t c2) {
return std::toupper (c1) == std::toupper (c2); });
383 return foundIt != it.end ();
385 return (!str || !subString || std::strstr (str, subString) ==
nullptr) ? false :
true;
391 if (!str || !startString.str)
395 if (startStringLen > thisLen)
397 return std::strncmp (str, startString.str, startStringLen - 1) == 0;
405 if (endStringLen > thisLen)
407 return endString ==
UTF8StringView (str + (thisLen - endStringLen));
413 std::istringstream sstream (str);
414 sstream.imbue (std::locale::classic ());
415 sstream.precision (static_cast<std::streamsize> (precision));
424 return static_cast<float>(
toDouble (precision));
430 if (
auto number = toNumber<int64_t> ())
437 inline Optional<T> UTF8StringView::toNumber ()
const 439 static_assert (std::is_arithmetic<T>::value,
"only arithmetic types allowed");
440 std::istringstream sstream (str);
441 sstream.imbue (std::locale::classic ());
444 if (!sstream.fail ())
445 return makeOptional (number);
450 inline bool UTF8StringView::operator== (
const UTF8StringPtr otherString)
const 452 if (str == otherString)
return true;
453 return (str && otherString) ? (std::strcmp (str, otherString) == 0) :
false;
457 inline bool UTF8StringView::operator!= (
const UTF8StringPtr otherString)
const 459 return !(*
this == otherString);
463 inline bool UTF8StringView::operator== (UTF8StringView otherString)
const 465 if (byteCount && otherString.byteCount && *byteCount != *otherString.byteCount)
467 return operator==(otherString.str);
471 inline UTF8StringView::operator
const UTF8StringPtr ()
const 477 template<
typename BaseIterator>
478 inline UTF8CodePointIterator<BaseIterator>& UTF8CodePointIterator<BaseIterator>::operator++ () noexcept
480 auto firstByte = *it;
482 difference_type offset = 1;
484 if (firstByte & kFirstBitMask)
486 if (firstByte & kThirdBitMask)
488 if (firstByte & kFourthBitMask)
503 template<
typename BaseIterator>
504 inline UTF8CodePointIterator<BaseIterator>& UTF8CodePointIterator<BaseIterator>::operator-- () noexcept
507 if (*it & kFirstBitMask)
510 if ((*it & kSecondBitMask) == 0)
513 if ((*it & kSecondBitMask) == 0)
523 template<
typename BaseIterator>
524 inline UTF8CodePointIterator<BaseIterator> UTF8CodePointIterator<BaseIterator>::operator++ (
int) noexcept
532 template<
typename BaseIterator>
533 inline UTF8CodePointIterator<BaseIterator> UTF8CodePointIterator<BaseIterator>::operator-- (
int) noexcept
541 template<
typename BaseIterator>
542 inline bool UTF8CodePointIterator<BaseIterator>::operator== (
const UTF8CodePointIterator<BaseIterator>& other)
const noexcept
544 return it == other.it;
548 template<
typename BaseIterator>
549 inline bool UTF8CodePointIterator<BaseIterator>::operator!= (
const UTF8CodePointIterator<BaseIterator>& other)
const noexcept
551 return it != other.it;
555 template<
typename BaseIterator>
556 inline typename UTF8CodePointIterator<BaseIterator>::CodePoint UTF8CodePointIterator<BaseIterator>::operator* () const noexcept
558 CodePoint codePoint = 0;
560 auto firstByte = *it;
562 if (firstByte & kFirstBitMask)
564 if (firstByte & kThirdBitMask)
566 if (firstByte & kFourthBitMask)
568 codePoint =
static_cast<CodePoint
> ((firstByte & 0x07) << 18);
569 auto secondByte = *(it + 1);
570 codePoint +=
static_cast<CodePoint
> ((secondByte & 0x3f) << 12);
571 auto thirdByte = *(it + 2);
572 codePoint +=
static_cast<CodePoint
> ((thirdByte & 0x3f) << 6);
573 auto fourthByte = *(it + 3);
574 codePoint += (fourthByte & 0x3f);
578 codePoint =
static_cast<CodePoint
> ((firstByte & 0x0f) << 12);
579 auto secondByte = *(it + 1);
580 codePoint +=
static_cast<CodePoint
> ((secondByte & 0x3f) << 6);
581 auto thirdByte = *(it + 2);
582 codePoint +=
static_cast<CodePoint
> ((thirdByte & 0x3f));
587 codePoint =
static_cast<CodePoint
> ((firstByte & 0x1f) << 6);
588 auto secondByte = *(it + 1);
589 codePoint += (secondByte & 0x3f);
594 codePoint =
static_cast<CodePoint
> (firstByte);
601 #endif // __cstring__ bool contains(const UTF8StringPtr subString, bool ignoreCase=false) const
Definition: cstring.h:372
int64_t toInteger() const
Definition: cstring.h:428
size_t calculateCharacterCount() const
Definition: cstring.h:348
bool endsWith(const UTF8StringView &endString) const
Definition: cstring.h:401
Definition: cstring.h:223
double toDouble(uint32_t precision=8) const
Definition: cstring.h:411
size_t calculateByteCount() const
Definition: cstring.h:364
Definition: customcontrols.cpp:8
Definition: cstring.h:128
a view on a null terminated UTF-8 String
Definition: cstring.h:172
bool startsWith(const UTF8StringView &startString) const
Definition: cstring.h:389
holds an UTF8 encoded string and a platform representation of it
Definition: cstring.h:56
Definition: optional.h:18
float toFloat(uint32_t precision=8) const
Definition: cstring.h:422