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