[r8234]: main / trunk / interpreter / classes / MutableBufferClass.cpp Maximize Restore History

Download this file

MutableBufferClass.cpp    1670 lines (1490 with data), 63.3 kB

   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
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
/******************************************************************************/
/* REXX Kernel */
/* */
/* Primitive MutableBuffer Class */
/* */
/******************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "RexxCore.h"
#include "StringClass.hpp"
#include "MutableBufferClass.hpp"
#include "ProtectedObject.hpp"
#include "StringUtil.hpp"
// singleton class instance
RexxClass *RexxMutableBuffer::classInstance = OREF_NULL;
/**
* Create initial class object at bootstrap time.
*/
void RexxMutableBuffer::createInstance()
{
CLASS_CREATE(MutableBuffer, "MutableBuffer", RexxClass);
}
#define DEFAULT_BUFFER_LENGTH 256
RexxMutableBuffer *RexxMutableBufferClass::newRexx(RexxObject **args, size_t argc)
/******************************************************************************/
/* Function: Allocate (and initialize) a string object */
/******************************************************************************/
{
RexxString *string;
RexxMutableBuffer *newBuffer; /* new mutable buffer object */
size_t bufferLength = DEFAULT_BUFFER_LENGTH;
size_t defaultSize;
if (argc >= 1)
{
if (args[0] != NULL)
{
/* force argument to string value */
string = stringArgument(args[0], ARG_ONE);
}
else
{
string = OREF_NULLSTRING; /* default to empty content */
}
}
else /* minimum buffer size given? */
{
string = OREF_NULLSTRING;
}
if (argc >= 2)
{
bufferLength = optionalLengthArgument(args[1], DEFAULT_BUFFER_LENGTH, ARG_TWO);
}
defaultSize = bufferLength; /* remember initial default size */
/* input string longer than demanded */
/* minimum size? expand accordingly */
if (string->getLength() > bufferLength)
{
bufferLength = string->getLength();
}
/* allocate the new object */
newBuffer = new ((RexxClass *)this) RexxMutableBuffer(bufferLength, defaultSize);
newBuffer->dataLength = string->getLength();
/* copy the content */
newBuffer->copyData(0, string->getStringData(), string->getLength());
ProtectedObject p(newBuffer);
newBuffer->sendMessage(OREF_INIT, args, argc > 2 ? argc - 2 : 0);
return newBuffer;
}
/**
* Default constructor.
*/
RexxMutableBuffer::RexxMutableBuffer()
{
bufferLength = DEFAULT_BUFFER_LENGTH; /* save the length of the buffer */
defaultSize = bufferLength; /* store the default buffer size */
// NB: we clear this before we allocate the new buffer because allocating the
// new buffer might trigger a garbage collection, causing us to mark bogus
// reference.
data = OREF_NULL;
data = new_buffer(bufferLength);
}
/**
* Constructor with explicitly set size and default.
*
* @param l Initial length.
* @param d The explicit default size.
*/
RexxMutableBuffer::RexxMutableBuffer(size_t l, size_t d)
{
bufferLength = l; /* save the length of the buffer */
defaultSize = d; /* store the default buffer size */
// NB: As in the default constructor, we clear this before we allocate the
// new buffer in case garbage collection is triggered.
data = OREF_NULL;
data = new_buffer(bufferLength);
}
/**
* Create a new mutable buffer object from a potential subclass.
*
* @param size The size of the buffer object.
*
* @return A new instance of a mutable buffer, with the default class
* behaviour.
*/
void *RexxMutableBuffer::operator new(size_t size)
{
return new_object(size, T_MutableBuffer);
}
/**
* Create a new mutable buffer object from a potential subclass.
*
* @param size The size of the buffer object.
* @param bufferClass
* The class of the buffer object.
*
* @return A new instance of a mutable buffer, with the target class
* behaviour.
*/
void *RexxMutableBuffer::operator new(size_t size, RexxClass *bufferClass)
{
RexxObject * newObj = new_object(size, T_MutableBuffer);
newObj->setBehaviour(bufferClass->getInstanceBehaviour());
return newObj;
}
void RexxMutableBuffer::live(size_t liveMark)
/******************************************************************************/
/* Function: Normal garbage collection live marking */
/******************************************************************************/
{
memory_mark(this->objectVariables);
memory_mark(this->data);
}
void RexxMutableBuffer::liveGeneral(int reason)
/******************************************************************************/
/* Function: Generalized object marking */
/******************************************************************************/
{
memory_mark_general(this->objectVariables);
memory_mark_general(this->data);
}
void RexxMutableBuffer::flatten(RexxEnvelope *envelope)
/******************************************************************************/
/* Function: Flatten a mutable buffer */
/******************************************************************************/
{
setUpFlatten(RexxMutableBuffer)
flatten_reference(newThis->data, envelope);
flatten_reference(newThis->objectVariables, envelope);
cleanUpFlatten
}
RexxObject *RexxMutableBuffer::copy()
/******************************************************************************/
/* Function: copy an object */
/******************************************************************************/
{
RexxMutableBuffer *newObj = (RexxMutableBuffer *)this->clone();
/* see the comments in ::newRexx()!! */
newObj->data = new_buffer(bufferLength);
newObj->dataLength = this->dataLength;
newObj->copyData(0, data->getData(), bufferLength);
newObj->defaultSize = this->defaultSize;
newObj->bufferLength = this->bufferLength;
return newObj;
}
void RexxMutableBuffer::ensureCapacity(size_t addedLength)
/******************************************************************************/
/* Function: append to the mutable buffer */
/******************************************************************************/
{
size_t resultLength = this->dataLength + addedLength;
if (resultLength > bufferLength)
{ /* need to enlarge? */
bufferLength *= 2; /* double the buffer */
if (bufferLength < resultLength)
{ /* still too small? use new length */
bufferLength = resultLength;
}
RexxBuffer *newBuffer = new_buffer(bufferLength);
// copy the data into the new buffer
newBuffer->copyData(0, data->getData(), dataLength);
// replace the old data buffer
OrefSet(this, this->data, newBuffer);
}
}
/**
* Return the length of the data in the buffer currently.
*
* @return The current length, as an Integer object.
*/
RexxObject *RexxMutableBuffer::lengthRexx()
{
return new_integer(dataLength);
}
RexxMutableBuffer *RexxMutableBuffer::append(RexxObject *obj)
/******************************************************************************/
/* Function: append to the mutable buffer */
/******************************************************************************/
{
RexxString *string = stringArgument(obj, ARG_ONE);
ProtectedObject p(string);
// make sure we have enough room
ensureCapacity(string->getLength());
copyData(dataLength, string->getStringData(), string->getLength());
this->dataLength += string->getLength();
return this;
}
RexxMutableBuffer *RexxMutableBuffer::insert(RexxObject *str, RexxObject *pos, RexxObject *len, RexxObject *pad)
/******************************************************************************/
/* Function: insert string at given position */
/******************************************************************************/
{
// force this into string form
RexxString * string = stringArgument(str, ARG_ONE);
// we're using optional length because 0 is valid for insert.
size_t begin = optionalNonNegative(pos, 0, ARG_TWO);
size_t insertLength = optionalLengthArgument(len, string->getLength(), ARG_THREE);
char padChar = optionalPadArgument(pad, ' ', ARG_FOUR);
size_t copyLength = Numerics::minVal(insertLength, string->getLength());
size_t padLength = insertLength - copyLength;
// if inserting within the current bounds, we only need to add the length
// if inserting beyond the end, we need to make sure we add space for the gap too
if (begin < dataLength)
{
// if inserting a zero length string, this is simple!
if (insertLength == 0)
{
return this; /* do nothing */
}
ensureCapacity(insertLength);
}
else
{
ensureCapacity(insertLength + (begin - dataLength));
}
/* create space in the buffer */
if (begin < dataLength)
{
openGap(begin, insertLength, dataLength - begin);
}
else if (begin > this->dataLength)
{
/* pad before insertion */
setData(dataLength, padChar, begin - dataLength);
}
/* insert string contents */
copyData(begin, string->getStringData(), copyLength);
// do we need data padding?
if (padLength > 0)
{
setData(begin + string->getLength(), padChar, padLength);
}
// inserting after the end? the resulting length is measured from the insertion point
if (begin > this->dataLength)
{
this->dataLength = begin + insertLength;
}
else
{
// just add in the inserted length
this->dataLength += insertLength;
}
return this;
}
RexxMutableBuffer *RexxMutableBuffer::overlay(RexxObject *str, RexxObject *pos, RexxObject *len, RexxObject *pad)
/******************************************************************************/
/* Function: replace characters in buffer contents */
/******************************************************************************/
{
RexxString *string = stringArgument(str, ARG_ONE);
size_t begin = optionalPositionArgument(pos, 1, ARG_TWO) - 1;
size_t replaceLength = optionalLengthArgument(len, string->getLength(), ARG_THREE);
char padChar = optionalPadArgument(pad, ' ', ARG_FOUR);
// make sure we have room for this
ensureCapacity(begin + replaceLength);
// is our start position beyond the current data end?
if (begin > dataLength)
{
// add padding to the gap
setData(dataLength, padChar, begin - dataLength);
}
// now overlay the string data
copyData(begin, string->getStringData(), Numerics::minVal(replaceLength, string->getLength()));
// do we need additional padding?
if (replaceLength > string->getLength())
{
// pad the section after the overlay
setData(begin + string->getLength(), padChar, replaceLength - string->getLength());
}
// did this add to the size?
if (begin + replaceLength > dataLength)
{
//adjust upward
dataLength = begin + replaceLength;
}
return this;
}
/**
* Replace a target substring within a string with
* a new string value. This is similar overlay, but
* replacing might cause the characters following the
* replacement position to be shifted to the left or
* right.
*
* @param str The replacement string.
* @param pos The target position (required).
* @param len The target length (optional). If not specified, the
* length of the replacement string is used, and this
* is essentially an overlay operation.
* @param pad A padding character if padding is required. The default
* pad is a ' '. Padding only occurs if the replacement
* position is beyond the current data length.
*
* @return The target mutablebuffer object.
*/
RexxMutableBuffer *RexxMutableBuffer::replaceAt(RexxObject *str, RexxObject *pos, RexxObject *len, RexxObject *pad)
{
RexxString *string = stringArgument(str, ARG_ONE);
size_t begin = positionArgument(pos, ARG_TWO) - 1;
size_t newLength = string->getLength();
size_t replaceLength = optionalLengthArgument(len, newLength, ARG_THREE);
char padChar = optionalPadArgument(pad, ' ', ARG_FOUR);
size_t finalLength;
// if replaceLength extends beyond the end of the string
// then we cut it.
if (begin > dataLength)
{
replaceLength = 0;
}
else if (begin + replaceLength > dataLength)
{
replaceLength = dataLength - begin;
}
// We need to add the delta between the excised string and the inserted
// replacement string.
//
// If this extends beyond the end of the string, then we require space for
// the position + the replacement string length. Else we find the required
// size (may be smaller than before)
if (begin > dataLength)
{
finalLength = begin - replaceLength + newLength;
}
else
{
finalLength = dataLength - replaceLength + newLength;
}
// make sure we have room for this
ensureCapacity(finalLength);
// is our start position beyond the current data end?
// NB: Even though we've adjusted the buffer size, the dataLength is still
// the original entry length.
if (begin > dataLength)
{
// add padding to the gap
setData(dataLength, padChar, begin - dataLength);
// now overlay the string data
copyData(begin, string->getStringData(), newLength);
}
else
{
// if the strings are of different lengths, we need to adjust the size
// of the gap we're copying into. Only adjust if there is a real gap
if (replaceLength != newLength && begin + replaceLength < dataLength)
{
// snip out the original string
adjustGap(begin, replaceLength, newLength);
}
// now overlay the string data
copyData(begin, string->getStringData(), newLength);
}
// and finally adjust the length
dataLength = finalLength;
// our return value is always the target mutable buffer
return this;
}
RexxMutableBuffer *RexxMutableBuffer::mydelete(RexxObject *_start, RexxObject *len)
/******************************************************************************/
/* Function: delete character range in buffer */
/******************************************************************************/
{
size_t begin = positionArgument(_start, ARG_ONE) - 1;
size_t range = optionalLengthArgument(len, this->data->getDataLength() - begin, ARG_TWO);
// is the begin point actually within the string?
if (begin < dataLength)
{ /* got some work to do? */
// deleting from the middle?
if (begin + range < dataLength)
{
// shift everything over
closeGap(begin, range, dataLength - (begin + range));
dataLength -= range;
}
else
{
// we're just truncating
dataLength = begin;
}
}
return this;
}
RexxObject *RexxMutableBuffer::setBufferSize(RexxInteger *size)
/******************************************************************************/
/* Function: set the size of the buffer */
/******************************************************************************/
{
size_t newsize = lengthArgument(size, ARG_ONE);
// has a reset to zero been requested?
if (newsize == 0)
{
// have we increased the buffer size?
if (bufferLength > defaultSize)
{
// reallocate the buffer
OrefSet(this, this->data, new_buffer(defaultSize));
// reset the size to the default
bufferLength = defaultSize;
}
dataLength = 0;
}
// an actual resize?
else if (newsize != bufferLength)
{
// reallocate the buffer
RexxBuffer *newBuffer = new_buffer(newsize);
// if we're shrinking this, it truncates.
dataLength = Numerics::minVal(dataLength, newsize);
newBuffer->copyData(0, data->getData(), dataLength);
// replace the old buffer
OrefSet(this, this->data, newBuffer);
// and update the size....
bufferLength = newsize;
}
return this;
}
RexxString *RexxMutableBuffer::makeString()
/******************************************************************************/
/* Function: Handle a REQUEST('STRING') request for a mutablebuffer object */
/******************************************************************************/
{
return new_string(data->getData(), dataLength);
}
/**
* Baseclass optimization for handling request array calls.
*
* @return The string object converted to an array using default arguments.
*/
RexxArray *RexxMutableBuffer::makeArray()
{
// forward to the Rexx version with default arguments
return this->makeArrayRexx(OREF_NULL);
}
/**
* Handle the primitive class makeString optimization. This
* is required because MutableBuffer implements a
* STRING method.
*
* @return The string value of the buffer
*/
RexxString *RexxMutableBuffer::primitiveMakeString()
{
// go straight to the string handler
return this->makeString();
}
/******************************************************************************/
/* Arguments: String position for substr */
/* requested length of new string */
/* pad character to use, if necessary */
/* */
/* Returned: string, sub string of original. */
/******************************************************************************/
RexxString *RexxMutableBuffer::substr(RexxInteger *argposition,
RexxInteger *arglength,
RexxString *pad)
{
return StringUtil::substr(getStringData(), getLength(), argposition, arglength, pad);
}
/**
* Perform a search for a string within the buffer.
*
* @param needle The search needle.
* @param pstart the starting position.
*
* @return The index of the located string. Returns 0 if no matches
* are found.
*/
RexxInteger *RexxMutableBuffer::posRexx(RexxString *needle, RexxInteger *pstart, RexxInteger *range)
{
return StringUtil::posRexx(getStringData(), getLength(), needle, pstart, range);
}
/**
* Perform a search for the last position of a string within the
* buffer.
*
* @param needle The search needle.
* @param pstart the starting position.
*
* @return The index of the located string. Returns 0 if no matches
* are found.
*/
RexxInteger *RexxMutableBuffer::lastPos(RexxString *needle, RexxInteger *_start, RexxInteger *_range)
{
return StringUtil::lastPosRexx(getStringData(), getLength(), needle, _start, _range);
}
/**
* Perform a caseless search for a string within the buffer.
*
* @param needle The search needle.
* @param pstart the starting position.
*
* @return The index of the located string. Returns 0 if no matches
* are found.
*/
RexxInteger *RexxMutableBuffer::caselessPos(RexxString *needle, RexxInteger *pstart, RexxInteger *range)
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
/* get the starting position */
size_t _start = optionalPositionArgument(pstart, 1, ARG_TWO);
size_t _range = optionalLengthArgument(range, getLength() - _start + 1, ARG_THREE);
/* pass on to the primitive function */
/* and return as an integer object */
return new_integer(StringUtil::caselessPos(getStringData(), getLength(), needle , _start - 1, _range));
}
/**
* Perform a caseless search for the last position of a string
* within the buffer.
*
* @param needle The search needle.
* @param pstart the starting position.
*
* @return The index of the located string. Returns 0 if no matches
* are found.
*/
RexxInteger *RexxMutableBuffer::caselessLastPos(RexxString *needle, RexxInteger *pstart, RexxInteger *range)
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
/* get the starting position */
size_t _start = optionalPositionArgument(pstart, getLength(), ARG_TWO);
size_t _range = optionalLengthArgument(range, getLength(), ARG_THREE);
/* pass on to the primitive function */
/* and return as an integer object */
return new_integer(StringUtil::caselessLastPos(getStringData(), getLength(), needle , _start, _range));
}
/**
* Extract a single character from a string object.
* Returns a null string if the specified position is
* beyond the bounds of the string.
*
* @param positionArg
* The position of the target character. Must be a positive
* whole number.
*
* @return Returns the single character at the target position.
* Returns a null string if the position is beyond the end
* of the string.
*/
RexxString *RexxMutableBuffer::subchar(RexxInteger *positionArg)
{
return StringUtil::subchar(getStringData(), getLength(), positionArg);
}
RexxArray *RexxMutableBuffer::makeArrayRexx(RexxString *div)
/******************************************************************************/
/* Function: Split string into an array */
/******************************************************************************/
{
return StringUtil::makearray(getStringData(), getLength(), div);
}
RexxInteger *RexxMutableBuffer::countStrRexx(RexxString *needle)
/******************************************************************************/
/* Function: Count occurrences of one string in another. */
/******************************************************************************/
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
// delegate the counting to the string util
return new_integer(StringUtil::countStr(getStringData(), getLength(), needle));
}
RexxInteger *RexxMutableBuffer::caselessCountStrRexx(RexxString *needle)
/******************************************************************************/
/* Function: Count occurrences of one string in another. */
/******************************************************************************/
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
// delegate the counting to the string util
return new_integer(StringUtil::caselessCountStr(getStringData(), getLength(), needle));
}
/**
* Do an inplace changeStr operation on a mutablebuffer.
*
* @param needle The search needle.
* @param newNeedle The replacement string.
* @param countArg The number of occurrences to replace.
*
* @return The target MutableBuffer
*/
RexxMutableBuffer *RexxMutableBuffer::changeStr(RexxString *needle, RexxString *newNeedle, RexxInteger *countArg)
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
/* newneedle must be a string two */
newNeedle = stringArgument(newNeedle, ARG_TWO);
// we'll only change up to a specified count. If not there, we do everything.
size_t count = optionalPositive(countArg, Numerics::MAX_WHOLENUMBER, ARG_THREE);
// find the number of matches in the string
size_t matches = StringUtil::countStr(getStringData(), getLength(), needle);
if (matches > count) // the matches are bounded by the count
{
matches = count;
}
// no matches is easy!
if (matches == 0)
{
return this;
}
size_t needleLength = needle->getLength(); /* get the length of the needle */
size_t newLength = newNeedle->getLength(); /* and the replacement length */
// calculate the final length and make sure we have enough space
size_t resultLength = this->getLength() - (matches * needleLength) + (matches * newLength);
ensureCapacity(resultLength);
// an inplace update has complications, depending on whether the new string is shorter,
// the same length, or longer
// simplest case...same length strings. We can just overlay the existing occurrences
if (needleLength == newLength)
{
const char *source = getStringData();
size_t sourceLength = getLength();
size_t _start = 0; /* set a zero starting point */
for (size_t i = 0; i < matches; i++)
{
// search for the next occurrence...which should be there because we
// already know the count
size_t matchPos = StringUtil::pos(source, sourceLength, needle, _start, sourceLength);
copyData(matchPos - 1, newNeedle->getStringData(), newLength);
// step to the next search position
_start = matchPos + newLength - 1;
}
}
// this will be a shorter thing, so we can do things in place as if we were using two buffers
else if (needleLength > newLength)
{
// we start building from the beginning
size_t copyOffset = 0;
size_t _start = 0;
// get our string bounds
const char *source = getStringData();
size_t sourceLength = getLength();
const char *newPtr = newNeedle->getStringData();
// this is our scan offset
for (size_t i = 0; i < matches; i++)
{
// look for each instance and replace
size_t matchPos = StringUtil::pos(source, sourceLength, needle, _start, sourceLength);
size_t copyLength = (matchPos - 1) - _start; /* get the next length to copy */
// if this skipped over characters, we need to copy those
if (copyLength != 0)
{
copyData(copyOffset, source + _start, copyLength);
copyOffset += copyLength;
}
// replacing with a non-null string, copy the replacement string in
if (newLength != 0)
{
copyData(copyOffset, newPtr, newLength);
copyOffset += newLength;
}
_start = matchPos + needleLength - 1; /* step to the next position */
}
// we likely have some remainder that needs copying
if (_start < sourceLength)
{
copyData(copyOffset, source + _start, sourceLength - _start);
}
}
// hardest case...the string gets longer. We need to shift all of the data
// to the end and then pull the pieces back in as we go
else
{
size_t growth = (newLength - needleLength) * matches;
// we start building from the beginning
size_t copyOffset = 0;
size_t _start = 0;
// get our string bounds
const char *source = getStringData() + growth;
size_t sourceLength = getLength();
// this shifts everything to the end of the buffer. From there,
// we pull pieces back into place.
openGap(0, growth, sourceLength);
const char *newPtr = newNeedle->getStringData();
// this is our scan offset
for (size_t i = 0; i < matches; i++)
{
// look for each instance and replace
size_t matchPos = StringUtil::pos(source, sourceLength, needle, _start, sourceLength);
size_t copyLength = (matchPos - 1) - _start; /* get the next length to copy */
// if this skipped over characters, we need to copy those
if (copyLength != 0)
{
copyData(copyOffset, source + _start, copyLength);
copyOffset += copyLength;
}
// replacing with a non-null string, copy the replacement string in
if (newLength != 0)
{
copyData(copyOffset, newPtr, newLength);
copyOffset += newLength;
}
_start = matchPos + needleLength - 1; /* step to the next position */
}
// we likely have some remainder that needs copying
if (_start < sourceLength)
{
copyData(copyOffset, source + _start, sourceLength - _start);
}
}
// update the result length, and return
dataLength = resultLength;
return this;
}
/**
* Do an inplace caseless changeStr operation on a
* mutablebuffer.
*
* @param needle The search needle.
* @param newNeedle The replacement string.
* @param countArg The number of occurrences to replace.
*
* @return The target MutableBuffer
*/
RexxMutableBuffer *RexxMutableBuffer::caselessChangeStr(RexxString *needle, RexxString *newNeedle, RexxInteger *countArg)
{
/* force needle to a string */
needle = stringArgument(needle, ARG_ONE);
/* newneedle must be a string two */
newNeedle = stringArgument(newNeedle, ARG_TWO);
// we'll only change up to a specified count. If not there, we do everything.
size_t count = optionalPositive(countArg, Numerics::MAX_WHOLENUMBER, ARG_THREE);
// find the number of matches in the string
size_t matches = StringUtil::caselessCountStr(getStringData(), getLength(), needle);
if (matches > count) // the matches are bounded by the count
{
matches = count;
}
// no matches is easy!
if (matches == 0)
{
return this;
}
size_t needleLength = needle->getLength(); /* get the length of the needle */
size_t newLength = newNeedle->getLength(); /* and the replacement length */
// calculate the final length and make sure we have enough space
size_t resultLength = this->getLength() - (matches * needleLength) + (matches * newLength);
ensureCapacity(resultLength);
// an inplace update has complications, depending on whether the new string is shorter,
// the same length, or longer
// simplest case...same length strings. We can just overlay the existing occurrences
if (needleLength == newLength)
{
const char *source = getStringData();
size_t sourceLength = getLength();
size_t _start = 0; /* set a zero starting point */
for (size_t i = 0; i < matches; i++)
{
// search for the next occurrence...which should be there because we
// already know the count
size_t matchPos = StringUtil::caselessPos(source, sourceLength, needle, _start, sourceLength);
copyData(matchPos - 1, newNeedle->getStringData(), newLength);
// step to the next search position
_start = matchPos + newLength - 1;
}
}
// this will be a shorter thing, so we can do things in place as if we were using two buffers
else if (needleLength > newLength)
{
// we start building from the beginning
size_t copyOffset = 0;
size_t _start = 0;
// get our string bounds
const char *source = getStringData();
size_t sourceLength = getLength();
const char *newPtr = newNeedle->getStringData();
// this is our scan offset
for (size_t i = 0; i < matches; i++)
{
// look for each instance and replace
size_t matchPos = StringUtil::caselessPos(source, sourceLength, needle, _start, sourceLength);
size_t copyLength = (matchPos - 1) - _start; /* get the next length to copy */
// if this skipped over characters, we need to copy those
if (copyLength != 0)
{
copyData(copyOffset, source + _start, copyLength);
copyOffset += copyLength;
}
// replacing with a non-null string, copy the replacement string in
if (newLength != 0)
{
copyData(copyOffset, newPtr, newLength);
copyOffset += newLength;
}
_start = matchPos + needleLength - 1; /* step to the next position */
}
// we likely have some remainder that needs copying
if (_start < sourceLength)
{
copyData(copyOffset, source + _start, sourceLength - _start);
}
}
// hardest case...the string gets longer. We need to shift all of the data
// to the end and then pull the pieces back in as we go
else
{
size_t growth = (newLength - needleLength) * matches;
// we start building from the beginning
size_t copyOffset = 0;
size_t _start = 0;
// get our string bounds
const char *source = getStringData() + growth;
size_t sourceLength = getLength();
// this shifts everything to the end of the buffer. From there,
// we pull pieces back into place.
openGap(0, growth, sourceLength);
const char *newPtr = newNeedle->getStringData();
// this is our scan offset
for (size_t i = 0; i < matches; i++)
{
// look for each instance and replace
size_t matchPos = StringUtil::caselessPos(source, sourceLength, needle, _start, sourceLength);
size_t copyLength = (matchPos - 1) - _start; /* get the next length to copy */
// if this skipped over characters, we need to copy those
if (copyLength != 0)
{
copyData(copyOffset, source + _start, copyLength);
copyOffset += copyLength;
}
// replacing with a non-null string, copy the replacement string in
if (newLength != 0)
{
copyData(copyOffset, newPtr, newLength);
copyOffset += newLength;
}
_start = matchPos + needleLength - 1; /* step to the next position */
}
// we likely have some remainder that needs copying
if (_start < sourceLength)
{
copyData(copyOffset, source + _start, sourceLength - _start);
}
}
// update the result length, and return
dataLength = resultLength;
return this;
}
/**
* Rexx exported method stub for the lower() method.
*
* @param start The optional starting location. Defaults to the first character
* if not specified.
* @param length The length to convert. Defaults to the segment from the start
* position to the end of the string.
*
* @return A new string object with the case conversion applied.
*/
RexxMutableBuffer *RexxMutableBuffer::lower(RexxInteger *_start, RexxInteger *_length)
{
size_t startPos = optionalPositionArgument(_start, 1, ARG_ONE) - 1;
size_t rangeLength = optionalLengthArgument(_length, getLength(), ARG_TWO);
// if we're starting beyond the end bounds, return unchanged
if (startPos >= getLength())
{
return this;
}
rangeLength = Numerics::minVal(rangeLength, getLength() - startPos);
// a zero length value is also a non-change.
if (rangeLength == 0)
{
return this;
}
char *bufferData = getData() + startPos;
// now uppercase in place
for (size_t i = 0; i < rangeLength; i++)
{
*bufferData = tolower(*bufferData);
bufferData++;
}
return this;
}
/**
* Rexx exported method stub for the upper() method.
*
* @param start The optional starting location. Defaults to the first character
* if not specified.
* @param length The length to convert. Defaults to the segment from the start
* position to the end of the string.
*
* @return A new string object with the case conversion applied.
*/
RexxMutableBuffer *RexxMutableBuffer::upper(RexxInteger *_start, RexxInteger *_length)
{
size_t startPos = optionalPositionArgument(_start, 1, ARG_ONE) - 1;
size_t rangeLength = optionalLengthArgument(_length, getLength(), ARG_TWO);
// if we're starting beyond the end bounds, return unchanged
if (startPos >= getLength())
{
return this;
}
rangeLength = Numerics::minVal(rangeLength, getLength() - startPos);
// a zero length value is also a non-change.
if (rangeLength == 0)
{
return this;
}
char *bufferData = getData() + startPos;
// now uppercase in place
for (size_t i = 0; i < rangeLength; i++)
{
*bufferData = toupper(*bufferData);
bufferData++;
}
return this;
}
/**
* translate characters in the buffer using a translation table.
*
* @param tableo The output table specification
* @param tablei The input table specification
* @param pad An optional padding character (default is a space).
* @param _start The starting position to translate.
* @param _range The length to translate
*
* @return The target mutable buffer.
*/
RexxMutableBuffer *RexxMutableBuffer::translate(RexxString *tableo, RexxString *tablei, RexxString *pad, RexxInteger *_start, RexxInteger *_range)
{
// just a simple uppercase?
if (tableo == OREF_NULL && tablei == OREF_NULL && pad == OREF_NULL)
{
return this->upper(_start, _range);
}
/* validate the tables */
tableo = optionalStringArgument(tableo, OREF_NULLSTRING, ARG_ONE);
size_t outTableLength = tableo->getLength(); /* get the table length */
/* input table too */
tablei = optionalStringArgument(tablei, OREF_NULLSTRING, ARG_TWO);
size_t inTableLength = tablei->getLength(); /* get the table length */
const char *inTable = tablei->getStringData(); /* point at the input table */
const char *outTable = tableo->getStringData(); /* and the output table */
/* get the pad character */
char padChar = optionalPadArgument(pad, ' ', ARG_THREE);
size_t startPos = optionalPositionArgument(_start, 1, ARG_FOUR);
size_t range = optionalLengthArgument(_range, getLength() - startPos + 1, ARG_FOUR);
// if nothing to translate, we can return now
if (startPos > getLength() || range == 0)
{
return this;
}
// cape the real range
range = Numerics::minVal(range, getLength() - startPos + 1);
char *scanPtr = getData() + startPos - 1; /* point to data */
size_t scanLength = range; /* get the length too */
while (scanLength--)
{ /* spin thru input */
char ch = *scanPtr; /* get a character */
size_t position;
if (tablei != OREF_NULLSTRING) /* input table specified? */
{
/* search for the character */
position = StringUtil::memPos(inTable, inTableLength, ch);
}
else
{
position = ((size_t)ch) & 0xff; /* position is the character value */
}
if (position != (size_t)(-1))
{ /* found in the table? */
if (position < outTableLength) /* in the output table? */
{
/* convert the character */
*scanPtr = *(outTable + position);
}
else
{
*scanPtr = padChar; /* else use the pad character */
}
}
scanPtr++; /* step the pointer */
}
return this;
}
/**
* Test if regions within two strings match.
*
* @param start_ The starting compare position within the target string. This
* must be within the bounds of the string.
* @param other The other compare string.
* @param offset_ The starting offset of the compare string. This must be
* within the string bounds. The default start postion is 1.
* @param len_ The length of the compare substring. The length and the
* offset must specify a valid substring of other. If not
* specified, this defaults to the substring from the
* offset to the end of the string.
*
* @return True if the two regions match, false for any mismatch.
*/
RexxInteger *RexxMutableBuffer::match(RexxInteger *start_, RexxString *other, RexxInteger *offset_, RexxInteger *len_)
{
stringsize_t _start = positionArgument(start_, ARG_ONE);
// the start position must be within the string bounds
if (_start > getLength())
{
reportException(Error_Incorrect_method_position, start_);
}
other = stringArgument(other, ARG_TWO);
stringsize_t offset = optionalPositionArgument(offset_, 1, ARG_THREE);
if (offset > other->getLength())
{
reportException(Error_Incorrect_method_position, offset);
}
stringsize_t len = optionalLengthArgument(len_, other->getLength() - offset + 1, ARG_FOUR);
if ((offset + len - 1) > other->getLength())
{
reportException(Error_Incorrect_method_length, len);
}
return primitiveMatch(_start, other, offset, len) ? TheTrueObject : TheFalseObject;
}
/**
* Test if regions within two strings match.
*
* @param start_ The starting compare position within the target string. This
* must be within the bounds of the string.
* @param other The other compare string.
* @param offset_ The starting offset of the compare string. This must be
* within the string bounds. The default start postion is 1.
* @param len_ The length of the compare substring. The length and the
* offset must specify a valid substring of other. If not
* specified, this defaults to the substring from the
* offset to the end of the string.
*
* @return True if the two regions match, false for any mismatch.
*/
RexxInteger *RexxMutableBuffer::caselessMatch(RexxInteger *start_, RexxString *other, RexxInteger *offset_, RexxInteger *len_)
{
stringsize_t _start = positionArgument(start_, ARG_ONE);
// the start position must be within the string bounds
if (_start > getLength())
{
reportException(Error_Incorrect_method_position, start_);
}
other = stringArgument(other, ARG_TWO);
stringsize_t offset = optionalPositionArgument(offset_, 1, ARG_THREE);
if (offset > other->getLength())
{
reportException(Error_Incorrect_method_position, offset);
}
stringsize_t len = optionalLengthArgument(len_, other->getLength() - offset + 1, ARG_FOUR);
if ((offset + len - 1) > other->getLength())
{
reportException(Error_Incorrect_method_length, len);
}
return primitiveCaselessMatch(_start, other, offset, len) ? TheTrueObject : TheFalseObject;
}
/**
* Perform a compare of regions of two string objects. Returns
* true if the two regions match, returns false for mismatches.
*
* @param start The starting offset within the target string.
* @param other The source string for the compare.
* @param offset The offset of the substring of the other string to use.
* @param len The length of the substring to compare.
*
* @return True if the regions match, false otherwise.
*/
bool RexxMutableBuffer::primitiveMatch(stringsize_t _start, RexxString *other, stringsize_t offset, stringsize_t len)
{
_start--; // make the starting point origin zero
offset--;
// if the match is not possible in the target string, just return false now.
if ((_start + len) > getLength())
{
return false;
}
return memcmp(getStringData() + _start, other->getStringData() + offset, len) == 0;
}
/**
* Perform a caselesee compare of regions of two string objects.
* Returns true if the two regions match, returns false for
* mismatches.
*
* @param start The starting offset within the target string.
* @param other The source string for the compare.
* @param offset The offset of the substring of the other string to use.
* @param len The length of the substring to compare.
*
* @return True if the regions match, false otherwise.
*/
bool RexxMutableBuffer::primitiveCaselessMatch(stringsize_t _start, RexxString *other, stringsize_t offset, stringsize_t len)
{
_start--; // make the starting point origin zero
offset--;
// if the match is not possible in the target string, just return false now.
if ((_start + len) > getLength())
{
return false;
}
return StringUtil::caselessCompare(getStringData() + _start, other->getStringData() + offset, len) == 0;
}
/**
* Compare a single character at a give position against
* a set of characters to see if any of the characters is
* a match.
*
* @param position_ The character position
* @param matchSet The set to compare against.
*
* @return true if the character at the give position is any of the characters,
* false if none of them match.
*/
RexxInteger *RexxMutableBuffer::matchChar(RexxInteger *position_, RexxString *matchSet)
{
stringsize_t position = positionArgument(position_, ARG_ONE);
// the start position must be within the string bounds
if (position > getLength())
{
reportException(Error_Incorrect_method_position, position);
}
matchSet = stringArgument(matchSet, ARG_TWO);
stringsize_t _setLength = matchSet->getLength();
char _matchChar = getChar(position - 1);
// iterate through the match set looking for a match
for (stringsize_t i = 0; i < _setLength; i++)
{
if (_matchChar == matchSet->getChar(i))
{
return TheTrueObject;
}
}
return TheFalseObject;
}
/**
* Compare a single character at a give position against
* a set of characters to see if any of the characters is
* a match.
*
* @param position_ The character position
* @param matchSet The set to compare against.
*
* @return true if the character at the give position is any of the characters,
* false if none of them match.
*/
RexxInteger *RexxMutableBuffer::caselessMatchChar(RexxInteger *position_, RexxString *matchSet)
{
stringsize_t position = positionArgument(position_, ARG_ONE);
// the start position must be within the string bounds
if (position > getLength())
{
reportException(Error_Incorrect_method_position, position);
}
matchSet = stringArgument(matchSet, ARG_TWO);
stringsize_t _setLength = matchSet->getLength();
char _matchChar = getChar(position - 1);
_matchChar = toupper(_matchChar);
// iterate through the match set looking for a match, using a
// caseless compare
for (stringsize_t i = 0; i < _setLength; i++)
{
if (_matchChar == toupper(matchSet->getChar(i)))
{
return TheTrueObject;
}
}
return TheFalseObject;
}
/**
* Perform a character verify operation on a mutable buffer.
*
* @param ref The reference string.
* @param option The match/nomatch option.
* @param _start The start position for the verify.
* @param range The range to search
*
* @return The offset of the first match/mismatch within the buffer.
*/
RexxInteger *RexxMutableBuffer::verify(RexxString *ref, RexxString *option, RexxInteger *_start, RexxInteger *range)
{
return StringUtil::verify(getStringData(), getLength(), ref, option, _start, range);
}
/**
* Perform a subword extraction from a mutable buffer.
*
* @param position The first word to be extracted.
* @param plength The number of words to extract.
*
* @return The substring containing the extacted words.
*/
RexxString *RexxMutableBuffer::subWord(RexxInteger *position, RexxInteger *plength)
{
return StringUtil::subWord(getStringData(), getLength(), position, plength);
}
/**
* Returns an array of all words contained in the given range
* of the string, using the same extraction rules used
* for subWord() and word().
*
* @param position The optional starting position. If not provided, extraction
* starts with the first word.
* @param plength The number of words to extract. If omitted, will extract
* from the starting postion to the end of the string.
*
* @return An array containing the extracted words. If no words are
* available within the given range, this returns an empty
* array.
*/
RexxArray *RexxMutableBuffer::subWords(RexxInteger *position, RexxInteger *plength)
{
return StringUtil::subWords(getStringData(), getLength(), position, plength);
}
/**
* Extract a given word from a mutable buffer.
*
* @param position The target word position.
*
* @return The extracted word, as a string.
*/
RexxString *RexxMutableBuffer::word(RexxInteger *position)
{
return StringUtil::word(getStringData(), getLength(), position);
}
/**
* return the index of a given word position in a mutable buffer
*
*
* @param position The target word position.
*
* @return The position of the target word.
*/
RexxInteger *RexxMutableBuffer::wordIndex(RexxInteger *position)
{
return StringUtil::wordIndex(getStringData(), getLength(), position);
}
/**
* return the length of a given word position in a mutable
* buffer
*
*
* @param position The target word position.
*
* @return The length of the target word.
*/
RexxInteger *RexxMutableBuffer::wordLength(RexxInteger *position)
{
return StringUtil::wordLength(getStringData(), getLength(), position);
}
/**
* Return the count of words in the buffer.
*
* @return The buffer word count.
*/
RexxInteger *RexxMutableBuffer::words()
{
size_t tempCount = StringUtil::wordCount(this->getStringData(), this->getLength());
return new_integer(tempCount);
}
/**
* Perform a wordpos search on a mutablebuffer object.
*
* @param phrase The search phrase
* @param pstart The starting search position.
*
* @return The index of the match location.
*/
RexxInteger *RexxMutableBuffer::wordPos(RexxString *phrase, RexxInteger *pstart)
{
return StringUtil::wordPos(getStringData(), getLength(), phrase, pstart);
}
/**
* Perform a caseless wordpos search on a string object.
*
* @param phrase The search phrase
* @param pstart The starting search position.
*
* @return The index of the match location.
*/
RexxInteger *RexxMutableBuffer::caselessWordPos(RexxString *phrase, RexxInteger *pstart)
{
return StringUtil::caselessWordPos(getStringData(), getLength(), phrase, pstart);
}
/**
* Perform a delword operation on a mutable buffer
*
* @param position The position to delete.
* @param plength The number of words to delete
*
* @return Always returns the target mutable buffer.
*/
RexxMutableBuffer *RexxMutableBuffer::delWord(RexxInteger *position, RexxInteger *plength)
{
/* convert position to binary */
size_t _wordPos = positionArgument(position, ARG_ONE);
/* get num of words to delete, the */
/* default is "a very large number" */
size_t count = optionalLengthArgument(plength, Numerics::MAX_WHOLENUMBER, ARG_TWO);
size_t length = getLength(); /* get string length */
if (length == 0) /* null string? */
{
return this; /* nothing to delete */
}
if (count == 0) /* deleting zero words? */
{
return this; /* also very easy */
}
const char *_word = getStringData(); /* point to the string */
const char *nextSite = NULL;
/* get the first word */
size_t _wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
while (--_wordPos > 0 && _wordLength != 0)
{ /* loop until we reach tArget */
_word = nextSite; /* copy the start pointer */
/* get the next word */
_wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
}
if (_wordPos != 0) /* run out of words first */
{
return this; /* return the buffer unaltered */
}
// get the deletion point as an offset
size_t deletePosition = _word - this->getStringData();
while (--count > 0 && _wordLength != 0)
{ /* loop until we reach tArget */
_word = nextSite; /* copy the start pointer */
/* get the next word */
_wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
}
if (length != 0) /* didn't use up the string */
{
StringUtil::skipBlanks(&nextSite, &length);/* skip over trailing blanks */
}
size_t gapSize = dataLength - (deletePosition + length);
// close up the delete part
closeGap(deletePosition, gapSize, length);
// adjust for the deleted data
dataLength -= gapSize;
return this;
}
/**
* Do an inplace space() operation on a mutable buffer.
*
* @param space_count The number of pad characters between
* each word
* @param pad The pad character
*
* @return The target MutableBuffer
*/
RexxMutableBuffer *RexxMutableBuffer::space(RexxInteger *space_count, RexxString *pad)
{
size_t count = 0; /* count word interstices in buffer*/
/* get the spacing count */
const size_t padLength = optionalLengthArgument(space_count, 1, ARG_ONE);
/* get the pad character */
const char padChar = optionalPadArgument(pad, ' ', ARG_TWO);
// an inplace update has complications, depending on whether the new string
// is shorter or longer than the original.
// first execute padC with padLength == 0,1; later expand padC to padLength
const char padC = ' '; /* intermediate pad: single space */
const size_t padL = 1; /* intermediate pad length: 1 */
// With padC the new string is not longer, so we can just overlay in place.
// Set write position to start of buffer
// Find first word: start position and length
// While a word is found:
// Copy word to write position
// update write position
// Find next word: start position and length
// if no next word exists then leave
// select spacing count:
// when = 1 then append padChar and update write position
// when = 0 then don't pad
// otherwise append padC and update write position
// increment word interstice count
// iterate
// adjust string dataLength to write position
size_t writePos = 0; /* offset current write position */
const char *_word = getStringData(); /* point to the start of string */
const char *nextSite = NULL; /* start of the next word */
size_t length = getLength(); /* get string data length */
/* get the first word */
size_t _wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
while (_wordLength != 0)
{
/* copy first word to writePos */
copyData(writePos, _word, _wordLength);
writePos += _wordLength; /* update writePos for next word */
_word = nextSite; /* set start pointer to next word */
/* get the next word */
_wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
if (_wordLength == 0) /* is there no next word coming ? */
{
break; /* don't pad or count last word */
}
switch (padLength) /* handle different padLength */
{
case 1: /* more frequent case goes first */
setData(writePos, padChar, padLength); /* write pad character */
writePos += padLength; /* move write position one byte */
break;
case 0:
break; /* don't write pad character */
default: /* padLength > 1 */
setData(writePos, padC, padL); /* write padC pad character */
writePos += padL; /* move write position one byte */
}
count++; /* increment the word count */
}
this->dataLength = writePos; /* set data length in buffer */
if ( padLength > 1 ) /* do we need to expand padC ? */
{
size_t growth = count * (padLength-1); /* data grows by so many bytes */
ensureCapacity(growth); /* make sure we have room for this */
// As the string gets longer, we need to shift all data to the end and
// then pull the pieces back in as we go.
length = getLength(); /* get current string data length */
openGap(0, growth, length); /* shift towards end of the buffer */
writePos = 0;
while (growth>0)
{
setData(writePos, padC, padL); /* fill gap with whitespace */
writePos++;
growth--;
}
dataLength = getLength() + count * (padLength-1);/*adjust data to size*/
// Now we do the last loop over, using padChar and padLength
writePos = 0; /* offset current write position */
const char *_word = getStringData(); /*point to the start of string*/
const char *nextSite = NULL; /* start of the next word */
length = this->dataLength; /* get current string data length */
/* get the first word */
_wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
while (_wordLength != 0) /* while there is a word ... */
{
/* copy first word to writePos */
copyData(writePos, _word, _wordLength);
writePos += _wordLength; /* update writePos for next word */
_word = nextSite; /* set start pointer to next word */
/* get the next word */
_wordLength = StringUtil::nextWord(&_word, &length, &nextSite);
if (_wordLength != 0) /* except for the last word */
{
setData(writePos, padChar, padLength); /* write padChar chars */
writePos += padLength; /* update writePos for next word */
}
}
}
return this; /* return the mutable buffer */
}