forked from MrucznikRolePlay/Mrucznik-RP-Includes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strlib_fix.inc
1768 lines (1401 loc) · 40.6 KB
/
strlib_fix.inc
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
#if defined STRLIB_INC
#endinput
#endif
#define STRLIB_INC
#include <a_samp>
#if !defined STRLIB_BUFFER_SIZE
#define STRLIB_BUFFER_SIZE 2048
#endif
#if !defined STRLIB_RETURN_SIZE
#define STRLIB_RETURN_SIZE 128
#endif
#if !defined STRLIB_USE_FORMATEX
#if defined __fmt_funcinc
#if !defined FormatSpecifier
#error Please include formatex before strlib.
#endif
#define STRLIB_USE_FORMATEX true
#else
#define STRLIB_USE_FORMATEX false
#endif
#endif
// Used in strtrim (deprecated)
enum trim_edges {
trim_left = 1,
trim_right = 2,
trim_both = trim_left | trim_right
};
// Used in strtrim and strpad
enum string_edges {
edge_left = 1,
edge_right = 2,
edge_both = edge_left | edge_right
};
/*
* Returns a formatted string.
*
* Parameters:
* fmat[] - The format string.
* ... - The format variables.
*
* Returns:
* The formatted string.
*/
forward sprintf(const fmat[], {Float, _}:...);
/*
* Get the first character of a string
*
* Parameters:
* string[] - The string.
*
* Returns:
* The first character of the string.
*/
forward strgetfirstc(const string[]);
/*
* Get a character from a specific index in a string.
*
* Parameters:
* string[] - The string.
* index - The position in the string.
*
* Returns:
* The character at that index, or '\0' if out of range.
*/
forward strgetc(const string[], index);
/*
* Get the size of a string.
*
* Parameters:
* string[] - The string.
*
* Returns:
* The size of the string, in bytes.
*/
forward strsize(const string[]);
/*
* Find out if a string is empty.
*
* Parameters:
* string[] - The string.
*
* Returns:
* True if empty, otherwise false.
*/
forward bool:isempty(const string[]);
/*
* Compare two strings.
*
* Parameters:
* str1[] - The first string.
* str2[] - The second string.
* ignorecase - Whether to compare them in a case-insensitive manner.
*
* Returns:
* True if equal, otherwise false.
*/
forward bool:isequal(const str1[], const str2[], bool:ignorecase = false);
/*
* Split a string by a given delimiter.
*
* Parameters:
* output[][] - A multi-dimensional array that will be filled with substrings.
* input[] - The input string to split.
* delimiter[] - The delimiter to split by. Defaults to ",".
* limit - The max. no. substrings.
* trim - Whether to trim the substrings from whitespace. Defaults to true.
* ignorecase - Whether the search for "delimiter" should be case-insensitive.
* size1 - The size of the 1st dimension of output (otput[this][]). Defaults to sizeof(output).
* size2 - The size of the 2nd dimension of output (otput[][this]). Defaults to sizeof(output[]).
*
* Returns:
* The number of substrings that were copied into the array.
*/
forward strexplode(output[][], const input[], const delimiter[] = !",", limit = cellmax, bool:trim = true, bool:ignorecase = false, size1 = sizeof(output), size2 = sizeof(output[]));
/*
* Glue together strings into one.
*
* Parameters:
* glue[] - The string that will be between all other strings.
* output[] - The output string.
* maxlength - The size of "output". Defaults to sizeof(output).
* ...[] - Strings to glue together.
*
* Returns:
* Nothing
*/
forward strimplode(const glue[], output[], maxlength = sizeof(output), ...);
/*
* Replace occurrences of the search string with the replacement string.
*
* Parameters:
* string[] - The string to perform the replacing in.
* search[] - The string to look for.
* replacement[] - The string to put instead of "search".
* ignorecase - Whether the search for "search" should be case-insensitive. Defaults to false.
* pos - The position to start at. Defaults to 0 (the beginning).
* limit - Limit the number of replacements. Defaults to -1 (no limit).
* maxlength - The size of "string". Defaults to sizeof(string).
*
* Returns:
* The number of replacements that were made.
*/
forward strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string));
/*
* Trim whitespace or a specific group of characters from a string.
*
* Parameters:
* string[] - The string to trim.
* chars[] - A string with characters to trim, or all whitespace if empty. Default is all whitespace.
* edge - The edge(s) to trim (edge_left/edge_right/edge_both). Default is edge_both.
*
* Returns:
* Nothing
*/
forward strtrim(string[], const chars[] = !"", string_edges:edge = edge_both);
/*
* Pad edge(s) of a string with spaces.
*
* Parameters:
* string[] - The string to pad.
* length - The new length of the string.
* substr[] - The substring to pad with. Defaults to a space (" ").
* edge - The edge(s) to pad (edge_left/edge_right/edge_both). Default is edge_both.
* trim_first - Whether to trim the string before padding.
* trim_chars[] - The chars to trim, defaults is all whitespace.
* maxlength - The size of "string". Defaults to sizeof(string).
* input - Used internally.
*/
forward strpad(string[], length, const substr[] = !" ", string_edges:edge = edge_both, bool:trim_first = true, const trim_chars[] = !"", maxlength = sizeof(string), const input[] = !"");
/*
* Wrap a string inside two other strings.
*
* Parameters:
* left[] - The string on the left side.
* string[] - The middle string that will be modified.
* right[] - The string on the right side.
* maxlength - The size of "string". Defaults to sizeof(string).
*/
forward strwrap(const left[], string[], const right[], maxlength = sizeof(string));
/*
* Count substrings.
*
* Parameters:
* string[] - The string to search inside.
* sub[] - The string to search for.
* ignorecase - Whether the search should be case-insensitive.
* count_overlapped - Whether to count overlapping strings ("abcabc" in "abcabcabc" will count 2 instead of 1).
*
* Returns:
* The number of occurrences of "sub" in "string".
*/
forward strcount(const string[], const sub[], bool:ignorecase = false, bool:count_overlapped = false);
/*
* Read a string from a PAWN string literal.
*
* Parameters:
* output[] - The variable to save into.
* input[] - The string literal.
* pos - The position in "input" to start reading from. Will be modified to the end of the literal.
* maxlength - The size of "output". Defaults to sizeof(output).
*
* Returns:
* true on success, false on error.
*/
forward bool:strfromliteral(output[], const input[], &pos = 0, maxlength = sizeof(output));
/*
* Build a PAWN string literal from a given string.
*
* Parameters:
* output[] - The variable to save into.
* substrings[] - The string to build from.
* maxlength - The size of "output". Defaults to sizeof(output).
*
* Returns:
* Nothing
*/
forward strtoliteral(output[], const input[], maxlength = sizeof(output), bool:paranoid = true);
/*
* Convert an array to a string.
*
* Example: {0x1122, 0x5566} becomes "0000112200005566".
*
* Parameters:
* output[] - The variable to save into.
* input[] - The array to build from.
* inputlength - The size of "input". Defaults to sizeof(input).
* maxlength - The size of "output". Defaults to sizeof(output).
*
* Returns:
* Nothing
*/
forward strfrombin(output[], const input[], inputlength = sizeof(input), maxlength = sizeof(output));
/*
* Convert a string to an array.
*
* Example: "0000112200005566" becomes {0x1122, 0x5566}.
*
* Parameters:
* output[] - The variable to save into.
* input[] - The array to build from.
* maxlength - The size of "output". Defaults to sizeof(output).
*
* Returns:
* The length of the output, in cells.
*/
forward strtobin(output[], const input[], maxlength = sizeof(output));
/*
* Concatenate one string with a part of another.
*
* Parameters:
* dest[] - The variable to concatenate the other part with.
* source[] - The string to extract from.
* start - The start offset, defaults to 0.
* end - The start offset, defaults to end of string.
* maxlength - The size of "dest". Defaults to sizeof(dest).
*/
forward strcatmid(dest[], const source[], start = 0, end = -1, maxlength = sizeof(dest));
/*
* UTF-8 encode a string. Characters above 127 will be encoded into
* two or more characters.
*
* Parameters:
* dest[] - The output variable.
* source[] - The string to encode.
* maxlength - The size of "dest". Defaults to sizeof(dest).
*/
forward utf8encode(dest[], const source[], maxlength = sizeof(dest));
/*
* UTF-8 decode a string. UTF-8 characters will be collapsed into single
* characters in the array.
*
* Parameters:
* dest[] - The output variable.
* source[] - The string to encode.
* maxlength - The size of "dest". Defaults to sizeof(dest).
*/
forward utf8decode(dest[], const source[], maxlength = sizeof(dest));
/*
* Decode an encoded URL.
*
* Parameters:
* output[] - The output variable.
* input[] - The string to decode.
* maxlength - The size of "output". Defaults to sizeof(output).
*/
forward strurldecode(output[], const input[], maxlength = sizeof(output));
/*
* URL encode a string.
*
* Parameters:
* output[] - The output variable.
* input[] - The string to encode.
* maxlength - The size of "output". Defaults to sizeof(output).
* pack - Whether to pack the output. Defaults to false.
*/
forward strurlencode(output[], const input[], maxlength = sizeof(output), bool:pack = false);
// Same as above, but output is returned
forward ret_strcatmid(const string[], const source[], start = 0, end = -1);
forward ret_strfrombin(const input[], inputlength = sizeof(input));
forward ret_strimplode(const glue[], ...);
forward ret_strreplace(const string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1);
forward ret_strfromliteral(const input[], &pos = 0);
forward ret_strtoliteral(const input[], bool:paranoid = true);
forward ret_strtrim(const string[], const chars[] = !"", string_edges:edge = edge_both);
forward ret_strpad(const string[], length, const substr[] = !" ", string_edges:edge = edge_both, bool:trim_first = true, const trim_chars[] = !"");
forward ret_strwrap(const left[], const string[], const right[]);
forward ret_strurldecode(const input[]);
forward ret_strurlencode(const input[], bool:pack = false);
forward ret_utf8encode(const input[]);
forward ret_utf8decode(const input[]);
// Return from native functions
forward ret_strpack(const source[]);
forward ret_strunpack(const source[]);
forward ret_strcat(const string1[], const string2[]);
forward ret_strmid(const source[], start, end);
forward ret_strins(const string[], const substr[], pos, maxlength = sizeof(string));
forward ret_strdel(const string[], start, end);
forward ret_valstr(value, bool:pack = false);
forward ret_GetPlayerName(playerid, bool:pack = false);
stock
// Used throughout the library
g_StrlibBuffer[2048]
;
// Workaround for compiler bug
forward _strlib_funcinc();
public _strlib_funcinc() {
new temp[1];
format(!"", 0, !"");
strcat(temp, temp);
strpack(temp, temp);
strunpack(temp, temp);
}
// Internal functions
static stock RedirectArgument(arg, ...) {
#emit LOAD.S.pri 0
#emit ADD.C 12
#emit LOAD.S.alt arg
#emit SHL.C.alt 2
#emit ADD
#emit MOVE.alt
#emit LOAD.S.pri 16
#emit STOR.I
}
static stock CopyArgumentToHeap(arg, bool:pack = false, const argptr[] = "") {
new arg_address, address;
#emit LOAD.S.pri 0
#emit ADD.C 12
#emit LOAD.S.alt arg
#emit SHL.C.alt 2
#emit ADD
#emit LOAD.I
#emit STOR.S.pri arg_address
#emit STOR.S.pri argptr
if (pack) {
new bytes = ((strlen(argptr) + 1 + 3) / 4) * 4;
#emit LCTRL 2
#emit STOR.S.pri address
#emit LOAD.S.alt bytes
#emit ADD
#emit SCTRL 2
//strpack(dest[], const source[], maxlength = sizeof dest)
#emit LOAD.S.pri bytes
#emit SHR.C.pri 2
#emit PUSH.pri
#emit PUSH.S arg_address
#emit PUSH.S address
#emit PUSH.C 12
#emit SYSREQ.C strpack
#emit STACK 16
} else {
new bytes = (strlen(argptr) + 1) * 4;
#emit LCTRL 2
#emit STOR.S.pri address
#emit LOAD.S.alt bytes
#emit ADD
#emit SCTRL 2
//strunpack(dest[], const source[], maxlength = sizeof dest)
#emit LOAD.S.pri bytes
#emit SHR.C.pri 2
#emit PUSH.pri
#emit PUSH.S arg_address
#emit PUSH.S address
#emit PUSH.C 12
#emit SYSREQ.C strunpack
#emit STACK 16
}
#emit LOAD.S.pri 0
#emit ADD.C 12
#emit LOAD.S.alt arg
#emit SHL.C.alt 2
#emit ADD
#emit MOVE.alt
#emit LOAD.S.pri address
#emit STOR.I
return address;
}
static stock RestoreHeapToAddress(address) {
#emit LOAD.S.pri address
#emit SCTRL 2
}
static stock IsOverlapping(const str1[], size1 = sizeof(str1), const str2[], size2 = sizeof(str2)) {
new addr1, addr2;
if (size1 == -1) {
size1 = strsize(str1);
} else {
size1 *= 4;
}
if (size2 == -1) {
size2 = strsize(str2);
} else {
size2 *= 4;
}
#emit LOAD.S.pri str1
#emit STOR.S.pri addr1
#emit LOAD.S.pri str2
#emit STOR.S.pri addr2
return (addr1 < addr2 + size2) && (addr2 < addr1 + size1);
}
// strlib functions
#define ispacked(%1) \
((%1)[0] > 255)
stock strgetfirstc(const string[]) {
return ispacked(string) ? string{0} : string[0];
}
stock strgetc(const string[], index) {
if (index < 0)
return '\0';
new len = strlen(string);
if (index >= len)
return '\0';
return ispacked(string) ? string{index} : string[index];
}
stock strsize(const string[]) {
new len = strlen(string);
if (ispacked(string))
return len + 1;
return (len + 1) * 4;
}
stock bool:isempty(const string[]) {
if (ispacked(string))
return string{0} == '\0';
else
return string[0] == '\0';
}
stock bool:isequal(const str1[], const str2[], bool:ignorecase = false) {
new
c1 = (str1[0] > 255) ? str1{0} : str1[0],
c2 = (str2[0] > 255) ? str2{0} : str2[0]
;
if (!c1 != !c2)
return false;
return !strcmp(str1, str2, ignorecase);
}
stock strimplode(const glue[], output[], maxlength = sizeof(output), ...) {
new args = numargs();
// Null-out "output"
output[0] = '\0';
// Loop the variable arguments (the ones after "maxlength").
for (new arg = 3; arg < args; arg++) {
// If this isn't the first string, append the glue.
if (arg != 3)
strcat(output, glue, maxlength);
// Wrap these in braces or they will be a part of the above if statement (compiler bug)
{
// Get the address of argument no. <arg>
#emit LCTRL 5
#emit ADD.C 12
#emit LOAD.S.alt arg
#emit SHL.C.alt 2
#emit ADD
#emit LOAD.I
// Push the maxlength, arg address, and output address
#emit PUSH.S maxlength
#emit PUSH.pri
#emit PUSH.S output
// Push the argument count
#emit PUSH.C 12
// call strcat
#emit SYSREQ.C strcat
// Restore the stack
#emit STACK 16
}
}
}
stock strexplode(output[][], const input[], const delimiter[] = !",", limit = cellmax, bool:trim = true, bool:ignorecase = false, size1 = sizeof(output), size2 = sizeof(output[])) {
if (!size1 || !size2) {
printf("(strexplode) ERROR: size1 = %d, size2 = %d. Can't be 0.", size1, size2);
return 0;
}
if (isempty(delimiter)) {
print(!"(strexplode) ERROR: delimiter is empty.");
return 0;
}
if (trim) {
new i = -1;
if (ispacked(input)) {
while (input{++i}) {
if (input{i} > ' ') {
i = -1;
break;
}
}
} else {
while (input[++i]) {
if (input[i] > ' ') {
i = -1;
break;
}
}
}
if (i != -1)
return 0;
} else if (isempty(input)) {
return 0;
}
if (limit == 0) {
return 0;
} else if (limit == cellmax) {
limit = 0;
}
new
pos = 0,
next,
bool:packed = ispacked(input),
dlen = strlen(delimiter),
count = 0,
end
;
while (pos != -1) {
++count;
if (limit > 0 && count >= limit) {
next = -1;
} else {
next = strfind(input, delimiter, ignorecase, pos);
}
end = (next == -1) ? cellmax : next;
if (trim) {
if (end == cellmax)
end = strlen(input);
if (packed) {
while (0 < input{pos} <= ' ') pos++;
while (end > 0 && input{end - 1} <= ' ') end--;
} else {
while (0 < input[pos] <= ' ') pos++;
while (end > 0 && input[end - 1] <= ' ') end--;
}
}
strmid(output[count - 1], input, pos, end, size2);
if (count >= size1 || next == -1 || (limit < 0 && count >= -limit))
break;
pos = next + dlen;
}
return count;
}
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
// No need to do anything if the limit is 0.
if (limit == 0)
return 0;
new
sublen = strlen(search),
replen = strlen(replacement),
bool:packed = ispacked(string),
maxlen = maxlength,
len = strlen(string),
count = 0
;
// "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
// Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
if (packed)
maxlen *= 4;
// If the length of the substring is 0, we have nothing to look for..
if (!sublen)
return 0;
// In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
// Delete the string we found
strdel(string, pos, pos + sublen);
len -= sublen;
// If there's anything to put as replacement, insert it. Make sure there's enough room first.
if (replen && len + replen < maxlen) {
strins(string, replacement, pos, maxlength);
pos += replen;
len += replen;
}
// Is there a limit of number of replacements, if so, did we break it?
if (limit != -1 && ++count >= limit)
break;
}
return count;
}
stock strtrim(string[], const chars[] = !"", string_edges:edge = edge_both) {
new bool:packed = ispacked(string);
// If "chars" is empty, trim whitespace
if (!strgetfirstc(chars)) {
// Should the left side be trimmed?
if (edge & edge_left) {
new i = 0;
if (packed)
while (0 < string{i} <= ' ') i++;
else
while (0 < string[i] <= ' ') i++;
if (i) {
strdel(string, 0, i);
}
}
// Should the right side be trimmed?
if (edge & edge_right) {
new i = strlen(string);
if (i) {
if (packed) {
while (--i && 0 < string{i} <= ' ') {}
string{i + 1} = '\0';
} else {
while (--i && 0 < string[i] <= ' ') {}
string[i + 1] = '\0';
}
}
}
} else {
// Should the left side be trimmed?
if (edge & edge_left) {
new i = 0, sub[2];
if (packed) {
while ((sub[0] = string{i})) {
if (strfind(chars, sub) == -1)
break;
i++;
}
if (i) {
strdel(string, 0, i);
}
} else {
while ((sub[0] = string[i])) {
if (strfind(chars, sub) == -1)
break;
i++;
}
if (i) strdel(string, 0, i);
}
}
// Should the right side be trimmed?
if (edge & edge_right) {
new i = strlen(string), sub[2];
if (i >= 0) {
if (packed) {
while (i--) {
sub[0] = string{i};
if (strfind(chars, sub) == -1)
break;
}
string{i + 1} = '\0';
} else {
while (i--) {
sub[0] = string[i];
if (strfind(chars, sub) == -1)
break;
}
string[i + 1] = '\0';
}
}
}
}
}
stock strpad(string[], length, const substr[] = !" ", string_edges:edge = edge_both, bool:trim_first = true, const trim_chars[] = !"", maxlength = sizeof(string), const input[] = !"") {
if (trim_first) {
strtrim(string, trim_chars, edge);
}
new
heap,
length_left = 0,
length_right = 0,
len = strlen(string),
sublen = strlen(substr),
bool:packed,
bool:subpacked = ispacked(substr)
;
if (len > length)
return;
else
length -= len;
// Make "input" a pointer to "string"
#emit LOAD.S.pri string
#emit STOR.S.pri input
// Copy "input" to the heap so it won't be linked to "string" anymore.
heap = CopyArgumentToHeap(7);
string[0] = '\0';
len = 0;
switch (edge) {
case edge_left:
length_left = length;
case edge_right:
length_right = length;
default:
length_left = length / 2, length_right = length - length_left;
}
if (length_left) {
while (len < length_left) {
if (subpacked)
strcat(string, substr, length_left * 4);
else
strcat(string, substr, length_left + 1);
len += sublen;
}
if (subpacked)
string{length_left} = 0;
}
strcat(string, input, maxlength);
if (length_right) {
len = strlen(string);
length_right += len;
packed = ispacked(string);
while (len < length_right) {
if (packed)
strcat(string, substr, length_right / 4 + 1);
else
strcat(string, substr, length_right + 1);
len += sublen;
}
if (packed)
string{length_right + 1} = 0;
}
RestoreHeapToAddress(heap);
}
stock strwrap(const left[], string[], const right[], maxlength = sizeof(string)) {
strins(string, left, 0, maxlength);
strcat(string, right, maxlength);
}
stock strcount(const string[], const sub[], bool:ignorecase = false, bool:count_overlapped = false) {
new
increment = count_overlapped ? 1 : strlen(sub),
pos = -increment,
count = 0
;
while (-1 != (pos = strfind(string, sub, ignorecase, pos + increment)))
count++;
return count;
}
stock bool:strfromliteral(output[], const input[], &pos = 0, maxlength = sizeof(output)) {
new
length = strlen(input),
c,
outlen = 0,
heap = 0
;
// No need to do anything else.
if (!length)
return true;
if (IsOverlapping(output, maxlength, input, -1))
heap = CopyArgumentToHeap(1);
output[0] = '\0';
if (input[0] == '"')
pos++;
for (;; pos++) {
if (outlen >= maxlength - 1 || pos >= length)
break;
c = input[pos];
switch (c) {
// String ended
case '"': break;
case '\\': {}
default: {
output[outlen++] = c;
continue;
}
}
// String ends with a backslash - invalid.
if (pos == length - 1)
goto return_false;
// We're after a backslash now, let's see what's there.
c = input[++pos];
switch (c) {
case '"',
'\'',
'\\',
'%': output[outlen++] = c;
case 'a': output[outlen++] = '\a';
case 'b': output[outlen++] = '\b';
case 'e': output[outlen++] = '\e';
case 'f': output[outlen++] = '\f';
case 'r': output[outlen++] = '\r';
case 'n': output[outlen++] = '\n';
case 't': output[outlen++] = '\t';
case 'v': output[outlen++] = '\v';
case 'x': {
new val = 0;
// String ends with "\x" - invalid.
if (c == length - 1)
goto return_false;
while ((c = input[pos + 1])) {
if ('a' <= c <= 'f' || 'A' <= c <= 'F') {
val = (val << 4) + (tolower(c) - 'a' + 10);
} else if ('0' <= c <= '9') {
val = (val << 4) + (c - '0');
} else {
break;
}
pos++;
}
if (c == ';')
pos++;
output[outlen++] = val;
}
case '0' .. '9': {
new val = 0;
while ((c = input[pos])) {
if ('0' <= c <= '9') {
val = val * 10 + (c - '0');
} else {
break;
}
pos++;
}
if (c != ';') pos--;
output[outlen++] = val;
}
default: {
goto return_false;
}
}
}
output[outlen] = '\0';
pos++;
new bool:ret = true;