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