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