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
|
/* -*- 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 .
*/
/*
* This file is part of LibreOffice published API.
*/
#ifndef INCLUDED_RTL_USTRBUF_HXX
#define INCLUDED_RTL_USTRBUF_HXX
#include "sal/config.h"
#include <cassert>
#include <cstring>
#include <limits>
#include <new>
#if defined LIBO_INTERNAL_ONLY
#include <string_view>
#include <type_traits>
#include <utility>
#endif
#include "rtl/ustrbuf.h"
#include "rtl/ustring.hxx"
#include "rtl/stringutils.hxx"
#include "sal/types.h"
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
#include "o3tl/safeint.hxx"
#include "rtl/stringconcat.hxx"
#endif
#ifdef RTL_STRING_UNITTEST
extern bool rtl_string_unittest_invalid_conversion;
#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
{
#ifdef RTL_STRING_UNITTEST
#undef rtl
#endif
/** A string buffer implements a mutable sequence of characters.
*/
class SAL_WARN_UNUSED OUStringBuffer
{
friend class OUString;
public:
/**
Constructs a string buffer with no characters in it and an
initial capacity of 16 characters.
*/
OUStringBuffer()
: pData(NULL)
, nCapacity( 16 )
{
rtl_uString_new_WithLength( &pData, nCapacity );
}
/**
Allocates a new string buffer that contains the same sequence of
characters as the string buffer argument.
@param value a <code>OUStringBuffer</code>.
*/
OUStringBuffer( const OUStringBuffer & value )
: pData(NULL)
, nCapacity( value.nCapacity )
{
rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
}
/**
Constructs a string buffer with no characters in it and an
initial capacity specified by the <code>length</code> argument.
@param length the initial capacity.
*/
explicit OUStringBuffer(sal_Int32 length)
: pData(NULL)
, nCapacity( length )
{
rtl_uString_new_WithLength( &pData, length );
}
#if defined LIBO_INTERNAL_ONLY
template<typename T>
explicit OUStringBuffer(T length, std::enable_if_t<std::is_integral_v<T>, int> = 0)
: OUStringBuffer(static_cast<sal_Int32>(length))
{
assert(
length >= 0
&& static_cast<std::make_unsigned_t<T>>(length)
<= static_cast<std::make_unsigned_t<sal_Int32>>(
std::numeric_limits<sal_Int32>::max()));
}
// avoid (obvious) bugs
explicit OUStringBuffer(bool) = delete;
explicit OUStringBuffer(char) = delete;
explicit OUStringBuffer(wchar_t) = delete;
#if !(defined _MSC_VER && _MSC_VER >= 1930 && _MSC_VER <= 1939 && defined _MANAGED)
explicit OUStringBuffer(char8_t) = delete;
#endif
explicit OUStringBuffer(char16_t) = delete;
explicit OUStringBuffer(char32_t) = delete;
#endif
/**
Constructs a string buffer so that it represents the same
sequence of characters as the string argument.
The initial
capacity of the string buffer is <code>16</code> plus the length
of the string argument.
@param value the initial contents of the buffer.
*/
#if defined LIBO_INTERNAL_ONLY
OUStringBuffer(std::u16string_view sv)
: pData(nullptr)
, nCapacity( sv.length() + 16 )
{
if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
throw std::bad_alloc();
}
rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
}
#else
OUStringBuffer(const OUString& value)
: pData(NULL)
, nCapacity( value.getLength() + 16 )
{
rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
}
#endif
template< typename T >
OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
: pData(NULL)
, nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
{
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
rtl_uString_newFromLiteral(
&pData,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length, 16);
#ifdef RTL_STRING_UNITTEST
rtl_string_unittest_const_literal = true;
#endif
}
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T>
OUStringBuffer(
T & literal,
typename libreoffice_internal::ConstCharArrayDetector<
T, libreoffice_internal::Dummy>::TypeUtf16
= libreoffice_internal::Dummy()):
pData(nullptr),
nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
{
rtl_uStringbuffer_newFromStr_WithLength(
&pData,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
}
#endif
#if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
/// @cond INTERNAL
/**
* Only used by unittests to detect incorrect conversions.
* @internal
*/
template< typename T >
OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
{
pData = NULL;
nCapacity = 10;
rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
rtl_string_unittest_invalid_conversion = true;
}
/**
* Only used by unittests to detect incorrect conversions.
* @internal
*/
template< typename T >
OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
{
pData = NULL;
nCapacity = 10;
rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
rtl_string_unittest_invalid_conversion = true;
}
/// @endcond
#endif
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
@overload
@internal
*/
template< typename T1, typename T2 >
OUStringBuffer( OUStringConcat< T1, T2 >&& c )
{
const sal_Int32 l = c.length();
nCapacity = l + 16;
pData = rtl_uString_alloc( nCapacity );
sal_Unicode* end = c.addData( pData->buffer );
*end = '\0';
pData->length = l;
}
/**
@overload
@internal
*/
template< std::size_t N >
OUStringBuffer( OUStringNumber< N >&& n )
: pData(NULL)
, nCapacity( n.length + 16 )
{
rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
}
#endif
#if defined LIBO_INTERNAL_ONLY
operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
#endif
/** Assign to this a copy of value.
*/
OUStringBuffer& operator = ( const OUStringBuffer& value )
{
if (this != &value)
{
rtl_uStringbuffer_newFromStringBuffer(&pData,
value.nCapacity,
value.pData);
nCapacity = value.nCapacity;
}
return *this;
}
#if defined LIBO_INTERNAL_ONLY
/** Move assignment
* @since LibreOffice 7.3
*/
OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
{
rtl_uString_release( pData );
pData = value.pData;
nCapacity = value.nCapacity;
value.pData = nullptr;
value.nCapacity = 0;
rtl_uString_new( &value.pData );
return *this;
}
#endif
/** Assign from a string.
@since LibreOffice 5.3
*/
#if defined LIBO_INTERNAL_ONLY
OUStringBuffer & operator =(std::u16string_view string) {
sal_Int32 n = string.length();
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
std::memcpy(
pData->buffer, string.data(),
n * sizeof (sal_Unicode));
pData->buffer[n] = '\0';
pData->length = n;
return *this;
}
#else
OUStringBuffer & operator =(OUString const & string) {
sal_Int32 n = string.getLength();
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
std::memcpy(
pData->buffer, string.pData->buffer,
(n + 1) * sizeof (sal_Unicode));
pData->length = n;
return *this;
}
#endif
/** Assign from a string literal.
@since LibreOffice 5.3
*/
template<typename T>
typename
libreoffice_internal::ConstCharArrayDetector<T, OUStringBuffer &>::Type
operator =(T & literal) {
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
sal_Int32 const n
= libreoffice_internal::ConstCharArrayDetector<T>::length;
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
char const * from
= libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
literal);
sal_Unicode * to = pData->buffer;
for (sal_Int32 i = 0; i <= n; ++i) {
to[i] = from[i];
}
pData->length = n;
return *this;
}
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T>
typename libreoffice_internal::ConstCharArrayDetector<
T, OUStringBuffer &>::TypeUtf16
operator =(T & literal) {
sal_Int32 const n
= libreoffice_internal::ConstCharArrayDetector<T>::length;
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
// For OUStringChar, which is covered by this template's ConstCharArrayDetector TypeUtf16
// check, toPointer does not return a NUL-terminated string, so we can't just memcpy n+1
// elements but rather need to add the terminating NUL manually:
std::memcpy(
pData->buffer,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
n * sizeof (sal_Unicode));
pData->buffer[n] = '\0';
pData->length = n;
return *this;
}
#endif
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T1, typename T2>
OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
sal_Int32 const n = concat.length();
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
*concat.addData(pData->buffer) = 0;
pData->length = n;
return *this;
}
/** @overload @internal */
template<std::size_t N>
OUStringBuffer & operator =(OUStringNumber<N> && n)
{
return operator =(std::u16string_view(n));
}
#endif
/**
Release the string data.
*/
~OUStringBuffer()
{
rtl_uString_release( pData );
}
/**
Fill the string data in the new string and clear the buffer.
This method is more efficient than the constructor of the string. It does
not copy the buffer.
@return the string previously contained in the buffer.
*/
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
{
return OUString(
rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
SAL_NO_ACQUIRE );
}
/**
Returns the length (character count) of this string buffer.
@return the number of characters in this string buffer.
*/
sal_Int32 getLength() const
{
return pData->length;
}
/**
Checks if a string buffer is empty.
@return true if the string buffer is empty;
false, otherwise.
@since LibreOffice 4.1
*/
bool isEmpty() const
{
return pData->length == 0;
}
/**
Returns the current capacity of the String buffer.
The capacity
is the amount of storage available for newly inserted
characters. The real buffer size is 2 bytes longer, because
all strings are 0 terminated.
@return the current capacity of this string buffer.
*/
sal_Int32 getCapacity() const
{
return nCapacity;
}
/**
Ensures that the capacity of the buffer is at least equal to the
specified minimum.
The new capacity will be at least as large as the maximum of the current
length (so that no contents of the buffer is destroyed) and the given
minimumCapacity. If the given minimumCapacity is negative, nothing is
changed.
@param minimumCapacity the minimum desired capacity.
*/
void ensureCapacity(sal_Int32 minimumCapacity)
{
rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
}
/**
Sets the length of this String buffer.
If the <code>newLength</code> argument is less than the current
length of the string buffer, the string buffer is truncated to
contain exactly the number of characters given by the
<code>newLength</code> argument.
<p>
If the <code>newLength</code> argument is greater than or equal
to the current length, sufficient null characters
(<code>'\u0000'</code>) are appended to the string buffer so that
length becomes the <code>newLength</code> argument.
<p>
The <code>newLength</code> argument must be greater than or equal
to <code>0</code>.
@param newLength the new length of the buffer.
*/
void setLength(sal_Int32 newLength)
{
assert(newLength >= 0);
// Avoid modifications if pData points to const empty string:
if( newLength != pData->length )
{
if( newLength > nCapacity )
rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
else
pData->buffer[newLength] = 0;
pData->length = newLength;
}
}
/**
Returns the character at a specific index in this string buffer.
The first character of a string buffer is at index
<code>0</code>, the next at index <code>1</code>, and so on, for
array indexing.
<p>
The index argument must be greater than or equal to
<code>0</code>, and less than the length of this string buffer.
@param index the index of the desired character.
@return the character at the specified index of this string buffer.
*/
SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
sal_Unicode charAt( sal_Int32 index ) const
{
assert(index >= 0 && index < pData->length);
return pData->buffer[ index ];
}
/**
The character at the specified index of this string buffer is set
to <code>ch</code>.
The index argument must be greater than or equal to
<code>0</code>, and less than the length of this string buffer.
@param index the index of the character to modify.
@param ch the new character.
*/
SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
{
assert(index >= 0 && index < pData->length);
pData->buffer[ index ] = ch;
return *this;
}
/**
Return a null terminated unicode character array.
*/
const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
/**
Access to individual characters.
@param index must be non-negative and less than length.
@return a reference to the character at the given index.
@since LibreOffice 3.5
*/
sal_Unicode & operator [](sal_Int32 index)
{
assert(index >= 0 && index < pData->length);
return pData->buffer[index];
}
/**
Access to individual characters.
@param index must be non-negative and less than length.
@return a reference to the character at the given index.
@since LibreOffice 4.2
*/
const sal_Unicode & operator [](sal_Int32 index) const
{
assert(index >= 0 && index < pData->length);
return pData->buffer[index];
}
/**
Return an OUString instance reflecting the current content
of this OUStringBuffer.
*/
OUString toString() const
{
return OUString(pData->buffer, pData->length);
}
/**
Appends the string to this string buffer.
The characters of the <code>OUString</code> argument are appended, in
order, to the contents of this string buffer, increasing the
length of this string buffer by the length of the argument.
@param str a string.
@return this string buffer.
*/
#if !defined LIBO_INTERNAL_ONLY
OUStringBuffer & append(const OUString &str)
#else
OUStringBuffer & append(std::u16string_view str)
#endif
{
return insert(getLength(), str);
}
#if !defined LIBO_INTERNAL_ONLY
/**
Appends the content of a stringbuffer to this string buffer.
The characters of the <code>OUStringBuffer</code> argument are appended, in
order, to the contents of this string buffer, increasing the
length of this string buffer by the length of the argument.
@param str a string.
@return this string buffer.
@since LibreOffice 4.0
*/
OUStringBuffer & append(const OUStringBuffer &str)
{
if(!str.isEmpty())
{
append( str.getStr(), str.getLength() );
}
return *this;
}
#endif
/**
Appends the string representation of the <code>char</code> array
argument to this string buffer.
The characters of the array argument are appended, in order, to
the contents of this string buffer. The length of this string
buffer increases by the length of the argument.
@param str the characters to be appended.
@return this string buffer.
*/
#if defined LIBO_INTERNAL_ONLY
template<typename T>
typename libreoffice_internal::CharPtrDetector<T, OUStringBuffer &>::TypeUtf16
append(T const & str)
#else
OUStringBuffer & append( const sal_Unicode * str )
#endif
{
return insert(getLength(), str);
}
/**
Appends the string representation of the <code>char</code> array
argument to this string buffer.
Characters of the character array <code>str</code> are appended,
in order, to the contents of this string buffer. The length of this
string buffer increases by the value of <code>len</code>.
@param str the characters to be appended; must be non-null, and must
point to at least len characters
@param len the number of characters to append; must be non-negative
@return this string buffer.
*/
OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
{
return insert(getLength(), str, len);
}
/**
@overload
This function accepts an ASCII string literal as its argument.
@since LibreOffice 3.6
*/
template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
{
return insert(getLength(), literal);
}
#if defined LIBO_INTERNAL_ONLY
template<typename T>
typename libreoffice_internal::NonConstCharArrayDetector<T, OUStringBuffer &>::TypeUtf16
append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
/** @overload @since LibreOffice 5.3 */
template<typename T>
typename libreoffice_internal::ConstCharArrayDetector<
T, OUStringBuffer &>::TypeUtf16
append(T & literal) {
return insert(getLength(), literal);
}
#endif
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
@overload
@internal
*/
template< typename T1, typename T2 >
OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
{
return insert(getLength(), std::move(c));
}
#endif
/**
Appends a 8-Bit ASCII character string to this string buffer.
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.
<p>
The characters of the array argument are appended, in order, to
the contents of this string buffer. The length of this string
buffer increases by the length of the argument.
@param str the 8-Bit ASCII characters to be appended.
@return this string buffer.
*/
OUStringBuffer & appendAscii( const char * str )
{
return appendAscii( str, rtl_str_getLength( str ) );
}
/**
Appends a 8-Bit ASCII character string to this string buffer.
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.
<p>
Characters of the character array <code>str</code> are appended,
in order, to the contents of this string buffer. The length of this
string buffer increases by the value of <code>len</code>.
@param str the 8-Bit ASCII characters to be appended; must be non-null,
and must point to at least len characters
@param len the number of characters to append; must be non-negative
@return this string buffer.
*/
OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
{
rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
return *this;
}
/**
Appends the string representation of the <code>bool</code>
argument to the string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param b a <code>bool</code>.
@return this string buffer.
@since LibreOffice 4.1
*/
OUStringBuffer & append(bool b)
{
return insert(getLength(), b);
}
/// @cond INTERNAL
// Pointer can be automatically converted to bool, which is unwanted here.
// Explicitly delete all pointer append() overloads to prevent this
// (except for char* and sal_Unicode* overloads, which are handled elsewhere).
template< typename T >
typename libreoffice_internal::Enable< void,
!libreoffice_internal::CharPtrDetector< T* >::ok && !libreoffice_internal::SalUnicodePtrDetector< T* >::ok >::Type
append( T* ) SAL_DELETED_FUNCTION;
/// @endcond
// This overload is needed because OUString has a ctor from rtl_uString*, but
// the bool overload above would be preferred to the conversion.
/**
@internal
*/
OUStringBuffer & append(rtl_uString* str)
{
return append( OUString::unacquired( &str ));
}
/**
Appends the string representation of the <code>sal_Bool</code>
argument to the string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param b a <code>sal_Bool</code>.
@return this string buffer.
*/
OUStringBuffer & append(sal_Bool b)
{
return insert(getLength(), b);
}
/**
Appends the string representation of the ASCII <code>char</code>
argument to this string buffer.
The argument is appended to the contents of this string buffer.
The length of this string buffer increases by <code>1</code>.
@param c an ASCII <code>char</code>.
@return this string buffer.
@since LibreOffice 3.5
*/
OUStringBuffer & append(char c)
{
assert(static_cast< unsigned char >(c) <= 0x7F);
return insert(getLength(), c);
}
/**
Appends the string representation of the <code>char</code>
argument to this string buffer.
The argument is appended to the contents of this string buffer.
The length of this string buffer increases by <code>1</code>.
@param c a <code>char</code>.
@return this string buffer.
*/
OUStringBuffer & append(sal_Unicode c)
{
return insert(getLength(), c);
}
#if defined LIBO_INTERNAL_ONLY
void append(sal_uInt16) = delete;
#endif
/**
Appends the string representation of the <code>sal_Int32</code>
argument to this string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param i an <code>sal_Int32</code>.
@param radix the radix
@return this string buffer.
*/
OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
{
return insert(getLength(), i, radix);
}
/**
Appends the string representation of the <code>long</code>
argument to this string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param l a <code>long</code>.
@param radix the radix
@return this string buffer.
*/
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
{
return insert(getLength(), l, radix);
}
/**
Appends the string representation of the <code>float</code>
argument to this string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param f a <code>float</code>.
@return this string buffer.
*/
OUStringBuffer & append(float f)
{
return insert(getLength(), f);
}
/**
Appends the string representation of the <code>double</code>
argument to this string buffer.
The argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then appended to this string buffer.
@param d a <code>double</code>.
@return this string buffer.
*/
OUStringBuffer & append(double d)
{
return insert(getLength(), d);
}
/**
Appends a single UTF-32 character to this string buffer.
<p>The single UTF-32 character will be represented within the string
buffer as either one or two UTF-16 code units.</p>
@param c a well-formed UTF-32 code unit (that is, a value in the range
<code>0</code>–<code>0x10FFFF</code>, but excluding
<code>0xD800</code>–<code>0xDFFF</code>)
@return
this string buffer
*/
OUStringBuffer & appendUtf32(sal_uInt32 c) {
return insertUtf32(getLength(), c);
}
/**
Unsafe way to make space for a fixed amount of characters to be appended
into this OUStringBuffer.
A call to this function must immediately be followed by code that
completely fills the uninitialized block pointed to by the return value.
@param length the length of the uninitialized block of sal_Unicode
entities; must be non-negative
@return a pointer to the start of the uninitialized block; only valid
until this OUStringBuffer's capacity changes
@since LibreOffice 4.4
*/
sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
sal_Int32 n = getLength();
rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
return pData->buffer + n;
}
#if defined LIBO_INTERNAL_ONLY
/**
"Stream" operator to append a value to this OUStringBuffer.
@internal
@since LibreOffice 7.5
*/
template<typename T>
OUStringBuffer& operator<<(T&& rValue)
{
return append(std::forward<T>(rValue));
}
#endif
/**
Inserts the string into this string buffer.
The characters of the <code>String</code> argument are inserted, in
order, into this string buffer at the indicated offset. The length
of this string buffer is increased by the length of the argument.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param str a string.
@return this string buffer.
*/
#if defined LIBO_INTERNAL_ONLY
OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
{
if (str.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
throw std::bad_alloc();
}
return insert( offset, str.data(), str.length() );
}
#else
OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
{
return insert( offset, str.getStr(), str.getLength() );
}
#endif
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
@overload
@internal
*/
template <typename T1, typename T2>
OUStringBuffer& insert(sal_Int32 offset, OUStringConcat<T1, T2>&& c)
{
const size_t l = c.length();
if (l == 0)
return *this;
if (l > o3tl::make_unsigned(std::numeric_limits<sal_Int32>::max() - pData->length))
throw std::bad_alloc();
rtl_uStringbuffer_insert(&pData, &nCapacity, offset, nullptr, l);
/* insert the new characters */
c.addData(pData->buffer + offset);
return *this;
}
#endif
/**
Inserts the string representation of the <code>char</code> array
argument into this string buffer.
The characters of the array argument are inserted into the
contents of this string buffer at the position indicated by
<code>offset</code>. The length of this string buffer increases by
the length of the argument.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param str a character array.
@return this string buffer.
*/
OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
{
return insert( offset, str, rtl_ustr_getLength( str ) );
}
/**
Inserts the string representation of the <code>char</code> array
argument into this string buffer.
The characters of the array argument are inserted into the
contents of this string buffer at the position indicated by
<code>offset</code>. The length of this string buffer increases by
the length of the argument.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param str a character array.
@param len the number of characters to append.
@return this string buffer.
*/
OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
{
assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
return *this;
}
/**
@overload
This function accepts an ASCII string literal as its argument.
@since LibreOffice 3.6
*/
template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
{
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
rtl_uStringbuffer_insert_ascii(
&pData, &nCapacity, offset,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
return *this;
}
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T>
typename libreoffice_internal::ConstCharArrayDetector<
T, OUStringBuffer &>::TypeUtf16
insert(sal_Int32 offset, T & literal) {
return insert(
offset,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
}
#endif
/**
Inserts the string representation of the <code>sal_Bool</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param b a <code>sal_Bool</code>.
@return this string buffer.
*/
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
{
sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
}
/**
Inserts the string representation of the <code>bool</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>OUString::boolean</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param b a <code>bool</code>.
@return this string buffer.
@since LibreOffice 4.3
*/
OUStringBuffer & insert(sal_Int32 offset, bool b)
{
sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
}
/**
Inserts the string representation of the <code>char</code>
argument into this string buffer.
The second argument is inserted into the contents of this string
buffer at the position indicated by <code>offset</code>. The length
of this string buffer increases by one.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param c a <code>char</code>.
@return this string buffer.
@since LibreOffice 3.6
*/
OUStringBuffer & insert(sal_Int32 offset, char c)
{
sal_Unicode u = c;
return insert( offset, &u, 1 );
}
/**
Inserts the string representation of the <code>char</code>
argument into this string buffer.
The second argument is inserted into the contents of this string
buffer at the position indicated by <code>offset</code>. The length
of this string buffer increases by one.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param c a <code>char</code>.
@return this string buffer.
*/
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
{
return insert( offset, &c, 1 );
}
/**
Inserts the string representation of the second <code>sal_Int32</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param i an <code>sal_Int32</code>.
@param radix the radix.
@return this string buffer.
@exception StringIndexOutOfBoundsException if the offset is invalid.
*/
OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
{
sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
}
/**
Inserts the string representation of the <code>long</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param l a <code>long</code>.
@param radix the radix.
@return this string buffer.
@exception StringIndexOutOfBoundsException if the offset is invalid.
*/
OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
{
sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
}
/**
Inserts the string representation of the <code>float</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param f a <code>float</code>.
@return this string buffer.
@exception StringIndexOutOfBoundsException if the offset is invalid.
*/
OUStringBuffer & insert(sal_Int32 offset, float f)
{
// Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat
rtl_math_doubleToUString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G,
RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
NULL, 0, true);
return *this;
}
/**
Inserts the string representation of the <code>double</code>
argument into this string buffer.
The second argument is converted to a string as if by the method
<code>String.valueOf</code>, and the characters of that
string are then inserted into this string buffer at the indicated
offset.
<p>
The offset argument must be greater than or equal to
<code>0</code>, and less than or equal to the length of this
string buffer.
@param offset the offset.
@param d a <code>double</code>.
@return this string buffer.
@exception StringIndexOutOfBoundsException if the offset is invalid.
*/
OUStringBuffer & insert(sal_Int32 offset, double d)
{
// Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble
rtl_math_doubleToUString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G,
RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
NULL, 0, true);
return *this;
}
/**
Inserts a single UTF-32 character into this string buffer.
<p>The single UTF-32 character will be represented within the string
buffer as either one or two UTF-16 code units.</p>
@param offset the offset into this string buffer (from zero to the length
of this string buffer, inclusive)
@param c a well-formed UTF-32 code unit (that is, a value in the range
<code>0</code>–<code>0x10FFFF</code>, but excluding
<code>0xD800</code>–<code>0xDFFF</code>)
@return this string buffer
*/
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
return *this;
}
/**
Removes the characters in a substring of this sequence.
The substring begins at the specified <code>start</code> and
is <code>len</code> characters long.
start must be >= 0 && <= This->length
@param start The beginning index, inclusive
@param len The substring length
@return this string buffer.
*/
OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
{
rtl_uStringbuffer_remove( &pData, start, len );
return *this;
}
/**
Removes the tail of a string buffer start at the indicate position
start must be >= 0 && <= This->length
@param start The beginning index, inclusive. default to 0
@return this string buffer.
@since LibreOffice 4.0
*/
OUStringBuffer & truncate( sal_Int32 start = 0 )
{
rtl_uStringbuffer_remove( &pData, start, getLength() - start );
return *this;
}
/**
Replace all occurrences of
oldChar in this string buffer with newChar.
@since LibreOffice 4.0
@param oldChar the old character.
@param newChar the new character.
@return this string buffer
*/
OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
{
sal_Int32 index = 0;
while((index = indexOf(oldChar, index)) >= 0)
{
pData->buffer[ index ] = newChar;
}
return *this;
}
/** Allows access to the internal data of this OUStringBuffer, for effective
manipulation.
This method should be used with care. After you have called this
method, you may use the returned pInternalData or pInternalCapacity only
as long as you make no other method call on this OUStringBuffer.
@param pInternalData
This output parameter receives a pointer to the internal data
(rtl_uString pointer). pInternalData itself must not be null.
@param pInternalCapacity
This output parameter receives a pointer to the internal capacity.
pInternalCapacity itself must not be null.
*/
void accessInternals(rtl_uString *** pInternalData,
sal_Int32 ** pInternalCapacity)
{
*pInternalData = &pData;
*pInternalCapacity = &nCapacity;
}
/**
Returns the index within this string of the first occurrence of the
specified character, starting the search at the specified index.
@since LibreOffice 4.0
@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_Unicode ch, sal_Int32 fromIndex = 0 ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
sal_Int32 ret = rtl_ustr_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.
@since LibreOffice 4.0
@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_Unicode ch ) const
{
return rtl_ustr_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.
@since LibreOffice 4.0
@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_Unicode ch, sal_Int32 fromIndex ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
return rtl_ustr_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.
@since LibreOffice 4.0
@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.
*/
#if defined LIBO_INTERNAL_ONLY
sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
str.data(), str.length() );
return (ret < 0 ? ret : ret+fromIndex);
}
#else
sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
str.pData->buffer, str.pData->length );
return (ret < 0 ? ret : ret+fromIndex);
}
#endif
/**
@overload
This function accepts an ASCII string literal as its argument.
@since LibreOffice 4.0
*/
template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
{
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
pData->buffer + fromIndex, pData->length - fromIndex,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
return n < 0 ? n : n + fromIndex;
}
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T>
typename
libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
indexOf(T & literal, sal_Int32 fromIndex = 0) const {
assert(fromIndex >= 0);
auto n = rtl_ustr_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;
}
#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.
@since LibreOffice 4.0
@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.
*/
#if defined LIBO_INTERNAL_ONLY
sal_Int32 lastIndexOf( std::u16string_view str ) const
{
return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
str.data(), str.length() );
}
#else
sal_Int32 lastIndexOf( const OUString & str ) const
{
return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
str.pData->buffer, str.pData->length );
}
#endif
/**
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.
@since LibreOffice 4.0
@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.
*/
#if defined LIBO_INTERNAL_ONLY
sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
str.data(), str.length() );
}
#else
sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
{
assert( fromIndex >= 0 && fromIndex <= pData->length );
return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
str.pData->buffer, str.pData->length );
}
#endif
/**
@overload
This function accepts an ASCII string literal as its argument.
@since LibreOffice 4.0
*/
template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
{
assert(
libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
return rtl_ustr_lastIndexOfAscii_WithLength(
pData->buffer, pData->length,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
}
#if defined LIBO_INTERNAL_ONLY
/** @overload @since LibreOffice 5.3 */
template<typename T>
typename
libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
lastIndexOf(T & literal) const {
return rtl_ustr_lastIndexOfStr_WithLength(
pData->buffer, pData->length,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
}
#endif
/**
Strip the given character from the start of the buffer.
@since LibreOffice 4.0
@param c the character to strip
@return The number of characters stripped
*/
sal_Int32 stripStart(sal_Unicode c = ' ')
{
sal_Int32 index;
for(index = 0; index < getLength() ; index++)
{
if(pData->buffer[ index ] != c)
{
break;
}
}
if(index)
{
remove(0, index);
}
return index;
}
/**
Strip the given character from the end of the buffer.
@since LibreOffice 4.0
@param c the character to strip
@return The number of characters stripped
*/
sal_Int32 stripEnd(sal_Unicode c = ' ')
{
sal_Int32 result = getLength();
sal_Int32 index;
for(index = getLength(); index > 0 ; index--)
{
if(pData->buffer[ index - 1 ] != c)
{
break;
}
}
if(index < getLength())
{
truncate(index);
}
return result - getLength();
}
/**
Strip the given character from the both end of the buffer.
@since LibreOffice 4.0
@param c the character to strip
@return The number of characters stripped
*/
sal_Int32 strip(sal_Unicode c = ' ')
{
return stripStart(c) + stripEnd(c);
}
#if defined LIBO_INTERNAL_ONLY
/**
Returns a std::u16string_view that is a view of 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 std::u16string_view subView( sal_Int32 beginIndex ) const
{
assert(beginIndex >= 0);
assert(beginIndex <= getLength());
return subView(beginIndex, getLength() - beginIndex);
}
/**
Returns a std::u16string_view that is a view of 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 std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
{
assert(beginIndex >= 0);
assert(count >= 0);
assert(beginIndex <= getLength());
assert(count <= getLength() - beginIndex);
return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
}
#endif
/**
Returns a new string buffer 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.
@since LibreOffice 4.1
*/
OUStringBuffer copy( sal_Int32 beginIndex ) const
{
return copy( beginIndex, getLength() - beginIndex );
}
/**
Returns a new string buffer 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.
@since LibreOffice 4.1
*/
OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
{
assert(beginIndex >= 0 && beginIndex <= getLength());
assert(count >= 0 && count <= getLength() - beginIndex);
rtl_uString *pNew = NULL;
rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
return OUStringBuffer( pNew, count + 16 );
}
private:
OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
{
pData = value;
nCapacity = capacity;
}
/**
A pointer to the data structure which contains the data.
*/
rtl_uString * pData;
/**
The len of the pData->buffer.
*/
sal_Int32 nCapacity;
};
#if defined LIBO_INTERNAL_ONLY
template<> struct ToStringHelper<OUStringBuffer> {
static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
sal_Unicode * operator()(sal_Unicode * buffer, OUStringBuffer const & s) const SAL_RETURNS_NONNULL
{ return addDataHelper(buffer, s.getStr(), s.getLength()); }
};
#endif
#if defined LIBO_INTERNAL_ONLY
// Define this here to avoid circular includes
inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
{
// Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
// acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
if (isEmpty())
return operator=(str.toString());
else
return internalAppend(str.pData);
}
inline OUString const& OUString::unacquired(const OUStringBuffer& str)
{
return unacquired(&str.pData);
}
#endif
}
#ifdef RTL_STRING_UNITTEST
namespace rtl
{
typedef rtlunittest::OUStringBuffer OUStringBuffer;
}
#endif
#if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
using ::rtl::OUStringBuffer;
#endif
#endif // INCLUDED_RTL_USTRBUF_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|