vb6parse 1.0.1

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
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
//! ## `Join` Function
//!
//! Returns a string created by joining a number of substrings contained in an array.
//!
//! ## Syntax
//!
//! ```text
//! Join(sourcearray, [delimiter])
//! ```
//!
//! ## Parameters
//!
//! - **sourcearray** (Required): One-dimensional array containing substrings to be joined
//! - **delimiter** (Optional): `String` used to separate the substrings in the returned string
//!   - If omitted, space character (" ") is used
//!   - If empty string (""), items are concatenated with no separator
//!
//! ## Return Value
//!
//! Returns a `String`
//!
//! - `String` containing all elements of the array joined by the delimiter
//! - `Empty` string ("") if array has zero length
//! - Returns `Null` if `sourcearray` is `Null`
//! - Each array element is converted to `String` before joining
//! - Non-string elements are automatically converted using `Str`/`CStr`
//! - `Empty` array elements become empty strings in result
//! - Trailing / leading spaces in delimiter are preserved
//!
//! ## Remarks
//!
//! The `Join` function is the inverse of the `Split` function:
//!
//! - Combines array elements into a single string
//! - Only works with one-dimensional arrays
//! - Array elements are converted to strings automatically
//! - Default delimiter is a space (" ")
//! - `Empty` string delimiter concatenates without separators
//! - `Null` array returns `Null` (not an error)
//! - Empty array (zero length) returns empty string
//! - Preserves empty array elements as empty strings
//! - Very efficient for building strings from multiple parts
//! - Much faster than repeated string concatenation in loops
//! - Available in VB6 and VBA (added in VB6/Office 2000)
//! - Common in text processing and file generation
//! - Works with `Variant` arrays containing mixed types
//! - Does not add delimiter after last element
//!
//! ### Common Errors
//!
//! - **Error 13** (Type Mismatch): `sourcearray` is a multi-dimensional array.
//!
//! ## Performance Considerations
//!
//! - **Very Efficient**: Join is much faster than repeated concatenation
//! - **String Building**: Use Join instead of concatenation in loops
//! - **Memory Usage**: Creates single string allocation for result
//! - **Large Arrays**: Handles large arrays efficiently
//!
//! ### Performance comparison:
//!
//! ```vb6
//! ' SLOW: Repeated concatenation
//! Dim result As String
//! For i = 0 To 999
//!     result = result & arr(i) & ","
//! Next i
//!
//! ' FAST: Using Join
//! result = Join(arr, ",")
//! ```
//!
//! ## Typical Uses
//!
//! 1. **CSV Generation**: Create comma-separated value strings
//! 2. **Path Building**: Combine path components with backslashes
//! 3. **SQL Generation**: Build SQL queries from parts
//! 4. **Text Formatting**: Create formatted text from arrays
//! 5. **File Output**: Generate text file content
//! 6. **URL Building**: Construct URLs from components
//! 7. **String Building**: Efficient alternative to concatenation loops
//! 8. **Report Generation**: Format report lines from data arrays
//!
//! ## Limitations
//!
//! - Cannot join multi-dimensional arrays (use loops to flatten first)
//! - Returns `Null` for `Null` array (not empty `String`)
//! - No built-in escaping for CSV (must implement manually)
//! - Cannot skip empty elements automatically
//! - No formatting options for numeric values
//! - Delimiter is applied between all elements (no custom logic)
//!
//! ### Platform and Version Notes
//!
//! - Added in VB6 and Office 2000 VBA
//! - Not available in VB5 or earlier
//! - Part of `VBA.Strings` module
//! - Returns `String` type
//! - Only works with one-dimensional arrays
//! - Automatically converts array elements to `String`
//!
//! ## Related Functions
//!
//! - `Split`: Split string into array (inverse of `Join`)
//! - `Filter`: Filter array elements based on criteria
//! - `UBound`/`LBound`: Get array bounds
//! - `Array`: Create array from values
//! - `Replace`: Replace substrings in `String`
//!
//! ## Examples
//!
//! ### Join With Default Delimiter (space)
//!
//! ```vb6
//! Dim words(2) As String
//! words(0) = "Hello"
//! words(1) = "Visual"
//! words(2) = "Basic"
//!
//! Debug.Print Join(words)              ' "Hello Visual Basic"
//! ```
//!
//! ### Join With Custom Delimiter
//!
//! ```vb6
//! ' Example 2: Join with custom delimiter
//! Dim values(3) As String
//! values(0) = "apple"
//! values(1) = "banana"
//! values(2) = "cherry"
//! values(3) = "date"
//!
//! Debug.Print Join(values, ", ")       ' "apple, banana, cherry, date"
//! Debug.Print Join(values, " | ")      ' "apple | banana | cherry | date"
//! Debug.Print Join(values, "")         ' "applebananacherrydate"
//! ```
//!
//! ### CSV Generation
//!
//! ```vb6
//! Dim fields(2) As String
//! fields(0) = "John Doe"
//! fields(1) = "Engineer"
//! fields(2) = "50000"
//!
//! Dim csvLine As String
//! csvLine = Join(fields, ",")
//! Debug.Print csvLine                  ' "John Doe,Engineer,50000"
//! ```
//!
//! ### Working with Split and Join
//!
//! ```vb6
//! Dim original As String
//! Dim parts() As String
//! Dim rebuilt As String
//!
//! original = "one-two-three-four"
//! parts = Split(original, "-")
//! rebuilt = Join(parts, " ")
//! Debug.Print rebuilt                  ' "one two three four"
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Build CSV Row
//!
//! ```vb6
//! Function BuildCSVRow(fields As Variant) As String
//!     BuildCSVRow = Join(fields, ",")
//! End Function
//! ```
//!
//! ### Pattern 2: Join With Line Breaks
//!
//! ```vb6
//! Function JoinLines(lines As Variant) As String
//!     JoinLines = Join(lines, vbCrLf)
//! End Function
//! ```
//!
//! ### Pattern 3: Build Path From Components
//!
//! ```vb6
//! Function BuildPath(ParamArray parts() As Variant) As String
//!     Dim arr() As String
//!     Dim i As Long
//!     
//!     ReDim arr(LBound(parts) To UBound(parts))
//!     For i = LBound(parts) To UBound(parts)
//!         arr(i) = CStr(parts(i))
//!     Next i
//!     
//!     BuildPath = Join(arr, "\")
//! End Function
//! ```
//!
//! ### Pattern 4: Create Comma-Separated List
//!
//! ```vb6
//! Function ToCommaSeparated(items As Variant) As String
//!     If IsArray(items) Then
//!         ToCommaSeparated = Join(items, ", ")
//!     Else
//!         ToCommaSeparated = CStr(items)
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 5: Build SQL IN Clause
//!
//! ```vb6
//! Function BuildInClause(values As Variant) As String
//!     Dim i As Long
//!     Dim quoted() As String
//!     
//!     If Not IsArray(values) Then Exit Function
//!     
//!     ReDim quoted(LBound(values) To UBound(values))
//!     For i = LBound(values) To UBound(values)
//!         quoted(i) = "'" & Replace(CStr(values(i)), "'", "''") & "'"
//!     Next i
//!     
//!     BuildInClause = Join(quoted, ", ")
//! End Function
//! ```
//!
//! ### Pattern 6: Join Non-Empty Values Only
//!
//! ```vb6
//! Function JoinNonEmpty(arr As Variant, delimiter As String) As String
//!     Dim result As Collection
//!     Dim i As Long
//!     Dim temp() As String
//!     Dim count As Long
//!     
//!     If Not IsArray(arr) Then Exit Function
//!     
//!     Set result = New Collection
//!     For i = LBound(arr) To UBound(arr)
//!         If Len(arr(i)) > 0 Then
//!             result.Add CStr(arr(i))
//!         End If
//!     Next i
//!     
//!     If result.Count = 0 Then
//!         JoinNonEmpty = ""
//!         Exit Function
//!     End If
//!     
//!     ReDim temp(0 To result.Count - 1)
//!     For i = 1 To result.Count
//!         temp(i - 1) = result(i)
//!     Next i
//!     
//!     JoinNonEmpty = Join(temp, delimiter)
//! End Function
//! ```
//!
//! ### Pattern 7: Format Array For Display
//!
//! ```vb6
//! Function FormatArray(arr As Variant) As String
//!     If Not IsArray(arr) Then
//!         FormatArray = CStr(arr)
//!     Else
//!         FormatArray = "[" & Join(arr, ", ") & "]"
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 8: Build WHERE Clause
//!
//! ```vb6
//! Function BuildWhereClause(conditions As Variant) As String
//!     If Not IsArray(conditions) Then Exit Function
//!     
//!     If UBound(conditions) < LBound(conditions) Then
//!         BuildWhereClause = ""
//!     Else
//!         BuildWhereClause = Join(conditions, " AND ")
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 9: Create Delimited String With Quotes
//!
//! ```vb6
//! Function JoinQuoted(items As Variant, delimiter As String) As String
//!     Dim i As Long
//!     Dim quoted() As String
//!     
//!     If Not IsArray(items) Then Exit Function
//!     
//!     ReDim quoted(LBound(items) To UBound(items))
//!     For i = LBound(items) To UBound(items)
//!         quoted(i) = Chr(34) & items(i) & Chr(34)  ' Chr(34) = "
//!     Next i
//!     
//!     JoinQuoted = Join(quoted, delimiter)
//! End Function
//! ```
//!
//! ### Pattern 10: Reverse of Split for Round-Trip
//!
//! ```vb6
//! Function ReverseTransform(text As String) As String
//!     Dim parts() As String
//!     Dim i As Long
//!     
//!     parts = Split(text, " ")
//!     
//!     ' Reverse array
//!     For i = LBound(parts) To (UBound(parts) - LBound(parts)) \ 2 + LBound(parts)
//!         Dim temp As String
//!         temp = parts(i)
//!         parts(i) = parts(UBound(parts) - (i - LBound(parts)))
//!         parts(UBound(parts) - (i - LBound(parts))) = temp
//!     Next i
//!     
//!     ReverseTransform = Join(parts, " ")
//! End Function
//! ```
//!
//! ## Advanced Usage Examples
//!
//! ### Example 1: CSV Builder with proper escaping
//!
//! ```vb6
//! Public Class CSVBuilder
//!     Private m_rows As Collection
//!     
//!     Private Sub Class_Initialize()
//!         Set m_rows = New Collection
//!     End Sub
//!     
//!     Public Sub AddRow(ParamArray values() As Variant)
//!         Dim i As Long
//!         Dim fields() As String
//!         
//!         ReDim fields(LBound(values) To UBound(values))
//!         For i = LBound(values) To UBound(values)
//!             fields(i) = EscapeCSV(CStr(values(i)))
//!         Next i
//!         
//!         m_rows.Add Join(fields, ",")
//!     End Sub
//!     
//!     Private Function EscapeCSV(value As String) As String
//!         If InStr(value, ",") > 0 Or InStr(value, Chr(34)) > 0 Or _
//!            InStr(value, vbCrLf) > 0 Then
//!             ' Need to quote and escape
//!             EscapeCSV = Chr(34) & Replace(value, Chr(34), Chr(34) & Chr(34)) & Chr(34)
//!         Else
//!             EscapeCSV = value
//!         End If
//!     End Function
//!     
//!     Public Function GetCSV() As String
//!         Dim i As Long
//!         Dim lines() As String
//!         
//!         If m_rows.Count = 0 Then
//!             GetCSV = ""
//!             Exit Function
//!         End If
//!         
//!         ReDim lines(0 To m_rows.Count - 1)
//!         For i = 1 To m_rows.Count
//!             lines(i - 1) = m_rows(i)
//!         Next i
//!         
//!         GetCSV = Join(lines, vbCrLf)
//!     End Function
//!     
//!     Public Sub Clear()
//!         Set m_rows = New Collection
//!     End Sub
//! End Class
//! ```
//!
//! ### Example 2: String Builder For Efficient Concatenation
//!
//! ```vb6
//! Public Class StringBuilder
//!     Private m_parts As Collection
//!     Private m_delimiter As String
//!     
//!     Private Sub Class_Initialize()
//!         Set m_parts = New Collection
//!         m_delimiter = ""
//!     End Sub
//!     
//!     Public Property Let Delimiter(value As String)
//!         m_delimiter = value
//!     End Property
//!     
//!     Public Sub Append(text As String)
//!         m_parts.Add text
//!     End Sub
//!     
//!     Public Sub AppendLine(text As String)
//!         m_parts.Add text & vbCrLf
//!     End Sub
//!     
//!     Public Function ToString() As String
//!         Dim i As Long
//!         Dim arr() As String
//!         
//!         If m_parts.Count = 0 Then
//!             ToString = ""
//!             Exit Function
//!         End If
//!         
//!         ReDim arr(0 To m_parts.Count - 1)
//!         For i = 1 To m_parts.Count
//!             arr(i - 1) = m_parts(i)
//!         Next i
//!         
//!         ToString = Join(arr, m_delimiter)
//!     End Function
//!     
//!     Public Sub Clear()
//!         Set m_parts = New Collection
//!     End Sub
//!     
//!     Public Function Length() As Long
//!         Length = Len(ToString())
//!     End Function
//! End Class
//! ```
//!
//! ### Example 3: Query Builder Using Join
//!
//! ```vb6
//! Public Class QueryBuilder
//!     Private m_select As Collection
//!     Private m_from As String
//!     Private m_where As Collection
//!     Private m_orderBy As Collection
//!     
//!     Private Sub Class_Initialize()
//!         Set m_select = New Collection
//!         Set m_where = New Collection
//!         Set m_orderBy = New Collection
//!     End Sub
//!     
//!     Public Sub AddField(fieldName As String)
//!         m_select.Add fieldName
//!     End Sub
//!     
//!     Public Sub SetTable(tableName As String)
//!         m_from = tableName
//!     End Sub
//!     
//!     Public Sub AddCondition(condition As String)
//!         m_where.Add condition
//!     End Sub
//!     
//!     Public Sub AddOrderBy(fieldName As String)
//!         m_orderBy.Add fieldName
//!     End Sub
//!     
//!     Public Function BuildSQL() As String
//!         Dim sql As String
//!         Dim fields() As String
//!         Dim conditions() As String
//!         Dim orderFields() As String
//!         Dim i As Long
//!         
//!         ' SELECT clause
//!         If m_select.Count = 0 Then
//!             sql = "SELECT *"
//!         Else
//!             ReDim fields(0 To m_select.Count - 1)
//!             For i = 1 To m_select.Count
//!                 fields(i - 1) = m_select(i)
//!             Next i
//!             sql = "SELECT " & Join(fields, ", ")
//!         End If
//!         
//!         ' FROM clause
//!         If m_from <> "" Then
//!             sql = sql & " FROM " & m_from
//!         End If
//!         
//!         ' WHERE clause
//!         If m_where.Count > 0 Then
//!             ReDim conditions(0 To m_where.Count - 1)
//!             For i = 1 To m_where.Count
//!                 conditions(i - 1) = m_where(i)
//!             Next i
//!             sql = sql & " WHERE " & Join(conditions, " AND ")
//!         End If
//!         
//!         ' ORDER BY clause
//!         If m_orderBy.Count > 0 Then
//!             ReDim orderFields(0 To m_orderBy.Count - 1)
//!             For i = 1 To m_orderBy.Count
//!                 orderFields(i - 1) = m_orderBy(i)
//!             Next i
//!             sql = sql & " ORDER BY " & Join(orderFields, ", ")
//!         End If
//!         
//!         BuildSQL = sql
//!     End Function
//!     
//!     Public Sub Clear()
//!         Set m_select = New Collection
//!         m_from = ""
//!         Set m_where = New Collection
//!         Set m_orderBy = New Collection
//!     End Sub
//! End Class
//! ```
//!
//! ### Example 4: Report Formatter
//!
//! ```vb6
//! Public Class ReportFormatter
//!     Public Function FormatTable(data As Variant, headers As Variant, _
//!                                  Optional delimiter As String = " | ") As String
//!         Dim lines As Collection
//!         Dim i As Long, j As Long
//!         Dim row() As String
//!         Dim allLines() As String
//!         
//!         Set lines = New Collection
//!         
//!         ' Add header
//!         If IsArray(headers) Then
//!             lines.Add Join(headers, delimiter)
//!             
//!             ' Add separator
//!             ReDim row(LBound(headers) To UBound(headers))
//!             For j = LBound(headers) To UBound(headers)
//!                 row(j) = String(Len(headers(j)), "-")
//!             Next j
//!             lines.Add Join(row, delimiter)
//!         End If
//!         
//!         ' Add data rows
//!         If IsArray(data) Then
//!             For i = LBound(data) To UBound(data)
//!                 If IsArray(data(i)) Then
//!                     lines.Add Join(data(i), delimiter)
//!                 Else
//!                     lines.Add CStr(data(i))
//!                 End If
//!             Next i
//!         End If
//!         
//!         ' Convert collection to array and join
//!         ReDim allLines(0 To lines.Count - 1)
//!         For i = 1 To lines.Count
//!             allLines(i - 1) = lines(i)
//!         Next i
//!         
//!         FormatTable = Join(allLines, vbCrLf)
//!     End Function
//!     
//!     Public Function FormatList(items As Variant, _
//!                                Optional prefix As String = "- ") As String
//!         Dim i As Long
//!         Dim lines() As String
//!         
//!         If Not IsArray(items) Then
//!             FormatList = prefix & CStr(items)
//!             Exit Function
//!         End If
//!         
//!         ReDim lines(LBound(items) To UBound(items))
//!         For i = LBound(items) To UBound(items)
//!             lines(i) = prefix & CStr(items(i))
//!         Next i
//!         
//!         FormatList = Join(lines, vbCrLf)
//!     End Function
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! Join handles several special cases:
//!
//! ### Empty Array Returns Empty String
//!
//! ```vb6
//! Dim emptyArr() As String
//! ReDim emptyArr(0 To -1)  ' Zero-length array
//! Debug.Print Join(emptyArr, ",")  ' Returns ""
//! ```
//!
//! ### Null Array Returns Null
//!
//! ```vb6
//! Dim nullArr As Variant
//! nullArr = Null
//! Debug.Print IsNull(Join(nullArr, ","))  ' True
//! ```
//!
//! ### Works With Mixed-Type Variant Arrays
//!
//! ```vb6
//! Dim mixed(2) As Variant
//! mixed(0) = 123
//! mixed(1) = "text"
//! mixed(2) = True
//! Debug.Print Join(mixed, "-")  ' "123-text-True"
//! ```
//!
//! ### Multi-Dimensional Arrays Cause Type Mismatch Error
//!
//! ```vb6
//! Dim multi(1, 1) As String
//! ' Join(multi, ",")  ' Error 13: Type Mismatch
//! ```
//!
//! ## Best Practices
//!
//! 1. **Use Join for String Building**: Much faster than repeated concatenation
//! 2. **CSV Generation**: Properly escape values containing delimiters
//! 3. **Empty Delimiter**: Use "" to concatenate without separators
//! 4. **Check Array**: Verify array exists before calling `Join`
//! 5. **Null Handling**: Be aware `Join` returns `Null` for `Null` arrays
//! 6. **Line Breaks**: Use `vbCrLf`, `vbLf`, or `vbCr` as delimiter for multi-line text
//! 7. **Collection to String**: Convert `Collection` to array first, then `Join`
//! 8. **Type Conversion**: `Join` automatically converts non-string elements
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Input | Output |
//! |----------|---------|-------|--------|
//! | `Join` | Combine array to string | `Array` | `String` |
//! | `Split` | Split string to array | `String` | `Array` |
//! | `String` concatenation (&) | Combine two strings | `Strings` | `String` |
//! | `Filter` | Filter array elements | `Array` | `Array` |
//! | `UBound`/`LBound` | Get array bounds | `Array` | `Long` |
//!
//! ## Join vs String Concatenation
//!
//! ```vb6
//! Dim arr(2) As String
//! arr(0) = "A"
//! arr(1) = "B"
//! arr(2) = "C"
//!
//! ' Using Join (FAST)
//! result = Join(arr, ",")              ' "A,B,C"
//!
//! ' Using concatenation (SLOW)
//! result = arr(0) & "," & arr(1) & "," & arr(2)  ' "A,B,C"
//!
//! ' For large arrays, Join is dramatically faster
//! ```
//!
//! ## Join and Split Round-Trip
//!
//! ```vb6
//! ' Original string
//! original = "apple,banana,cherry"
//!
//! ' Split into array
//! parts = Split(original, ",")         ' ["apple", "banana", "cherry"]
//!
//! ' Join back to string
//! rebuilt = Join(parts, ",")           ' "apple,banana,cherry"
//!
//! Debug.Print original = rebuilt       ' True - perfect round-trip
//! ```
//!

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

    #[test]
    fn join_basic() {
        let source = r"
Sub Test()
    result = Join(myArray)
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_with_delimiter() {
        let source = r#"
Sub Test()
    result = Join(items, ", ")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_if_statement() {
        let source = r#"
Sub Test()
    If Len(Join(parts, "-")) > 0 Then
        ProcessResult
    End If
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_function_return() {
        let source = r#"
Function GetCSV(fields As Variant) As String
    GetCSV = Join(fields, ",")
End Function
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_debug_print() {
        let source = r#"
Sub Test()
    Debug.Print Join(values, " | ")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_msgbox() {
        let source = r"
Sub Test()
    MsgBox Join(names, vbCrLf)
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_variable_assignment() {
        let source = r#"
Sub Test()
    Dim combined As String
    combined = Join(words, " ")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_property_assignment() {
        let source = r"
Sub Test()
    obj.DisplayText = Join(obj.Lines, vbCrLf)
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_concatenation() {
        let source = r#"
Sub Test()
    result = "Values: " & Join(data, ", ")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_in_class() {
        let source = r#"
Private Sub Class_Initialize()
    m_text = Join(m_parts, "")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_with_statement() {
        let source = r"
Sub Test()
    With builder
        .Output = Join(.Parts, .Delimiter)
    End With
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_function_argument() {
        let source = r"
Sub Test()
    Call ProcessText(Join(lines, vbCrLf))
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_select_case() {
        let source = r#"
Sub Test()
    Select Case Join(tags, ",")
        Case "A,B,C"
            HandleABC
        Case Else
            HandleOther
    End Select
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn join_for_loop() {
        let source = r#"
Sub Test()
    Dim i As Integer
    For i = 0 To UBound(dataRows)
        lines(i) = Join(dataRows(i), ",")
    Next i
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn join_elseif() {
        let source = r#"
Sub Test()
    If format = "csv" Then
        output = Join(data, ",")
    ElseIf format = "tsv" Then
        output = Join(data, vbTab)
    End If
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_iif() {
        let source = r#"
Sub Test()
    result = IIf(useComma, Join(arr, ","), Join(arr, ";"))
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_parentheses() {
        let source = r#"
Sub Test()
    result = (Join(values, "-"))
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_array_assignment() {
        let source = r#"
Sub Test()
    csvRows(i) = Join(fields(i), ",")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_collection_add() {
        let source = r"
Sub Test()
    lines.Add Join(row, vbTab)
End Sub
";
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_comparison() {
        let source = r#"
Sub Test()
    If Join(actual, ",") = Join(expected, ",") Then
        MsgBox "Match!"
    End If
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_nested_call() {
        let source = r#"
Sub Test()
    result = UCase(Join(names, " "))
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_while_wend() {
        let source = r#"
Sub Test()
    While Len(Join(buffer, "")) < maxLen
        buffer.Add GetNextItem()
    Wend
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_do_while() {
        let source = r#"
Sub Test()
    Do While Len(Join(parts, "")) > 0
        ProcessParts
    Loop
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_do_until() {
        let source = r#"
Sub Test()
    Do Until Join(fields, "") = ""
        fields = GetNextFields()
    Loop
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_with_split() {
        let source = r#"
Sub Test()
    parts = Split(text, "-")
    result = Join(parts, " ")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_csv_builder() {
        let source = r#"
Function BuildCSV(fields As Variant) As String
    BuildCSV = Join(fields, ",")
End Function
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn join_empty_delimiter() {
        let source = r#"
Sub Test()
    concatenated = Join(chars, "")
End Sub
"#;
        let (cst_opt, _failure) = 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/arrays/join");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}