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
//! ## `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 or is undimensioned
//! - Each array element is converted to `String` before joining
//! - Non-string elements are automatically converted using `Str`/`CStr` semantics
//! - 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
//! - Empty/undimensioned array 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 not an array, or is a multi-dimensional array.
//! - **Error 94** (Invalid use of Null): `sourcearray` is `Null`.
//!
//! ## 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)
//! - 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
//! 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(parts() As String) As String
//! BuildPath = Join(parts, "\")
//! 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 String
//! Dim count As Long
//! Dim i As Long
//!
//! If Not IsArray(arr) Then Exit Function
//!
//! ' Count non-empty elements
//! count = 0
//! For i = LBound(arr) To UBound(arr)
//! If Len(arr(i)) > 0 Then count = count + 1
//! Next i
//!
//! If count = 0 Then
//! JoinNonEmpty = ""
//! Exit Function
//! End If
//!
//! ReDim result(0 To count - 1)
//! count = 0
//! For i = LBound(arr) To UBound(arr)
//! If Len(arr(i)) > 0 Then
//! result(count) = CStr(arr(i))
//! count = count + 1
//! End If
//! Next i
//!
//! JoinNonEmpty = Join(result, 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
//! ```
//!
//! ## 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
//! 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
//! 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 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
//! 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 Sub Class_Initialize()
//! Set m_select = New Collection
//! Set m_where = 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 Function BuildSQL() As String
//! Dim fields() As String
//!
//! ' SELECT clause
//! If m_select.Count > 0 Then
//! ReDim fields(0 To m_select.Count - 1)
//! For i = 1 To m_select.Count
//! fields(i - 1) = m_select(i)
//! Next i
//! BuildSQL = "SELECT " & Join(fields, ", ")
//! Else
//! BuildSQL = "SELECT *"
//! End If
//!
//! ' FROM clause
//! If m_from <> "" Then
//! BuildSQL = BuildSQL & " FROM " & m_from
//! End If
//! End Function
//! 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
//! Set lines = New Collection
//!
//! ' Add header
//! If IsArray(headers) Then
//! lines.Add Join(headers, 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
//! 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 ""
//! ```
//!
//! ### 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. **Line Breaks**: Use `vbCrLf`, `vbLf`, or `vbCr` as delimiter for multi-line text
//! 6. **Type Conversion**: `Join` automatically converts non-string elements
//! 7. **Collection to String**: Convert `Collection` to array first, then `Join`
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Input | Output |
//! |----------|---------|-------|--------|
//! | `Join` | Combine array to string | `Array` | `String` |
//! | `Split` | Split string to array | `String` | `Array` |
//! | `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"
//! ```
//!
//! ## Join and Split Round-Trip
//!
//! ```vb6
//! original = "apple,banana,cherry"
//! parts = Split(original, ",") ' ["apple", "banana", "cherry"]
//! rebuilt = Join(parts, ",") ' "apple,banana,cherry"
//! Debug.Print original = rebuilt ' True - perfect round-trip
//! ```
use crate;
use crate;
/// Implementation of the `Join` function.
///
/// Joins the elements of a one-dimensional array into a single string with a
/// delimiter between each element.
///
/// VB6 behavior:
/// - Default delimiter is a space (" ")
/// - Works only with one-dimensional arrays
/// - Non-string elements are converted to string via `CStr`-like semantics
/// - Empty or undimensioned arrays return an empty string
/// - Raises error 94 if `sourcearray` is `Null`
/// - Raises error 13 if `sourcearray` is not an array or is multi-dimensional