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