Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _RTL_USTRING_HXX_
21 : #define _RTL_USTRING_HXX_
22 :
23 : #include "sal/config.h"
24 :
25 : #include <cassert>
26 : #include <ostream>
27 : #include <string.h>
28 :
29 : #include "osl/diagnose.h"
30 : #include <rtl/ustring.h>
31 : #include <rtl/string.hxx>
32 : #include <rtl/stringutils.hxx>
33 : #include <rtl/textenc.h>
34 : #include "sal/log.hxx"
35 :
36 : #ifdef RTL_FAST_STRING
37 : #include <rtl/stringconcat.hxx>
38 : #endif
39 :
40 : #if defined EXCEPTIONS_OFF
41 : #include <stdlib.h>
42 : #else
43 : #include <new>
44 : #endif
45 :
46 : // The unittest uses slightly different code to help check that the proper
47 : // calls are made. The class is put into a different namespace to make
48 : // sure the compiler generates a different (if generating also non-inline)
49 : // copy of the function and does not merge them together. The class
50 : // is "brought" into the proper rtl namespace by a typedef below.
51 : #ifdef RTL_STRING_UNITTEST
52 : #define rtl rtlunittest
53 : #endif
54 :
55 : namespace rtl
56 : {
57 :
58 : #ifdef RTL_STRING_UNITTEST
59 : #undef rtl
60 : #endif
61 :
62 : /* ======================================================================= */
63 :
64 : /**
65 : This String class provides base functionality for C++ like Unicode
66 : character array handling. The advantage of this class is that it
67 : handles all the memory management for you - and it does it
68 : more efficiently. If you assign a string to another string, the
69 : data of both strings are shared (without any copy operation or
70 : memory allocation) as long as you do not change the string. This class
71 : also stores the length of the string, so that many operations are
72 : faster than the C-str-functions.
73 :
74 : This class provides only readonly string handling. So you could create
75 : a string and you could only query the content from this string.
76 : It provides also functionality to change the string, but this results
77 : in every case in a new string instance (in the most cases with a
78 : memory allocation). You don't have functionality to change the
79 : content of the string. If you want to change the string content, then
80 : you should use the OStringBuffer class, which provides these
81 : functionalities and avoids too much memory allocation.
82 :
83 : The design of this class is similar to the string classes in Java so
84 : less people should have understanding problems when they use this class.
85 : */
86 :
87 : class SAL_WARN_UNUSED OUString
88 : {
89 : public:
90 : /// @cond INTERNAL
91 : rtl_uString * pData;
92 : /// @endcond
93 :
94 : private:
95 : class DO_NOT_ACQUIRE{};
96 :
97 4411577 : OUString( rtl_uString * value, SAL_UNUSED_PARAMETER DO_NOT_ACQUIRE * )
98 : {
99 4411577 : pData = value;
100 4411577 : }
101 :
102 : public:
103 : /**
104 : New string containing no characters.
105 : */
106 31773206 : OUString() SAL_THROW(())
107 : {
108 31773206 : pData = 0;
109 31773206 : rtl_uString_new( &pData );
110 31773164 : }
111 :
112 : /**
113 : New string from OUString.
114 :
115 : @param str a OUString.
116 : */
117 49803154 : OUString( const OUString & str ) SAL_THROW(())
118 : {
119 49803154 : pData = str.pData;
120 49803154 : rtl_uString_acquire( pData );
121 49803154 : }
122 :
123 : /**
124 : New string from OUString data.
125 :
126 : @param str a OUString data.
127 : */
128 2362909 : OUString( rtl_uString * str ) SAL_THROW(())
129 : {
130 2362909 : pData = str;
131 2362909 : rtl_uString_acquire( pData );
132 2362909 : }
133 :
134 : /** New OUString from OUString data without acquiring it. Takeover of ownership.
135 :
136 : The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
137 : from other constructors.
138 :
139 : @param str
140 : OUString data
141 : */
142 10056931 : inline OUString( rtl_uString * str, __sal_NoAcquire ) SAL_THROW(())
143 10056931 : { pData = str; }
144 :
145 : /**
146 : New string from a single Unicode character.
147 :
148 : @param value a Unicode character.
149 : */
150 825514 : explicit OUString( sal_Unicode value ) SAL_THROW(())
151 825514 : : pData (0)
152 : {
153 825514 : rtl_uString_newFromStr_WithLength( &pData, &value, 1 );
154 825514 : }
155 :
156 : /**
157 : New string from a Unicode character buffer array.
158 :
159 : @param value a NULL-terminated Unicode character array.
160 : */
161 5353870 : OUString( const sal_Unicode * value ) SAL_THROW(())
162 : {
163 5353870 : pData = 0;
164 5353870 : rtl_uString_newFromStr( &pData, value );
165 5353870 : }
166 :
167 : /**
168 : New string from a Unicode character buffer array.
169 :
170 : @param value a Unicode character array.
171 : @param length the number of character which should be copied.
172 : The character array length must be greater than
173 : or equal to this value.
174 : */
175 671659 : OUString( const sal_Unicode * value, sal_Int32 length ) SAL_THROW(())
176 : {
177 671659 : pData = 0;
178 671659 : rtl_uString_newFromStr_WithLength( &pData, value, length );
179 671659 : }
180 :
181 : /**
182 : New string from an 8-Bit string literal that is expected to contain only
183 : characters in the ASCII set (i.e. first 128 characters). This constructor
184 : allows an efficient and convenient way to create OUString
185 : instances from ASCII literals. When creating strings from data that
186 : is not pure ASCII, it needs to be converted to OUString by explicitly
187 : providing the encoding to use for the conversion.
188 :
189 : If there are any embedded \0's in the string literal, the result is undefined.
190 : Use the overload that explicitly accepts length.
191 :
192 : @param literal the 8-bit ASCII string literal
193 :
194 : @since LibreOffice 3.6
195 : */
196 : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
197 : // Old gcc can try to convert anonymous enums to OUString and give compile error.
198 : // So instead have a variant for const and non-const char[].
199 : template< int N >
200 : OUString( const char (&literal)[ N ] )
201 : {
202 : // Check that the string literal is in fact N - 1 long (no embedded \0's),
203 : // any decent compiler should optimize out calls to strlen with literals.
204 : assert( strlen( literal ) == N - 1 );
205 : pData = 0;
206 : rtl_uString_newFromLiteral( &pData, literal, N - 1, 0 );
207 : #ifdef RTL_STRING_UNITTEST
208 : rtl_string_unittest_const_literal = true;
209 : #endif
210 : }
211 :
212 : /**
213 : * It is an error to call this overload. Strings cannot directly use non-const char[].
214 : * @internal
215 : */
216 : template< int N >
217 : OUString( char (&value)[ N ] )
218 : #ifndef RTL_STRING_UNITTEST
219 : ; // intentionally not implemented
220 : #else
221 : {
222 : (void) value; // unused
223 : pData = 0;
224 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
225 : rtl_string_unittest_invalid_conversion = true;
226 : }
227 : #endif
228 : #else // HAVE_SFINAE_ANONYMOUS_BROKEN
229 : template< typename T >
230 2643186 : OUString( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() )
231 : {
232 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
233 2643186 : pData = 0;
234 2643186 : rtl_uString_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
235 : #ifdef RTL_STRING_UNITTEST
236 60 : rtl_string_unittest_const_literal = true;
237 : #endif
238 2643186 : }
239 :
240 : #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
241 :
242 :
243 : #ifdef RTL_STRING_UNITTEST
244 : /**
245 : * Only used by unittests to detect incorrect conversions.
246 : * @internal
247 : */
248 : template< typename T >
249 42 : OUString( T&, typename internal::ExceptConstCharArrayDetector< T >::Type = internal::Dummy() )
250 : {
251 42 : pData = 0;
252 42 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
253 42 : rtl_string_unittest_invalid_conversion = true;
254 42 : }
255 : /**
256 : * Only used by unittests to detect incorrect conversions.
257 : * @internal
258 : */
259 : template< typename T >
260 4 : OUString( const T&, typename internal::ExceptCharArrayDetector< T >::Type = internal::Dummy() )
261 : {
262 4 : pData = 0;
263 4 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
264 4 : rtl_string_unittest_invalid_conversion = true;
265 4 : }
266 : #endif
267 :
268 : /**
269 : New string from an 8-Bit character buffer array.
270 :
271 : @param value An 8-Bit character array.
272 : @param length The number of character which should be converted.
273 : The 8-Bit character array length must be
274 : greater than or equal to this value.
275 : @param encoding The text encoding from which the 8-Bit character
276 : sequence should be converted.
277 : @param convertFlags Flags which control the conversion.
278 : see RTL_TEXTTOUNICODE_FLAGS_...
279 :
280 : @exception std::bad_alloc is thrown if an out-of-memory condition occurs
281 : */
282 4957989 : OUString( const sal_Char * value, sal_Int32 length,
283 : rtl_TextEncoding encoding,
284 : sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
285 : {
286 4957989 : pData = 0;
287 4957989 : rtl_string2UString( &pData, value, length, encoding, convertFlags );
288 4957989 : if (pData == 0) {
289 : #if defined EXCEPTIONS_OFF
290 : abort();
291 : #else
292 0 : throw std::bad_alloc();
293 : #endif
294 : }
295 4957989 : }
296 :
297 : /** Create a new string from an array of Unicode code points.
298 :
299 : @param codePoints
300 : an array of at least codePointCount code points, which each must be in
301 : the range from 0 to 0x10FFFF, inclusive. May be null if codePointCount
302 : is zero.
303 :
304 : @param codePointCount
305 : the non-negative number of code points.
306 :
307 : @exception std::bad_alloc
308 : is thrown if either an out-of-memory condition occurs or the resulting
309 : number of UTF-16 code units would have been larger than SAL_MAX_INT32.
310 :
311 : @since UDK 3.2.7
312 : */
313 6952 : inline explicit OUString(
314 : sal_uInt32 const * codePoints, sal_Int32 codePointCount):
315 6952 : pData(NULL)
316 : {
317 6952 : rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
318 6952 : if (pData == NULL) {
319 : #if defined EXCEPTIONS_OFF
320 : abort();
321 : #else
322 0 : throw std::bad_alloc();
323 : #endif
324 : }
325 6952 : }
326 :
327 : #ifdef RTL_FAST_STRING
328 : /**
329 : @overload
330 : @internal
331 : */
332 : template< typename T1, typename T2 >
333 881613 : OUString( const OUStringConcat< T1, T2 >& c )
334 : {
335 881613 : const sal_Int32 l = c.length();
336 881613 : pData = rtl_uString_alloc( l );
337 881613 : if (l != 0)
338 : {
339 881609 : sal_Unicode* end = c.addData( pData->buffer );
340 881609 : pData->length = end - pData->buffer;
341 881609 : *end = '\0';
342 : // TODO realloc in case pData->length is noticeably smaller than l?
343 : }
344 881613 : }
345 : #endif
346 :
347 : /**
348 : Release the string data.
349 : */
350 112440645 : ~OUString() SAL_THROW(())
351 : {
352 112440645 : rtl_uString_release( pData );
353 112440645 : }
354 :
355 : /** Provides an OUString const & passing a storage pointer of an
356 : rtl_uString * handle.
357 : It is more convenient to use C++ OUString member functions when dealing
358 : with rtl_uString * handles. Using this function avoids unnecessary
359 : acquire()/release() calls for a temporary OUString object.
360 :
361 : @param ppHandle
362 : pointer to storage
363 : @return
364 : OUString const & based on given storage
365 : */
366 125711 : static inline OUString const & unacquired( rtl_uString * const * ppHandle )
367 125711 : { return * reinterpret_cast< OUString const * >( ppHandle ); }
368 :
369 : /**
370 : Assign a new string.
371 :
372 : @param str a OUString.
373 : */
374 30690745 : OUString & operator=( const OUString & str ) SAL_THROW(())
375 : {
376 30690745 : rtl_uString_assign( &pData, str.pData );
377 30690745 : return *this;
378 : }
379 :
380 : /**
381 : Assign a new string from an 8-Bit string literal that is expected to contain only
382 : characters in the ASCII set (i.e. first 128 characters). This operator
383 : allows an efficient and convenient way to assign OUString
384 : instances from ASCII literals. When assigning strings from data that
385 : is not pure ASCII, it needs to be converted to OUString by explicitly
386 : providing the encoding to use for the conversion.
387 :
388 : @param literal the 8-bit ASCII string literal
389 :
390 : @since LibreOffice 3.6
391 : */
392 : template< typename T >
393 1227485 : typename internal::ConstCharArrayDetector< T, OUString& >::Type operator=( T& literal )
394 : {
395 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
396 1227485 : rtl_uString_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
397 1227485 : return *this;
398 : }
399 :
400 : /**
401 : Append a string to this string.
402 :
403 : @param str a OUString.
404 : */
405 1764162 : OUString & operator+=( const OUString & str ) SAL_THROW(())
406 : {
407 1764162 : rtl_uString_newConcat( &pData, pData, str.pData );
408 1764162 : return *this;
409 : }
410 :
411 : #ifdef RTL_FAST_STRING
412 : /**
413 : @overload
414 : @internal
415 : */
416 : template< typename T1, typename T2 >
417 1157 : OUString& operator+=( const OUStringConcat< T1, T2 >& c )
418 : {
419 1157 : const int l = c.length();
420 1157 : if( l == 0 )
421 0 : return *this;
422 1157 : rtl_uString_ensureCapacity( &pData, pData->length + l );
423 1157 : sal_Unicode* end = c.addData( pData->buffer + pData->length );
424 1157 : *end = '\0';
425 1157 : pData->length = end - pData->buffer;
426 1157 : return *this;
427 : }
428 : #endif
429 :
430 : /**
431 : Returns the length of this string.
432 :
433 : The length is equal to the number of Unicode characters in this string.
434 :
435 : @return the length of the sequence of characters represented by this
436 : object.
437 : */
438 27082525 : sal_Int32 getLength() const SAL_THROW(()) { return pData->length; }
439 :
440 : /**
441 : Checks if a string is empty.
442 :
443 : @return sal_True if the string is empty;
444 : sal_False, otherwise.
445 :
446 : @since LibreOffice 3.4
447 : */
448 7226013 : bool isEmpty() const SAL_THROW(())
449 : {
450 7226013 : return pData->length == 0;
451 : }
452 :
453 : /**
454 : Returns a pointer to the Unicode character buffer for this string.
455 :
456 : It isn't necessarily NULL terminated.
457 :
458 : @return a pointer to the Unicode characters buffer for this object.
459 : */
460 24758496 : const sal_Unicode * getStr() const SAL_THROW(()) { return pData->buffer; }
461 :
462 : /**
463 : Access to individual characters.
464 :
465 : @param index must be non-negative and less than length.
466 :
467 : @return the character at the given index.
468 :
469 : @since LibreOffice 3.5
470 : */
471 17069234 : sal_Unicode operator [](sal_Int32 index) const { return getStr()[index]; }
472 :
473 : /**
474 : Compares two strings.
475 :
476 : The comparison is based on the numeric value of each character in
477 : the strings and return a value indicating their relationship.
478 : This function can't be used for language specific sorting.
479 :
480 : @param str the object to be compared.
481 : @return 0 - if both strings are equal
482 : < 0 - if this string is less than the string argument
483 : > 0 - if this string is greater than the string argument
484 : */
485 53790822 : sal_Int32 compareTo( const OUString & str ) const SAL_THROW(())
486 : {
487 : return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
488 53790822 : str.pData->buffer, str.pData->length );
489 : }
490 :
491 : /**
492 : Compares two strings with a maximum count of characters.
493 :
494 : The comparison is based on the numeric value of each character in
495 : the strings and return a value indicating their relationship.
496 : This function can't be used for language specific sorting.
497 :
498 : @param str the object to be compared.
499 : @param maxLength the maximum count of characters to be compared.
500 : @return 0 - if both strings are equal
501 : < 0 - if this string is less than the string argument
502 : > 0 - if this string is greater than the string argument
503 :
504 : @since UDK 3.2.7
505 : */
506 95220 : sal_Int32 compareTo( const OUString & str, sal_Int32 maxLength ) const SAL_THROW(())
507 : {
508 : return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
509 95220 : str.pData->buffer, str.pData->length, maxLength );
510 : }
511 :
512 : /**
513 : Compares two strings in reverse order.
514 :
515 : The comparison is based on the numeric value of each character in
516 : the strings and return a value indicating their relationship.
517 : This function can't be used for language specific sorting.
518 :
519 : @param str the object to be compared.
520 : @return 0 - if both strings are equal
521 : < 0 - if this string is less than the string argument
522 : > 0 - if this string is greater than the string argument
523 : */
524 0 : sal_Int32 reverseCompareTo( const OUString & str ) const SAL_THROW(())
525 : {
526 : return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
527 0 : str.pData->buffer, str.pData->length );
528 : }
529 :
530 : /**
531 : Perform a comparison of two strings.
532 :
533 : The result is true if and only if second string
534 : represents the same sequence of characters as the first string.
535 : This function can't be used for language specific comparison.
536 :
537 : @param str the object to be compared.
538 : @return sal_True if the strings are equal;
539 : sal_False, otherwise.
540 : */
541 64522259 : sal_Bool equals( const OUString & str ) const SAL_THROW(())
542 : {
543 64522259 : if ( pData->length != str.pData->length )
544 54928337 : return sal_False;
545 9593922 : if ( pData == str.pData )
546 3970400 : return sal_True;
547 : return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
548 5623522 : str.pData->buffer, str.pData->length ) == 0;
549 : }
550 :
551 : /**
552 : Perform a ASCII lowercase comparison of two strings.
553 :
554 : The result is true if and only if second string
555 : represents the same sequence of characters as the first string,
556 : ignoring the case.
557 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
558 : values between 97 and 122 (ASCII a-z).
559 : This function can't be used for language specific comparison.
560 :
561 : @param str the object to be compared.
562 : @return sal_True if the strings are equal;
563 : sal_False, otherwise.
564 : */
565 39917 : sal_Bool equalsIgnoreAsciiCase( const OUString & str ) const SAL_THROW(())
566 : {
567 39917 : if ( pData->length != str.pData->length )
568 12300 : return sal_False;
569 27617 : if ( pData == str.pData )
570 9056 : return sal_True;
571 : return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
572 18561 : str.pData->buffer, str.pData->length ) == 0;
573 : }
574 :
575 : /**
576 : Perform a ASCII lowercase comparison of two strings.
577 :
578 : Compare the two strings with uppercase ASCII
579 : character values between 65 and 90 (ASCII A-Z) interpreted as
580 : values between 97 and 122 (ASCII a-z).
581 : This function can't be used for language specific comparison.
582 :
583 : @param str the object to be compared.
584 : @return 0 - if both strings are equal
585 : < 0 - if this string is less than the string argument
586 : > 0 - if this string is greater than the string argument
587 :
588 : @since LibreOffice 4.0
589 : */
590 6 : sal_Int32 compareToIgnoreAsciiCase( const OUString & str ) const SAL_THROW(())
591 : {
592 : return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
593 6 : str.pData->buffer, str.pData->length );
594 : }
595 :
596 :
597 : /**
598 : @overload
599 : This function accepts an ASCII string literal as its argument.
600 : @since LibreOffice 3.6
601 : */
602 : template< typename T >
603 20537 : typename internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const SAL_THROW(())
604 : {
605 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
606 20537 : if ( pData->length != internal::ConstCharArrayDetector< T, void >::size - 1 )
607 20219 : return sal_False;
608 :
609 318 : return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, literal ) == 0;
610 : }
611 :
612 : /**
613 : Match against a substring appearing in this string.
614 :
615 : The result is true if and only if the second string appears as a substring
616 : of this string, at the given position.
617 : This function can't be used for language specific comparison.
618 :
619 : @param str the object (substring) to be compared.
620 : @param fromIndex the index to start the comparion from.
621 : The index must be greater than or equal to 0
622 : and less or equal as the string length.
623 : @return sal_True if str match with the characters in the string
624 : at the given position;
625 : sal_False, otherwise.
626 : */
627 2265568 : sal_Bool match( const OUString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
628 : {
629 2265568 : return rtl_ustr_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
630 4531136 : str.pData->buffer, str.pData->length, str.pData->length ) == 0;
631 : }
632 :
633 : /**
634 : @overload
635 : This function accepts an ASCII string literal as its argument.
636 : @since LibreOffice 3.6
637 : */
638 : template< typename T >
639 66141 : typename internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
640 : {
641 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
642 : return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
643 66141 : literal, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
644 : }
645 :
646 : /**
647 : Match against a substring appearing in this string, ignoring the case of
648 : ASCII letters.
649 :
650 : The result is true if and only if the second string appears as a substring
651 : of this string, at the given position.
652 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
653 : values between 97 and 122 (ASCII a-z).
654 : This function can't be used for language specific comparison.
655 :
656 : @param str the object (substring) to be compared.
657 : @param fromIndex the index to start the comparion from.
658 : The index must be greater than or equal to 0
659 : and less than or equal to the string length.
660 : @return sal_True if str match with the characters in the string
661 : at the given position;
662 : sal_False, otherwise.
663 : */
664 6652 : sal_Bool matchIgnoreAsciiCase( const OUString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
665 : {
666 6652 : return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
667 : str.pData->buffer, str.pData->length,
668 13304 : str.pData->length ) == 0;
669 : }
670 :
671 : /**
672 : @overload
673 : This function accepts an ASCII string literal as its argument.
674 : @since LibreOffice 3.6
675 : */
676 : template< typename T >
677 228 : typename internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
678 : {
679 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
680 : return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
681 228 : literal, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
682 : }
683 :
684 : /**
685 : Compares two strings.
686 :
687 : The comparison is based on the numeric value of each character in
688 : the strings and return a value indicating their relationship.
689 : Since this method is optimized for performance, the ASCII character
690 : values are not converted in any way. The caller has to make sure that
691 : all ASCII characters are in the allowed range between 0 and
692 : 127. The ASCII string must be NULL-terminated.
693 : This function can't be used for language specific sorting.
694 :
695 : @param asciiStr the 8-Bit ASCII character string to be compared.
696 : @return 0 - if both strings are equal
697 : < 0 - if this string is less than the string argument
698 : > 0 - if this string is greater than the string argument
699 : */
700 957837 : sal_Int32 compareToAscii( const sal_Char* asciiStr ) const SAL_THROW(())
701 : {
702 957837 : return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length, asciiStr );
703 : }
704 :
705 : /**
706 : Compares two strings with a maximum count of characters.
707 :
708 : The comparison is based on the numeric value of each character in
709 : the strings and return a value indicating their relationship.
710 : Since this method is optimized for performance, the ASCII character
711 : values are not converted in any way. The caller has to make sure that
712 : all ASCII characters are in the allowed range between 0 and
713 : 127. The ASCII string must be NULL-terminated.
714 : This function can't be used for language specific sorting.
715 :
716 : @param asciiStr the 8-Bit ASCII character string to be compared.
717 : @param maxLength the maximum count of characters to be compared.
718 : @return 0 - if both strings are equal
719 : < 0 - if this string is less than the string argument
720 : > 0 - if this string is greater than the string argument
721 : */
722 24087 : sal_Int32 compareToAscii( const sal_Char * asciiStr, sal_Int32 maxLength ) const SAL_THROW(())
723 : {
724 : return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer, pData->length,
725 24087 : asciiStr, maxLength );
726 : }
727 :
728 : /**
729 : Compares two strings in reverse order.
730 :
731 : This could be useful, if normally both strings start with the same
732 : content. The comparison is based on the numeric value of each character
733 : in the strings and return a value indicating their relationship.
734 : Since this method is optimized for performance, the ASCII character
735 : values are not converted in any way. The caller has to make sure that
736 : all ASCII characters are in the allowed range between 0 and 127.
737 : The ASCII string must be NULL-terminated and must be greater than
738 : or equal to asciiStrLength.
739 : This function can't be used for language specific sorting.
740 :
741 : @param asciiStr the 8-Bit ASCII character string to be compared.
742 : @param asciiStrLength the length of the ascii string
743 : @return 0 - if both strings are equal
744 : < 0 - if this string is less than the string argument
745 : > 0 - if this string is greater than the string argument
746 : */
747 67661 : sal_Int32 reverseCompareToAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const SAL_THROW(())
748 : {
749 : return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
750 67661 : asciiStr, asciiStrLength );
751 : }
752 :
753 : /**
754 : Perform a comparison of two strings.
755 :
756 : The result is true if and only if second string
757 : represents the same sequence of characters as the first string.
758 : Since this method is optimized for performance, the ASCII character
759 : values are not converted in any way. The caller has to make sure that
760 : all ASCII characters are in the allowed range between 0 and
761 : 127. The ASCII string must be NULL-terminated.
762 : This function can't be used for language specific comparison.
763 :
764 : @param asciiStr the 8-Bit ASCII character string to be compared.
765 : @return sal_True if the strings are equal;
766 : sal_False, otherwise.
767 : */
768 6695963 : sal_Bool equalsAscii( const sal_Char* asciiStr ) const SAL_THROW(())
769 : {
770 : return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length,
771 6695963 : asciiStr ) == 0;
772 : }
773 :
774 : /**
775 : Perform a comparison of two strings.
776 :
777 : The result is true if and only if second string
778 : represents the same sequence of characters as the first string.
779 : Since this method is optimized for performance, the ASCII character
780 : values are not converted in any way. The caller has to make sure that
781 : all ASCII characters are in the allowed range between 0 and
782 : 127. The ASCII string must be NULL-terminated and must be greater than
783 : or equal to asciiStrLength.
784 : This function can't be used for language specific comparison.
785 :
786 : @param asciiStr the 8-Bit ASCII character string to be compared.
787 : @param asciiStrLength the length of the ascii string
788 : @return sal_True if the strings are equal;
789 : sal_False, otherwise.
790 : */
791 12305066 : sal_Bool equalsAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength ) const SAL_THROW(())
792 : {
793 12305066 : if ( pData->length != asciiStrLength )
794 8740170 : return sal_False;
795 :
796 : return rtl_ustr_asciil_reverseEquals_WithLength(
797 3564896 : pData->buffer, asciiStr, asciiStrLength );
798 : }
799 :
800 : /**
801 : Perform a ASCII lowercase comparison of two strings.
802 :
803 : The result is true if and only if second string
804 : represents the same sequence of characters as the first string,
805 : ignoring the case.
806 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
807 : values between 97 and 122 (ASCII a-z).
808 : Since this method is optimized for performance, the ASCII character
809 : values are not converted in any way. The caller has to make sure that
810 : all ASCII characters are in the allowed range between 0 and
811 : 127. The ASCII string must be NULL-terminated.
812 : This function can't be used for language specific comparison.
813 :
814 : @param asciiStr the 8-Bit ASCII character string to be compared.
815 : @return sal_True if the strings are equal;
816 : sal_False, otherwise.
817 : */
818 291144 : sal_Bool equalsIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const SAL_THROW(())
819 : {
820 291144 : return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
821 : }
822 :
823 : /**
824 : Compares two ASCII strings ignoring case
825 :
826 : The comparison is based on the numeric value of each character in
827 : the strings and return a value indicating their relationship.
828 : Since this method is optimized for performance, the ASCII character
829 : values are not converted in any way. The caller has to make sure that
830 : all ASCII characters are in the allowed range between 0 and
831 : 127. The ASCII string must be NULL-terminated.
832 : This function can't be used for language specific sorting.
833 :
834 : @param asciiStr the 8-Bit ASCII character string to be compared.
835 : @return 0 - if both strings are equal
836 : < 0 - if this string is less than the string argument
837 : > 0 - if this string is greater than the string argument
838 :
839 : @since LibreOffice 3.5
840 : */
841 69176 : sal_Int32 compareToIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const SAL_THROW(())
842 : {
843 69176 : return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr );
844 : }
845 :
846 : /**
847 : Perform an ASCII lowercase comparison of two strings.
848 :
849 : The result is true if and only if second string
850 : represents the same sequence of characters as the first string,
851 : ignoring the case.
852 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
853 : values between 97 and 122 (ASCII a-z).
854 : Since this method is optimized for performance, the ASCII character
855 : values are not converted in any way. The caller has to make sure that
856 : all ASCII characters are in the allowed range between 0 and 127.
857 : The ASCII string must be NULL-terminated and must be greater than
858 : or equal to asciiStrLength.
859 : This function can't be used for language specific comparison.
860 :
861 : @param asciiStr the 8-Bit ASCII character string to be compared.
862 : @param asciiStrLength the length of the ascii string
863 : @return sal_True if the strings are equal;
864 : sal_False, otherwise.
865 : */
866 224961 : sal_Bool equalsIgnoreAsciiCaseAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const SAL_THROW(())
867 : {
868 224961 : if ( pData->length != asciiStrLength )
869 112182 : return sal_False;
870 :
871 112779 : return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
872 : }
873 :
874 : /**
875 : Match against a substring appearing in this string.
876 :
877 : The result is true if and only if the second string appears as a substring
878 : of this string, at the given position.
879 : Since this method is optimized for performance, the ASCII character
880 : values are not converted in any way. The caller has to make sure that
881 : all ASCII characters are in the allowed range between 0 and
882 : 127. The ASCII string must be NULL-terminated and must be greater than or
883 : equal to asciiStrLength.
884 : This function can't be used for language specific comparison.
885 :
886 : @param asciiStr the object (substring) to be compared.
887 : @param asciiStrLength the length of asciiStr.
888 : @param fromIndex the index to start the comparion from.
889 : The index must be greater than or equal to 0
890 : and less than or equal to the string length.
891 : @return sal_True if str match with the characters in the string
892 : at the given position;
893 : sal_False, otherwise.
894 : */
895 84431 : sal_Bool matchAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
896 : {
897 84431 : return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
898 168862 : asciiStr, asciiStrLength ) == 0;
899 : }
900 :
901 : // This overload is left undefined, to detect calls of matchAsciiL that
902 : // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
903 : // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
904 : // platforms):
905 : #if SAL_TYPES_SIZEOFLONG == 8
906 : void matchAsciiL(char const *, sal_Int32, rtl_TextEncoding) const;
907 : #endif
908 :
909 : /**
910 : Match against a substring appearing in this string, ignoring the case of
911 : ASCII letters.
912 :
913 : The result is true if and only if the second string appears as a substring
914 : of this string, at the given position.
915 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
916 : values between 97 and 122 (ASCII a-z).
917 : Since this method is optimized for performance, the ASCII character
918 : values are not converted in any way. The caller has to make sure that
919 : all ASCII characters are in the allowed range between 0 and
920 : 127. The ASCII string must be NULL-terminated and must be greater than or
921 : equal to asciiStrLength.
922 : This function can't be used for language specific comparison.
923 :
924 : @param asciiStr the 8-Bit ASCII character string to be compared.
925 : @param asciiStrLength the length of the ascii string
926 : @param fromIndex the index to start the comparion from.
927 : The index must be greater than or equal to 0
928 : and less than or equal to the string length.
929 : @return sal_True if str match with the characters in the string
930 : at the given position;
931 : sal_False, otherwise.
932 : */
933 692 : sal_Bool matchIgnoreAsciiCaseAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
934 : {
935 692 : return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
936 1384 : asciiStr, asciiStrLength ) == 0;
937 : }
938 :
939 : // This overload is left undefined, to detect calls of
940 : // matchIgnoreAsciiCaseAsciiL that erroneously use
941 : // RTL_CONSTASCII_USTRINGPARAM instead of RTL_CONSTASCII_STRINGPARAM (but
942 : // would lead to ambiguities on 32 bit platforms):
943 : #if SAL_TYPES_SIZEOFLONG == 8
944 : void matchIgnoreAsciiCaseAsciiL(char const *, sal_Int32, rtl_TextEncoding)
945 : const;
946 : #endif
947 :
948 : /**
949 : Check whether this string starts with a given substring.
950 :
951 : @param str the substring to be compared
952 :
953 : @return true if and only if the given str appears as a substring at the
954 : start of this string
955 :
956 : @since LibreOffice 4.0
957 : */
958 2118956 : bool startsWith(OUString const & str) const {
959 2118956 : return match(str, 0);
960 : }
961 :
962 : /**
963 : @overload
964 : This function accepts an ASCII string literal as its argument.
965 : @since LibreOffice 4.0
966 : */
967 : template< typename T >
968 56 : typename internal::ConstCharArrayDetector< T, bool >::Type startsWith( T& literal ) const
969 : {
970 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
971 : return internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
972 : && rtl_ustr_asciil_reverseEquals_WithLength( pData->buffer, literal,
973 56 : internal::ConstCharArrayDetector< T, void >::size - 1);
974 : }
975 :
976 : /**
977 : Check whether this string starts with a given string, ignoring the case of
978 : ASCII letters.
979 :
980 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
981 : values between 97 and 122 (ASCII a-z).
982 : This function can't be used for language specific comparison.
983 :
984 : @param str the object (substring) to be compared.
985 : @return true if this string starts with str, ignoring the case of ASCII
986 : letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
987 : @since LibreOffice 4.0
988 : */
989 : sal_Bool startsWithIgnoreAsciiCase( const OUString & str ) const SAL_THROW(())
990 : {
991 : return matchIgnoreAsciiCase(str, 0);
992 : }
993 :
994 : /**
995 : @overload
996 : This function accepts an ASCII string literal as its argument.
997 : @since LibreOffice 4.0
998 : */
999 : template< typename T >
1000 2 : typename internal::ConstCharArrayDetector< T, bool >::Type startsWithIgnoreAsciiCase( T& literal ) const SAL_THROW(())
1001 : {
1002 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1003 : return (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1004 : pData->buffer,
1005 : internal::ConstCharArrayDetector< T, void >::size - 1, literal,
1006 : internal::ConstCharArrayDetector< T, void >::size - 1)
1007 2 : == 0);
1008 : }
1009 :
1010 : /**
1011 : Check whether this string ends with a given substring.
1012 :
1013 : @param str the substring to be compared
1014 :
1015 : @return true if and only if the given str appears as a substring at the
1016 : end of this string
1017 :
1018 : @since LibreOffice 3.6
1019 : */
1020 10 : bool endsWith(OUString const & str) const {
1021 10 : return str.getLength() <= getLength()
1022 10 : && match(str, getLength() - str.getLength());
1023 : }
1024 :
1025 : /**
1026 : @overload
1027 : This function accepts an ASCII string literal as its argument.
1028 : @since LibreOffice 3.6
1029 : */
1030 : template< typename T >
1031 98 : typename internal::ConstCharArrayDetector< T, bool >::Type endsWith( T& literal ) const
1032 : {
1033 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1034 : return internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
1035 : && rtl_ustr_asciil_reverseEquals_WithLength(
1036 : pData->buffer + pData->length - ( internal::ConstCharArrayDetector< T, void >::size - 1 ), literal,
1037 98 : internal::ConstCharArrayDetector< T, void >::size - 1);
1038 : }
1039 :
1040 : /**
1041 : Check whether this string ends with a given ASCII string.
1042 :
1043 : @param asciiStr a sequence of at least asciiStrLength ASCII characters
1044 : (bytes in the range 0x00--0x7F)
1045 : @param asciiStrLength the length of asciiStr; must be non-negative
1046 : @return true if this string ends with asciiStr; otherwise, false is
1047 : returned
1048 :
1049 : @since UDK 3.2.7
1050 : */
1051 1912 : inline bool endsWithAsciiL(char const * asciiStr, sal_Int32 asciiStrLength)
1052 : const
1053 : {
1054 : return asciiStrLength <= pData->length
1055 : && rtl_ustr_asciil_reverseEquals_WithLength(
1056 1912 : pData->buffer + pData->length - asciiStrLength, asciiStr,
1057 3824 : asciiStrLength);
1058 : }
1059 :
1060 : /**
1061 : Check whether this string ends with a given string, ignoring the case of
1062 : ASCII letters.
1063 :
1064 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
1065 : values between 97 and 122 (ASCII a-z).
1066 : This function can't be used for language specific comparison.
1067 :
1068 : @param str the object (substring) to be compared.
1069 : @return true if this string ends with str, ignoring the case of ASCII
1070 : letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
1071 : @since LibreOffice 3.6
1072 : */
1073 6270 : sal_Bool endsWithIgnoreAsciiCase( const OUString & str ) const SAL_THROW(())
1074 : {
1075 6270 : return str.getLength() <= getLength()
1076 6270 : && matchIgnoreAsciiCase(str, getLength() - str.getLength());
1077 : }
1078 :
1079 : /**
1080 : @overload
1081 : This function accepts an ASCII string literal as its argument.
1082 : @since LibreOffice 3.6
1083 : */
1084 : template< typename T >
1085 2 : typename internal::ConstCharArrayDetector< T, bool >::Type endsWithIgnoreAsciiCase( T& literal ) const SAL_THROW(())
1086 : {
1087 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1088 : return internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
1089 : && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1090 : pData->buffer + pData->length - ( internal::ConstCharArrayDetector< T, void >::size - 1 ),
1091 : internal::ConstCharArrayDetector< T, void >::size - 1, literal,
1092 : internal::ConstCharArrayDetector< T, void >::size - 1)
1093 2 : == 0);
1094 : }
1095 :
1096 : /**
1097 : Check whether this string ends with a given ASCII string, ignoring the
1098 : case of ASCII letters.
1099 :
1100 : @param asciiStr a sequence of at least asciiStrLength ASCII characters
1101 : (bytes in the range 0x00--0x7F)
1102 : @param asciiStrLength the length of asciiStr; must be non-negative
1103 : @return true if this string ends with asciiStr, ignoring the case of ASCII
1104 : letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
1105 : */
1106 271120 : inline bool endsWithIgnoreAsciiCaseAsciiL(
1107 : char const * asciiStr, sal_Int32 asciiStrLength) const
1108 : {
1109 : return asciiStrLength <= pData->length
1110 : && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1111 192369 : pData->buffer + pData->length - asciiStrLength,
1112 192369 : asciiStrLength, asciiStr, asciiStrLength)
1113 463489 : == 0);
1114 : }
1115 :
1116 39218514 : friend sal_Bool operator == ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1117 39218514 : { return rStr1.equals(rStr2); }
1118 0 : friend sal_Bool operator == ( const OUString& rStr1, const sal_Unicode * pStr2 ) SAL_THROW(())
1119 0 : { return rStr1.compareTo( pStr2 ) == 0; }
1120 : friend sal_Bool operator == ( const sal_Unicode * pStr1, const OUString& rStr2 ) SAL_THROW(())
1121 : { return OUString( pStr1 ).compareTo( rStr2 ) == 0; }
1122 :
1123 511923 : friend sal_Bool operator != ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1124 511923 : { return !(operator == ( rStr1, rStr2 )); }
1125 0 : friend sal_Bool operator != ( const OUString& rStr1, const sal_Unicode * pStr2 ) SAL_THROW(())
1126 0 : { return !(operator == ( rStr1, pStr2 )); }
1127 : friend sal_Bool operator != ( const sal_Unicode * pStr1, const OUString& rStr2 ) SAL_THROW(())
1128 : { return !(operator == ( pStr1, rStr2 )); }
1129 :
1130 52020233 : friend sal_Bool operator < ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1131 52020233 : { return rStr1.compareTo( rStr2 ) < 0; }
1132 65064 : friend sal_Bool operator > ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1133 65064 : { return rStr1.compareTo( rStr2 ) > 0; }
1134 0 : friend sal_Bool operator <= ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1135 0 : { return rStr1.compareTo( rStr2 ) <= 0; }
1136 13763 : friend sal_Bool operator >= ( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1137 13763 : { return rStr1.compareTo( rStr2 ) >= 0; }
1138 :
1139 : /**
1140 : * Compare string to an ASCII string literal.
1141 : *
1142 : * This operator is equal to calling equalsAsciiL().
1143 : *
1144 : * @since LibreOffice 3.6
1145 : */
1146 : template< typename T >
1147 11289820 : friend inline typename internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& string, T& literal )
1148 : {
1149 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1150 11289820 : return string.equalsAsciiL( literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
1151 : }
1152 : /**
1153 : * Compare string to an ASCII string literal.
1154 : *
1155 : * This operator is equal to calling equalsAsciiL().
1156 : *
1157 : * @since LibreOffice 3.6
1158 : */
1159 : template< typename T >
1160 2 : friend inline typename internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& string )
1161 : {
1162 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1163 2 : return string.equalsAsciiL( literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
1164 : }
1165 : /**
1166 : * Compare string to an ASCII string literal.
1167 : *
1168 : * This operator is equal to calling !equalsAsciiL().
1169 : *
1170 : * @since LibreOffice 3.6
1171 : */
1172 : template< typename T >
1173 336528 : friend inline typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& string, T& literal )
1174 : {
1175 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1176 336528 : return !string.equalsAsciiL( literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
1177 : }
1178 : /**
1179 : * Compare string to an ASCII string literal.
1180 : *
1181 : * This operator is equal to calling !equalsAsciiL().
1182 : *
1183 : * @since LibreOffice 3.6
1184 : */
1185 : template< typename T >
1186 2 : friend inline typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& string )
1187 : {
1188 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1189 2 : return !string.equalsAsciiL( literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
1190 : }
1191 :
1192 : /**
1193 : Returns a hashcode for this string.
1194 :
1195 : @return a hash code value for this object.
1196 :
1197 : @see rtl::OUStringHash for convenient use of boost::unordered_map
1198 : */
1199 5395615 : sal_Int32 hashCode() const SAL_THROW(())
1200 : {
1201 5395615 : return rtl_ustr_hashCode_WithLength( pData->buffer, pData->length );
1202 : }
1203 :
1204 : /**
1205 : Returns the index within this string of the first occurrence of the
1206 : specified character, starting the search at the specified index.
1207 :
1208 : @param ch character to be located.
1209 : @param fromIndex the index to start the search from.
1210 : The index must be greater than or equal to 0
1211 : and less than or equal to the string length.
1212 : @return the index of the first occurrence of the character in the
1213 : character sequence represented by this string that is
1214 : greater than or equal to fromIndex, or
1215 : -1 if the character does not occur.
1216 : */
1217 1569953 : sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1218 : {
1219 1569953 : sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1220 1569953 : return (ret < 0 ? ret : ret+fromIndex);
1221 : }
1222 :
1223 : /**
1224 : Returns the index within this string of the last occurrence of the
1225 : specified character, searching backward starting at the end.
1226 :
1227 : @param ch character to be located.
1228 : @return the index of the last occurrence of the character in the
1229 : character sequence represented by this string, or
1230 : -1 if the character does not occur.
1231 : */
1232 319762 : sal_Int32 lastIndexOf( sal_Unicode ch ) const SAL_THROW(())
1233 : {
1234 319762 : return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1235 : }
1236 :
1237 : /**
1238 : Returns the index within this string of the last occurrence of the
1239 : specified character, searching backward starting before the specified
1240 : index.
1241 :
1242 : @param ch character to be located.
1243 : @param fromIndex the index before which to start the search.
1244 : @return the index of the last occurrence of the character in the
1245 : character sequence represented by this string that
1246 : is less than fromIndex, or -1
1247 : if the character does not occur before that point.
1248 : */
1249 7246 : sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const SAL_THROW(())
1250 : {
1251 7246 : return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1252 : }
1253 :
1254 : /**
1255 : Returns the index within this string of the first occurrence of the
1256 : specified substring, starting at the specified index.
1257 :
1258 : If str doesn't include any character, always -1 is
1259 : returned. This is also the case, if both strings are empty.
1260 :
1261 : @param str the substring to search for.
1262 : @param fromIndex the index to start the search from.
1263 : @return If the string argument occurs one or more times as a substring
1264 : within this string at the starting index, then the index
1265 : of the first character of the first such substring is
1266 : returned. If it does not occur as a substring starting
1267 : at fromIndex or beyond, -1 is returned.
1268 : */
1269 3493810 : sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1270 : {
1271 3493810 : sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1272 6987620 : str.pData->buffer, str.pData->length );
1273 3493810 : return (ret < 0 ? ret : ret+fromIndex);
1274 : }
1275 :
1276 : /**
1277 : @overload
1278 : This function accepts an ASCII string literal as its argument.
1279 : @since LibreOffice 3.6
1280 : */
1281 : template< typename T >
1282 71068 : typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1283 : {
1284 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1285 : sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1286 : pData->buffer + fromIndex, pData->length - fromIndex, literal,
1287 71068 : internal::ConstCharArrayDetector< T, void >::size - 1);
1288 71068 : return ret < 0 ? ret : ret + fromIndex;
1289 : }
1290 :
1291 : /**
1292 : Returns the index within this string of the first occurrence of the
1293 : specified ASCII substring, starting at the specified index.
1294 :
1295 : @param str
1296 : the substring to be searched for. Need not be null-terminated, but must
1297 : be at least as long as the specified len. Must only contain characters
1298 : in the ASCII range 0x00--7F.
1299 :
1300 : @param len
1301 : the length of the substring; must be non-negative.
1302 :
1303 : @param fromIndex
1304 : the index to start the search from. Must be in the range from zero to
1305 : the length of this string, inclusive.
1306 :
1307 : @return
1308 : the index (starting at 0) of the first character of the first occurrence
1309 : of the substring within this string starting at the given fromIndex, or
1310 : -1 if the substring does not occur. If len is zero, -1 is returned.
1311 :
1312 : @since UDK 3.2.7
1313 : */
1314 330 : sal_Int32 indexOfAsciiL(
1315 : char const * str, sal_Int32 len, sal_Int32 fromIndex = 0) const
1316 : SAL_THROW(())
1317 : {
1318 : sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1319 330 : pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1320 330 : return ret < 0 ? ret : ret + fromIndex;
1321 : }
1322 :
1323 : // This overload is left undefined, to detect calls of indexOfAsciiL that
1324 : // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1325 : // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1326 : // platforms):
1327 : #if SAL_TYPES_SIZEOFLONG == 8
1328 : void indexOfAsciiL(char const *, sal_Int32 len, rtl_TextEncoding) const;
1329 : #endif
1330 :
1331 : /**
1332 : Returns the index within this string of the last occurrence of
1333 : the specified substring, searching backward starting at the end.
1334 :
1335 : The returned index indicates the starting index of the substring
1336 : in this string.
1337 : If str doesn't include any character, always -1 is
1338 : returned. This is also the case, if both strings are empty.
1339 :
1340 : @param str the substring to search for.
1341 : @return If the string argument occurs one or more times as a substring
1342 : within this string, then the index of the first character of
1343 : the last such substring is returned. If it does not occur as
1344 : a substring, -1 is returned.
1345 : */
1346 64786 : sal_Int32 lastIndexOf( const OUString & str ) const SAL_THROW(())
1347 : {
1348 : return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1349 64786 : str.pData->buffer, str.pData->length );
1350 : }
1351 :
1352 : /**
1353 : Returns the index within this string of the last occurrence of
1354 : the specified substring, searching backward starting before the specified
1355 : index.
1356 :
1357 : The returned index indicates the starting index of the substring
1358 : in this string.
1359 : If str doesn't include any character, always -1 is
1360 : returned. This is also the case, if both strings are empty.
1361 :
1362 : @param str the substring to search for.
1363 : @param fromIndex the index before which to start the search.
1364 : @return If the string argument occurs one or more times as a substring
1365 : within this string before the starting index, then the index
1366 : of the first character of the last such substring is
1367 : returned. Otherwise, -1 is returned.
1368 : */
1369 0 : sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const SAL_THROW(())
1370 : {
1371 : return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1372 0 : str.pData->buffer, str.pData->length );
1373 : }
1374 :
1375 : /**
1376 : @overload
1377 : This function accepts an ASCII string literal as its argument.
1378 : @since LibreOffice 3.6
1379 : */
1380 : template< typename T >
1381 2 : typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const SAL_THROW(())
1382 : {
1383 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1384 : return rtl_ustr_lastIndexOfAscii_WithLength(
1385 2 : pData->buffer, pData->length, literal, internal::ConstCharArrayDetector< T, void >::size - 1);
1386 : }
1387 :
1388 : /**
1389 : Returns the index within this string of the last occurrence of the
1390 : specified ASCII substring.
1391 :
1392 : @param str
1393 : the substring to be searched for. Need not be null-terminated, but must
1394 : be at least as long as the specified len. Must only contain characters
1395 : in the ASCII range 0x00--7F.
1396 :
1397 : @param len
1398 : the length of the substring; must be non-negative.
1399 :
1400 : @return
1401 : the index (starting at 0) of the first character of the last occurrence
1402 : of the substring within this string, or -1 if the substring does not
1403 : occur. If len is zero, -1 is returned.
1404 :
1405 : @since UDK 3.2.7
1406 : */
1407 0 : sal_Int32 lastIndexOfAsciiL(char const * str, sal_Int32 len) const
1408 : SAL_THROW(())
1409 : {
1410 : return rtl_ustr_lastIndexOfAscii_WithLength(
1411 0 : pData->buffer, pData->length, str, len);
1412 : }
1413 :
1414 : /**
1415 : Returns a new string that is a substring of this string.
1416 :
1417 : The substring begins at the specified beginIndex. If
1418 : beginIndex is negative or be greater than the length of
1419 : this string, behaviour is undefined.
1420 :
1421 : @param beginIndex the beginning index, inclusive.
1422 : @return the specified substring.
1423 : */
1424 324927 : OUString copy( sal_Int32 beginIndex ) const SAL_THROW(())
1425 : {
1426 324927 : rtl_uString *pNew = 0;
1427 324927 : rtl_uString_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1428 324927 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1429 : }
1430 :
1431 : /**
1432 : Returns a new string that is a substring of this string.
1433 :
1434 : The substring begins at the specified beginIndex and contains count
1435 : characters. If either beginIndex or count are negative,
1436 : or beginIndex + count are greater than the length of this string
1437 : then behaviour is undefined.
1438 :
1439 : @param beginIndex the beginning index, inclusive.
1440 : @param count the number of characters.
1441 : @return the specified substring.
1442 : */
1443 1282445 : OUString copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(())
1444 : {
1445 1282445 : rtl_uString *pNew = 0;
1446 1282445 : rtl_uString_newFromSubString( &pNew, pData, beginIndex, count );
1447 1282445 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1448 : }
1449 :
1450 : /**
1451 : Concatenates the specified string to the end of this string.
1452 :
1453 : @param str the string that is concatenated to the end
1454 : of this string.
1455 : @return a string that represents the concatenation of this string
1456 : followed by the string argument.
1457 : */
1458 0 : SAL_WARN_UNUSED_RESULT OUString concat( const OUString & str ) const SAL_THROW(())
1459 : {
1460 0 : rtl_uString* pNew = 0;
1461 0 : rtl_uString_newConcat( &pNew, pData, str.pData );
1462 0 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1463 : }
1464 :
1465 : #ifndef RTL_FAST_STRING
1466 : friend OUString operator+( const OUString& rStr1, const OUString& rStr2 ) SAL_THROW(())
1467 : {
1468 : return rStr1.concat( rStr2 );
1469 : }
1470 : #endif
1471 :
1472 : /**
1473 : Returns a new string resulting from replacing n = count characters
1474 : from position index in this string with newStr.
1475 :
1476 : @param index the replacing index in str.
1477 : The index must be greater than or equal to 0 and
1478 : less than or equal to the length of the string.
1479 : @param count the count of charcters that will be replaced
1480 : The count must be greater than or equal to 0 and
1481 : less than or equal to the length of the string minus index.
1482 : @param newStr the new substring.
1483 : @return the new string.
1484 : */
1485 198546 : SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, const OUString& newStr ) const SAL_THROW(())
1486 : {
1487 198546 : rtl_uString* pNew = 0;
1488 198546 : rtl_uString_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1489 198546 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1490 : }
1491 :
1492 : /**
1493 : Returns a new string resulting from replacing all occurrences of
1494 : oldChar in this string with newChar.
1495 :
1496 : If the character oldChar does not occur in the character sequence
1497 : represented by this object, then the string is assigned with
1498 : str.
1499 :
1500 : @param oldChar the old character.
1501 : @param newChar the new character.
1502 : @return a string derived from this string by replacing every
1503 : occurrence of oldChar with newChar.
1504 : */
1505 124650 : SAL_WARN_UNUSED_RESULT OUString replace( sal_Unicode oldChar, sal_Unicode newChar ) const SAL_THROW(())
1506 : {
1507 124650 : rtl_uString* pNew = 0;
1508 124650 : rtl_uString_newReplace( &pNew, pData, oldChar, newChar );
1509 124650 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1510 : }
1511 :
1512 : /**
1513 : Returns a new string resulting from replacing the first occurrence of a
1514 : given substring with another substring.
1515 :
1516 : @param from the substring to be replaced
1517 :
1518 : @param to the replacing substring
1519 :
1520 : @param[in,out] index pointer to a start index; if the pointer is
1521 : non-null: upon entry to the function, its value is the index into this
1522 : string at which to start searching for the \p from substring, the value
1523 : must be non-negative and not greater than this string's length; upon exiting
1524 : the function its value is the index into this string at which the
1525 : replacement took place or -1 if no replacement took place; if the pointer
1526 : is null, searching always starts at index 0
1527 :
1528 : @since LibreOffice 3.6
1529 : */
1530 16 : SAL_WARN_UNUSED_RESULT OUString replaceFirst(
1531 : OUString const & from, OUString const & to, sal_Int32 * index = 0) const
1532 : {
1533 16 : rtl_uString * s = 0;
1534 16 : sal_Int32 i = 0;
1535 : rtl_uString_newReplaceFirst(
1536 16 : &s, pData, from.pData, to.pData, index == 0 ? &i : index);
1537 16 : return OUString(s, SAL_NO_ACQUIRE);
1538 : }
1539 :
1540 : /**
1541 : Returns a new string resulting from replacing the first occurrence of a
1542 : given substring with another substring.
1543 :
1544 : @param from ASCII string literal, the substring to be replaced
1545 :
1546 : @param to the replacing substring
1547 :
1548 : @param[in,out] index pointer to a start index; if the pointer is
1549 : non-null: upon entry to the function, its value is the index into the this
1550 : string at which to start searching for the \p from substring, the value
1551 : must be non-negative and not greater than this string's length; upon exiting
1552 : the function its value is the index into this string at which the
1553 : replacement took place or -1 if no replacement took place; if the pointer
1554 : is null, searching always starts at index 0
1555 :
1556 : @since LibreOffice 3.6
1557 : */
1558 : template< typename T >
1559 12 : SAL_WARN_UNUSED_RESULT typename internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( T& from, OUString const & to,
1560 : sal_Int32 * index = 0) const
1561 : {
1562 12 : rtl_uString * s = 0;
1563 12 : sal_Int32 i = 0;
1564 : assert( strlen( from ) == internal::ConstCharArrayDetector< T >::size - 1 );
1565 12 : rtl_uString_newReplaceFirstAsciiL(
1566 : &s, pData, from, internal::ConstCharArrayDetector< T, void >::size - 1, to.pData, index == 0 ? &i : index);
1567 12 : return OUString(s, SAL_NO_ACQUIRE);
1568 : }
1569 :
1570 : /**
1571 : Returns a new string resulting from replacing the first occurrence of a
1572 : given substring with another substring.
1573 :
1574 : @param from ASCII string literal, the substring to be replaced
1575 :
1576 : @param to ASCII string literal, the substring to be replaced
1577 :
1578 : @param[in,out] index pointer to a start index; if the pointer is
1579 : non-null: upon entry to the function, its value is the index into the this
1580 : string at which to start searching for the \p from substring, the value
1581 : must be non-negative and not greater than this string's length; upon exiting
1582 : the function its value is the index into this string at which the
1583 : replacement took place or -1 if no replacement took place; if the pointer
1584 : is null, searching always starts at index 0
1585 :
1586 : @since LibreOffice 3.6
1587 : */
1588 : template< typename T1, typename T2 >
1589 : SAL_WARN_UNUSED_RESULT typename internal::ConstCharArrayDetector< T1, typename internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
1590 12 : replaceFirst( T1& from, T2& to, sal_Int32 * index = 0) const
1591 : {
1592 12 : rtl_uString * s = 0;
1593 12 : sal_Int32 i = 0;
1594 : assert( strlen( from ) == internal::ConstCharArrayDetector< T1 >::size - 1 );
1595 : assert( strlen( to ) == internal::ConstCharArrayDetector< T2 >::size - 1 );
1596 12 : rtl_uString_newReplaceFirstAsciiLAsciiL(
1597 : &s, pData, from, internal::ConstCharArrayDetector< T1, void >::size - 1, to,
1598 : internal::ConstCharArrayDetector< T2, void >::size - 1, index == 0 ? &i : index);
1599 12 : return OUString(s, SAL_NO_ACQUIRE);
1600 : }
1601 :
1602 : /**
1603 : Returns a new string resulting from replacing all occurrences of a given
1604 : substring with another substring.
1605 :
1606 : Replacing subsequent occurrences picks up only after a given replacement.
1607 : That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1608 :
1609 : @param from the substring to be replaced
1610 :
1611 : @param to the replacing substring
1612 :
1613 : @param fromIndex the position in the string where we will begin searching
1614 :
1615 : @since LibreOffice 4.0
1616 : */
1617 4633 : SAL_WARN_UNUSED_RESULT OUString replaceAll(
1618 : OUString const & from, OUString const & to, sal_Int32 fromIndex = 0) const
1619 : {
1620 4633 : rtl_uString * s = 0;
1621 4633 : rtl_uString_newReplaceAllFromIndex(&s, pData, from.pData, to.pData, fromIndex);
1622 4633 : return OUString(s, SAL_NO_ACQUIRE);
1623 : }
1624 :
1625 : /**
1626 : Returns a new string resulting from replacing all occurrences of a given
1627 : substring with another substring.
1628 :
1629 : Replacing subsequent occurrences picks up only after a given replacement.
1630 : That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1631 :
1632 : @param from ASCII string literal, the substring to be replaced
1633 :
1634 : @param to the replacing substring
1635 :
1636 : @since LibreOffice 3.6
1637 : */
1638 : template< typename T >
1639 136 : SAL_WARN_UNUSED_RESULT typename internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( T& from, OUString const & to) const
1640 : {
1641 136 : rtl_uString * s = 0;
1642 : assert( strlen( from ) == internal::ConstCharArrayDetector< T >::size - 1 );
1643 136 : rtl_uString_newReplaceAllAsciiL(&s, pData, from, internal::ConstCharArrayDetector< T, void >::size - 1, to.pData);
1644 136 : return OUString(s, SAL_NO_ACQUIRE);
1645 : }
1646 :
1647 : /**
1648 : Returns a new string resulting from replacing all occurrences of a given
1649 : substring with another substring.
1650 :
1651 : Replacing subsequent occurrences picks up only after a given replacement.
1652 : That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1653 :
1654 : @param from ASCII string literal, the substring to be replaced
1655 :
1656 : @param to ASCII string literal, the substring to be replaced
1657 :
1658 : @since LibreOffice 3.6
1659 : */
1660 : template< typename T1, typename T2 >
1661 : SAL_WARN_UNUSED_RESULT typename internal::ConstCharArrayDetector< T1, typename internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
1662 5944 : replaceAll( T1& from, T2& to ) const
1663 : {
1664 5944 : rtl_uString * s = 0;
1665 : assert( strlen( from ) == internal::ConstCharArrayDetector< T1 >::size - 1 );
1666 : assert( strlen( to ) == internal::ConstCharArrayDetector< T2 >::size - 1 );
1667 5944 : rtl_uString_newReplaceAllAsciiLAsciiL(
1668 : &s, pData, from, internal::ConstCharArrayDetector< T1, void >::size - 1,
1669 : to, internal::ConstCharArrayDetector< T2, void >::size - 1);
1670 5944 : return OUString(s, SAL_NO_ACQUIRE);
1671 : }
1672 :
1673 : /**
1674 : Converts from this string all ASCII uppercase characters (65-90)
1675 : to ASCII lowercase characters (97-122).
1676 :
1677 : This function can't be used for language specific conversion.
1678 : If the string doesn't contain characters which must be converted,
1679 : then the new string is assigned with str.
1680 :
1681 : @return the string, converted to ASCII lowercase.
1682 : */
1683 309940 : SAL_WARN_UNUSED_RESULT OUString toAsciiLowerCase() const SAL_THROW(())
1684 : {
1685 309940 : rtl_uString* pNew = 0;
1686 309940 : rtl_uString_newToAsciiLowerCase( &pNew, pData );
1687 309940 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1688 : }
1689 :
1690 : /**
1691 : Converts from this string all ASCII lowercase characters (97-122)
1692 : to ASCII uppercase characters (65-90).
1693 :
1694 : This function can't be used for language specific conversion.
1695 : If the string doesn't contain characters which must be converted,
1696 : then the new string is assigned with str.
1697 :
1698 : @return the string, converted to ASCII uppercase.
1699 : */
1700 240571 : SAL_WARN_UNUSED_RESULT OUString toAsciiUpperCase() const SAL_THROW(())
1701 : {
1702 240571 : rtl_uString* pNew = 0;
1703 240571 : rtl_uString_newToAsciiUpperCase( &pNew, pData );
1704 240571 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1705 : }
1706 :
1707 : /**
1708 : Returns a new string resulting from removing white space from both ends
1709 : of the string.
1710 :
1711 : All characters that have codes less than or equal to
1712 : 32 (the space character) are considered to be white space.
1713 : If the string doesn't contain white spaces at both ends,
1714 : then the new string is assigned with str.
1715 :
1716 : @return the string, with white space removed from the front and end.
1717 : */
1718 65981 : SAL_WARN_UNUSED_RESULT OUString trim() const SAL_THROW(())
1719 : {
1720 65981 : rtl_uString* pNew = 0;
1721 65981 : rtl_uString_newTrim( &pNew, pData );
1722 65981 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
1723 : }
1724 :
1725 : /**
1726 : Returns a token in the string.
1727 :
1728 : Example:
1729 : sal_Int32 nIndex = 0;
1730 : do
1731 : {
1732 : ...
1733 : OUString aToken = aStr.getToken( 0, ';', nIndex );
1734 : ...
1735 : }
1736 : while ( nIndex >= 0 );
1737 :
1738 : @param token the number of the token to return
1739 : @param cTok the character which seperate the tokens.
1740 : @param index the position at which the token is searched in the
1741 : string.
1742 : The index must not be greater than the length of the
1743 : string.
1744 : This param is set to the position of the
1745 : next token or to -1, if it is the last token.
1746 : @return the token; if either token or index is negative, an empty token
1747 : is returned (and index is set to -1)
1748 : */
1749 601021 : OUString getToken( sal_Int32 token, sal_Unicode cTok, sal_Int32& index ) const SAL_THROW(())
1750 : {
1751 601021 : rtl_uString * pNew = 0;
1752 601021 : index = rtl_uString_getToken( &pNew, pData, token, cTok, index );
1753 601021 : return OUString( pNew, (DO_NOT_ACQUIRE *)0 );
1754 : }
1755 :
1756 : /**
1757 : Returns a token from the string.
1758 :
1759 : The same as getToken(sal_Int32, sal_Unicode, sal_Int32 &), but always
1760 : passing in 0 as the start index in the third argument.
1761 :
1762 : @param count the number of the token to return, starting with 0
1763 : @param separator the character which separates the tokens
1764 :
1765 : @return the given token, or an empty string
1766 :
1767 : @since LibreOffice 3.6
1768 : */
1769 1765 : OUString getToken(sal_Int32 count, sal_Unicode separator) const {
1770 1765 : sal_Int32 n = 0;
1771 1765 : return getToken(count, separator, n);
1772 : }
1773 :
1774 : /**
1775 : Returns the Boolean value from this string.
1776 :
1777 : This function can't be used for language specific conversion.
1778 :
1779 : @return sal_True, if the string is 1 or "True" in any ASCII case.
1780 : sal_False in any other case.
1781 : */
1782 424 : sal_Bool toBoolean() const SAL_THROW(())
1783 : {
1784 424 : return rtl_ustr_toBoolean( pData->buffer );
1785 : }
1786 :
1787 : /**
1788 : Returns the first character from this string.
1789 :
1790 : @return the first character from this string or 0, if this string
1791 : is emptry.
1792 : */
1793 7332 : sal_Unicode toChar() const SAL_THROW(())
1794 : {
1795 7332 : return pData->buffer[0];
1796 : }
1797 :
1798 : /**
1799 : Returns the int32 value from this string.
1800 :
1801 : This function can't be used for language specific conversion.
1802 :
1803 : @param radix the radix (between 2 and 36)
1804 : @return the int32 represented from this string.
1805 : 0 if this string represents no number.
1806 : */
1807 118293 : sal_Int32 toInt32( sal_Int16 radix = 10 ) const SAL_THROW(())
1808 : {
1809 118293 : return rtl_ustr_toInt32( pData->buffer, radix );
1810 : }
1811 :
1812 : /**
1813 : Returns the int64 value from this string.
1814 :
1815 : This function can't be used for language specific conversion.
1816 :
1817 : @param radix the radix (between 2 and 36)
1818 : @return the int64 represented from this string.
1819 : 0 if this string represents no number.
1820 : */
1821 656 : sal_Int64 toInt64( sal_Int16 radix = 10 ) const SAL_THROW(())
1822 : {
1823 656 : return rtl_ustr_toInt64( pData->buffer, radix );
1824 : }
1825 :
1826 : /**
1827 : Returns the float value from this string.
1828 :
1829 : This function can't be used for language specific conversion.
1830 :
1831 : @return the float represented from this string.
1832 : 0.0 if this string represents no number.
1833 : */
1834 0 : float toFloat() const SAL_THROW(())
1835 : {
1836 0 : return rtl_ustr_toFloat( pData->buffer );
1837 : }
1838 :
1839 : /**
1840 : Returns the double value from this string.
1841 :
1842 : This function can't be used for language specific conversion.
1843 :
1844 : @return the double represented from this string.
1845 : 0.0 if this string represents no number.
1846 : */
1847 2144 : double toDouble() const SAL_THROW(())
1848 : {
1849 2144 : return rtl_ustr_toDouble( pData->buffer );
1850 : }
1851 :
1852 :
1853 : /**
1854 : Return a canonical representation for a string.
1855 :
1856 : A pool of strings, initially empty is maintained privately
1857 : by the string class. On invocation, if present in the pool
1858 : the original string will be returned. Otherwise this string,
1859 : or a copy thereof will be added to the pool and returned.
1860 :
1861 : @return
1862 : a version of the string from the pool.
1863 :
1864 : @exception std::bad_alloc is thrown if an out-of-memory condition occurs
1865 :
1866 : @since UDK 3.2.7
1867 : */
1868 26254 : OUString intern() const
1869 : {
1870 26254 : rtl_uString * pNew = 0;
1871 26254 : rtl_uString_intern( &pNew, pData );
1872 26254 : if (pNew == 0) {
1873 : #if defined EXCEPTIONS_OFF
1874 : abort();
1875 : #else
1876 0 : throw std::bad_alloc();
1877 : #endif
1878 : }
1879 26254 : return OUString( pNew, (DO_NOT_ACQUIRE *)0 );
1880 : }
1881 :
1882 : /**
1883 : Return a canonical representation for a converted string.
1884 :
1885 : A pool of strings, initially empty is maintained privately
1886 : by the string class. On invocation, if present in the pool
1887 : the original string will be returned. Otherwise this string,
1888 : or a copy thereof will be added to the pool and returned.
1889 :
1890 : @param value a 8-Bit character array.
1891 : @param length the number of character which should be converted.
1892 : The 8-Bit character array length must be
1893 : greater than or equal to this value.
1894 : @param encoding the text encoding from which the 8-Bit character
1895 : sequence should be converted.
1896 : @param convertFlags flags which controls the conversion.
1897 : see RTL_TEXTTOUNICODE_FLAGS_...
1898 : @param pInfo pointer to return conversion status or NULL.
1899 :
1900 : @return
1901 : a version of the converted string from the pool.
1902 :
1903 : @exception std::bad_alloc is thrown if an out-of-memory condition occurs
1904 :
1905 : @since UDK 3.2.7
1906 : */
1907 41077 : static OUString intern( const sal_Char * value, sal_Int32 length,
1908 : rtl_TextEncoding encoding,
1909 : sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
1910 : sal_uInt32 *pInfo = NULL )
1911 : {
1912 41077 : rtl_uString * pNew = 0;
1913 : rtl_uString_internConvert( &pNew, value, length, encoding,
1914 41077 : convertFlags, pInfo );
1915 41077 : if (pNew == 0) {
1916 : #if defined EXCEPTIONS_OFF
1917 : abort();
1918 : #else
1919 0 : throw std::bad_alloc();
1920 : #endif
1921 : }
1922 41077 : return OUString( pNew, (DO_NOT_ACQUIRE *)0 );
1923 : }
1924 :
1925 : /**
1926 : Converts to an OString, signalling failure.
1927 :
1928 : @param pTarget
1929 : An out parameter receiving the converted OString. Must not be null; the
1930 : contents are not modified if conversion fails (convertToOString returns
1931 : false).
1932 :
1933 : @param nEncoding
1934 : The text encoding to convert into. Must be an octet encoding (i.e.,
1935 : rtl_isOctetTextEncoding(nEncoding) must return true).
1936 :
1937 : @param nFlags
1938 : A combination of RTL_UNICODETOTEXT_FLAGS that detail how to do the
1939 : conversion (see rtl_convertUnicodeToText). RTL_UNICODETOTEXT_FLAGS_FLUSH
1940 : need not be included, it is implicitly assumed. Typical uses are either
1941 : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
1942 : RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR (fail if a Unicode character cannot
1943 : be converted to the target nEncoding) or OUSTRING_TO_OSTRING_CVTFLAGS
1944 : (make a best efforts conversion).
1945 :
1946 : @return
1947 : True if the conversion succeeded, false otherwise.
1948 : */
1949 1019 : inline bool convertToString(OString * pTarget, rtl_TextEncoding nEncoding,
1950 : sal_uInt32 nFlags) const
1951 : {
1952 : return rtl_convertUStringToString(&pTarget->pData, pData->buffer,
1953 1019 : pData->length, nEncoding, nFlags);
1954 : }
1955 :
1956 : /** Iterate through this string based on code points instead of UTF-16 code
1957 : units.
1958 :
1959 : See Chapter 3 of The Unicode Standard 5.0 (Addison--Wesley, 2006) for
1960 : definitions of the various terms used in this description.
1961 :
1962 : This string is interpreted as a sequence of zero or more UTF-16 code
1963 : units. For each index into this sequence (from zero to one less than
1964 : the length of the sequence, inclusive), a code point represented
1965 : starting at the given index is computed as follows:
1966 :
1967 : - If the UTF-16 code unit addressed by the index constitutes a
1968 : well-formed UTF-16 code unit sequence, the computed code point is the
1969 : scalar value encoded by that UTF-16 code unit sequence.
1970 :
1971 : - Otherwise, if the index is at least two UTF-16 code units away from
1972 : the end of the sequence, and the sequence of two UTF-16 code units
1973 : addressed by the index constitutes a well-formed UTF-16 code unit
1974 : sequence, the computed code point is the scalar value encoded by that
1975 : UTF-16 code unit sequence.
1976 :
1977 : - Otherwise, the computed code point is the UTF-16 code unit addressed
1978 : by the index. (This last case catches unmatched surrogates as well as
1979 : indices pointing into the middle of surrogate pairs.)
1980 :
1981 : @param indexUtf16
1982 : pointer to a UTF-16 based index into this string; must not be null. On
1983 : entry, the index must be in the range from zero to the length of this
1984 : string (in UTF-16 code units), inclusive. Upon successful return, the
1985 : index will be updated to address the UTF-16 code unit that is the given
1986 : incrementCodePoints away from the initial index.
1987 :
1988 : @param incrementCodePoints
1989 : the number of code points to move the given *indexUtf16. If
1990 : non-negative, moving is done after determining the code point at the
1991 : index. If negative, moving is done before determining the code point
1992 : at the (then updated) index. The value must be such that the resulting
1993 : UTF-16 based index is in the range from zero to the length of this
1994 : string (in UTF-16 code units), inclusive.
1995 :
1996 : @return
1997 : the code point (an integer in the range from 0 to 0x10FFFF, inclusive)
1998 : that is represented within this string starting at the index computed as
1999 : follows: If incrementCodePoints is non-negative, the index is the
2000 : initial value of *indexUtf16; if incrementCodePoints is negative, the
2001 : index is the updated value of *indexUtf16. In either case, the computed
2002 : index must be in the range from zero to one less than the length of this
2003 : string (in UTF-16 code units), inclusive.
2004 :
2005 : @since UDK 3.2.7
2006 : */
2007 1262951 : inline sal_uInt32 iterateCodePoints(
2008 : sal_Int32 * indexUtf16, sal_Int32 incrementCodePoints = 1) const
2009 : {
2010 : return rtl_uString_iterateCodePoints(
2011 1262951 : pData, indexUtf16, incrementCodePoints);
2012 : }
2013 :
2014 : /**
2015 : Returns the string representation of the sal_Bool argument.
2016 :
2017 : If the sal_Bool is true, the string "true" is returned.
2018 : If the sal_Bool is false, the string "false" is returned.
2019 : This function can't be used for language specific conversion.
2020 :
2021 : @param b a sal_Bool.
2022 : @return a string with the string representation of the argument.
2023 : */
2024 0 : static OUString valueOf( sal_Bool b ) SAL_THROW(())
2025 : {
2026 : sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFBOOLEAN];
2027 0 : rtl_uString* pNewData = 0;
2028 0 : rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfBoolean( aBuf, b ) );
2029 0 : return OUString( pNewData, (DO_NOT_ACQUIRE*)0 );
2030 : }
2031 :
2032 : /**
2033 : Returns the string representation of the char argument.
2034 :
2035 : @param c a character.
2036 : @return a string with the string representation of the argument.
2037 : */
2038 1102 : static OUString valueOf( sal_Unicode c ) SAL_THROW(())
2039 : {
2040 1102 : return OUString( &c, 1 );
2041 : }
2042 :
2043 : /**
2044 : Returns the string representation of the int argument.
2045 :
2046 : This function can't be used for language specific conversion.
2047 :
2048 : @param i a int32.
2049 : @param radix the radix (between 2 and 36)
2050 : @return a string with the string representation of the argument.
2051 : */
2052 184207 : static OUString valueOf( sal_Int32 i, sal_Int16 radix = 10 ) SAL_THROW(())
2053 : {
2054 : sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT32];
2055 184207 : rtl_uString* pNewData = 0;
2056 184207 : rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt32( aBuf, i, radix ) );
2057 184207 : return OUString( pNewData, (DO_NOT_ACQUIRE*)0 );
2058 : }
2059 :
2060 : /**
2061 : Returns the string representation of the long argument.
2062 :
2063 : This function can't be used for language specific conversion.
2064 :
2065 : @param ll a int64.
2066 : @param radix the radix (between 2 and 36)
2067 : @return a string with the string representation of the argument.
2068 : */
2069 7935 : static OUString valueOf( sal_Int64 ll, sal_Int16 radix = 10 ) SAL_THROW(())
2070 : {
2071 : sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT64];
2072 7935 : rtl_uString* pNewData = 0;
2073 7935 : rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt64( aBuf, ll, radix ) );
2074 7935 : return OUString( pNewData, (DO_NOT_ACQUIRE*)0 );
2075 : }
2076 :
2077 : /**
2078 : Returns the string representation of the float argument.
2079 :
2080 : This function can't be used for language specific conversion.
2081 :
2082 : @param f a float.
2083 : @return a string with the string representation of the argument.
2084 : */
2085 0 : static OUString valueOf( float f ) SAL_THROW(())
2086 : {
2087 : sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFFLOAT];
2088 0 : rtl_uString* pNewData = 0;
2089 0 : rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfFloat( aBuf, f ) );
2090 0 : return OUString( pNewData, (DO_NOT_ACQUIRE*)0 );
2091 : }
2092 :
2093 : /**
2094 : Returns the string representation of the double argument.
2095 :
2096 : This function can't be used for language specific conversion.
2097 :
2098 : @param d a double.
2099 : @return a string with the string representation of the argument.
2100 : */
2101 2246 : static OUString valueOf( double d ) SAL_THROW(())
2102 : {
2103 : sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFDOUBLE];
2104 2246 : rtl_uString* pNewData = 0;
2105 2246 : rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfDouble( aBuf, d ) );
2106 2246 : return OUString( pNewData, (DO_NOT_ACQUIRE*)0 );
2107 : }
2108 :
2109 : /**
2110 : Returns a OUString copied without conversion from an ASCII
2111 : character string.
2112 :
2113 : Since this method is optimized for performance, the ASCII character
2114 : values are not converted in any way. The caller has to make sure that
2115 : all ASCII characters are in the allowed range between 0 and
2116 : 127. The ASCII string must be NULL-terminated.
2117 :
2118 : Note that for string literals it is simpler and more efficient
2119 : to directly use the OUString constructor.
2120 :
2121 : @param value the 8-Bit ASCII character string
2122 : @return a string with the string representation of the argument.
2123 : */
2124 1001777 : static OUString createFromAscii( const sal_Char * value ) SAL_THROW(())
2125 : {
2126 1001777 : rtl_uString* pNew = 0;
2127 1001777 : rtl_uString_newFromAscii( &pNew, value );
2128 1001777 : return OUString( pNew, (DO_NOT_ACQUIRE*)0 );
2129 : }
2130 : };
2131 :
2132 : /* ======================================================================= */
2133 :
2134 : #ifdef RTL_FAST_STRING
2135 : /**
2136 : A simple wrapper around string literal. It is usually not necessary to use, can
2137 : be mostly used to force OUString operator+ working with operands that otherwise would
2138 : not trigger it.
2139 :
2140 : This class is not part of public API and is meant to be used only in LibreOffice code.
2141 : @since LibreOffice 4.0
2142 : */
2143 : struct SAL_WARN_UNUSED OUStringLiteral
2144 : {
2145 : template< int N >
2146 8 : OUStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
2147 : int size;
2148 : const char* data;
2149 : };
2150 :
2151 : /**
2152 : @internal
2153 : */
2154 : template<>
2155 : struct ToStringHelper< OUString >
2156 : {
2157 2214556 : static int length( const OUString& s ) { return s.getLength(); }
2158 2214548 : static sal_Unicode* addData( sal_Unicode* buffer, const OUString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
2159 : static const bool allowOStringConcat = false;
2160 : static const bool allowOUStringConcat = true;
2161 : };
2162 :
2163 : /**
2164 : @internal
2165 : */
2166 : template<>
2167 : struct ToStringHelper< OUStringLiteral >
2168 : {
2169 6 : static int length( const OUStringLiteral& str ) { return str.size; }
2170 6 : static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataLiteral( buffer, str.data, str.size ); }
2171 : static const bool allowOStringConcat = false;
2172 : static const bool allowOUStringConcat = true;
2173 : };
2174 :
2175 : /**
2176 : @internal
2177 : */
2178 : template< typename charT, typename traits, typename T1, typename T2 >
2179 : inline std::basic_ostream<charT, traits> & operator <<(
2180 : std::basic_ostream<charT, traits> & stream, const OUStringConcat< T1, T2 >& concat)
2181 : {
2182 : return stream << OUString( concat );
2183 : }
2184 : #else
2185 : // non-RTL_FAST_CODE needs this to compile
2186 : typedef OUString OUStringLiteral;
2187 : #endif
2188 :
2189 : /** A helper to use OUStrings with hash maps.
2190 :
2191 : Instances of this class are unary function objects that can be used as
2192 : hash function arguments to boost::unordered_map and similar constructs.
2193 : */
2194 : struct OUStringHash
2195 : {
2196 : /** Compute a hash code for a string.
2197 :
2198 : @param rString
2199 : a string.
2200 :
2201 : @return
2202 : a hash code for the string. This hash code should not be stored
2203 : persistently, as its computation may change in later revisions.
2204 : */
2205 3377635 : size_t operator()(const OUString& rString) const
2206 3377635 : { return (size_t)rString.hashCode(); }
2207 : };
2208 :
2209 : /* ======================================================================= */
2210 :
2211 : /** Convert an OString to an OUString, using a specific text encoding.
2212 :
2213 : The lengths of the two strings may differ (e.g., for double-byte
2214 : encodings, UTF-7, UTF-8).
2215 :
2216 : @param rStr
2217 : an OString to convert.
2218 :
2219 : @param encoding
2220 : the text encoding to use for conversion.
2221 :
2222 : @param convertFlags
2223 : flags which control the conversion. Either use
2224 : OSTRING_TO_OUSTRING_CVTFLAGS, or see
2225 : <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
2226 : details.
2227 : */
2228 425093 : inline OUString OStringToOUString( const OString & rStr,
2229 : rtl_TextEncoding encoding,
2230 : sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
2231 : {
2232 425093 : return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
2233 : }
2234 :
2235 : /** Convert an OUString to an OString, using a specific text encoding.
2236 :
2237 : The lengths of the two strings may differ (e.g., for double-byte
2238 : encodings, UTF-7, UTF-8).
2239 :
2240 : @param rUnicode
2241 : an OUString to convert.
2242 :
2243 : @param encoding
2244 : the text encoding to use for conversion.
2245 :
2246 : @param convertFlags
2247 : flags which control the conversion. Either use
2248 : OUSTRING_TO_OSTRING_CVTFLAGS, or see
2249 : <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
2250 : details.
2251 : */
2252 1658183 : inline OString OUStringToOString( const OUString & rUnicode,
2253 : rtl_TextEncoding encoding,
2254 : sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
2255 : {
2256 1658183 : return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
2257 : }
2258 :
2259 : /* ======================================================================= */
2260 :
2261 : /**
2262 : Support for rtl::OUString in std::ostream (and thus in
2263 : CPPUNIT_ASSERT or SAL_INFO macros, for example).
2264 :
2265 : The rtl::OUString is converted to UTF-8.
2266 :
2267 : @since LibreOffice 3.5.
2268 : */
2269 : template< typename charT, typename traits >
2270 468 : inline std::basic_ostream<charT, traits> & operator <<(
2271 : std::basic_ostream<charT, traits> & stream, OUString const & string)
2272 : {
2273 : return stream <<
2274 468 : OUStringToOString(string, RTL_TEXTENCODING_UTF8).getStr();
2275 : // best effort; potentially loses data due to conversion failures
2276 : // (stray surrogate halves) and embedded null characters
2277 : }
2278 :
2279 : } // namespace
2280 :
2281 : #ifdef RTL_STRING_UNITTEST
2282 : namespace rtl
2283 : {
2284 : typedef rtlunittest::OUString OUString;
2285 : }
2286 : #endif
2287 :
2288 : // RTL_USING is defined by gbuild for all modules except those with stable public API
2289 : // (as listed in ure/source/README). It allows to use classes like OUString without
2290 : // having to explicitly refer to the rtl namespace, which is kind of superfluous
2291 : // given that OUString itself is namespaced by its OU prefix.
2292 : #ifdef RTL_USING
2293 : using ::rtl::OUString;
2294 : using ::rtl::OUStringHash;
2295 : using ::rtl::OStringToOUString;
2296 : using ::rtl::OUStringToOString;
2297 : using ::rtl::OUStringLiteral;
2298 : #endif
2299 :
2300 : #endif /* _RTL_USTRING_HXX */
2301 :
2302 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|