forked from verhas/ScriptBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
4287 lines (3707 loc) · 118 KB
/
string.c
File metadata and controls
4287 lines (3707 loc) · 118 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
/*string.c
--GNU LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include "../command.h"
#include "../match.h"
#include "../matchc.h"
#define FMT_xMIN 1e-8 /* lowest limit to use the exp. format */
#define FMT_xMAX 1e+9 /* highest limit to use the exp. format */
#define FMT_RND 9 /* rounding on x digits */
#define FMT_xRND 1e+9 /* 1 * 10 ^ FMT_RND */
#define FMT_xRND2 1e+8 /* 1 * 10 ^ (FMT_RND-1) */
static double nfta_eplus[]=
{
1e+8, 1e+16, 1e+24, 1e+32, 1e+40, 1e+48, 1e+56, 1e+64, /* 8 */
1e+72, 1e+80, 1e+88, 1e+96, 1e+104, 1e+112, 1e+120, 1e+128, /* 16 */
1e+136, 1e+144, 1e+152, 1e+160, 1e+168, 1e+176, 1e+184, 1e+192, /* 24 */
1e+200, 1e+208, 1e+216, 1e+224, 1e+232, 1e+240, 1e+248, 1e+256, /* 32 */
1e+264, 1e+272, 1e+280, 1e+288, 1e+296, 1e+304 /* 38 */
};
static double nfta_eminus[]=
{
1e-8, 1e-16, 1e-24, 1e-32, 1e-40, 1e-48, 1e-56, 1e-64, /* 8 */
1e-72, 1e-80, 1e-88, 1e-96, 1e-104, 1e-112, 1e-120, 1e-128, /* 16 */
1e-136, 1e-144, 1e-152, 1e-160, 1e-168, 1e-176, 1e-184, 1e-192, /* 24 */
1e-200, 1e-208, 1e-216, 1e-224, 1e-232, 1e-240, 1e-248, 1e-256, /* 32 */
1e-264, 1e-272, 1e-280, 1e-288, 1e-296, 1e-304 /* 38 */
};
/*
String operator and functions
This file conatins the commands for the string operator concatendate and
string handling functions. When string parameters are needed conversion is done
automatic as usually.
*/
/* stringcompare two sub strings case sensitiveor case insensitive
return 0 if they match and return non zero otherwise (used in InStr)
*/
static int SUBSTRCMP(char *a, char *b, long length, int iCase){
char ca,cb;
iCase &= 1;/* only the lowest bit is about case sensitivity */
while( length-- ){
ca = *a;
cb = *b;
if( iCase ){
if( isupper(ca) )ca = tolower(ca);
if( isupper(cb) )cb = tolower(cb);
}
if( ca != cb )return ( (ca)-(cb) );
a++;
b++;
}
return 0;
}
/**CONCATENATE
=section string
=display &
=title Concatenate operator &
This operator concatenates two strings. The resulting string will contain the characters of the string standing on the left side of the operator followed by the characters of the string standing on the right hand side of the operator. The ScriptBasic interpreter automatically allocates the resulting string.
*/
COMMAND(CONCATENATE)
#if NOTIMP_CONCATENATE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2;
long lFinalStringLength,lLen;
char *s,*r;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* CONVERT2STRING never modifies the parameter, therefore it is more efficient
to use _EVALUATEEXPRESSION */
Op1 = CONVERT2STRING(_EVALUATEEXPRESSION(CAR(nItem)));
ASSERTOKE;
nItem = CDR(nItem);
Op2 = CONVERT2STRING(_EVALUATEEXPRESSION(CAR(nItem)));
ASSERTOKE;
lFinalStringLength = Op1 ? STRLEN(Op1) : 0;
lFinalStringLength += Op2 ? STRLEN(Op2) : 0;
RESULT = NEWMORTALSTRING(lFinalStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
/* copy the characters of the strings to the new location */
s = Op1 ? STRINGVALUE(Op1) : NULL;
lLen = Op1 ? STRLEN(Op1) : 0;
while( s && lLen ){
*r++ = *s++;
lLen--;
}
s = Op2 ? STRINGVALUE(Op2) : NULL;
lLen = Op2 ? STRLEN(Op2) : 0;
while( s && lLen ){
*r++ = *s++;
lLen--;
}
#endif
END
/**LEN
=section string
=display LEN()
=title LEN()
This function interprets its argument as a string and returns the length of the string. In ScriptBasic strings can hold any value thus the length of the string is the number of characters contained in the string containing any binary characters, even binary zero.
If the argument is not a string it is converted to string automatically and the length of the converted string is returned. The only exception is T<undef> for which the result is also T<undef>.
*/
COMMAND(LEN)
#if NOTIMP_LEN
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
RESULT = NEWMORTALLONG;
ASSERTNULL(RESULT)
LONGVALUE(RESULT) = STRLEN(Op1);
#endif
END
/**UCASE
=title UCASE()
=display UCASE()
=section string
Uppercase a string.
*/
COMMAND(UCASE)
#if NOTIMP_UCASE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
char *r;
unsigned long lLen;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
RESULT = Op1;
r = STRINGVALUE(RESULT);
lLen = STRLEN(RESULT);
while( lLen-- ){
if( islower( *r ) )*r = toupper( *r );
r++;
}
#endif
END
/**LCASE
=title LCASE()
=display LCASE()
=section string
Lowercase a string.
*/
COMMAND(LCASE)
#if NOTIMP_LCASE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
char *r;
unsigned long lLen;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
RESULT = Op1;
r = STRINGVALUE(RESULT);
lLen = STRLEN(RESULT);
while( lLen-- ){
if( isupper( *r ) )*r = tolower( *r );
r++;
}
#endif
END
/**LTRIM
=section string
=title LTRIM()
=display LTRIM()
Remove the space from the left of the string.
*/
COMMAND(LTRIM)
#if NOTIMP_LTRIM
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
char *r,*s;
unsigned long lStringLength,lLen;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
r = STRINGVALUE(Op1);
lLen = STRLEN(Op1);
while( lLen && isspace(*r) )r++,lLen--;
s = r;
lStringLength = 0;
while( lLen ){
lStringLength++;
r++;
lLen--;
}
RESULT = NEWMORTALSTRING(lStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
while( lStringLength-- )*r++ = *s++;
#endif
END
/**RTRIM
=section string
=title RTRIM()
=display RTRIM()
Remove the space from the right of the string.
*/
COMMAND(RTRIM)
#if NOTIMP_RTRIM
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
char *r,*s;
unsigned long lStringLength;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
r = STRINGVALUE(Op1);
lStringLength = STRLEN(Op1);
while( lStringLength && isspace(r[lStringLength-1]) )lStringLength--;
RESULT = NEWMORTALSTRING(lStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
s = STRINGVALUE(Op1);
while( lStringLength ){
*r++ = *s++;
lStringLength--;
}
#endif
END
/**TRIM
=section string
=title TRIM()
=display TRIM()
Remove the space from both ends of the string.
*/
COMMAND(TRIM)
#if NOTIMP_TRIM
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
char *r,*s;
unsigned long lStringLength,lLen;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
r = STRINGVALUE(Op1);
lLen = STRLEN(Op1);
lStringLength = STRLEN(Op1);
while( lLen && isspace( *r ) )r++,lLen--,lStringLength--;
s = r;
if( lStringLength ){
lStringLength --; /* convert length to char array index */
while( lStringLength && isspace(r[lStringLength]) )lStringLength--;
lStringLength++; /* convert char array index back to length */
}
RESULT = NEWMORTALSTRING(lStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
while( lStringLength ){
*r++ = *s++;
lStringLength--;
}
#endif
END
/**INSTR
=section string
=title INSTR(base_string,search_string [ ,position ] )
=display INSTR()
This function can be used to search a sub-string in a string.
The first argument is the string we are searching in.
The second argument is the string that we actually want to find in the
first argument. The third optional argument is the position where the
search is to be started. If this argu-ment is missing the search starts
with the first character position of the string.
The function returns the position where the sub-string
can be found in the first string. If the searched sub-string
is not found in the string then the return value is undef.
See R<INSTRREV>
*/
COMMAND(INSTR)
#if NOTIMP_INSTR
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2,Op3;
long lStart,lLength,lStringLength;
char *r,*s;
int iCase = OPTION("compare")&1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* this is the base string that we are searching in */
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
lLength = STRLEN(Op1);
r = STRINGVALUE(Op1);
/* this is the string that we search */
Op2 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op2) ){
RESULT = NULL;
RETURN;
}
Op2 = CONVERT2STRING(Op2);
nItem = CDR(nItem);
lStringLength = STRLEN(Op2);
s = STRINGVALUE(Op2);
Op3 = NULL;
if( nItem ){
Op3 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
}
if( memory_IsUndef(Op3) )
lStart = 1;
else
lStart = LONGVALUE(CONVERT2LONG(Op3));
if( lStart < 1 )lStart = 1;
if( lLength < lStringLength ){
RESULT = NULL;
RETURN;
}
while( lStart-1 <= lLength - lStringLength ){
if( ! SUBSTRCMP(r+lStart-1,s, lStringLength,iCase ) ){
RESULT = NEWMORTALLONG;
ASSERTNULL(RESULT)
LONGVALUE(RESULT) = lStart;
RETURN;
}
lStart ++;
}
RESULT = NULL;
RETURN;
#endif
END
/**INSTRREV
=section string
=display INSTRREV()
=title INSTRREV(base_string,search_string [ ,position ] )
This function can be used to search a sub-string in a string in reverse
order starting from the end of the string. The first argument is the
string we are searching in. The second argument is the string that
we actually want to find in the first argument. The third optional argument
is the position where the search is to be started. If this argument is
missing the search starts with the last character position of the string.
The function returns the position where the sub-string can be found in the
first string. If the searched sub-string is not found in the string then the
return value is undef.
See R<INSTR>
*/
COMMAND(INSTRREV)
#if NOTIMP_INSTRREV
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2,Op3;
long lStart,lLength,lStringLength;
char *r,*s;
int iCase = OPTION("compare")&1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* this is the base string that we are searching in */
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
lLength = STRLEN(Op1);
r = STRINGVALUE(Op1);
/* this is the string that we search */
Op2 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op2) ){
RESULT = NULL;
RETURN;
}
Op2 = CONVERT2STRING(Op2);
nItem = CDR(nItem);
lStringLength = STRLEN(Op2);
s = STRINGVALUE(Op2);
Op3 = NULL;
if( nItem ){
Op3 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
}
if( lLength < lStringLength ){
RESULT = NULL;
RETURN;
}
if( memory_IsUndef(Op3) )
lStart = lLength - lStringLength+1;
else
lStart = LONGVALUE(CONVERT2LONG(Op3));
if( lStart > lLength - lStringLength+1)lStart = lLength - lStringLength+1;
while( lStart >= 1 ){
if( ! SUBSTRCMP(r+lStart-1,s, lStringLength,iCase ) ){
RESULT = NEWMORTALLONG;
ASSERTNULL(RESULT)
LONGVALUE(RESULT) = lStart;
RETURN;
}
lStart --;
}
RESULT = NULL;
RETURN;
#endif
END
/**REPLACE
=section string
=title REPLACE(base_string,search_string,replace_string [,number_of_replaces] [,position])
=display REPLACE()
This function replaces one or more occurrences of a sub-string in a string.
T<REPLACE(a,b,c)> searches the string T<a> seeking for occurrences of sub-string T<b>
and replaces each of them with the string T<c>.
The fourth and fifth arguments are optional. The fourth argument specifies the number of
replaces to be performed. If this is missing or is T<undef> then all occurrences of string
T<b> will be replaced. The fifth argument may specify the start position of the operation.
For example the function call
=verbatim
REPLACE("alabama mama", "a","x",3,5)
=noverbatim
will replace only three occurrences of string T<"a"> starting at position 5.
The result is T<"alabxmx mxma">.
*/
COMMAND(REPLACE)
#if NOTIMP_REPLACE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2,Op3,Op4,Op5;
long lRepetitions;
long lCalculatedRepetitions;
int ReplaceAll;
long l_start,lStart,lLength,lSearchLength,lReplaceLength,lResult;
char *r,*s,*q,*w;
int iCase = OPTION("compare")&1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* this is the base string that we are searching in */
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
lLength = STRLEN(Op1);
r = STRINGVALUE(Op1);
/* this is the string that we search to replace */
Op2 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op2) ){
RESULT = NULL;
RETURN;
}
Op2 = CONVERT2STRING(Op2);
nItem = CDR(nItem);
lSearchLength = STRLEN(Op2);
s = STRINGVALUE(Op2);
/* this is the string that we put into the place of the searched string */
Op3 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op3) ){
RESULT = NULL;
RETURN;
}
Op3 = CONVERT2STRING(Op3);
lReplaceLength = STRLEN(Op3);
nItem = CDR(nItem);
w = STRINGVALUE(Op3);
Op4 = NULL;
if( nItem ){
Op4 = EVALUATEEXPRESSION(CAR(nItem));
nItem = CDR(nItem);
ASSERTOKE;
}
if( memory_IsUndef(Op4) ){
lRepetitions = 0;
ReplaceAll = 1;
}else{
lRepetitions = GETLONGVALUE(Op4);
ReplaceAll = 0;
}
if( lRepetitions < 0 )lRepetitions = 0;
Op5 = NULL;
if( nItem ){
Op5 = EVALUATEEXPRESSION(CAR(nItem));
nItem = CDR(nItem);
ASSERTOKE;
}
if( memory_IsUndef(Op5) )
l_start = 1;
else{
l_start = GETLONGVALUE(Op5);
}
if( l_start < 1 )l_start = 1;
lStart = l_start;
/* first calculate the repeat actions */
lCalculatedRepetitions = 0;
while( lStart-1 <= lLength - lSearchLength ){
if( ! SUBSTRCMP(r+lStart-1,s, lSearchLength,iCase ) ){
lCalculatedRepetitions++;
lStart += lSearchLength;
}else lStart ++;
}
if( ! ReplaceAll && lCalculatedRepetitions > lRepetitions )lCalculatedRepetitions = lRepetitions;
/* calculate the length of the new string */
lResult = STRLEN(Op1) + lCalculatedRepetitions * (lReplaceLength-lSearchLength);
/* allocate space for the result */
RESULT = NEWMORTALSTRING(lResult);
ASSERTNULL(RESULT)
/* perform the replacements */
lStart = l_start;
q = STRINGVALUE(RESULT);
if( lStart > 1 ){
memcpy(q,r,lStart-1);
q+=lStart-1;
}
while( lStart <= lLength ){
if( lCalculatedRepetitions && ! SUBSTRCMP(r+lStart-1,s, lSearchLength,iCase ) ){
memcpy(q,w,lReplaceLength);
q += lReplaceLength;
lStart += lSearchLength;
lCalculatedRepetitions--;
}else{
*q++ = r[lStart-1];
lStart ++;
}
}
#endif
END
/**MID
=section string
=display MID()
=title MID(string,start [ ,len ])
Return a subpart of the string. The first argument is the string, the second argument is the start position.
The third argument is the length of the sub-part in terms of characters.
If this argument is missing then the
subpart lasts to the last character of the argument T<string>.
See also R<LEFT>, R<RIGHT>.
=details
T<mid(x,y,[z])> cuts out a sub-string from the string T<x>.
If the first argument of the function is undefined the result is T<undef>.
Otherwise the first argument is converted to string and the second and third
arguments are converted to numeric value. The third argument is optional.
The second argument specifies the start position of the resulting
substring in the original string x; and the last argument specifies
the number of characters to take from the original string T<x>.
If the third argument is missing the substring lasts from the start
position to the end of the string. If the second argu-ment is
not defined the start of the substring is at the start of the
original string. In other words if the second argument is missing
it is the same as value 1. If the second argument is zero or negative
it will specify the start position counting the
characters from the end of the string.
If the staring position T<y> points beyond the end of the string the result
is empty string. If the length of the substring is larger than the number
of characters between the starting position and end of the original string then the
result will be the substring between the start position and the end of the original string.
If the length of the substring is negative the characters before the starting position
are taken. No more than the available characters can be taken in this
case either. In other words if the length is negative and is larger in
absolute value than the starting position the resulting sub-string is the character
between the position specified by the second argument and the start of the string.
Note that the order of the characters is never changed even if some position or length
parameters are negative.
For compatibility reasons you can append a dollar (T<$>) sign to the end of the function
identifier.
Example:
=verbatim
a$ = "superqualifragilisticexpialidosys"
print mid(a$,undef)
print mid(a$,1,5)
print mid(a$,undef,6)
print mid(a$,6,5)
print mid(a$,"-3")
print "*",mid(a$,0),"*"
print mid(undef,"66")
print mid(a$,6,-3)
print mid(a$,6,3)
print mid(a$,-4,-3)
print mid(a$,-4,3)
=noverbatim
will print
=verbatim
superqualifragilisticexpialidosys
super
superq
quali
sys
**
undef
erq
qua
ido
osy
=noverbatim
*/
COMMAND(MID)
#if NOTIMP_MID
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2,Op3;
long lStart,lLength,lStringLength;
char *r,*s;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* we need not duplicate the argument in case it is a left value, because we don't
alter it. (And convert to string anyway that duplicates.) */
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
Op2 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op2) )
lStart = 1;
else
lStart = LONGVALUE(CONVERT2LONG(Op2));
/* if the start value is negative then it is
the number of characters from the end of the string */
if( lStart <= 0 ){
lStart += STRLEN(Op1) + 1;
if( lStart < 0 )lStart = 1;
}
nItem = CDR(nItem);
if( nItem ){
Op3 = EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op3) )
lLength = -1;
else{
lLength = LONGVALUE(CONVERT2LONG(Op3));
/* if the length is negative then it counts the substring backward */
if( lLength < 0 ){
if( lStart < lLength ){
lLength = lStart;
lStart = 1;
}else{
lStart += lLength +1;
lLength = -lLength;
}
}
}
}else
lLength = -1;
lStart --; /* normalize to zero */
lStringLength = STRLEN(Op1);
if( lStart < lStringLength ){
r = STRINGVALUE(Op1) + lStart;
lStringLength -= lStart;
}else{
r = STRINGVALUE(Op1) + lStringLength;
lStringLength = 0L;
}
s = r;
if( lLength != -1 && lLength < lStringLength )lStringLength = lLength;
RESULT = NEWMORTALSTRING(lStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
while( lStringLength ){
*r++ = *s++;
lStringLength--;
}
#endif
END
/**LEFT
=section string
=display LEFT()
=title LEFT(string,len)
Creates the left of a string. The first argument is the string. The second argument is the
number of characters that should be put into the result. If this value is larger than the
number of characters in the string then all the string is returned.
See also R<MID>, R<RIGHT>
=details
T<left(x,y)> cuts out a substring of T<y> characters from the left of the string T<x>.
If the first argument is not defined the result is also T<undef>. Otherwise the first
argument is converted to string and the second ar-gument is converted to integer value.
If the second argument is not defined or has negative value it is treated as numeric zero
and as such the result string will be empty string.
For compatibility reasons you can append a dollar (T<$>) sign to the end of the function
identifier.
Example
=verbatim
a$ = _
"superqualifragilisticexpialidosys"
print "*",left(a$,undef),"*"
print "*",left(a$,7),"*"
print "*",left(a$,-6),"*"
print "*",left(a$,0),"*"
print left(undef,"66")
=noverbatim
will print
=verbatim
**
*superqu*
**
**
undef
=noverbatim
*/
COMMAND(LEFT)
#if NOTIMP_LEFT
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1;
long lLength,lStringLength;
char *r,*s;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
lLength = LONGVALUE(CONVERT2LONG(EVALUATEEXPRESSION(CAR(nItem))));
ASSERTOKE;
if( lLength < 0 )lLength = 0;
s = STRINGVALUE(Op1);
lStringLength = STRLEN(Op1);
if( lLength < lStringLength )lStringLength = lLength;
RESULT = NEWMORTALSTRING(lStringLength);
ASSERTNULL(RESULT)
r = STRINGVALUE(RESULT);
while( lStringLength ){
*r++ = *s++;
lStringLength--;
}
#endif
END
/**RIGHT
=section string
=display RIGHT()
=title RIGHT(string,len)
Creates the right of a string. The first argument is the string. The second argument is the
number of characters that should be put into the result. If this value is larger than the
number of characters in the string then all the string is returned.
See also R<MID>, R<LEFT>.
=details
T<RIGHT(x,y)> cuts out a substring of T<y> characters from the right of the string T<x>.
If the first argument is not defined the result is also T<undef>. Otherwise the first argument
is converted to string and the second argument is converted to integer value.
If the second argument is not defined or has negative value it is treated as numeric zero
and as such the result string will be empty string.
For compatibility reasons you can append a dollar (T<$>) sign to the end of the function
identifier.
*/
COMMAND(RIGHT)
#if NOTIMP_RIGHT