55
edits
m (→Data access) |
|||
| Line 46: | Line 46: | ||
Stringp s = getSomeString(); | Stringp s = getSomeString(); | ||
Stringp s16 = s->getFixedWidthString (String::k16); | Stringp s16 = s->getFixedWidthString(String::k16); | ||
if (!s16) | if (!s16) | ||
return error; | return error; | ||
do16BitBufferOperation ((const wchar*) s16->getData(), s16->length()); | do16BitBufferOperation((const wchar*) s16->getData(), s16->length()); | ||
To retrieve a character string that contains UTF-8 or UTF-16 data, use the methods <tt>toUTF8String()</tt> or <tt>toUTF16String()</tt>. These methods return a character string in UTF-8 or UTF-16 that is | To retrieve a character string that contains UTF-8 or UTF-16 data, use the methods <tt>toUTF8String()</tt> or <tt>toUTF16String()</tt>. These methods return a character string in UTF-8 or UTF-16 that is NUL-terminated. Note that this is not a String instance, but a simple data buffer containing the character data. The <tt>UTF8String</tt> class contains optimized methods that compute a code point index out of a byte index and vice versa, and the <tt>UTF16String</TT> class converts 32-bit characters into surrogate pairs if necessary (and if 32-bit support is enabled). If you do not need NUL-terminated strings, always try to use <tt>getFixedWidthString()</tt>, since the string already may contain characters in the desired width. | ||
String indexing is slower than usual because an index operation first has to determine the starting point of the string data according to the string type, and then add up the correct width. | String indexing is slower than usual because an index operation first has to determine the starting point of the string data according to the string type, and then add up the correct width. Therefore, the index operator has been removed. For single character access, use the <tt>charAt()</tt> method. There is a <tt>StringIndexer</tt> class that removes the first step of the calculation, which greatly speeds up indexed access to a string: | ||
Stringp s = getSomeString(); | Stringp s = getSomeString(); | ||
// slow, but sufficient for single characters | // slow, but sufficient for single characters | ||
wchar ch = ( | wchar ch = s->charAt(0); | ||
// much better for loops; note that -> also is overloaded for quick access to the string | // much better for loops; note that -> also is overloaded for quick access to the string | ||
StringIndexer str (s); | StringIndexer str(s); | ||
for (int32_t i = 0; i < str->length(); i++) | for (int32_t i = 0; i < str->length(); i++) | ||
ch = str[i]; | ch = str[i]; | ||
edits