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_STRING_HXX_
21 : #define _RTL_STRING_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/textenc.h>
31 : #include <rtl/string.h>
32 : #include <rtl/stringutils.hxx>
33 :
34 : #ifdef RTL_FAST_STRING
35 : #include <rtl/stringconcat.hxx>
36 : #endif
37 :
38 : #include "sal/log.hxx"
39 :
40 : #if !defined EXCEPTIONS_OFF
41 : #include <new>
42 : #endif
43 :
44 : // The unittest uses slightly different code to help check that the proper
45 : // calls are made. The class is put into a different namespace to make
46 : // sure the compiler generates a different (if generating also non-inline)
47 : // copy of the function and does not merge them together. The class
48 : // is "brought" into the proper rtl namespace by a typedef below.
49 : #ifdef RTL_STRING_UNITTEST
50 : #define rtl rtlunittest
51 : #endif
52 :
53 : namespace rtl
54 : {
55 :
56 : #ifdef RTL_STRING_UNITTEST
57 : #undef rtl
58 : // helper macro to make functions appear more readable
59 : #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
60 : #else
61 : #define RTL_STRING_CONST_FUNCTION
62 : #endif
63 :
64 : /* ======================================================================= */
65 :
66 : /**
67 : This String class provide base functionality for C++ like 8-Bit
68 : character array handling. The advantage of this class is, that it
69 : handle all the memory managament for you - and it do it
70 : more efficient. If you assign a string to another string, the
71 : data of both strings are shared (without any copy operation or
72 : memory allocation) as long as you do not change the string. This class
73 : stores also the length of the string, so that many operations are
74 : faster as the C-str-functions.
75 :
76 : This class provide only readonly string handling. So you could create
77 : a string and you could only query the content from this string.
78 : It provide also functionality to change the string, but this results
79 : in every case in a new string instance (in the most cases with an
80 : memory allocation). You don't have functionality to change the
81 : content of the string. If you want change the string content, than
82 : you should us the OStringBuffer class, which provide these
83 : functionality and avoid to much memory allocation.
84 :
85 : The design of this class is similar to the string classes in Java
86 : and so more people should have fewer understanding problems when they
87 : use this class.
88 : */
89 :
90 : class SAL_WARN_UNUSED OString
91 : {
92 : public:
93 : /// @cond INTERNAL
94 : rtl_String * pData;
95 : /// @endcond
96 :
97 : private:
98 : class DO_NOT_ACQUIRE;
99 :
100 15050589 : OString( rtl_String * value, SAL_UNUSED_PARAMETER DO_NOT_ACQUIRE * )
101 : {
102 15050589 : pData = value;
103 15050589 : }
104 :
105 : public:
106 : /**
107 : New string containing no characters.
108 : */
109 8151794 : OString() SAL_THROW(())
110 : {
111 8151794 : pData = 0;
112 8151794 : rtl_string_new( &pData );
113 8151794 : }
114 :
115 : /**
116 : New string from OString.
117 :
118 : @param str a OString.
119 : */
120 6771060 : OString( const OString & str ) SAL_THROW(())
121 : {
122 6771060 : pData = str.pData;
123 6771060 : rtl_string_acquire( pData );
124 6771060 : }
125 :
126 : /**
127 : New string from OString data.
128 :
129 : @param str a OString data.
130 : */
131 2986185 : OString( rtl_String * str ) SAL_THROW(())
132 : {
133 2986185 : pData = str;
134 2986185 : rtl_string_acquire( pData );
135 2986185 : }
136 :
137 : /** New string from OString data without acquiring it. Takeover of ownership.
138 :
139 : The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
140 : from other constructors.
141 :
142 : @param str a OString data.
143 : */
144 92746 : inline OString( rtl_String * str, __sal_NoAcquire ) SAL_THROW(())
145 : {
146 92746 : pData = str;
147 92746 : }
148 :
149 : /**
150 : New string from a single character.
151 :
152 : @param value a character.
153 : */
154 254152 : explicit OString( sal_Char value ) SAL_THROW(())
155 254152 : : pData (0)
156 : {
157 254152 : rtl_string_newFromStr_WithLength( &pData, &value, 1 );
158 254152 : }
159 :
160 : /**
161 : New string from a character buffer array.
162 :
163 : Note: The argument type is always either char* or const char*. The template is
164 : used only for technical reasons, as is the second argument.
165 :
166 : @param value a NULL-terminated character array.
167 : */
168 : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
169 : // Old gcc can try to convert anonymous enums to OString and give compile error.
170 : // So there's no special-cased handling of string literals.
171 : // These are inline functions and technically both variants should work
172 : // the same in practice, so there should be no compatibility problem.
173 : OString( const sal_Char * value ) SAL_THROW(())
174 : {
175 : pData = 0;
176 : rtl_string_newFromStr( &pData, value );
177 : }
178 : #else
179 : template< typename T >
180 7783600 : OString( const T& value, typename internal::CharPtrDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
181 : {
182 7783600 : pData = 0;
183 7783600 : rtl_string_newFromStr( &pData, value );
184 7783600 : }
185 :
186 : template< typename T >
187 10440 : OString( T& value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
188 : {
189 10440 : pData = 0;
190 10440 : rtl_string_newFromStr( &pData, value );
191 10440 : }
192 :
193 : /**
194 : New string from a string literal.
195 :
196 : If there are any embedded \0's in the string literal, the result is undefined.
197 : Use the overload that explicitly accepts length.
198 :
199 : @since LibreOffice 3.6
200 :
201 : @param literal a string literal
202 : */
203 : template< typename T >
204 2527195 : OString( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
205 : {
206 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
207 2527195 : pData = 0;
208 2527195 : rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
209 : #ifdef RTL_STRING_UNITTEST
210 26 : rtl_string_unittest_const_literal = true;
211 : #endif
212 2527195 : }
213 :
214 : #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
215 :
216 : /**
217 : New string from a character buffer array.
218 :
219 : @param value a character array.
220 : @param length the number of character which should be copied.
221 : The character array length must be greater or
222 : equal than this value.
223 : */
224 986401 : OString( const sal_Char * value, sal_Int32 length ) SAL_THROW(())
225 : {
226 986401 : pData = 0;
227 986401 : rtl_string_newFromStr_WithLength( &pData, value, length );
228 986401 : }
229 :
230 : /**
231 : New string from a Unicode character buffer array.
232 :
233 : @param value a Unicode character array.
234 : @param length the number of character which should be converted.
235 : The Unicode character array length must be
236 : greater or equal than this value.
237 : @param encoding the text encoding in which the Unicode character
238 : sequence should be converted.
239 : @param convertFlags flags which controls the conversion.
240 : see RTL_UNICODETOTEXT_FLAGS_...
241 :
242 : @exception std::bad_alloc is thrown if an out-of-memory condition occurs
243 : */
244 3575988 : OString( const sal_Unicode * value, sal_Int32 length,
245 : rtl_TextEncoding encoding,
246 : sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
247 : {
248 3575988 : pData = 0;
249 3575988 : rtl_uString2String( &pData, value, length, encoding, convertFlags );
250 3575988 : if (pData == 0) {
251 : #if defined EXCEPTIONS_OFF
252 : abort();
253 : #else
254 0 : throw std::bad_alloc();
255 : #endif
256 : }
257 3575988 : }
258 :
259 : #ifdef RTL_FAST_STRING
260 : /**
261 : @overload
262 : @internal
263 : */
264 : template< typename T1, typename T2 >
265 584920 : OString( const OStringConcat< T1, T2 >& c )
266 : {
267 584920 : const sal_Int32 l = c.length();
268 584920 : pData = rtl_string_alloc( l );
269 584920 : if (l != 0)
270 : {
271 584918 : char* end = c.addData( pData->buffer );
272 584918 : pData->length = end - pData->buffer;
273 584918 : *end = '\0';
274 : }
275 584920 : }
276 : #endif
277 :
278 : /**
279 : Release the string data.
280 : */
281 46149326 : ~OString() SAL_THROW(())
282 : {
283 46149326 : rtl_string_release( pData );
284 46149326 : }
285 :
286 : /**
287 : Assign a new string.
288 :
289 : @param str a OString.
290 : */
291 9639576 : OString & operator=( const OString & str ) SAL_THROW(())
292 : {
293 9639576 : rtl_string_assign( &pData, str.pData );
294 9639576 : return *this;
295 : }
296 :
297 : /**
298 : @overload
299 : This function accepts an ASCII string literal as its argument.
300 : @since LibreOffice 3.6
301 : */
302 : template< typename T >
303 41077 : typename internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal ) SAL_THROW(())
304 : {
305 1 : RTL_STRING_CONST_FUNCTION
306 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
307 41077 : rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
308 41077 : return *this;
309 : }
310 :
311 : /**
312 : Append a string to this string.
313 :
314 : @param str a OString.
315 : */
316 6212350 : OString & operator+=( const OString & str ) SAL_THROW(())
317 : {
318 6212350 : rtl_string_newConcat( &pData, pData, str.pData );
319 6212350 : return *this;
320 : }
321 :
322 : #ifdef RTL_FAST_STRING
323 : /**
324 : @overload
325 : @internal
326 : */
327 : template< typename T1, typename T2 >
328 1 : OString& operator+=( const OStringConcat< T1, T2 >& c )
329 : {
330 1 : const int l = c.length();
331 1 : if( l == 0 )
332 0 : return *this;
333 1 : rtl_string_ensureCapacity( &pData, pData->length + l );
334 1 : char* end = c.addData( pData->buffer + pData->length );
335 1 : *end = '\0';
336 1 : pData->length = end - pData->buffer;
337 1 : return *this;
338 : }
339 : #endif
340 : /**
341 : Returns the length of this string.
342 :
343 : The length is equal to the number of characters in this string.
344 :
345 : @return the length of the sequence of characters represented by this
346 : object.
347 : */
348 24724280 : sal_Int32 getLength() const SAL_THROW(()) { return pData->length; }
349 :
350 : /**
351 : Checks if a string is empty.
352 :
353 : @return sal_True if the string is empty;
354 : sal_False, otherwise.
355 :
356 : @since LibreOffice 3.4
357 : */
358 22271599 : bool isEmpty() const SAL_THROW(())
359 : {
360 22271599 : return pData->length == 0;
361 : }
362 :
363 : /**
364 : Returns a pointer to the characters of this string.
365 :
366 : <p>The returned pointer is guaranteed to point to a null-terminated byte
367 : string. But note that this string object may contain embedded null
368 : characters, which will thus also be embedded in the returned
369 : null-terminated byte string.</p>
370 :
371 : @return a pointer to a null-terminated byte string representing the
372 : characters of this string object.
373 : */
374 27330719 : const sal_Char * getStr() const SAL_THROW(()) { return pData->buffer; }
375 :
376 : /**
377 : Access to individual characters.
378 :
379 : @param index must be non-negative and less than length.
380 :
381 : @return the character at the given index.
382 :
383 : @since LibreOffice 3.5
384 : */
385 14931490 : sal_Char operator [](sal_Int32 index) const { return getStr()[index]; }
386 :
387 : /**
388 : Compares two strings.
389 :
390 : The comparison is based on the numeric value of each character in
391 : the strings and return a value indicating their relationship.
392 : This function can't be used for language specific sorting.
393 :
394 : @param str the object to be compared.
395 : @return 0 - if both strings are equal
396 : < 0 - if this string is less than the string argument
397 : > 0 - if this string is greater than the string argument
398 : */
399 3503813 : sal_Int32 compareTo( const OString & str ) const SAL_THROW(())
400 : {
401 : return rtl_str_compare_WithLength( pData->buffer, pData->length,
402 3503813 : str.pData->buffer, str.pData->length );
403 : }
404 :
405 : /**
406 : Compares two strings with an maximum count of characters.
407 :
408 : The comparison is based on the numeric value of each character in
409 : the strings and return a value indicating their relationship.
410 : This function can't be used for language specific sorting.
411 :
412 : @param rObj the object to be compared.
413 : @param maxLength the maximum count of characters to be compared.
414 : @return 0 - if both strings are equal
415 : < 0 - if this string is less than the string argument
416 : > 0 - if this string is greater than the string argument
417 : */
418 16242 : sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const SAL_THROW(())
419 : {
420 : return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
421 16242 : rObj.pData->buffer, rObj.pData->length, maxLength );
422 : }
423 :
424 : /**
425 : Compares two strings in reverse order.
426 :
427 : The comparison is based on the numeric value of each character in
428 : the strings and return a value indicating their relationship.
429 : This function can't be used for language specific sorting.
430 :
431 : @param str the object to be compared.
432 : @return 0 - if both strings are equal
433 : < 0 - if this string is less than the string argument
434 : > 0 - if this string is greater than the string argument
435 : */
436 : sal_Int32 reverseCompareTo( const OString & str ) const SAL_THROW(())
437 : {
438 : return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
439 : str.pData->buffer, str.pData->length );
440 : }
441 :
442 : /**
443 : Perform a comparison of two strings.
444 :
445 : The result is true if and only if second string
446 : represents the same sequence of characters as the first string.
447 : This function can't be used for language specific comparison.
448 :
449 : @param str the object to be compared.
450 : @return sal_True if the strings are equal;
451 : sal_False, otherwise.
452 : */
453 27768725 : sal_Bool equals( const OString & str ) const SAL_THROW(())
454 : {
455 27768725 : if ( pData->length != str.pData->length )
456 22751448 : return sal_False;
457 5017277 : if ( pData == str.pData )
458 243977 : return sal_True;
459 : return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
460 4773300 : str.pData->buffer, str.pData->length ) == 0;
461 : }
462 :
463 : /**
464 : Perform a comparison of two strings.
465 :
466 : The result is true if and only if second string
467 : represents the same sequence of characters as the first string.
468 : The ASCII string must be NULL-terminated and must be greater or
469 : equal as length.
470 : This function can't be used for language specific comparison.
471 :
472 :
473 : @param value a character array.
474 : @param length the length of the character array.
475 : @return sal_True if the strings are equal;
476 : sal_False, otherwise.
477 : */
478 281770 : sal_Bool equalsL( const sal_Char* value, sal_Int32 length ) const SAL_THROW(())
479 : {
480 281770 : if ( pData->length != length )
481 110065 : return sal_False;
482 :
483 : return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
484 171705 : value, length ) == 0;
485 : }
486 :
487 : /**
488 : Perform a ASCII lowercase comparison of two strings.
489 :
490 : The result is true if and only if second string
491 : represents the same sequence of characters as the first string,
492 : ignoring the case.
493 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
494 : values between 97 and 122 (ASCII a-z).
495 : This function can't be used for language specific comparison.
496 :
497 : @param str the object to be compared.
498 : @return sal_True if the strings are equal;
499 : sal_False, otherwise.
500 : */
501 993593 : sal_Bool equalsIgnoreAsciiCase( const OString & str ) const SAL_THROW(())
502 : {
503 993593 : if ( pData->length != str.pData->length )
504 694230 : return sal_False;
505 299363 : if ( pData == str.pData )
506 0 : return sal_True;
507 : return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
508 299363 : str.pData->buffer, str.pData->length ) == 0;
509 : }
510 :
511 : /**
512 : Perform a ASCII lowercase comparison of two strings.
513 :
514 : The result is true if and only if second string
515 : represents the same sequence of characters as the first string,
516 : ignoring the case.
517 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
518 : values between 97 and 122 (ASCII a-z).
519 : Since this method is optimized for performance, the ASCII character
520 : values are not converted in any way. The caller has to make sure that
521 : all ASCII characters are in the allowed range between 0 and
522 : 127. The ASCII string must be NULL-terminated.
523 : This function can't be used for language specific comparison.
524 :
525 : Note: The argument type is always either char* or const char*, the return type is bool.
526 : The template is used only for technical reasons.
527 :
528 : @param asciiStr the 8-Bit ASCII character string to be compared.
529 : @return sal_True if the strings are equal;
530 : sal_False, otherwise.
531 : */
532 : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
533 : sal_Bool equalsIgnoreAsciiCase( const sal_Char * asciiStr ) const SAL_THROW(())
534 : {
535 : return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
536 : }
537 : #else
538 : template< typename T >
539 1 : typename internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const SAL_THROW(())
540 : {
541 1 : return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
542 : }
543 :
544 : template< typename T >
545 1 : typename internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const SAL_THROW(())
546 : {
547 1 : return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
548 : }
549 :
550 : /**
551 : @overload
552 : This function accepts an ASCII string literal as its argument.
553 : @since LibreOffice 3.6
554 : */
555 : template< typename T >
556 77 : typename internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const SAL_THROW(())
557 : {
558 1 : RTL_STRING_CONST_FUNCTION
559 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
560 77 : if ( pData->length != internal::ConstCharArrayDetector< T, void >::size - 1 )
561 1 : return false;
562 : return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
563 76 : literal, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
564 : }
565 : #endif
566 :
567 : /**
568 : Perform a ASCII lowercase comparison of two strings.
569 :
570 : The result is true if and only if second string
571 : represents the same sequence of characters as the first string,
572 : ignoring the case.
573 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
574 : values between 97 and 122 (ASCII a-z).
575 : Since this method is optimized for performance, the ASCII character
576 : values are not converted in any way. The caller has to make sure that
577 : all ASCII characters are in the allowed range between 0 and
578 : 127. The ASCII string must be greater or equal in length as asciiStrLength.
579 : This function can't be used for language specific comparison.
580 :
581 : @param asciiStr the 8-Bit ASCII character string to be compared.
582 : @param asciiStrLength the length of the ascii string
583 : @return sal_True if the strings are equal;
584 : sal_False, otherwise.
585 : */
586 435 : sal_Bool equalsIgnoreAsciiCaseL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const SAL_THROW(())
587 : {
588 435 : if ( pData->length != asciiStrLength )
589 70 : return sal_False;
590 :
591 : return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
592 365 : asciiStr, asciiStrLength ) == 0;
593 : }
594 :
595 : /**
596 : Match against a substring appearing in this string.
597 :
598 : The result is true if and only if the second string appears as a substring
599 : of this string, at the given position.
600 : This function can't be used for language specific comparison.
601 :
602 : @param str the object (substring) to be compared.
603 : @param fromIndex the index to start the comparion from.
604 : The index must be greater or equal than 0
605 : and less or equal as the string length.
606 : @return sal_True if str match with the characters in the string
607 : at the given position;
608 : sal_False, otherwise.
609 : */
610 73 : sal_Bool match( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
611 : {
612 73 : return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
613 146 : str.pData->buffer, str.pData->length, str.pData->length ) == 0;
614 : }
615 :
616 : /**
617 : @overload
618 : This function accepts an ASCII string literal as its argument.
619 : @since LibreOffice 3.6
620 : */
621 : template< typename T >
622 63 : typename internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
623 : {
624 4 : RTL_STRING_CONST_FUNCTION
625 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
626 : return rtl_str_shortenedCompare_WithLength(
627 : pData->buffer + fromIndex, pData->length - fromIndex,
628 63 : literal, internal::ConstCharArrayDetector< T, void >::size - 1, internal::ConstCharArrayDetector< T, void >::size - 1) == 0;
629 : }
630 :
631 : /**
632 : Match against a substring appearing in this string.
633 :
634 : @param str the substring to be compared; must not be null and must point
635 : to memory of at least strLength bytes
636 :
637 : @param strLength the length of the substring; must be non-negative
638 :
639 : @param fromIndex the index into this string to start the comparison at;
640 : must be non-negative and not greater than this string's length
641 :
642 : @return true if and only if the given str is contained as a substring of
643 : this string at the given fromIndex
644 :
645 : @since LibreOffice 3.6
646 : */
647 0 : bool matchL(
648 : char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
649 : const
650 : {
651 : return rtl_str_shortenedCompare_WithLength(
652 0 : pData->buffer + fromIndex, pData->length - fromIndex,
653 0 : str, strLength, strLength) == 0;
654 : }
655 :
656 : // This overload is left undefined, to detect calls of matchL that
657 : // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
658 : // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
659 : // platforms):
660 : #if SAL_TYPES_SIZEOFLONG == 8
661 : void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
662 : #endif
663 :
664 : /**
665 : Match against a substring appearing in this string, ignoring the case of
666 : ASCII letters.
667 :
668 : The result is true if and only if the second string appears as a substring
669 : of this string, at the given position.
670 : Character values between 65 and 90 (ASCII A-Z) are interpreted as
671 : values between 97 and 122 (ASCII a-z).
672 : This function can't be used for language specific comparison.
673 :
674 : @param str the object (substring) to be compared.
675 : @param fromIndex the index to start the comparion from.
676 : The index must be greater or equal than 0
677 : and less or equal as the string length.
678 : @return sal_True if str match with the characters in the string
679 : at the given position;
680 : sal_False, otherwise.
681 : */
682 36696 : sal_Bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
683 : {
684 36696 : return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
685 : str.pData->buffer, str.pData->length,
686 73392 : str.pData->length ) == 0;
687 : }
688 :
689 : /**
690 : @overload
691 : This function accepts an ASCII string literal as its argument.
692 : @since LibreOffice 3.6
693 : */
694 : template< typename T >
695 2 : typename internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
696 : {
697 2 : RTL_STRING_CONST_FUNCTION
698 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
699 : return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
700 2 : literal, internal::ConstCharArrayDetector< T, void >::size - 1, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
701 : }
702 :
703 : /**
704 : Check whether this string starts with a given substring.
705 :
706 : @param str the substring to be compared
707 :
708 : @return true if and only if the given str appears as a substring at the
709 : start of this string
710 :
711 : @since LibreOffice 4.0
712 : */
713 2 : bool startsWith(OString const & str) const {
714 2 : return match(str, 0);
715 : }
716 :
717 : /**
718 : @overload
719 : This function accepts an ASCII string literal as its argument.
720 : @since LibreOffice 4.0
721 : */
722 : template< typename T >
723 1 : typename internal::ConstCharArrayDetector< T, bool >::Type startsWith( T& literal ) const
724 : {
725 1 : RTL_STRING_CONST_FUNCTION
726 1 : return match(literal, 0);
727 : }
728 :
729 : /**
730 : Check whether this string ends with a given substring.
731 :
732 : @param str the substring to be compared
733 :
734 : @return true if and only if the given str appears as a substring at the
735 : end of this string
736 :
737 : @since LibreOffice 3.6
738 : */
739 2 : bool endsWith(OString const & str) const {
740 2 : return str.getLength() <= getLength()
741 2 : && match(str, getLength() - str.getLength());
742 : }
743 :
744 : /**
745 : @overload
746 : This function accepts an ASCII string literal as its argument.
747 : @since LibreOffice 3.6
748 : */
749 : template< typename T >
750 60 : typename internal::ConstCharArrayDetector< T, bool >::Type endsWith( T& literal ) const
751 : {
752 1 : RTL_STRING_CONST_FUNCTION
753 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
754 : return internal::ConstCharArrayDetector< T, void >::size - 1 <= getLength()
755 60 : && match(literal, getLength() - ( internal::ConstCharArrayDetector< T, void >::size - 1 ));
756 : }
757 :
758 : /**
759 : Check whether this string ends with a given substring.
760 :
761 : @param str the substring to be compared; must not be null and must point
762 : to memory of at least strLength bytes
763 :
764 : @param strLength the length of the substring; must be non-negative
765 :
766 : @return true if and only if the given str appears as a substring at the
767 : end of this string
768 :
769 : @since LibreOffice 3.6
770 : */
771 0 : bool endsWithL(char const * str, sal_Int32 strLength) const {
772 0 : return strLength <= getLength()
773 0 : && matchL(str, strLength, getLength() - strLength);
774 : }
775 :
776 21143953 : friend sal_Bool operator == ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
777 21143953 : { return rStr1.equals(rStr2); }
778 211542 : friend sal_Bool operator != ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
779 211542 : { return !(operator == ( rStr1, rStr2 )); }
780 3252353 : friend sal_Bool operator < ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
781 3252353 : { return rStr1.compareTo( rStr2 ) < 0; }
782 0 : friend sal_Bool operator > ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
783 0 : { return rStr1.compareTo( rStr2 ) > 0; }
784 : friend sal_Bool operator <= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
785 : { return rStr1.compareTo( rStr2 ) <= 0; }
786 : friend sal_Bool operator >= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
787 : { return rStr1.compareTo( rStr2 ) >= 0; }
788 :
789 : template< typename T >
790 250751 : friend typename internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value ) SAL_THROW(())
791 : {
792 250751 : return rStr1.compareTo( value ) == 0;
793 : }
794 :
795 : template< typename T >
796 2 : friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value ) SAL_THROW(())
797 : {
798 2 : return rStr1.compareTo( value ) == 0;
799 : }
800 :
801 : template< typename T >
802 609 : friend typename internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 ) SAL_THROW(())
803 : {
804 609 : return rStr2.compareTo( value ) == 0;
805 : }
806 :
807 : template< typename T >
808 2 : friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 ) SAL_THROW(())
809 : {
810 2 : return rStr2.compareTo( value ) == 0;
811 : }
812 :
813 : /**
814 : @overload
815 : This function accepts an ASCII string literal as its argument.
816 : @since LibreOffice 3.6
817 : */
818 : template< typename T >
819 2934510 : friend typename internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal ) SAL_THROW(())
820 : {
821 2 : RTL_STRING_CONST_FUNCTION
822 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
823 : return rStr.getLength() == internal::ConstCharArrayDetector< T, void >::size - 1
824 : && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal,
825 2934510 : internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
826 : }
827 :
828 : /**
829 : @overload
830 : This function accepts an ASCII string literal as its argument.
831 : @since LibreOffice 3.6
832 : */
833 : template< typename T >
834 238 : friend typename internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr ) SAL_THROW(())
835 : {
836 2 : RTL_STRING_CONST_FUNCTION
837 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
838 : return rStr.getLength() == internal::ConstCharArrayDetector< T, void >::size - 1
839 : && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal,
840 238 : internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
841 : }
842 :
843 : template< typename T >
844 1 : friend typename internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value ) SAL_THROW(())
845 : {
846 1 : return !(operator == ( rStr1, value ));
847 : }
848 :
849 : template< typename T >
850 1 : friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value ) SAL_THROW(())
851 : {
852 1 : return !(operator == ( rStr1, value ));
853 : }
854 :
855 : template< typename T >
856 1 : friend typename internal::CharPtrDetector< T, bool >::Type operator!=( const T& value, const OString& rStr2 ) SAL_THROW(())
857 : {
858 1 : return !(operator == ( value, rStr2 ));
859 : }
860 :
861 : template< typename T >
862 1 : friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value, const OString& rStr2 ) SAL_THROW(())
863 : {
864 1 : return !(operator == ( value, rStr2 ));
865 : }
866 :
867 : /**
868 : @overload
869 : This function accepts an ASCII string literal as its argument.
870 : @since LibreOffice 3.6
871 : */
872 : template< typename T >
873 123875 : friend typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal ) SAL_THROW(())
874 : {
875 123875 : return !( rStr == literal );
876 : }
877 :
878 : /**
879 : @overload
880 : This function accepts an ASCII string literal as its argument.
881 : @since LibreOffice 3.6
882 : */
883 : template< typename T >
884 1 : friend typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr ) SAL_THROW(())
885 : {
886 1 : return !( literal == rStr );
887 : }
888 :
889 : /**
890 : Returns a hashcode for this string.
891 :
892 : @return a hash code value for this object.
893 :
894 : @see rtl::OStringHash for convenient use of boost::unordered_map
895 : */
896 4235835 : sal_Int32 hashCode() const SAL_THROW(())
897 : {
898 4235835 : return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
899 : }
900 :
901 : /**
902 : Returns the index within this string of the first occurrence of the
903 : specified character, starting the search at the specified index.
904 :
905 : @param ch character to be located.
906 : @param fromIndex the index to start the search from.
907 : The index must be greater or equal than 0
908 : and less or equal as the string length.
909 : @return the index of the first occurrence of the character in the
910 : character sequence represented by this string that is
911 : greater than or equal to fromIndex, or
912 : -1 if the character does not occur.
913 : */
914 4401436 : sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
915 : {
916 4401436 : sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
917 4401436 : return (ret < 0 ? ret : ret+fromIndex);
918 : }
919 :
920 : /**
921 : Returns the index within this string of the last occurrence of the
922 : specified character, searching backward starting at the end.
923 :
924 : @param ch character to be located.
925 : @return the index of the last occurrence of the character in the
926 : character sequence represented by this string, or
927 : -1 if the character does not occur.
928 : */
929 356084 : sal_Int32 lastIndexOf( sal_Char ch ) const SAL_THROW(())
930 : {
931 356084 : return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
932 : }
933 :
934 : /**
935 : Returns the index within this string of the last occurrence of the
936 : specified character, searching backward starting before the specified
937 : index.
938 :
939 : @param ch character to be located.
940 : @param fromIndex the index before which to start the search.
941 : @return the index of the last occurrence of the character in the
942 : character sequence represented by this string that
943 : is less than fromIndex, or -1
944 : if the character does not occur before that point.
945 : */
946 : sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const SAL_THROW(())
947 : {
948 : return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
949 : }
950 :
951 : /**
952 : Returns the index within this string of the first occurrence of the
953 : specified substring, starting at the specified index.
954 :
955 : If str doesn't include any character, always -1 is
956 : returned. This is also the case, if both strings are empty.
957 :
958 : @param str the substring to search for.
959 : @param fromIndex the index to start the search from.
960 : @return If the string argument occurs one or more times as a substring
961 : within this string at the starting index, then the index
962 : of the first character of the first such substring is
963 : returned. If it does not occur as a substring starting
964 : at fromIndex or beyond, -1 is returned.
965 : */
966 1754 : sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
967 : {
968 1754 : sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
969 3508 : str.pData->buffer, str.pData->length );
970 1754 : return (ret < 0 ? ret : ret+fromIndex);
971 : }
972 :
973 : /**
974 : @overload
975 : This function accepts an ASCII string literal as its argument.
976 : @since LibreOffice 3.6
977 : */
978 : template< typename T >
979 1161674 : typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
980 : {
981 1 : RTL_STRING_CONST_FUNCTION
982 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
983 : sal_Int32 n = rtl_str_indexOfStr_WithLength(
984 1161674 : pData->buffer + fromIndex, pData->length - fromIndex, literal, internal::ConstCharArrayDetector< T, void >::size - 1);
985 1161674 : return n < 0 ? n : n + fromIndex;
986 : }
987 :
988 : /**
989 : Returns the index within this string of the first occurrence of the
990 : specified substring, starting at the specified index.
991 :
992 : If str doesn't include any character, always -1 is
993 : returned. This is also the case, if both strings are empty.
994 :
995 : @param str the substring to search for.
996 : @param len the length of the substring.
997 : @param fromIndex the index to start the search from.
998 : @return If the string argument occurs one or more times as a substring
999 : within this string at the starting index, then the index
1000 : of the first character of the first such substring is
1001 : returned. If it does not occur as a substring starting
1002 : at fromIndex or beyond, -1 is returned.
1003 :
1004 : @since LibreOffice 3.6
1005 : */
1006 195435 : sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
1007 : const SAL_THROW(())
1008 : {
1009 : sal_Int32 n = rtl_str_indexOfStr_WithLength(
1010 195435 : pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1011 195435 : return n < 0 ? n : n + fromIndex;
1012 : }
1013 :
1014 : // This overload is left undefined, to detect calls of indexOfL that
1015 : // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1016 : // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1017 : // platforms):
1018 : #if SAL_TYPES_SIZEOFLONG == 8
1019 : void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
1020 : #endif
1021 :
1022 : /**
1023 : Returns the index within this string of the last occurrence of
1024 : the specified substring, searching backward starting at the end.
1025 :
1026 : The returned index indicates the starting index of the substring
1027 : in this string.
1028 : If str doesn't include any character, always -1 is
1029 : returned. This is also the case, if both strings are empty.
1030 :
1031 : @param str the substring to search for.
1032 : @return If the string argument occurs one or more times as a substring
1033 : within this string, then the index of the first character of
1034 : the last such substring is returned. If it does not occur as
1035 : a substring, -1 is returned.
1036 : */
1037 5954 : sal_Int32 lastIndexOf( const OString & str ) const SAL_THROW(())
1038 : {
1039 : return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1040 5954 : str.pData->buffer, str.pData->length );
1041 : }
1042 :
1043 : /**
1044 : Returns the index within this string of the last occurrence of
1045 : the specified substring, searching backward starting before the specified
1046 : index.
1047 :
1048 : The returned index indicates the starting index of the substring
1049 : in this string.
1050 : If str doesn't include any character, always -1 is
1051 : returned. This is also the case, if both strings are empty.
1052 :
1053 : @param str the substring to search for.
1054 : @param fromIndex the index before which to start the search.
1055 : @return If the string argument occurs one or more times as a substring
1056 : within this string before the starting index, then the index
1057 : of the first character of the last such substring is
1058 : returned. Otherwise, -1 is returned.
1059 : */
1060 : sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const SAL_THROW(())
1061 : {
1062 : return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1063 : str.pData->buffer, str.pData->length );
1064 : }
1065 :
1066 : /**
1067 : Returns a new string that is a substring of this string.
1068 :
1069 : The substring begins at the specified beginIndex. If
1070 : beginIndex is negative or be greater than the length of
1071 : this string, behaviour is undefined.
1072 :
1073 : @param beginIndex the beginning index, inclusive.
1074 : @return the specified substring.
1075 : */
1076 685310 : OString copy( sal_Int32 beginIndex ) const SAL_THROW(())
1077 : {
1078 685310 : rtl_String *pNew = 0;
1079 685310 : rtl_string_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1080 685310 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1081 : }
1082 :
1083 : /**
1084 : Returns a new string that is a substring of this string.
1085 :
1086 : The substring begins at the specified beginIndex and contains count
1087 : characters. If either beginIndex or count are negative,
1088 : or beginIndex + count are greater than the length of this string
1089 : then behaviour is undefined.
1090 :
1091 : @param beginIndex the beginning index, inclusive.
1092 : @param count the number of characters.
1093 : @return the specified substring.
1094 : */
1095 1175660 : OString copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(())
1096 : {
1097 1175660 : rtl_String *pNew = 0;
1098 1175660 : rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
1099 1175660 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1100 : }
1101 :
1102 : /**
1103 : Concatenates the specified string to the end of this string.
1104 :
1105 : @param str the string that is concatenated to the end
1106 : of this string.
1107 : @return a string that represents the concatenation of this string
1108 : followed by the string argument.
1109 : */
1110 0 : SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const SAL_THROW(())
1111 : {
1112 0 : rtl_String* pNew = 0;
1113 0 : rtl_string_newConcat( &pNew, pData, str.pData );
1114 0 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1115 : }
1116 :
1117 : #ifndef RTL_FAST_STRING
1118 : friend OString operator+( const OString & str1, const OString & str2 ) SAL_THROW(())
1119 : {
1120 : return str1.concat( str2 );
1121 : }
1122 : #endif
1123 :
1124 : /**
1125 : Returns a new string resulting from replacing n = count characters
1126 : from position index in this string with newStr.
1127 :
1128 : @param index the replacing index in str.
1129 : The index must be greater or equal as 0 and
1130 : less or equal as the length of the string.
1131 : @param count the count of charcters that will replaced
1132 : The count must be greater or equal as 0 and
1133 : less or equal as the length of the string minus index.
1134 : @param newStr the new substring.
1135 : @return the new string.
1136 : */
1137 82633 : SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const SAL_THROW(())
1138 : {
1139 82633 : rtl_String* pNew = 0;
1140 82633 : rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1141 82633 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1142 : }
1143 :
1144 : /**
1145 : Returns a new string resulting from replacing all occurrences of
1146 : oldChar in this string with newChar.
1147 :
1148 : If the character oldChar does not occur in the character sequence
1149 : represented by this object, then the string is assigned with
1150 : str.
1151 :
1152 : @param oldChar the old character.
1153 : @param newChar the new character.
1154 : @return a string derived from this string by replacing every
1155 : occurrence of oldChar with newChar.
1156 : */
1157 93646 : SAL_WARN_UNUSED_RESULT OString replace( sal_Char oldChar, sal_Char newChar ) const SAL_THROW(())
1158 : {
1159 93646 : rtl_String* pNew = 0;
1160 93646 : rtl_string_newReplace( &pNew, pData, oldChar, newChar );
1161 93646 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1162 : }
1163 :
1164 : /**
1165 : Returns a new string resulting from replacing the first occurrence of a
1166 : given substring with another substring.
1167 :
1168 : @param from the substring to be replaced
1169 :
1170 : @param to the replacing substring
1171 :
1172 : @param[in,out] index pointer to a start index; if the pointer is
1173 : non-null: upon entry to the function, its value is the index into the this
1174 : string at which to start searching for the \p from substring, the value
1175 : must be non-negative and not greater than this string's length; upon exit
1176 : from the function its value is the index into this string at which the
1177 : replacement took place or -1 if no replacement took place; if the pointer
1178 : is null, searching always starts at index 0
1179 :
1180 : @since LibreOffice 3.6
1181 : */
1182 32895 : SAL_WARN_UNUSED_RESULT OString replaceFirst(
1183 : OString const & from, OString const & to, sal_Int32 * index = 0) const
1184 : {
1185 32895 : rtl_String * s = 0;
1186 32895 : sal_Int32 i = 0;
1187 : rtl_string_newReplaceFirst(
1188 : &s, pData, from.pData->buffer, from.pData->length,
1189 32895 : to.pData->buffer, to.pData->length, index == 0 ? &i : index);
1190 32895 : return OString(s, SAL_NO_ACQUIRE);
1191 : }
1192 :
1193 : /**
1194 : Returns a new string resulting from replacing all occurrences of a given
1195 : substring with another substring.
1196 :
1197 : Replacing subsequent occurrences picks up only after a given replacement.
1198 : That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1199 :
1200 : @param from the substring to be replaced
1201 :
1202 : @param to the replacing substring
1203 :
1204 : @since LibreOffice 3.6
1205 : */
1206 24249 : SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
1207 24249 : rtl_String * s = 0;
1208 : rtl_string_newReplaceAll(
1209 : &s, pData, from.pData->buffer, from.pData->length,
1210 24249 : to.pData->buffer, to.pData->length);
1211 24249 : return OString(s, SAL_NO_ACQUIRE);
1212 : }
1213 :
1214 : /**
1215 : Converts from this string all ASCII uppercase characters (65-90)
1216 : to ASCII lowercase characters (97-122).
1217 :
1218 : This function can't be used for language specific conversion.
1219 : If the string doesn't contain characters which must be converted,
1220 : then the new string is assigned with str.
1221 :
1222 : @return the string, converted to ASCII lowercase.
1223 : */
1224 1636303 : SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const SAL_THROW(())
1225 : {
1226 1636303 : rtl_String* pNew = 0;
1227 1636303 : rtl_string_newToAsciiLowerCase( &pNew, pData );
1228 1636303 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1229 : }
1230 :
1231 : /**
1232 : Converts from this string all ASCII lowercase characters (97-122)
1233 : to ASCII uppercase characters (65-90).
1234 :
1235 : This function can't be used for language specific conversion.
1236 : If the string doesn't contain characters which must be converted,
1237 : then the new string is assigned with str.
1238 :
1239 : @return the string, converted to ASCII uppercase.
1240 : */
1241 298313 : SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const SAL_THROW(())
1242 : {
1243 298313 : rtl_String* pNew = 0;
1244 298313 : rtl_string_newToAsciiUpperCase( &pNew, pData );
1245 298313 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1246 : }
1247 :
1248 : /**
1249 : Returns a new string resulting from removing white space from both ends
1250 : of the string.
1251 :
1252 : All characters that have codes less than or equal to
1253 : 32 (the space character) are considered to be white space.
1254 : If the string doesn't contain white spaces at both ends,
1255 : then the new string is assigned with str.
1256 :
1257 : @return the string, with white space removed from the front and end.
1258 : */
1259 532486 : SAL_WARN_UNUSED_RESULT OString trim() const SAL_THROW(())
1260 : {
1261 532486 : rtl_String* pNew = 0;
1262 532486 : rtl_string_newTrim( &pNew, pData );
1263 532486 : return OString( pNew, (DO_NOT_ACQUIRE*)0 );
1264 : }
1265 :
1266 : /**
1267 : Returns a token in the string.
1268 :
1269 : Example:
1270 : sal_Int32 nIndex = 0;
1271 : do
1272 : {
1273 : ...
1274 : OString aToken = aStr.getToken( 0, ';', nIndex );
1275 : ...
1276 : }
1277 : while ( nIndex >= 0 );
1278 :
1279 : @param token the number of the token to return.
1280 : @param cTok the character which seperate the tokens.
1281 : @param index the position at which the token is searched in the
1282 : string.
1283 : The index must not be greater thanthe length of the
1284 : string.
1285 : This param is set to the position of the
1286 : next token or to -1, if it is the last token.
1287 : @return the token; if either token or index is negative, an empty token
1288 : is returned (and index is set to -1)
1289 : */
1290 9982272 : OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const SAL_THROW(())
1291 : {
1292 9982272 : rtl_String * pNew = 0;
1293 9982272 : index = rtl_string_getToken( &pNew, pData, token, cTok, index );
1294 9982272 : return OString( pNew, (DO_NOT_ACQUIRE *)0 );
1295 : }
1296 :
1297 : /**
1298 : Returns a token from the string.
1299 :
1300 : The same as getToken(sal_Int32, sal_Char, sal_Int32 &), but always passing
1301 : in 0 as the start index in the third argument.
1302 :
1303 : @param count the number of the token to return, starting with 0
1304 : @param separator the character which separates the tokens
1305 :
1306 : @return the given token, or an empty string
1307 :
1308 : @since LibreOffice 3.6
1309 : */
1310 97915 : OString getToken(sal_Int32 count, char separator) const {
1311 97915 : sal_Int32 n = 0;
1312 97915 : return getToken(count, separator, n);
1313 : }
1314 :
1315 : /**
1316 : Returns the Boolean value from this string.
1317 :
1318 : This function can't be used for language specific conversion.
1319 :
1320 : @return sal_True, if the string is 1 or "True" in any ASCII case.
1321 : sal_False in any other case.
1322 : */
1323 0 : sal_Bool toBoolean() const SAL_THROW(())
1324 : {
1325 0 : return rtl_str_toBoolean( pData->buffer );
1326 : }
1327 :
1328 : /**
1329 : Returns the first character from this string.
1330 :
1331 : @return the first character from this string or 0, if this string
1332 : is emptry.
1333 : */
1334 913 : sal_Char toChar() const SAL_THROW(())
1335 : {
1336 913 : return pData->buffer[0];
1337 : }
1338 :
1339 : /**
1340 : Returns the int32 value from this string.
1341 :
1342 : This function can't be used for language specific conversion.
1343 :
1344 : @param radix the radix (between 2 and 36)
1345 : @return the int32 represented from this string.
1346 : 0 if this string represents no number.
1347 : */
1348 130079 : sal_Int32 toInt32( sal_Int16 radix = 10 ) const SAL_THROW(())
1349 : {
1350 130079 : return rtl_str_toInt32( pData->buffer, radix );
1351 : }
1352 :
1353 : /**
1354 : Returns the int64 value from this string.
1355 :
1356 : This function can't be used for language specific conversion.
1357 :
1358 : @param radix the radix (between 2 and 36)
1359 : @return the int64 represented from this string.
1360 : 0 if this string represents no number.
1361 : */
1362 1795 : sal_Int64 toInt64( sal_Int16 radix = 10 ) const SAL_THROW(())
1363 : {
1364 1795 : return rtl_str_toInt64( pData->buffer, radix );
1365 : }
1366 :
1367 : /**
1368 : Returns the float value from this string.
1369 :
1370 : This function can't be used for language specific conversion.
1371 :
1372 : @return the float represented from this string.
1373 : 0.0 if this string represents no number.
1374 : */
1375 0 : float toFloat() const SAL_THROW(())
1376 : {
1377 0 : return rtl_str_toFloat( pData->buffer );
1378 : }
1379 :
1380 : /**
1381 : Returns the double value from this string.
1382 :
1383 : This function can't be used for language specific conversion.
1384 :
1385 : @return the double represented from this string.
1386 : 0.0 if this string represents no number.
1387 : */
1388 5458 : double toDouble() const SAL_THROW(())
1389 : {
1390 5458 : return rtl_str_toDouble( pData->buffer );
1391 : }
1392 :
1393 : /**
1394 : Returns the string representation of the sal_Bool argument.
1395 :
1396 : If the sal_Bool is true, the string "true" is returned.
1397 : If the sal_Bool is false, the string "false" is returned.
1398 : This function can't be used for language specific conversion.
1399 :
1400 : @param b a sal_Bool.
1401 : @return a string with the string representation of the argument.
1402 : */
1403 : static OString valueOf( sal_Bool b ) SAL_THROW(())
1404 : {
1405 : sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
1406 : rtl_String* pNewData = 0;
1407 : rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) );
1408 : return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
1409 : }
1410 :
1411 : /**
1412 : Returns the string representation of the char argument.
1413 :
1414 : @param c a character.
1415 : @return a string with the string representation of the argument.
1416 : */
1417 20 : static OString valueOf( sal_Char c ) SAL_THROW(())
1418 : {
1419 20 : return OString( &c, 1 );
1420 : }
1421 :
1422 : /**
1423 : Returns the string representation of the int argument.
1424 :
1425 : This function can't be used for language specific conversion.
1426 :
1427 : @param i a int32.
1428 : @param radix the radix (between 2 and 36)
1429 : @return a string with the string representation of the argument.
1430 : */
1431 543433 : static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 ) SAL_THROW(())
1432 : {
1433 : sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
1434 543433 : rtl_String* pNewData = 0;
1435 543433 : rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt32( aBuf, i, radix ) );
1436 543433 : return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
1437 : }
1438 :
1439 : /**
1440 : Returns the string representation of the long argument.
1441 :
1442 : This function can't be used for language specific conversion.
1443 :
1444 : @param ll a int64.
1445 : @param radix the radix (between 2 and 36)
1446 : @return a string with the string representation of the argument.
1447 : */
1448 20396 : static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 ) SAL_THROW(())
1449 : {
1450 : sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
1451 20396 : rtl_String* pNewData = 0;
1452 20396 : rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) );
1453 20396 : return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
1454 : }
1455 :
1456 : /**
1457 : Returns the string representation of the float argument.
1458 :
1459 : This function can't be used for language specific conversion.
1460 :
1461 : @param f a float.
1462 : @return a string with the string representation of the argument.
1463 : */
1464 90 : static OString valueOf( float f ) SAL_THROW(())
1465 : {
1466 : sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
1467 90 : rtl_String* pNewData = 0;
1468 90 : rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) );
1469 90 : return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
1470 : }
1471 :
1472 : /**
1473 : Returns the string representation of the double argument.
1474 :
1475 : This function can't be used for language specific conversion.
1476 :
1477 : @param d a double.
1478 : @return a string with the string representation of the argument.
1479 : */
1480 47 : static OString valueOf( double d ) SAL_THROW(())
1481 : {
1482 : sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
1483 47 : rtl_String* pNewData = 0;
1484 47 : rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) );
1485 47 : return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
1486 : }
1487 : };
1488 :
1489 : /* ======================================================================= */
1490 :
1491 : #ifdef RTL_FAST_STRING
1492 : /**
1493 : A simple wrapper around string literal. It is usually not necessary to use, can
1494 : be mostly used to force OString operator+ working with operands that otherwise would
1495 : not trigger it.
1496 :
1497 : This class is not part of public API and is meant to be used only in LibreOffice code.
1498 : @since LibreOffice 4.0
1499 : */
1500 : struct SAL_WARN_UNUSED OStringLiteral
1501 : {
1502 : template< int N >
1503 5 : OStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
1504 : int size;
1505 : const char* data;
1506 : };
1507 :
1508 : /**
1509 : @internal
1510 : */
1511 : template<>
1512 : struct ToStringHelper< OString >
1513 : {
1514 1403353 : static int length( const OString& s ) { return s.getLength(); }
1515 1403349 : static char* addData( char* buffer, const OString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1516 : static const bool allowOStringConcat = true;
1517 : static const bool allowOUStringConcat = false;
1518 : };
1519 :
1520 : /**
1521 : @internal
1522 : */
1523 : template<>
1524 : struct ToStringHelper< OStringLiteral >
1525 : {
1526 4 : static int length( const OStringLiteral& str ) { return str.size; }
1527 4 : static char* addData( char* buffer, const OStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
1528 : static const bool allowOStringConcat = true;
1529 : static const bool allowOUStringConcat = false;
1530 : };
1531 :
1532 : /**
1533 : @internal
1534 : */
1535 : template< typename charT, typename traits, typename T1, typename T2 >
1536 : inline std::basic_ostream<charT, traits> & operator <<(
1537 : std::basic_ostream<charT, traits> & stream, const OStringConcat< T1, T2 >& concat)
1538 : {
1539 : return stream << OString( concat );
1540 : }
1541 : #else
1542 : // non-RTL_FAST_CODE needs this to compile
1543 : typedef OString OStringLiteral;
1544 : #endif
1545 :
1546 :
1547 : /** A helper to use OStrings with hash maps.
1548 :
1549 : Instances of this class are unary function objects that can be used as
1550 : hash function arguments to boost::unordered_map and similar constructs.
1551 : */
1552 : struct OStringHash
1553 : {
1554 : /** Compute a hash code for a string.
1555 :
1556 : @param rString
1557 : a string.
1558 :
1559 : @return
1560 : a hash code for the string. This hash code should not be stored
1561 : persistently, as its computation may change in later revisions.
1562 : */
1563 3676002 : size_t operator()( const OString& rString ) const
1564 3676002 : { return (size_t)rString.hashCode(); }
1565 : };
1566 :
1567 : /* ======================================================================= */
1568 :
1569 : /**
1570 : Support for rtl::OString in std::ostream (and thus in
1571 : CPPUNIT_ASSERT or SAL_INFO macros, for example).
1572 :
1573 : @since LibreOffice 4.0
1574 : */
1575 : template< typename charT, typename traits > std::basic_ostream<charT, traits> &
1576 0 : operator <<(
1577 : std::basic_ostream<charT, traits> & stream, OString const & string)
1578 : {
1579 0 : return stream << string.getStr();
1580 : // best effort; potentially loses data due to embedded null characters
1581 : }
1582 :
1583 : } /* Namespace */
1584 :
1585 : #ifdef RTL_STRING_UNITTEST
1586 : namespace rtl
1587 : {
1588 : typedef rtlunittest::OString OString;
1589 : }
1590 : #undef RTL_STRING_CONST_FUNCTION
1591 : #endif
1592 :
1593 : #ifdef RTL_USING
1594 : using ::rtl::OString;
1595 : using ::rtl::OStringHash;
1596 : using ::rtl::OStringLiteral;
1597 : #endif
1598 :
1599 : #endif /* _RTL_STRING_HXX_ */
1600 :
1601 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|