vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
//! VB6 `String` Function
//!
//! The `String` function returns a string consisting of a repeating character.
//!
//! ## Syntax
//! ```vb6
//! String(number, character)
//! ```
//!
//! ## Parameters
//! - `number`: Required. Long integer specifying the length of the returned string. Must be between 0 and approximately 2 billion (limited by available memory).
//! - `character`: Required. Variant specifying the character code or string expression whose first character is used.
//!   - If `character` is a numeric value, it's treated as a character code (0-255)
//!   - If `character` is a string, only the first character is used
//!
//! ## Returns
//! Returns a `Variant` (String) containing a string of length `number` composed of the repeating character.
//!
//! ## Remarks
//! The `String` function creates strings filled with repeating characters:
//!
//! - **Character code**: If `character` is numeric (0-255), it's interpreted as an ASCII/ANSI character code
//! - **String argument**: If `character` is a string, only the first character is used (rest is ignored)
//! - **Empty string**: If `number` is 0, returns an empty string ("")
//! - **Negative number**: Causes Error 5 (Invalid procedure call or argument)
//! - **Performance**: Very efficient for creating repeating character strings
//! - **Common uses**: Creating separators, padding, borders, rulers, progress bars
//! - **Memory limit**: `number` is limited by available memory (typically up to ~2 billion characters)
//! - **Related function**: `Space` function is equivalent to `String(n, 32)` or `String(n, " ")`
//!
//! ### Character Code Examples
//! - `String(5, 65)` returns "AAAAA" (65 is ASCII code for 'A')
//! - `String(3, 42)` returns "***" (42 is ASCII code for '*')
//! - `String(10, 45)` returns "----------" (45 is ASCII code for '-')
//! - `String(4, 61)` returns "====" (61 is ASCII code for '=')
//!
//! ### String Argument Examples
//! - `String(5, "A")` returns "AAAAA"
//! - `String(3, "*")` returns "***"
//! - `String(10, "-")` returns "----------"
//! - `String(3, "Hello")` returns "HHH" (only first character used)
//!
//! ## Typical Uses
//! 1. **Line Separators**: Create horizontal lines for text output or reports
//! 2. **Padding**: Pad strings to specific widths for alignment
//! 3. **Borders**: Create box borders for text displays
//! 4. **Progress Indicators**: Build progress bars or loading indicators
//! 5. **Masking**: Create mask strings (asterisks for passwords)
//! 6. **Rulers**: Create ruler lines for text editors or debuggers
//! 7. **Fill Characters**: Initialize strings with specific fill characters
//! 8. **Visual Markers**: Create visual separators in console or debug output
//!
//! ## Basic Examples
//!
//! ### Example 1: Creating Line Separators
//! ```vb6
//! Dim separator As String
//!
//! separator = String(50, "-")     ' "--------------------------------------------------"
//! separator = String(40, "=")     ' "========================================"
//! separator = String(30, "*")     ' "******************************"
//! separator = String(20, 45)      ' "--------------------" (45 = '-')
//! ```
//!
//! ### Example 2: Padding Strings
//! ```vb6
//! Dim text As String
//! Dim padded As String
//!
//! text = "Title"
//!
//! ' Left padding with spaces
//! padded = String(10 - Len(text), " ") & text  ' "     Title"
//!
//! ' Right padding with dots
//! padded = text & String(20 - Len(text), ".")  ' "Title..............."
//! ```
//!
//! ### Example 3: Creating Box Borders
//! ```vb6
//! Sub DrawBox(width As Integer)
//!     Dim topBottom As String
//!     Dim side As String
//!     
//!     topBottom = "+" & String(width - 2, "-") & "+"
//!     side = "|" & String(width - 2, " ") & "|"
//!     
//!     Debug.Print topBottom
//!     Debug.Print side
//!     Debug.Print side
//!     Debug.Print topBottom
//! End Sub
//! ```
//!
//! ### Example 4: Progress Bar
//! ```vb6
//! Function CreateProgressBar(percent As Integer, width As Integer) As String
//!     Dim filled As Integer
//!     Dim empty As Integer
//!     
//!     filled = (percent * width) \ 100
//!     empty = width - filled
//!     
//!     CreateProgressBar = "[" & String(filled, "#") & String(empty, " ") & "]"
//!     ' Example: [#####     ] for 50%
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Create Horizontal Line
//! ```vb6
//! Function HorizontalLine(width As Integer, Optional char As String = "-") As String
//!     HorizontalLine = String(width, char)
//! End Function
//! ```
//!
//! ### Pattern 2: Center Text
//! ```vb6
//! Function CenterText(text As String, width As Integer) As String
//!     Dim padding As Integer
//!     Dim leftPad As Integer
//!     Dim rightPad As Integer
//!     
//!     If Len(text) >= width Then
//!         CenterText = Left$(text, width)
//!         Exit Function
//!     End If
//!     
//!     padding = width - Len(text)
//!     leftPad = padding \ 2
//!     rightPad = padding - leftPad
//!     
//!     CenterText = String(leftPad, " ") & text & String(rightPad, " ")
//! End Function
//! ```
//!
//! ### Pattern 3: Pad Number with Zeros
//! ```vb6
//! Function PadWithZeros(value As Long, totalWidth As Integer) As String
//!     Dim numStr As String
//!     numStr = CStr(value)
//!     
//!     If Len(numStr) >= totalWidth Then
//!         PadWithZeros = numStr
//!     Else
//!         PadWithZeros = String(totalWidth - Len(numStr), "0") & numStr
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 4: Create Table Row Separator
//! ```vb6
//! Function TableSeparator(columnWidths() As Integer) As String
//!     Dim i As Integer
//!     Dim result As String
//!     
//!     result = "+"
//!     For i = LBound(columnWidths) To UBound(columnWidths)
//!         result = result & String(columnWidths(i), "-") & "+"
//!     Next i
//!     
//!     TableSeparator = result
//! End Function
//! ```
//!
//! ### Pattern 5: Mask Sensitive Data
//! ```vb6
//! Function MaskString(text As String, Optional maskChar As String = "*") As String
//!     MaskString = String(Len(text), maskChar)
//! End Function
//! ```
//!
//! ### Pattern 6: Indent Text
//! ```vb6
//! Function IndentText(text As String, level As Integer, _
//!                     Optional indentSize As Integer = 4) As String
//!     IndentText = String(level * indentSize, " ") & text
//! End Function
//! ```
//!
//! ### Pattern 7: Create Ruler
//! ```vb6
//! Function CreateRuler(length As Integer) As String
//!     Dim i As Integer
//!     Dim ruler As String
//!     Dim markers As String
//!     
//!     ' Create tick marks every 10 characters
//!     ruler = ""
//!     markers = ""
//!     For i = 1 To length
//!         If i Mod 10 = 0 Then
//!             ruler = ruler & "|"
//!             markers = markers & CStr(i \ 10 Mod 10)
//!         Else
//!             ruler = ruler & "."
//!             markers = markers & " "
//!         End If
//!     Next i
//!     
//!     CreateRuler = markers & vbCrLf & ruler
//! End Function
//! ```
//!
//! ### Pattern 8: Fill to Width
//! ```vb6
//! Function FillToWidth(text As String, width As Integer, _
//!                      Optional fillChar As String = " ") As String
//!     If Len(text) >= width Then
//!         FillToWidth = Left$(text, width)
//!     Else
//!         FillToWidth = text & String(width - Len(text), fillChar)
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 9: Create Loading Animation
//! ```vb6
//! Function LoadingBar(step As Integer, totalSteps As Integer, width As Integer) As String
//!     Dim filled As Integer
//!     filled = (step * width) \ totalSteps
//!     LoadingBar = String(filled, "=") & ">" & String(width - filled - 1, " ")
//! End Function
//! ```
//!
//! ### Pattern 10: Duplicate Character
//! ```vb6
//! Function RepeatChar(char As String, count As Integer) As String
//!     ' Alias for String function with clearer name
//!     RepeatChar = String(count, char)
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Text Box Drawer
//! ```vb6
//! ' Class: TextBoxDrawer
//! ' Draws ASCII boxes around text
//! Option Explicit
//!
//! Private m_Width As Integer
//! Private m_Padding As Integer
//!
//! Public Sub Initialize(width As Integer, Optional padding As Integer = 1)
//!     m_Width = width
//!     m_Padding = padding
//! End Sub
//!
//! Public Function DrawBox(title As String, lines() As String) As String
//!     Dim result As String
//!     Dim i As Integer
//!     Dim maxLen As Integer
//!     
//!     ' Find maximum line length
//!     maxLen = Len(title)
//!     For i = LBound(lines) To UBound(lines)
//!         If Len(lines(i)) > maxLen Then maxLen = Len(lines(i))
//!     Next i
//!     
//!     ' Adjust width if needed
//!     If m_Width < maxLen + (m_Padding * 2) + 2 Then
//!         m_Width = maxLen + (m_Padding * 2) + 2
//!     End If
//!     
//!     ' Top border
//!     result = "+" & String(m_Width - 2, "-") & "+" & vbCrLf
//!     
//!     ' Title (centered)
//!     If Len(title) > 0 Then
//!         result = result & "|" & CenterInWidth(title, m_Width - 2) & "|" & vbCrLf
//!         result = result & "+" & String(m_Width - 2, "-") & "+" & vbCrLf
//!     End If
//!     
//!     ' Content lines
//!     For i = LBound(lines) To UBound(lines)
//!         result = result & "|" & PadLine(lines(i), m_Width - 2) & "|" & vbCrLf
//!     Next i
//!     
//!     ' Bottom border
//!     result = result & "+" & String(m_Width - 2, "-") & "+"
//!     
//!     DrawBox = result
//! End Function
//!
//! Private Function CenterInWidth(text As String, width As Integer) As String
//!     Dim leftPad As Integer
//!     Dim rightPad As Integer
//!     Dim totalPad As Integer
//!     
//!     totalPad = width - Len(text)
//!     leftPad = totalPad \ 2
//!     rightPad = totalPad - leftPad
//!     
//!     CenterInWidth = String(leftPad, " ") & text & String(rightPad, " ")
//! End Function
//!
//! Private Function PadLine(text As String, width As Integer) As String
//!     Dim content As String
//!     content = String(m_Padding, " ") & text
//!     PadLine = content & String(width - Len(content), " ")
//! End Function
//! ```
//!
//! ### Example 2: Progress Bar Generator
//! ```vb6
//! ' Class: ProgressBarGenerator
//! ' Creates customizable progress bars
//! Option Explicit
//!
//! Private m_Width As Integer
//! Private m_FilledChar As String
//! Private m_EmptyChar As String
//! Private m_ShowPercent As Boolean
//!
//! Public Sub Initialize(width As Integer, Optional filledChar As String = "#", _
//!                       Optional emptyChar As String = "-", _
//!                       Optional showPercent As Boolean = True)
//!     m_Width = width
//!     m_FilledChar = filledChar
//!     m_EmptyChar = emptyChar
//!     m_ShowPercent = showPercent
//! End Sub
//!
//! Public Function Generate(current As Long, total As Long) As String
//!     Dim percent As Integer
//!     Dim filled As Integer
//!     Dim empty As Integer
//!     Dim bar As String
//!     
//!     If total = 0 Then
//!         Generate = "[" & String(m_Width, m_EmptyChar) & "]"
//!         Exit Function
//!     End If
//!     
//!     percent = CInt((current * 100) / total)
//!     If percent > 100 Then percent = 100
//!     
//!     filled = (percent * m_Width) \ 100
//!     empty = m_Width - filled
//!     
//!     bar = "[" & String(filled, m_FilledChar) & String(empty, m_EmptyChar) & "]"
//!     
//!     If m_ShowPercent Then
//!         bar = bar & " " & Format$(percent, "000") & "%"
//!     End If
//!     
//!     Generate = bar
//! End Function
//!
//! Public Function GenerateIndeterminate(step As Integer) As String
//!     ' Animated indeterminate progress bar
//!     Dim pos As Integer
//!     Dim blockSize As Integer
//!     
//!     blockSize = 5
//!     pos = step Mod (m_Width + blockSize)
//!     
//!     If pos < blockSize Then
//!         GenerateIndeterminate = "[" & String(pos, m_FilledChar) & _
//!                                String(m_Width - pos, m_EmptyChar) & "]"
//!     ElseIf pos < m_Width Then
//!         GenerateIndeterminate = "[" & String(pos - blockSize, m_EmptyChar) & _
//!                                String(blockSize, m_FilledChar) & _
//!                                String(m_Width - pos, m_EmptyChar) & "]"
//!     Else
//!         GenerateIndeterminate = "[" & String(m_Width - (pos - m_Width), m_EmptyChar) & _
//!                                String(pos - m_Width, m_FilledChar) & "]"
//!     End If
//! End Function
//! ```
//!
//! ### Example 3: Table Formatter Module
//! ```vb6
//! ' Module: TableFormatter
//! ' Creates formatted ASCII tables
//! Option Explicit
//!
//! Public Function CreateTable(headers() As String, data() As String, _
//!                             columnWidths() As Integer) As String
//!     Dim result As String
//!     Dim i As Integer
//!     Dim j As Integer
//!     Dim row As Integer
//!     Dim col As Integer
//!     
//!     ' Top border
//!     result = CreateBorder(columnWidths, True) & vbCrLf
//!     
//!     ' Headers
//!     result = result & CreateRow(headers, columnWidths) & vbCrLf
//!     
//!     ' Separator
//!     result = result & CreateBorder(columnWidths, False) & vbCrLf
//!     
//!     ' Data rows
//!     row = LBound(data, 1)
//!     Do While row <= UBound(data, 1)
//!         Dim rowData() As String
//!         ReDim rowData(LBound(headers) To UBound(headers))
//!         
//!         For col = LBound(headers) To UBound(headers)
//!             rowData(col) = data(row, col)
//!         Next col
//!         
//!         result = result & CreateRow(rowData, columnWidths) & vbCrLf
//!         row = row + 1
//!     Loop
//!     
//!     ' Bottom border
//!     result = result & CreateBorder(columnWidths, True)
//!     
//!     CreateTable = result
//! End Function
//!
//! Private Function CreateBorder(columnWidths() As Integer, heavy As Boolean) As String
//!     Dim i As Integer
//!     Dim result As String
//!     Dim corner As String
//!     Dim line As String
//!     
//!     If heavy Then
//!         corner = "+"
//!         line = "="
//!     Else
//!         corner = "+"
//!         line = "-"
//!     End If
//!     
//!     result = corner
//!     For i = LBound(columnWidths) To UBound(columnWidths)
//!         result = result & String(columnWidths(i) + 2, line) & corner
//!     Next i
//!     
//!     CreateBorder = result
//! End Function
//!
//! Private Function CreateRow(cells() As String, columnWidths() As Integer) As String
//!     Dim i As Integer
//!     Dim result As String
//!     Dim cell As String
//!     
//!     result = "|"
//!     For i = LBound(cells) To UBound(cells)
//!         cell = " " & cells(i)
//!         If Len(cell) < columnWidths(i) + 1 Then
//!             cell = cell & String(columnWidths(i) + 1 - Len(cell), " ")
//!         End If
//!         result = result & cell & " |"
//!     Next i
//!     
//!     CreateRow = result
//! End Function
//! ```
//!
//! ### Example 4: Text Padding Utilities
//! ```vb6
//! ' Module: TextPaddingUtils
//! ' Utilities for padding and aligning text
//! Option Explicit
//!
//! Public Function PadLeft(text As String, width As Integer, _
//!                         Optional padChar As String = " ") As String
//!     If Len(text) >= width Then
//!         PadLeft = text
//!     Else
//!         PadLeft = String(width - Len(text), padChar) & text
//!     End If
//! End Function
//!
//! Public Function PadRight(text As String, width As Integer, _
//!                          Optional padChar As String = " ") As String
//!     If Len(text) >= width Then
//!         PadRight = text
//!     Else
//!         PadRight = text & String(width - Len(text), padChar)
//!     End If
//! End Function
//!
//! Public Function PadCenter(text As String, width As Integer, _
//!                           Optional padChar As String = " ") As String
//!     Dim totalPad As Integer
//!     Dim leftPad As Integer
//!     Dim rightPad As Integer
//!     
//!     If Len(text) >= width Then
//!         PadCenter = text
//!         Exit Function
//!     End If
//!     
//!     totalPad = width - Len(text)
//!     leftPad = totalPad \ 2
//!     rightPad = totalPad - leftPad
//!     
//!     PadCenter = String(leftPad, padChar) & text & String(rightPad, padChar)
//! End Function
//!
//! Public Function CreateLine(width As Integer, Optional lineChar As String = "-") As String
//!     CreateLine = String(width, lineChar)
//! End Function
//!
//! Public Function FrameText(text As String, width As Integer, _
//!                           Optional frameChar As String = "*") As String
//!     Dim topBottom As String
//!     Dim middle As String
//!     
//!     topBottom = String(width, frameChar)
//!     middle = frameChar & PadCenter(text, width - 2) & frameChar
//!     
//!     FrameText = topBottom & vbCrLf & middle & vbCrLf & topBottom
//! End Function
//!
//! Public Function Underline(text As String, Optional underlineChar As String = "-") As String
//!     Underline = text & vbCrLf & String(Len(text), underlineChar)
//! End Function
//!
//! Public Function NumberedLine(lineNum As Integer, text As String, _
//!                              totalWidth As Integer) As String
//!     Dim numStr As String
//!     numStr = PadLeft(CStr(lineNum), 4) & ": "
//!     NumberedLine = numStr & text & String(totalWidth - Len(numStr) - Len(text), " ")
//! End Function
//! ```
//!
//! ## Error Handling
//! The `String` function can raise the following errors:
//!
//! - **Error 5 (Invalid procedure call or argument)**: If `number` is negative
//! - **Error 6 (Overflow)**: If `number` is too large (exceeds memory limits)
//! - **Error 13 (Type mismatch)**: If `character` cannot be converted to a valid character
//! - **Error 5 (Invalid procedure call)**: If `character` is a numeric value outside 0-255 range
//!
//! ## Performance Notes
//! - Very fast and efficient for creating repeating character strings
//! - More efficient than concatenating characters in a loop
//! - Memory allocation is done once for the entire string
//! - For very large strings (millions of characters), consider memory constraints
//! - Slightly faster than equivalent loop-based approaches
//!
//! ## Best Practices
//! 1. **Use descriptive variable names** when storing String results (e.g., `separator`, `padding`)
//! 2. **Validate number parameter** to ensure it's non-negative before calling
//! 3. **Use named constants** for common character codes (e.g., `Const ASTERISK = 42`)
//! 4. **Prefer string literals** over character codes for clarity (e.g., `String(5, "*")` vs `String(5, 42)`)
//! 5. **Consider Space function** for creating space-filled strings (`Space(n)` vs `String(n, " ")`)
//! 6. **Cache repeated strings** if used multiple times in a function
//! 7. **Check string length** before padding operations to avoid overflow
//! 8. **Use for visual elements** like separators, borders, and progress indicators
//! 9. **Document character choice** when using character codes instead of string literals
//! 10. **Test with edge cases** like `number = 0` and very large values
//!
//! ## Comparison Table
//!
//! | Function | Purpose | Example | Result |
//! |----------|---------|---------|--------|
//! | `String` | Repeat character | `String(5, "*")` | `"*****"` |
//! | `Space` | Repeat space | `Space(5)` | `"     "` |
//! | `String$` | Type-declared variant | `String$(5, 42)` | `"*****"` |
//!
//! ## Platform Notes
//! - Available in VB6 and VBA
//! - Not available in `VBScript` (use custom function instead)
//! - `String$` variant returns String type directly (slightly faster)
//! - Behavior consistent across all platforms
//! - Character codes follow ANSI/ASCII standard (0-255)
//! - Maximum string length limited by available memory
//!
//! ## Limitations
//! - Cannot create strings with multiple different repeating characters in one call
//! - Character code must be 0-255 (no Unicode character codes)
//! - If `character` is a string, only first character is used (no validation of length)
//! - Very large `number` values can cause out-of-memory errors
//! - No built-in way to create alternating patterns
//! - Cannot specify different characters at different positions

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn string_basic() {
        let source = r#"
Sub Test()
    result = String(50, "-")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_variable_assignment() {
        let source = r"
Sub Test()
    Dim separator As String
    separator = String(width, char)
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_character_code() {
        let source = r"
Sub Test()
    result = String(10, 65)
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_asterisk() {
        let source = r#"
Sub Test()
    line = String(20, "*")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_padding() {
        let source = r#"
Sub Test()
    padded = text & String(width - Len(text), " ")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_concatenation() {
        let source = r#"
Sub Test()
    box = "+" & String(width - 2, "-") & "+"
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_if_statement() {
        let source = r#"
Sub Test()
    If Len(text) < width Then
        text = text & String(width - Len(text), " ")
    End If
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_for_loop() {
        let source = r#"
Sub Test()
    For i = 1 To 10
        Debug.Print String(i, "*")
    Next i
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_function_return() {
        let source = r#"
Function CreateLine(width As Integer) As String
    CreateLine = String(width, "-")
End Function
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_function_argument() {
        let source = r#"
Sub Test()
    Call DrawLine(String(50, "="))
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_array_assignment() {
        let source = r#"
Sub Test()
    lines(i) = String(width, "-")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_debug_print() {
        let source = r#"
Sub Test()
    Debug.Print String(40, "=")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_msgbox() {
        let source = r#"
Sub Test()
    MsgBox String(5, "*") & " Alert " & String(5, "*")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_select_case() {
        let source = r#"
Sub Test()
    Select Case lineType
        Case 1
            line = String(width, "-")
        Case 2
            line = String(width, "=")
    End Select
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_do_while() {
        let source = r#"
Sub Test()
    Do While Len(buffer) < targetSize
        buffer = buffer & String(10, " ")
    Loop
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_do_until() {
        let source = r"
Sub Test()
    Do Until Len(str) >= width
        str = str & String(1, fillChar)
    Loop
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_while_wend() {
        let source = r#"
Sub Test()
    While i < count
        output = output & String(5, "*")
        i = i + 1
    Wend
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_iif() {
        let source = r#"
Sub Test()
    line = IIf(style = 1, String(width, "-"), String(width, "="))
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_with_statement() {
        let source = r#"
Sub Test()
    With textBox
        .Border = String(.Width, "-")
    End With
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_parentheses() {
        let source = r#"
Sub Test()
    result = (String(10, "*") & text & String(10, "*"))
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_error_handling() {
        let source = r#"
Sub Test()
    On Error Resume Next
    separator = String(width, char)
    If Err.Number <> 0 Then
        separator = ""
    End If
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_property_assignment() {
        let source = r#"
Sub Test()
    obj.Separator = String(100, "=")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_progress_bar() {
        let source = r##"
Sub Test()
    bar = "[" & String(filled, "#") & String(empty, " ") & "]"
End Sub
"##;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_zero_length() {
        let source = r#"
Sub Test()
    empty = String(0, "*")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_expression_length() {
        let source = r#"
Sub Test()
    line = String(maxWidth - currentWidth, " ")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_print_statement() {
        let source = r#"
Sub Test()
    Print #1, String(80, "-")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_class_usage() {
        let source = r#"
Sub Test()
    Set formatter = New TextFormatter
    formatter.SetBorder String(50, "*")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/string/string_function",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}