-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodStringMethods.bas
More file actions
419 lines (336 loc) · 12.2 KB
/
modStringMethods.bas
File metadata and controls
419 lines (336 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Attribute VB_Name = "modStringMethods"
'Author: David Zimmer <dzzie@yahoo.com>
'AI: Claude.ai
'Site: http://sandsprite.com
'License: MIT
Option Explicit
'------------------------------------------------------------
' Professional implementation using native VB6 functions
'------------------------------------------------------------
' Check if a name is a valid string method
Public Function IsStringMethod(methodName As String) As Boolean
Select Case methodName
Case "charAt", "charCodeAt", "indexOf", "lastIndexOf", _
"substring", "substr", "slice", "toLowerCase", "toUpperCase", _
"trim", "split", "replace", "replaceAll", _
"startsWith", "endsWith", "includes", "repeat", _
"padStart", "padEnd", "concat"
IsStringMethod = True
Case Else:
IsStringMethod = False
End Select
End Function
' Execute a string method and return result as CValue
Public Function CallStringMethod(strValue As String, methodName As String, args As Collection) As CValue
Dim result As New CValue
Select Case methodName
Case "charAt":
result.vType = vtString
result.strVal = str_charAt(strValue, GetArg(args, 1, 0))
Case "charCodeAt":
result.vType = vtNumber
result.numVal = str_charCodeAt(strValue, GetArg(args, 1, 0))
Case "indexOf":
result.vType = vtNumber
result.numVal = str_indexOf(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, 0))
Case "lastIndexOf":
result.vType = vtNumber
result.numVal = str_lastIndexOf(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, -1))
Case "substring":
result.vType = vtString
result.strVal = str_substring(strValue, GetArg(args, 1, 0), GetArg(args, 2, Len(strValue)))
Case "substr":
result.vType = vtString
result.strVal = str_substr(strValue, GetArg(args, 1, 0), GetArg(args, 2, Len(strValue)))
Case "slice":
result.vType = vtString
result.strVal = str_slice(strValue, GetArg(args, 1, 0), GetArg(args, 2, Len(strValue)))
Case "toLowerCase":
result.vType = vtString
result.strVal = LCase$(strValue)
Case "toUpperCase":
result.vType = vtString
result.strVal = UCase$(strValue)
Case "trim":
result.vType = vtString
result.strVal = Trim$(strValue)
Case "split":
result.vType = vtArray
Set result.arrayVal = str_split(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, -1))
Case "replace":
result.vType = vtString
result.strVal = str_replace(strValue, GetArgStr(args, 1, ""), GetArgStr(args, 2, ""))
Case "replaceAll":
result.vType = vtString
result.strVal = Replace(strValue, GetArgStr(args, 1, ""), GetArgStr(args, 2, ""))
Case "startsWith":
result.vType = vtBoolean
result.boolVal = str_startsWith(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, 0))
Case "endsWith":
result.vType = vtBoolean
result.boolVal = str_endsWith(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, Len(strValue)))
Case "includes":
result.vType = vtBoolean
result.boolVal = str_includes(strValue, GetArgStr(args, 1, ""), GetArg(args, 2, 0))
Case "repeat":
result.vType = vtString
result.strVal = str_repeat(strValue, GetArg(args, 1, 0))
Case "padStart":
result.vType = vtString
result.strVal = str_padStart(strValue, GetArg(args, 1, 0), GetArgStr(args, 2, " "))
Case "padEnd":
result.vType = vtString
result.strVal = str_padEnd(strValue, GetArg(args, 1, 0), GetArgStr(args, 2, " "))
Case "concat":
result.vType = vtString
result.strVal = str_concat(strValue, args)
Case Else:
result.vType = vtUndefined
End Select
Set CallStringMethod = result
End Function
' Helper: Get numeric argument
Private Function GetArg(args As Collection, index As Long, defaultVal As Long) As Long
If args.count >= index Then
Dim val As CValue
Set val = args(index)
GetArg = CLng(val.ToNumber())
Else
GetArg = defaultVal
End If
End Function
' Helper: Get string argument
Private Function GetArgStr(args As Collection, index As Long, defaultVal As String) As String
If args.count >= index Then
Dim val As CValue
Set val = args(index)
GetArgStr = val.ToString()
Else
GetArgStr = defaultVal
End If
End Function
' ============================================
' STRING METHOD IMPLEMENTATIONS
' ============================================
Private Function str_charAt(s As String, index As Long) As String
If index >= 0 And index < Len(s) Then
str_charAt = Mid$(s, index + 1, 1)
Else
str_charAt = ""
End If
End Function
Private Function str_charCodeAt(s As String, index As Long) As Double
If index >= 0 And index < Len(s) Then
str_charCodeAt = Asc(Mid$(s, index + 1, 1))
Else
str_charCodeAt = 0 ' Should be NaN
End If
End Function
Private Function str_indexOf(s As String, searchValue As String, fromIndex As Long) As Long
Dim pos As Long
pos = InStr(fromIndex + 1, s, searchValue)
If pos > 0 Then
str_indexOf = pos - 1
Else
str_indexOf = -1
End If
End Function
Private Function str_lastIndexOf(s As String, searchValue As String, fromIndex As Long) As Long
Dim pos As Long
Dim searchStart As Long
If fromIndex < 0 Then
searchStart = Len(s)
Else
searchStart = fromIndex + 1
End If
pos = InStrRev(s, searchValue, searchStart)
If pos > 0 Then
str_lastIndexOf = pos - 1
Else
str_lastIndexOf = -1
End If
End Function
Private Function str_substring(s As String, startIndex As Long, endIndex As Long) As String
Dim actualStart As Long
Dim actualEnd As Long
Dim strLen As Long
strLen = Len(s)
' Clamp indices
actualStart = IIf(startIndex < 0, 0, IIf(startIndex > strLen, strLen, startIndex))
actualEnd = IIf(endIndex < 0, 0, IIf(endIndex > strLen, strLen, endIndex))
' Swap if needed
If actualStart > actualEnd Then
Dim temp As Long
temp = actualStart
actualStart = actualEnd
actualEnd = temp
End If
If actualStart >= actualEnd Then
str_substring = ""
Else
str_substring = Mid$(s, actualStart + 1, actualEnd - actualStart)
End If
End Function
Private Function str_substr(s As String, startIndex As Long, length As Long) As String
Dim strLen As Long
Dim actualStart As Long
Dim actualLength As Long
strLen = Len(s)
' Handle negative start
If startIndex < 0 Then
actualStart = strLen + startIndex
If actualStart < 0 Then actualStart = 0
Else
actualStart = startIndex
End If
actualLength = IIf(length < 0, 0, length)
If actualStart >= strLen Or actualLength = 0 Then
str_substr = ""
Else
str_substr = Mid$(s, actualStart + 1, actualLength)
End If
End Function
Private Function str_slice(s As String, startIndex As Long, endIndex As Long) As String
Dim strLen As Long
Dim actualStart As Long
Dim actualEnd As Long
strLen = Len(s)
' Handle negative start
If startIndex < 0 Then
actualStart = strLen + startIndex
If actualStart < 0 Then actualStart = 0
Else
actualStart = IIf(startIndex > strLen, strLen, startIndex)
End If
' Handle end
If endIndex < 0 Then
actualEnd = strLen + endIndex
If actualEnd < 0 Then actualEnd = 0
Else
actualEnd = IIf(endIndex > strLen, strLen, endIndex)
End If
If actualStart >= actualEnd Then
str_slice = ""
Else
str_slice = Mid$(s, actualStart + 1, actualEnd - actualStart)
End If
End Function
Private Function str_split(s As String, separator As String, limit As Long) As Collection
Dim result As New Collection
Dim parts() As String
Dim i As Long
Dim maxParts As Long
Dim charVal As CValue
Dim partVal As CValue
If separator = "" Then
' Split into individual characters
For i = 1 To Len(s)
Set charVal = New CValue
charVal.vType = vtString
charVal.strVal = Mid$(s, i, 1)
result.add charVal
If limit > 0 And result.count >= limit Then Exit For
Next
Else
' Split by separator
parts = VBA.Split(s, separator)
If limit < 0 Then
maxParts = UBound(parts) + 1
Else
maxParts = IIf(limit > UBound(parts) + 1, UBound(parts) + 1, limit)
End If
For i = 0 To maxParts - 1
Set partVal = New CValue
partVal.vType = vtString
partVal.strVal = parts(i)
result.add partVal
Next
End If
Set str_split = result
End Function
Private Function str_replace(s As String, searchValue As String, replaceValue As String) As String
' Replace only first occurrence
Dim pos As Long
pos = InStr(1, s, searchValue)
If pos > 0 Then
str_replace = Left$(s, pos - 1) & replaceValue & Mid$(s, pos + Len(searchValue))
Else
str_replace = s
End If
End Function
Private Function str_startsWith(s As String, searchString As String, position As Long) As Boolean
Dim checkStr As String
checkStr = Mid$(s, position + 1, Len(searchString))
str_startsWith = (checkStr = searchString)
End Function
Private Function str_endsWith(s As String, searchString As String, length As Long) As Boolean
Dim checkLen As Long
Dim checkStr As String
checkLen = IIf(length > Len(s), Len(s), length)
If checkLen < Len(searchString) Then
str_endsWith = False
Else
checkStr = Mid$(s, checkLen - Len(searchString) + 1, Len(searchString))
str_endsWith = (checkStr = searchString)
End If
End Function
Private Function str_includes(s As String, searchString As String, position As Long) As Boolean
str_includes = (InStr(position + 1, s, searchString) > 0)
End Function
Private Function str_repeat(s As String, count As Long) As String
Dim i As Long
Dim result As String
If count < 0 Then
Err.Raise vbObjectError + 2000, "modStringMethods", "repeat count must be non-negative"
End If
result = ""
For i = 1 To count
result = result & s
Next
str_repeat = result
End Function
Private Function str_padStart(s As String, targetLength As Long, padString As String) As String
Dim currentLen As Long
Dim padLen As Long
Dim padding As String
currentLen = Len(s)
If currentLen >= targetLength Then
str_padStart = s
Exit Function
End If
padLen = targetLength - currentLen
' Build padding
padding = ""
Do While Len(padding) < padLen
padding = padding & padString
Loop
str_padStart = Left$(padding, padLen) & s
End Function
Private Function str_padEnd(s As String, targetLength As Long, padString As String) As String
Dim currentLen As Long
Dim padLen As Long
Dim padding As String
currentLen = Len(s)
If currentLen >= targetLength Then
str_padEnd = s
Exit Function
End If
padLen = targetLength - currentLen
' Build padding
padding = ""
Do While Len(padding) < padLen
padding = padding & padString
Loop
str_padEnd = s & Left$(padding, padLen)
End Function
Private Function str_concat(s As String, args As Collection) As String
Dim result As String
Dim i As Long
result = s
For i = 1 To args.count
Dim val As CValue
Set val = args(i)
result = result & val.ToString()
Next
str_concat = result
End Function