-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNAVFoundation.StringUtils.axi
More file actions
2964 lines (2561 loc) · 86.5 KB
/
NAVFoundation.StringUtils.axi
File metadata and controls
2964 lines (2561 loc) · 86.5 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
PROGRAM_NAME='NAVFoundation.StringUtils'
/*
_ _ _ ___ __
| \ | | ___ _ __ __ _ __ _| |_ ___ / \ \ / /
| \| |/ _ \| '__/ _` |/ _` | __/ _ \ / _ \ \ / /
| |\ | (_) | | | (_| | (_| | || __// ___ \ V /
|_| \_|\___/|_| \__, |\__,_|\__\___/_/ \_\_/
|___/
MIT License
Copyright (c) 2010-2026 Norgate AV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#IF_NOT_DEFINED __NAV_FOUNDATION_STRINGUTILS__
#DEFINE __NAV_FOUNDATION_STRINGUTILS__ 'NAVFoundation.StringUtils'
#include 'NAVFoundation.Core.h.axi'
#include 'NAVFoundation.StringUtils.h.axi'
#include 'NAVFoundation.ErrorLogUtils.axi'
// #DEFINE USING_NAV_STRING_GATHER_CALLBACK
// define_function NAVStringGatherCallback(_NAVStringGatherResult args) {}
/**
* @function NAVStripCharsFromRight
* @public
* @description Removes a specified number of characters from the right end of a string.
*
* @param {char[]} buffer - Input string to modify
* @param {integer} count - Number of characters to remove from the right
*
* @returns {char[]} Modified string with characters removed
*
* @example
* stack_var char text[50]
* text = 'Hello World'
* text = NAVStripCharsFromRight(text, 3) // Returns 'Hello Wo'
*/
define_function char[NAV_MAX_BUFFER] NAVStripCharsFromRight(char buffer[], integer count) {
stack_var integer length
length = length_array(buffer)
if (count <= 0 || !length) {
return buffer
}
if (count >= length) {
return ''
}
return left_string(buffer, length_array(buffer) - count)
}
/**
* @function NAVStripRight
* @public
* @description Alias for NAVStripCharsFromRight. Removes characters from the right end of a string.
*
* @param {char[]} buffer - Input string to modify
* @param {integer} count - Number of characters to remove from the right
*
* @returns {char[]} Modified string with characters removed
*
* @see NAVStripCharsFromRight
*/
define_function char[NAV_MAX_BUFFER] NAVStripRight(char buffer[], integer count) {
return NAVStripCharsFromRight(buffer, count)
}
/**
* @function NAVStripCharsFromLeft
* @public
* @description Removes a specified number of characters from the left end of a string.
*
* @param {char[]} buffer - Input string to modify
* @param {integer} count - Number of characters to remove from the left
*
* @returns {char[]} Modified string with characters removed
*
* @example
* stack_var char text[50]
* text = 'Hello World'
* text = NAVStripCharsFromLeft(text, 3) // Returns 'lo World'
*/
define_function char[NAV_MAX_BUFFER] NAVStripCharsFromLeft(char buffer[], integer count) {
stack_var integer length
length = length_array(buffer)
if (count <= 0 || !length) {
return buffer
}
if (count >= length) {
return ''
}
return right_string(buffer, length_array(buffer) - count)
}
/**
* @function NAVStripLeft
* @public
* @description Alias for NAVStripCharsFromLeft. Removes characters from the left end of a string.
*
* @param {char[]} buffer - Input string to modify
* @param {integer} count - Number of characters to remove from the left
*
* @returns {char[]} Modified string with characters removed
*
* @see NAVStripCharsFromLeft
*/
define_function char[NAV_MAX_BUFFER] NAVStripLeft(char buffer[], integer count) {
return NAVStripCharsFromLeft(buffer, count)
}
/**
* @function NAVRemoveStringByLength
* @public
* @description Removes a specified number of characters from the beginning of a string and returns them.
* Note that the original string buffer is mutated to contain the remaining characters after removal.
*
* @param {char[]} buffer - Input string buffer to modify (will be mutated)
* @param {integer} count - Number of characters to remove from the start of the buffer
*
* @returns {char[]} The characters that were removed from the beginning of the string
*
* @example
* stack_var char buffer[50]
* stack_var char text[50]
* buffer = 'Hello World'
* text = NAVRemoveStringByLength(buffer, 6) // Returns 'Hello '
* // text = 'Hello '
* // buffer = 'World'
*/
define_function char[NAV_MAX_BUFFER] NAVRemoveStringByLength(char buffer[], integer count) {
stack_var integer length
length = length_array(buffer)
if (count <= 0 || !length) {
return ''
}
if (count >= length) {
stack_var char result[NAV_MAX_BUFFER]
result = buffer
buffer = ''
return result
}
return remove_string(buffer, left_string(buffer, count), 1)
}
/**
* @function NAVStringSubstring
* @public
* @description Extracts a substring from a string starting at a specified position with a specified length.
*
* @param {char[]} buffer - Input string
* @param {integer} start - Starting position (1-based index)
* @param {integer} count - Number of characters to extract, or 0 for all remaining characters
*
* @returns {char[]} Extracted substring
*
* @example
* stack_var char text[50]
* stack_var char result[50]
* text = 'Hello World'
* result = NAVStringSubstring(text, 7, 5) // Returns 'World'
* result = NAVStringSubstring(text, 3, 0) // Returns 'llo World'
*
* @note If count is 0, extracts all characters from start to the end of the string
*/
define_function char[NAV_MAX_BUFFER] NAVStringSubstring(char buffer[], integer start, integer count) {
stack_var integer length
length = length_array(buffer)
if (start <= 0 || start > length) {
return ''
}
if (count < 0) {
return ''
}
if (count > 0) {
return mid_string(buffer, start, count)
}
return mid_string(buffer, start, (length - start) + 1)
}
/**
* @function NAVStringSlice
* @public
* @description Extracts a section of a string between start and end positions.
*
* @param {char[]} buffer - Input string
* @param {integer} start - Starting position (1-based index, inclusive)
* @param {integer} end - Ending position (1-based index, exclusive)
*
* @returns {char[]} Extracted substring
*
* @example
* stack_var char text[50]
* stack_var char result[50]
* text = 'Hello World'
* result = NAVStringSlice(text, 1, 6) // Returns 'Hello'
*
* @note This function is similar to the slice methods in JavaScript but with 1-based indexing
*/
define_function char[NAV_MAX_BUFFER] NAVStringSlice(char buffer[], integer start, integer end) {
if (start <= 0 || start > length_array(buffer)) {
return ''
}
if (end == 0) {
return NAVStringSubstring(buffer, start, end)
}
if (end < start) {
return ''
}
if ((end - start) <= 0) {
return ''
}
return NAVStringSubstring(buffer, start, end - start)
}
/**
* @function NAVFindAndReplace
* @public
* @description Replaces all occurrences of a substring with another string.
*
* @param {char[]} buffer - Input string
* @param {char[]} match - Substring to find
* @param {char[]} value - Replacement string
*
* @returns {char[]} String with all replacements made
*
* @example
* stack_var char text[50]
* text = 'Hello World'
* text = NAVFindAndReplace(text, 'o', 'X') // Returns 'HellX WXrld'
*/
define_function char[NAV_MAX_BUFFER] NAVFindAndReplace(char buffer[], char match[], char value[]) {
stack_var integer index
stack_var char result[NAV_MAX_BUFFER]
stack_var integer matchLength
stack_var integer searchPos
matchLength = length_array(match)
if (!length_array(buffer) || !matchLength) {
return buffer
}
result = buffer
searchPos = 1 // Start searching from position 1
if (!NAVContains(result, match)) {
return result
}
while (true) {
index = NAVIndexOf(result, match, searchPos)
if (index == 0) {
// No more matches found
break
}
if (index == 1) {
result = "value, right_string(result, length_array(result) - matchLength)"
// Move search position past the replacement
searchPos = length_array(value) + 1
continue
}
result = "left_string(result, index - 1), value, right_string(result, (length_array(result) - (index + matchLength)) + 1)"
// Move search position past the replacement - search from AFTER the replacement in the result string
searchPos = (index - 1) + length_array(value) + 1
}
return result
}
/**
* @function NAVStringReplace
* @public
* @description Alias for NAVFindAndReplace. Replaces all occurrences of a substring.
*
* @param {char[]} buffer - Input string
* @param {char[]} match - Substring to find
* @param {char[]} value - Replacement string
*
* @returns {char[]} String with all replacements made
*
* @see NAVFindAndReplace
*/
define_function char[NAV_MAX_BUFFER] NAVStringReplace(char buffer[], char match[], char value[]) {
return NAVFindAndReplace(buffer, match, value)
}
/**
* @function NAVStringNormalizeAndReplace
* @public
* @description Normalizes multiple consecutive occurrences of a substring to a single occurrence,
* then replaces it with another string.
*
* @param {char[]} buffer - Input string
* @param {char[]} match - Substring to normalize and replace
* @param {char[]} replacement - Replacement string
*
* @returns {char[]} Normalized and replaced string
*
* @example
* stack_var char text[50]
* text = 'Hello World' // Note: double space
* text = NAVStringNormalizeAndReplace(text, ' ', '-') // Returns 'Hello-World'
*/
define_function char[NAV_MAX_BUFFER] NAVStringNormalizeAndReplace(char buffer[], char match[], char replacement[]) {
stack_var char result[NAV_MAX_BUFFER]
stack_var char doubleMatch[NAV_MAX_BUFFER]
if (!length_array(buffer) || !length_array(match)) {
return buffer
}
if (!NAVContains(buffer, match)) {
return buffer
}
result = buffer
doubleMatch = "match, match"
while (NAVContains(result, doubleMatch)) {
result = NAVStringReplace(result, doubleMatch, match)
}
result = NAVStringReplace(result, match, replacement)
return result
}
/**
* @function NAVStringCount
* @public
* @description Counts the number of occurrences of a substring in a string.
*
* @param {char[]} buffer - Input string to search in
* @param {char[]} value - Substring to count
* @param {integer} caseSensitivity - NAV_CASE_SENSITIVE or NAV_CASE_INSENSITIVE
*
* @returns {integer} Number of occurrences found
*
* @example
* stack_var char text[50]
* stack_var integer count
* text = 'Hello World, hello universe'
* count = NAVStringCount(text, 'hello', NAV_CASE_INSENSITIVE) // Returns 2
* count = NAVStringCount(text, 'hello', NAV_CASE_SENSITIVE) // Returns 1
*/
define_function integer NAVStringCount(char buffer[], char value[], integer caseSensitivity) {
stack_var integer index
stack_var integer result
stack_var char tempBuffer[65534]
stack_var char tempValue[NAV_MAX_BUFFER]
stack_var integer x
result = 0
if (!length_array(buffer) || !length_array(value)) {
return result
}
tempBuffer = buffer
tempValue = value
if (caseSensitivity == NAV_CASE_INSENSITIVE) {
tempBuffer = lower_string(tempBuffer)
tempValue = lower_string(tempValue)
}
for (x = 1; x <= length_array(tempBuffer); x++) {
index = NAVIndexOf(tempBuffer, tempValue, x)
if (index == 0) {
break
}
result++
x = index + length_array(tempValue) - 1
}
return result
}
/**
* @function NAVIsWhitespace
* @public
* @description Determines if a character is whitespace.
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is whitespace, false otherwise
*
* @example
* stack_var char isSpace
* isSpace = NAVIsWhitespace(' ') // Returns true
* isSpace = NAVIsWhitespace('A') // Returns false
*
* @note Whitespace includes space, tab, CR, LF, VT, FF, and NULL
*/
define_function char NAVIsWhitespace(char byte) {
return (
(byte == NAV_NULL) ||
(byte == NAV_TAB) ||
(byte == NAV_LF)||
(byte == NAV_VT) ||
(byte == NAV_FF) ||
(byte == NAV_CR) ||
(byte == ' ')
)
}
/**
* @function NAVIsSpace
* @public
* @description Alias for NAVIsWhitespace. Determines if a character is whitespace.
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is whitespace, false otherwise
*
* @see NAVIsWhitespace
*/
define_function char NAVIsSpace(char byte) {
return NAVIsWhitespace(byte)
}
/**
* @function NAVIsAlpha
* @public
* @description Determines if a character is alphabetic (a-z or A-Z).
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is alphabetic, false otherwise
*
* @example
* stack_var char isAlpha
* isAlpha = NAVIsAlpha('A') // Returns true
* isAlpha = NAVIsAlpha('1') // Returns false
*/
define_function char NAVIsAlpha(char byte) {
return (
((byte >= 'a') && (byte <= 'z')) ||
((byte >= 'A') && (byte <= 'Z'))
)
}
/**
* @function NAVIsDigit
* @public
* @description Determines if a character is a digit (0-9).
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is a digit, false otherwise
*
* @example
* stack_var char isDigit
* isDigit = NAVIsDigit('5') // Returns true
* isDigit = NAVIsDigit('A') // Returns false
*/
define_function char NAVIsDigit(char byte) {
return ((byte >= '0') && (byte <= '9'))
}
/**
* @function NAVIsAlphaNumeric
* @public
* @description Determines if a character is alphanumeric (a-z, A-Z, 0-9) or underscore.
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is alphanumeric or underscore, false otherwise
*
* @example
* stack_var char isAlphaNum
* isAlphaNum = NAVIsAlphaNumeric('A') // Returns true
* isAlphaNum = NAVIsAlphaNumeric('5') // Returns true
* isAlphaNum = NAVIsAlphaNumeric('_') // Returns true
* isAlphaNum = NAVIsAlphaNumeric('!') // Returns false
*/
define_function char NAVIsAlphaNumeric(char byte) {
return (
NAVIsAlpha(byte) ||
NAVIsDigit(byte) ||
(byte == '_')
)
}
/**
* @function NAVIsHexDigit
* @public
* @description Determines if a character is a valid hexadecimal digit (0-9, A-F, a-f).
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is a valid hex digit, false otherwise
*
* @example
* stack_var char isHex
* isHex = NAVIsHexDigit('A') // Returns true
* isHex = NAVIsHexDigit('f') // Returns true
* isHex = NAVIsHexDigit('5') // Returns true
* isHex = NAVIsHexDigit('G') // Returns false
*/
define_function char NAVIsHexDigit(char byte) {
return (
(byte >= '0' && byte <= '9') ||
(byte >= 'A' && byte <= 'F') ||
(byte >= 'a' && byte <= 'f')
)
}
/**
* @function NAVIsBinaryDigit
* @public
* @description Determines if a character is a valid binary digit (0-1).
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is a valid binary digit, false otherwise
*
* @example
* stack_var char isBinary
* isBinary = NAVIsBinaryDigit('0') // Returns true
* isBinary = NAVIsBinaryDigit('1') // Returns true
* isBinary = NAVIsBinaryDigit('2') // Returns false
*/
define_function char NAVIsBinaryDigit(char byte) {
return (byte >= '0' && byte <= '1')
}
/**
* @function NAVIsOctalDigit
* @public
* @description Determines if a character is a valid octal digit (0-7).
*
* @param {char} byte - Character to test
*
* @returns {char} True if the character is a valid octal digit, false otherwise
*
* @example
* stack_var char isOctal
* isOctal = NAVIsOctalDigit('7') // Returns true
* isOctal = NAVIsOctalDigit('8') // Returns false
*/
define_function char NAVIsOctalDigit(char byte) {
return (byte >= '0' && byte <= '7')
}
/**
* @function NAVTrimStringLeft
* @public
* @description Removes all leading whitespace characters from a string.
*
* @param {char[]} buffer - Input string to trim
*
* @returns {char[]} String with leading whitespace removed
*
* @example
* stack_var char text[50]
* text = ' Hello World'
* text = NAVTrimStringLeft(text) // Returns 'Hello World'
*/
define_function char[NAV_MAX_BUFFER] NAVTrimStringLeft(char buffer[]) {
stack_var char result[NAV_MAX_BUFFER]
stack_var integer count
stack_var char byte
result = buffer
for (count = 1; count <= length_array(result); count++) {
byte = NAVCharCodeAt(result, count)
if (!NAVIsWhitespace(byte)) {
break
}
}
if(count > 1) {
result = NAVStripCharsFromLeft(result, count - 1)
}
return result
}
/**
* @function NAVTrimStringRight
* @public
* @description Removes all trailing whitespace characters from a string.
*
* @param {char[]} buffer - Input string to trim
*
* @returns {char[]} String with trailing whitespace removed
*
* @example
* stack_var char text[50]
* text = 'Hello World '
* text = NAVTrimStringRight(text) // Returns 'Hello World'
*/
define_function char[NAV_MAX_BUFFER] NAVTrimStringRight(char buffer[]) {
stack_var char result[NAV_MAX_BUFFER]
stack_var integer count
stack_var char byte
stack_var integer length
result = buffer
length = length_array(result)
for (count = length; count >= 1; count--) {
byte = NAVCharCodeAt(result, count)
if (!NAVIsWhitespace(byte)) {
break
}
}
if(count < length) {
result = NAVStripCharsFromRight(result, length - count)
}
return result
}
/**
* @function NAVTrimString
* @public
* @description Removes all leading and trailing whitespace characters from a string.
*
* @param {char[]} buffer - Input string to trim
*
* @returns {char[]} String with leading and trailing whitespace removed
*
* @example
* stack_var char text[50]
* text = ' Hello World '
* text = NAVTrimString(text) // Returns 'Hello World'
*/
define_function char[NAV_MAX_BUFFER] NAVTrimString(char buffer[]) {
return NAVTrimStringLeft(NAVTrimStringRight(buffer))
}
/**
* @function NAVTrimStringArray
* @public
* @description Trims all strings in an array, removing leading and trailing whitespace.
*
* @param {char[][]} array - Array of strings to trim (modified in place)
*
* @returns {void}
*
* @example
* stack_var char texts[3][50]
* texts[1] = ' Hello '
* texts[2] = ' World '
* texts[3] = ' ! '
* NAVTrimStringArray(texts) // Modifies texts to ['Hello', 'World', '!']
*/
define_function NAVTrimStringArray(char array[][]) {
stack_var integer length
stack_var integer x
length = length_array(array)
for(x = 1; x <= length; x++) {
array[x] = NAVTrimString(array[x])
}
}
/**
* @function NAVGetStringBefore
* @public
* @description Extracts the portion of a string that comes before a specified token.
*
* @param {char[]} buffer - Input string
* @param {char[]} token - The token to search for
*
* @returns {char[]} Substring before the token, empty string if token is at the beginning, or the entire string if token not found
*
* @example
* stack_var char text[50]
* stack_var char result[50]
* text = 'Hello World'
* result = NAVGetStringBefore(text, ' ') // Returns 'Hello'
* result = NAVGetStringBefore(text, 'Hello') // Returns ''
*/
define_function char[NAV_MAX_BUFFER] NAVGetStringBefore(char buffer[], char token[]) {
stack_var integer index
if (!length_array(buffer) || !length_array(token)) {
return buffer
}
index = NAVIndexOf(buffer, token, 1)
if (index == 0) {
return buffer
}
if (index == 1) {
return ''
}
return NAVStringSubstring(buffer, 1, index - 1)
}
/**
* @function NAVStringBefore
* @public
* @description Alias for NAVGetStringBefore. Gets the substring before a token.
*
* @param {char[]} buffer - Input string
* @param {char[]} token - The token to search for
*
* @returns {char[]} Substring before the token, or the entire string if token not found
*
* @see NAVGetStringBefore
*/
define_function char[NAV_MAX_BUFFER] NAVStringBefore(char buffer[], char token[]) {
return NAVGetStringBefore(buffer, token)
}
/**
* @function NAVGetStringAfter
* @public
* @description Extracts the portion of a string that comes after a specified token.
*
* @param {char[]} buffer - Input string
* @param {char[]} token - The token to search for
*
* @returns {char[]} Substring after the token, or the entire string if token not found
*
* @example
* stack_var char text[50]
* stack_var char result[50]
* text = 'Hello World'
* result = NAVGetStringAfter(text, ' ') // Returns 'World'
*/
define_function char[NAV_MAX_BUFFER] NAVGetStringAfter(char buffer[], char token[]) {
stack_var integer index
if (!length_array(buffer) || !length_array(token)) {
return buffer
}
index = NAVIndexOf(buffer, token, 1)
if (index == 0) {
return buffer
}
return NAVStringSubstring(buffer, index + length_array(token), length_array(buffer))
}
/**
* @function NAVStringAfter
* @public
* @description Alias for NAVGetStringAfter. Gets the substring after a token.
*
* @param {char[]} buffer - Input string
* @param {char[]} token - The token to search for
*
* @returns {char[]} Substring after the token, or the entire string if token not found
*
* @see NAVGetStringAfter
*/
define_function char[NAV_MAX_BUFFER] NAVStringAfter(char buffer[], char token[]) {
return NAVGetStringAfter(buffer, token)
}
/**
* @function NAVGetStringBetween
* @public
* @description Extracts the portion of a string between two tokens.
*
* @param {char[]} buffer - Input string
* @param {char[]} token1 - The starting token
* @param {char[]} token2 - The ending token
*
* @returns {char[]} Substring between the tokens, or empty string if tokens not found
*
* @example
* stack_var char text[50]
* stack_var char result[50]
* text = 'Hello [World] Goodbye'
* result = NAVGetStringBetween(text, '[', ']') // Returns 'World'
*/
define_function char[NAV_MAX_BUFFER] NAVGetStringBetween(char buffer[], char token1[], char token2[]) {
stack_var integer tokenIndex[2]
stack_var integer startIndex
stack_var integer count
if(!length_array(buffer)) {
return ''
}
tokenIndex[1] = NAVIndexOf(buffer, token1, 1)
if (!tokenIndex[1]) {
return ''
}
startIndex = tokenIndex[1] + length_array(token1)
tokenIndex[2] = NAVIndexOf(buffer, token2, startIndex)
if (!tokenIndex[2]) {
return ''
}
if (tokenIndex[1] >= tokenIndex[2]) {
return ''
}
count = tokenIndex[2] - startIndex
if (count <= 0) {
return ''
}
return NAVStringSubstring(buffer, startIndex, count)
}
/**
* @function NAVStringBetween
* @public
* @description Alias for NAVGetStringBetween. Gets the substring between two tokens.
*
* @param {char[]} buffer - Input string
* @param {char[]} token1 - The starting token
* @param {char[]} token2 - The ending token
*
* @returns {char[]} Substring between the tokens, or empty string if tokens not found
*
* @see NAVGetStringBetween
*/
define_function char[NAV_MAX_BUFFER] NAVStringBetween(char buffer[], char token1[], char token2[]) {
return NAVGetStringBetween(buffer, token1, token2)
}
/**
* @function NAVGetStringBetweenGreedy
* @public
* @description Extracts the portion of a string between the first occurrence of token1 and the last occurrence of token2.
*
* @param {char[]} buffer - Input string
* @param {char[]} token1 - The starting token
* @param {char[]} token2 - The ending token
*
* @returns {char[]} Substring between the tokens, or empty string if tokens not found
*
* @example
* stack_var char text[70]
* stack_var char result[50]
* text = 'Hello [World] and [Universe]'
* result = NAVGetStringBetweenGreedy(text, '[', ']') // Returns 'World] and [Universe'
*
* @note This is a "greedy" match, capturing everything between the first token1 and the last token2
*/
define_function char[NAV_MAX_BUFFER] NAVGetStringBetweenGreedy(char buffer[], char token1[], char token2[]) {
stack_var integer tokenIndex[2]
stack_var integer startIndex
stack_var integer count
if(!length_array(buffer)) {
return ''
}
tokenIndex[1] = NAVIndexOf(buffer, token1, 1)
if (!tokenIndex[1]) {
return ''
}
startIndex = tokenIndex[1] + length_array(token1)
tokenIndex[2] = NAVLastIndexOf(buffer, token2)
if (!tokenIndex[2]) {
return ''
}
if (tokenIndex[1] >= tokenIndex[2]) {
return ''
}
count = tokenIndex[2] - startIndex
if (count <= 0) {
return ''
}
return NAVStringSubstring(buffer, startIndex, count)
}
/**
* @function NAVStringBetweenGreedy
* @public
* @description Alias for NAVGetStringBetweenGreedy. Gets the substring between first token1 and last token2.
*
* @param {char[]} buffer - Input string
* @param {char[]} token1 - The starting token
* @param {char[]} token2 - The ending token
*
* @returns {char[]} Substring between the tokens, or empty string if tokens not found
*
* @see NAVGetStringBetweenGreedy
*/
define_function char[NAV_MAX_BUFFER] NAVStringBetweenGreedy(char buffer[], char token1[], char token2[]) {
return NAVGetStringBetweenGreedy(buffer, token1, token2)
}
/**
* @function NAVStartsWith
* @public
* @description Determines whether a string begins with a specified substring.
*
* @param {char[]} buffer - String to check
* @param {char[]} match - The prefix to search for
*
* @returns {char} True if the string starts with the prefix, false otherwise
*
* @example
* stack_var char text[50]
* stack_var char result
* text = 'Hello World'
* result = NAVStartsWith(text, 'Hello') // Returns true