27 #ifndef __UNICODESAVVYSTRING 28 #define __UNICODESAVVYSTRING 30 #include "PlatformChar.h" 33 #include <boost/static_assert.hpp> 34 #include <adobe/move.hpp> 36 int32 CharOffsetToUTF16Offset(
const UTF16TextChar *buffer, int32 length, int32 charOffset);
37 int UTF16TextCharCompare(
const UTF16TextChar *utf16str1,
const UTF16TextChar *utf16str2);
38 size_t UTF16TextCharLength(
const UTF16TextChar *utf16str);
43 #include "StringStorage.h" 60 typedef int32 size_type;
61 typedef std::ptrdiff_t difference_type;
62 typedef UTF16TextChar code_value;
64 typedef code_value* code_value_iterator;
65 typedef code_value
const* const_code_value_iterator;
67 typedef UTF16TextChar value_type;
74 return fNumChars != fUTF16BufferLength;
93 return fUTF16BufferLength;
103 return (UnicodeBufferIsValid()) ? fStorage->
capacity() : kMaxSmallString;
116 void reserve(size_type newCapacity);
127 void resize(size_type newSize, code_value fill = code_value());
141 std::swap_ranges(lhs.fSmallStorage, lhs.fSmallStorage + kMaxSmallString + 1, rhs.fSmallStorage);
142 std::swap(lhs.fStorage, rhs.fStorage);
143 std::swap(lhs.fUTF16BufferLength, rhs.fUTF16BufferLength);
144 std::swap(lhs.fNumChars, rhs.fNumChars);
177 void Remove(int32 position, CharCounter count);
190 const_code_value_iterator
begin()
const 192 return ConstBuffer();
198 const_code_value_iterator
end()
const 200 return ConstBuffer() + fUTF16BufferLength;
207 fSmallStorage[0] = UTF16TextChar();
228 std::swap(fStorage, other.fStorage);
229 fUTF16BufferLength = other.fUTF16BufferLength;
230 fNumChars = other.fNumChars;
232 if (!fStorage && fUTF16BufferLength)
233 std::copy(other.fSmallStorage, other.fSmallStorage + kMaxSmallString + 1, fSmallStorage);
235 fSmallStorage[0] = code_value();
237 other.fUTF16BufferLength = other.fNumChars = 0;
250 template <
class IteratorType>
252 : fStorage(nil), fUTF16BufferLength(0), fNumChars(0)
254 assign(b, e, nCodePoints);
257 int32 CountChars()
const;
258 int32 CountCharsUtil(
const UTF16TextChar* buffer, int32 bufferLength)
const;
261 void InsertGap(uint32 wordWiseIndex, size_type numberOfSpaces);
262 void RemoveGap(uint32 wordWiseIndex, size_type numberOfSpaces);
265 void InsertUTF16String(
const UTF16TextChar* buf, int32 len, int32 position = 0);
278 void CheckPairedSurrogates(
const UTF16TextChar* buffer, int32 utf16Count)
const;
282 void InvariantCheck()
const;
286 struct InvariantChecker
290 fString.InvariantCheck();
295 fString.InvariantCheck();
310 template <
class IteratorType>
324 code_value
const* s, size_type n2);
337 const UTF16TextChar* ConstBuffer()
const 339 return UnicodeBufferIsValid() ? fStorage->
ConstBuffer() : fSmallStorage;
344 void insert_safe(code_value_iterator i, const_code_value_iterator sb, const_code_value_iterator se);
345 void erase_safe(code_value_iterator b, code_value_iterator e);
346 void replace_safe(code_value_iterator b, code_value_iterator e,
347 const_code_value_iterator sb, const_code_value_iterator se);
350 template <
class InputIterator>
351 void assign_impl(InputIterator b, InputIterator e, size_type nCodePoints, std::input_iterator_tag);
353 template <
class FwdIterator>
354 void assign_impl(FwdIterator b, FwdIterator e, size_type nCodePoints, std::forward_iterator_tag);
360 bool16 UnicodeBufferIsValid()
const 362 return (fStorage ?
true :
false);
365 UTF16TextChar* GetBufferForWriting(size_type size);
370 enum {kMaxSmallString = 23};
371 UTF16TextChar fSmallStorage[kMaxSmallString + 1];
373 size_type fUTF16BufferLength;
381 template <
class IteratorType>
385 typedef typename std::iterator_traits<IteratorType>::value_type iterator_value_type;
386 BOOST_STATIC_ASSERT(
sizeof(iterator_value_type)*CHAR_BIT == 16);
387 typedef typename std::iterator_traits<IteratorType>::iterator_category iterator_category;
390 InvariantChecker checkThis(*
this);
394 assign_impl(b, e, nCodePoints, iterator_category());
399 template <
class InputIterator>
400 inline void UnicodeSavvyString::assign_impl(InputIterator b, InputIterator e, size_type nCodePoints, std::input_iterator_tag)
410 std::copy(b, e, std::back_inserter(temp));
414 template <
class FwdIterator>
415 inline void UnicodeSavvyString::assign_impl(FwdIterator b, FwdIterator e, size_type nCodePoints, std::forward_iterator_tag)
417 const difference_type nCodeValues = std::distance(b, e);
418 ASSERT(nCodeValues >= 0);
421 code_value* buffer = GetBufferForWriting(nCodeValues);
424 std::copy(b, e, buffer);
427 buffer[nCodeValues] = code_value();
429 fUTF16BufferLength = nCodeValues;
432 if (nCodePoints == 0 && nCodeValues)
434 fNumChars = CountCharsUtil(buffer, nCodeValues);
438 ASSERT((size_type)CountCharsUtil(buffer, nCodeValues) == nCodePoints);
439 fNumChars = nCodePoints;
447 return HasMultiWordUnicode() ? CharOffsetToUTF16Offset(ConstBuffer(), fUTF16BufferLength, index) : index;
454 *numUTF16s = fUTF16BufferLength;
456 return ConstBuffer();
465 inline int32 UnicodeSavvyString::CountChars()
const 467 return CountCharsUtil(ConstBuffer(), fUTF16BufferLength);
472 inline UTF16TextChar * UnicodeSavvyString::GetBufferForWriting(UnicodeSavvyString::size_type len)
475 return fStorage ? fStorage->
GetBuffer() : &fSmallStorage[0];