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
//! VB6 `Str` Function
//!
//! The `Str` function converts a number to a string representation.
//!
//! ## Syntax
//! ```vb6
//! Str(number)
//! ```
//!
//! ## Parameters
//! - `number`: Required. Any valid numeric expression (Long, Integer, Single, Double, Currency, Byte, Boolean, Variant).
//!
//! ## Returns
//! Returns a `String` representing the numeric value. Positive numbers include a leading space for the sign position,
//! negative numbers include a leading minus sign.
//!
//! ## Remarks
//! The `Str` function converts numeric values to their string representation:
//!
//! - **Always includes sign position**: Positive numbers have a leading space, negative numbers have a leading minus sign (`-`)
//! - **No thousands separators**: Returns plain numeric string without commas or other formatting
//! - **Scientific notation**: Very large or very small numbers may use scientific notation (e.g., "1.23E+10")
//! - **Decimal point**: Uses period (`.`) as decimal separator, regardless of locale settings
//! - **Boolean conversion**: `True` becomes `"-1"`, `False` becomes `" 0"`
//! - **Null handling**: If `number` is `Null`, returns `Null` (not empty string)
//! - **Variant support**: Accepts Variant containing numeric values
//! - **No currency symbol**: Currency values converted without `$` or other symbols
//!
//! ### Comparison with Other Functions
//! - **Str vs `CStr`**: `Str` adds leading space for positive numbers, `CStr` does not
//! - **Str vs Format$**: `Str` has no formatting options, `Format$` provides extensive control
//! - **Str vs Val**: `Str` converts number to string, `Val` converts string to number (inverse operations)
//! - **Str vs `LTrim`$**: Often use `LTrim$(Str(number))` to remove leading space from positive numbers
//!
//! ## Typical Uses
//! 1. **Quick Number Display**: Convert numbers to strings for display without formatting
//! 2. **String Concatenation**: Build messages or output by combining numbers with text
//! 3. **File Output**: Write numeric values to text files in plain format
//! 4. **Debug Output**: Display numeric values in debug messages with minimal formatting
//! 5. **Data Export**: Export numeric data to plain text format
//! 6. **Sign Preservation**: Maintain visual alignment when displaying columns of numbers
//! 7. **Legacy Code**: Support older code that relied on `Str` function behavior
//! 8. **Simple Conversion**: Convert numeric types to string without locale-specific formatting
//!
//! ## Basic Examples
//!
//! ### Example 1: Basic Number Conversion
//! ```vb6
//! Dim result As String
//! result = Str(123)      ' Returns " 123" (note leading space)
//! result = Str(-456)     ' Returns "-456" (leading minus sign)
//! result = Str(0)        ' Returns " 0" (leading space)
//! result = Str(3.14159)  ' Returns " 3.14159"
//! ```
//!
//! ### Example 2: Removing Leading Space
//! ```vb6
//! Dim numStr As String
//! Dim value As Long
//! value = 100
//!
//! ' With leading space
//! numStr = Str(value)           ' " 100"
//!
//! ' Without leading space
//! numStr = LTrim$(Str(value))   ' "100"
//!
//! ' Or use CStr instead
//! numStr = CStr(value)          ' "100"
//! ```
//!
//! ### Example 3: String Concatenation
//! ```vb6
//! Dim message As String
//! Dim count As Integer
//! Dim total As Double
//!
//! count = 5
//! total = 123.45
//!
//! message = "Count:" & Str(count) & ", Total:" & Str(total)
//! ' Result: "Count: 5, Total: 123.45" (spaces from Str included)
//!
//! message = "Count:" & LTrim$(Str(count)) & ", Total:" & LTrim$(Str(total))
//! ' Result: "Count:5, Total:123.45" (no extra spaces)
//! ```
//!
//! ### Example 4: Boolean Conversion
//! ```vb6
//! Dim result As String
//! Dim flag As Boolean
//!
//! flag = True
//! result = Str(flag)   ' Returns "-1"
//!
//! flag = False
//! result = Str(flag)   ' Returns " 0"
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Build Display String
//! ```vb6
//! Function BuildSummary(items As Integer, amount As Currency) As String
//!     ' Build summary string with numbers
//!     BuildSummary = "Items:" & LTrim$(Str(items)) & _
//!                    " Amount: $" & LTrim$(Str(amount))
//! End Function
//! ```
//!
//! ### Pattern 2: Write to File
//! ```vb6
//! Sub WriteDataToFile(filename As String, values() As Double)
//!     Dim fileNum As Integer
//!     Dim i As Integer
//!     
//!     fileNum = FreeFile
//!     Open filename For Output As #fileNum
//!     
//!     For i = LBound(values) To UBound(values)
//!         Print #fileNum, LTrim$(Str(values(i)))
//!     Next i
//!     
//!     Close #fileNum
//! End Sub
//! ```
//!
//! ### Pattern 3: Debug Output
//! ```vb6
//! Sub DebugPrintValues(x As Double, y As Double, z As Double)
//!     Debug.Print "X=" & LTrim$(Str(x)) & _
//!                 " Y=" & LTrim$(Str(y)) & _
//!                 " Z=" & LTrim$(Str(z))
//! End Sub
//! ```
//!
//! ### Pattern 4: Array to String
//! ```vb6
//! Function ArrayToString(arr() As Integer) As String
//!     Dim i As Integer
//!     Dim result As String
//!     
//!     result = ""
//!     For i = LBound(arr) To UBound(arr)
//!         If i > LBound(arr) Then result = result & ","
//!         result = result & LTrim$(Str(arr(i)))
//!     Next i
//!     
//!     ArrayToString = result
//! End Function
//! ```
//!
//! ### Pattern 5: Aligned Column Output
//! ```vb6
//! Sub PrintAlignedNumbers(values() As Long)
//!     Dim i As Integer
//!     Dim numStr As String
//!     
//!     For i = LBound(values) To UBound(values)
//!         numStr = Str(values(i))  ' Keep leading space/sign
//!         Debug.Print Right$(Space(10) & numStr, 10)  ' Right-align
//!     Next i
//! End Sub
//! ```
//!
//! ### Pattern 6: Safe Null Handling
//! ```vb6
//! Function SafeStr(value As Variant) As String
//!     ' Handle potential Null values
//!     If IsNull(value) Then
//!         SafeStr = ""
//!     ElseIf IsNumeric(value) Then
//!         SafeStr = LTrim$(Str(value))
//!     Else
//!         SafeStr = CStr(value)
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 7: Build CSV Line
//! ```vb6
//! Function BuildCSVLine(id As Long, quantity As Integer, price As Currency) As String
//!     ' Build comma-separated values
//!     BuildCSVLine = LTrim$(Str(id)) & "," & _
//!                    LTrim$(Str(quantity)) & "," & _
//!                    LTrim$(Str(price))
//! End Function
//! ```
//!
//! ### Pattern 8: Number to Padded String
//! ```vb6
//! Function NumberToPaddedString(value As Long, width As Integer) As String
//!     Dim numStr As String
//!     numStr = LTrim$(Str(value))
//!     NumberToPaddedString = Right$(String$(width, "0") & numStr, width)
//! End Function
//! ```
//!
//! ### Pattern 9: Sign-Aware Display
//! ```vb6
//! Function DisplayWithSign(value As Double) As String
//!     Dim numStr As String
//!     numStr = Str(value)  ' Keep sign/space
//!     
//!     If Left$(numStr, 1) = " " Then
//!         DisplayWithSign = "+" & LTrim$(numStr)
//!     Else
//!         DisplayWithSign = numStr  ' Already has minus sign
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 10: Concatenate Multiple Values
//! ```vb6
//! Function ConcatenateValues(ParamArray values() As Variant) As String
//!     Dim i As Integer
//!     Dim result As String
//!     
//!     result = ""
//!     For i = LBound(values) To UBound(values)
//!         If i > LBound(values) Then result = result & " "
//!         result = result & LTrim$(Str(values(i)))
//!     Next i
//!     
//!     ConcatenateValues = result
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Data Export Utility
//! ```vb6
//! ' Class: DataExporter
//! ' Exports numeric data to various text formats
//! Option Explicit
//!
//! Private m_Delimiter As String
//! Private m_IncludeHeader As Boolean
//! Private m_TrimSpaces As Boolean
//!
//! Public Sub Initialize(delimiter As String, includeHeader As Boolean, trimSpaces As Boolean)
//!     m_Delimiter = delimiter
//!     m_IncludeHeader = includeHeader
//!     m_TrimSpaces = trimSpaces
//! End Sub
//!
//! Public Sub ExportToFile(filename As String, data() As Double, headers() As String)
//!     Dim fileNum As Integer
//!     Dim i As Integer
//!     Dim j As Integer
//!     Dim line As String
//!     
//!     fileNum = FreeFile
//!     Open filename For Output As #fileNum
//!     
//!     ' Write header if requested
//!     If m_IncludeHeader Then
//!         line = Join(headers, m_Delimiter)
//!         Print #fileNum, line
//!     End If
//!     
//!     ' Write data rows
//!     For i = LBound(data, 1) To UBound(data, 1)
//!         line = ""
//!         For j = LBound(data, 2) To UBound(data, 2)
//!             If j > LBound(data, 2) Then line = line & m_Delimiter
//!             If m_TrimSpaces Then
//!                 line = line & LTrim$(Str(data(i, j)))
//!             Else
//!                 line = line & Str(data(i, j))
//!             End If
//!         Next j
//!         Print #fileNum, line
//!     Next i
//!     
//!     Close #fileNum
//! End Sub
//!
//! Public Function ConvertRowToString(values() As Variant) As String
//!     Dim i As Integer
//!     Dim result As String
//!     
//!     result = ""
//!     For i = LBound(values) To UBound(values)
//!         If i > LBound(values) Then result = result & m_Delimiter
//!         
//!         If IsNumeric(values(i)) Then
//!             If m_TrimSpaces Then
//!                 result = result & LTrim$(Str(values(i)))
//!             Else
//!                 result = result & Str(values(i))
//!             End If
//!         Else
//!             result = result & CStr(values(i))
//!         End If
//!     Next i
//!     
//!     ConvertRowToString = result
//! End Function
//! ```
//!
//! ### Example 2: Number Formatter Module
//! ```vb6
//! ' Module: NumberFormatter
//! ' Utilities for converting numbers to formatted strings
//! Option Explicit
//!
//! Public Function ToStringWithoutSpace(value As Variant) As String
//!     ' Convert number to string without leading space
//!     If IsNull(value) Then
//!         ToStringWithoutSpace = ""
//!     ElseIf IsNumeric(value) Then
//!         ToStringWithoutSpace = LTrim$(Str(value))
//!     Else
//!         ToStringWithoutSpace = CStr(value)
//!     End If
//! End Function
//!
//! Public Function ToFixedWidth(value As Double, width As Integer) As String
//!     ' Convert number to fixed-width string, right-aligned
//!     Dim numStr As String
//!     numStr = Str(value)  ' Keep sign position
//!     
//!     If Len(numStr) >= width Then
//!         ToFixedWidth = numStr
//!     Else
//!         ToFixedWidth = Space(width - Len(numStr)) & numStr
//!     End If
//! End Function
//!
//! Public Function ToScientific(value As Double, decimals As Integer) As String
//!     ' Convert to scientific notation manually
//!     Dim exponent As Integer
//!     Dim mantissa As Double
//!     Dim absValue As Double
//!     
//!     If value = 0 Then
//!         ToScientific = "0.0E+00"
//!         Exit Function
//!     End If
//!     
//!     absValue = Abs(value)
//!     exponent = Int(Log(absValue) / Log(10))
//!     mantissa = value / (10 ^ exponent)
//!     
//!     ToScientific = LTrim$(Str(Round(mantissa, decimals))) & "E" & _
//!                    IIf(exponent >= 0, "+", "") & LTrim$(Str(exponent))
//! End Function
//!
//! Public Function PadInteger(value As Long, width As Integer) As String
//!     ' Pad integer with leading zeros
//!     Dim numStr As String
//!     numStr = LTrim$(Str(value))
//!     
//!     If Len(numStr) >= width Then
//!         PadInteger = numStr
//!     Else
//!         PadInteger = String$(width - Len(numStr), "0") & numStr
//!     End If
//! End Function
//! ```
//!
//! ### Example 3: Report Builder Class
//! ```vb6
//! ' Class: ReportBuilder
//! ' Builds formatted text reports with numeric data
//! Option Explicit
//!
//! Private m_Lines As Collection
//! Private m_ColumnWidths() As Integer
//! Private m_ColumnCount As Integer
//!
//! Public Sub Initialize(columnCount As Integer)
//!     Set m_Lines = New Collection
//!     m_ColumnCount = columnCount
//!     ReDim m_ColumnWidths(1 To columnCount)
//!     
//!     ' Default column widths
//!     Dim i As Integer
//!     For i = 1 To columnCount
//!         m_ColumnWidths(i) = 10
//!     Next i
//! End Sub
//!
//! Public Sub SetColumnWidth(column As Integer, width As Integer)
//!     If column >= 1 And column <= m_ColumnCount Then
//!         m_ColumnWidths(column) = width
//!     End If
//! End Sub
//!
//! Public Sub AddRow(ParamArray values() As Variant)
//!     Dim i As Integer
//!     Dim line As String
//!     Dim cell As String
//!     Dim colNum As Integer
//!     
//!     line = ""
//!     For i = LBound(values) To UBound(values)
//!         colNum = i + 1
//!         If colNum > m_ColumnCount Then Exit For
//!         
//!         ' Convert value to string
//!         If IsNumeric(values(i)) Then
//!             cell = Str(values(i))  ' Keep sign position for alignment
//!         Else
//!             cell = CStr(values(i))
//!         End If
//!         
//!         ' Pad to column width
//!         If Len(cell) < m_ColumnWidths(colNum) Then
//!             cell = Space(m_ColumnWidths(colNum) - Len(cell)) & cell
//!         End If
//!         
//!         line = line & cell
//!     Next i
//!     
//!     m_Lines.Add line
//! End Sub
//!
//! Public Function GetReport() As String
//!     Dim i As Integer
//!     Dim result As String
//!     
//!     result = ""
//!     For i = 1 To m_Lines.Count
//!         result = result & m_Lines(i) & vbCrLf
//!     Next i
//!     
//!     GetReport = result
//! End Function
//!
//! Public Sub Clear()
//!     Set m_Lines = New Collection
//! End Sub
//! ```
//!
//! ### Example 4: String Builder for Numbers
//! ```vb6
//! ' Module: NumericStringBuilder
//! ' Build strings from numeric arrays and collections
//! Option Explicit
//!
//! Public Function JoinNumbers(numbers() As Double, delimiter As String, trimSpaces As Boolean) As String
//!     Dim i As Integer
//!     Dim result As String
//!     Dim numStr As String
//!     
//!     result = ""
//!     For i = LBound(numbers) To UBound(numbers)
//!         If i > LBound(numbers) Then result = result & delimiter
//!         
//!         numStr = Str(numbers(i))
//!         If trimSpaces Then numStr = LTrim$(numStr)
//!         
//!         result = result & numStr
//!     Next i
//!     
//!     JoinNumbers = result
//! End Function
//!
//! Public Function CreateTable(data() As Variant, rowCount As Integer, colCount As Integer) As String
//!     Dim i As Integer
//!     Dim j As Integer
//!     Dim result As String
//!     Dim row As String
//!     
//!     result = ""
//!     For i = 1 To rowCount
//!         row = ""
//!         For j = 1 To colCount
//!             If j > 1 Then row = row & vbTab
//!             
//!             If IsNumeric(data(i, j)) Then
//!                 row = row & LTrim$(Str(data(i, j)))
//!             Else
//!                 row = row & CStr(data(i, j))
//!             End If
//!         Next j
//!         result = result & row & vbCrLf
//!     Next i
//!     
//!     CreateTable = result
//! End Function
//!
//! Public Function FormatNumberList(numbers As Collection, prefix As String, suffix As String) As String
//!     Dim item As Variant
//!     Dim result As String
//!     Dim first As Boolean
//!     
//!     first = True
//!     result = ""
//!     
//!     For Each item In numbers
//!         If Not first Then result = result & ", "
//!         result = result & prefix & LTrim$(Str(item)) & suffix
//!         first = False
//!     Next item
//!     
//!     FormatNumberList = result
//! End Function
//!
//! Public Function CreateSummaryLine(label As String, value As Double, width As Integer) As String
//!     Dim numStr As String
//!     Dim padding As Integer
//!     
//!     numStr = LTrim$(Str(value))
//!     padding = width - Len(label) - Len(numStr)
//!     
//!     If padding > 0 Then
//!         CreateSummaryLine = label & Space(padding) & numStr
//!     Else
//!         CreateSummaryLine = label & " " & numStr
//!     End If
//! End Function
//! ```
//!
//! ## Error Handling
//! The `Str` function can raise the following errors:
//!
//! - **Error 13 (Type mismatch)**: If `number` cannot be converted to a numeric value
//! - **Error 94 (Invalid use of Null)**: In some contexts when passing Null without proper handling
//!
//! ## Performance Notes
//! - Very fast for basic numeric types (Integer, Long, Single, Double)
//! - Slightly slower for Variant types due to type checking
//! - No performance overhead from locale or formatting
//! - Direct conversion without intermediate formatting steps
//! - Consider `CStr` if leading space is always unwanted (avoids `LTrim$` call)
//!
//! ## Best Practices
//! 1. **Use `LTrim$`** to remove leading space from positive numbers when space is unwanted
//! 2. **Consider `CStr`** if you never need the leading space (cleaner code)
//! 3. **Preserve sign position** when aligning columns of numbers (keep the `Str` space)
//! 4. **Handle Null values** explicitly with `IsNull` check before calling `Str`
//! 5. **Use `Format$`** instead for formatted output (thousands separators, decimal places)
//! 6. **Document behavior** when `Str` is used in shared code (leading space can surprise developers)
//! 7. **Combine with `Val`** for round-trip conversion (number → string → number)
//! 8. **Avoid locale issues** by using `Str` for culture-invariant numeric strings
//! 9. **Cache results** if converting the same number repeatedly in a loop
//! 10. **Use type-specific functions** like `CStr` when working with non-numeric types
//!
//! ## Comparison Table
//!
//! | Function | Input | Output | Leading Space | Formatting |
//! |----------|-------|--------|---------------|------------|
//! | `Str` | Number | String | Yes (positive) | None |
//! | `CStr` | Any | String | No | None |
//! | `Format$` | Any | String | No | Extensive |
//! | `Val` | String | Double | N/A | N/A |
//! | `LTrim$` | String | String | Removes | None |
//!
//! ## Platform Notes
//! - Available in VB6, VBA, and `VBScript`
//! - Behavior consistent across all platforms
//! - Uses period (`.`) as decimal separator regardless of locale
//! - Scientific notation format is standard across platforms
//! - No localization or culture-specific formatting applied
//!
//! ## Limitations
//! - Always includes leading space for positive numbers (often unwanted)
//! - No formatting options (thousands separators, decimal places, etc.)
//! - Cannot specify decimal precision
//! - Cannot control scientific notation threshold
//! - No currency symbol support
//! - Null input can cause errors if not handled
//! - No culture-aware formatting
//! - Return value length is unpredictable (depends on number magnitude)

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

    #[test]
    fn str_basic() {
        let source = r"
Sub Test()
    result = Str(123)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_variable_assignment() {
        let source = r"
Sub Test()
    Dim s As String
    s = Str(value)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_negative_number() {
        let source = r"
Sub Test()
    result = Str(-456)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_decimal() {
        let source = r"
Sub Test()
    result = Str(3.14159)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_with_ltrim() {
        let source = r"
Sub Test()
    result = LTrim$(Str(value))
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_concatenation() {
        let source = r#"
Sub Test()
    message = "Count:" & Str(count)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_in_print() {
        let source = r"
Sub Test()
    Print #1, Str(value)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_debug_print() {
        let source = r"
Sub Test()
    Debug.Print Str(x)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_if_statement() {
        let source = r#"
Sub Test()
    If Str(value) = " 0" Then
        MsgBox "Zero"
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_for_loop() {
        let source = r"
Sub Test()
    For i = 1 To 10
        result = result & Str(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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_array_assignment() {
        let source = r"
Sub Test()
    arr(i) = Str(values(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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_function_argument() {
        let source = r"
Sub Test()
    Call ProcessString(Str(number))
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_function_return() {
        let source = r"
Function GetNumberString() As String
    GetNumberString = Str(value)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_comparison() {
        let source = r#"
Sub Test()
    If Len(Str(num)) > 5 Then
        MsgBox "Long number"
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_select_case() {
        let source = r#"
Sub Test()
    Select Case Str(value)
        Case " 0"
            MsgBox "Zero"
        Case Else
            MsgBox "Other"
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_with_statement() {
        let source = r"
Sub Test()
    With obj
        .Text = Str(.Value)
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_do_while() {
        let source = r"
Sub Test()
    Do While Len(Str(counter)) < 10
        counter = counter + 1
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_do_until() {
        let source = r#"
Sub Test()
    Do Until Str(value) = " 100"
        value = value + 1
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_while_wend() {
        let source = r"
Sub Test()
    While i < 10
        output = output & Str(i)
        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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_iif() {
        let source = r"
Sub Test()
    result = IIf(flag, Str(value1), Str(value2))
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_parentheses() {
        let source = r"
Sub Test()
    result = Str((x + y) * z)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Value: " & Str(total)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_property_assignment() {
        let source = r"
Sub Test()
    obj.Caption = Str(count)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_error_handling() {
        let source = r#"
Sub Test()
    On Error Resume Next
    result = Str(varValue)
    If Err.Number <> 0 Then
        result = ""
    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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_class_usage() {
        let source = r"
Sub Test()
    Set obj = New MyClass
    obj.SetValue Str(counter)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_currency_conversion() {
        let source = r"
Sub Test()
    Dim amount As Currency
    amount = 123.45
    result = Str(amount)
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn str_csv_building() {
        let source = r#"
Sub Test()
    csvLine = LTrim$(Str(id)) & "," & LTrim$(Str(qty)) & "," & LTrim$(Str(price))
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/str");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}