1 | Class String
|
---|
2 | m_Length As Long
|
---|
3 | Public
|
---|
4 | Chars As *Char
|
---|
5 |
|
---|
6 | Sub String()
|
---|
7 | Chars = _System_calloc(SizeOf (Char) * 1)
|
---|
8 | m_Length = 0
|
---|
9 | End Sub
|
---|
10 |
|
---|
11 | Sub String(initStr As *Char)
|
---|
12 | String()
|
---|
13 | Assign(initStr)
|
---|
14 | End Sub
|
---|
15 |
|
---|
16 | Sub String(initStr As *Char, length As Long)
|
---|
17 | String()
|
---|
18 | Assign(initStr, length)
|
---|
19 | End Sub
|
---|
20 |
|
---|
21 | Sub String(ByRef initStr As String)
|
---|
22 | String()
|
---|
23 | Assign(initStr)
|
---|
24 | End Sub
|
---|
25 |
|
---|
26 | Sub String(length As Long)
|
---|
27 | String()
|
---|
28 | ReSize(length)
|
---|
29 | End Sub
|
---|
30 |
|
---|
31 | Sub String(initChar As Char, length As Long)
|
---|
32 | ReSize(length, initChar)
|
---|
33 | End Sub
|
---|
34 |
|
---|
35 | Sub ~String()
|
---|
36 | _System_free(Chars)
|
---|
37 | Chars = 0
|
---|
38 | #ifdef _DEBUG
|
---|
39 | m_Length = 0
|
---|
40 | #endif
|
---|
41 | End Sub
|
---|
42 |
|
---|
43 | Const Function Length() As Long
|
---|
44 | Return m_Length
|
---|
45 | End Function
|
---|
46 |
|
---|
47 | Function Operator() As *Char
|
---|
48 | Return Chars
|
---|
49 | End Function
|
---|
50 |
|
---|
51 | Sub Operator = (ByRef objString As String)
|
---|
52 | Assign(objString.Chars, objString.m_Length)
|
---|
53 | End Sub
|
---|
54 |
|
---|
55 | Sub Operator = (text As *Char)
|
---|
56 | Assign(text)
|
---|
57 | End Sub
|
---|
58 |
|
---|
59 | Const Function Operator[] (n As Long) As Char
|
---|
60 | Return Chars[n]
|
---|
61 | End Function
|
---|
62 |
|
---|
63 | Sub Operator[]= (n As Long, c As Char)
|
---|
64 | Chars[n] = c
|
---|
65 | End Sub
|
---|
66 |
|
---|
67 | Const Function Operator + (lpszText As *Char) As String
|
---|
68 | Return Concat(lpszText, lstrlen(lpszText))
|
---|
69 | End Function
|
---|
70 |
|
---|
71 | Const Function Operator + (ByRef objString As String) As String
|
---|
72 | Return Concat(objString, objString.m_Length)
|
---|
73 | End Function
|
---|
74 |
|
---|
75 | Const Function Operator & (lpszText As *Char) As String
|
---|
76 | Dim tempString = This + lpszText
|
---|
77 | Return tempString
|
---|
78 | End Function
|
---|
79 |
|
---|
80 | Const Function Operator & (ByRef objString As String) As String
|
---|
81 | Dim tempString = This + objString
|
---|
82 | Return tempString
|
---|
83 | End Function
|
---|
84 |
|
---|
85 | Function Operator== (ByRef objString As String) As Long
|
---|
86 | If lstrcmp(This, objString) = 0 Then
|
---|
87 | Return _System_TRUE
|
---|
88 | Else
|
---|
89 | Return _System_FALSE
|
---|
90 | End If
|
---|
91 | End Function
|
---|
92 |
|
---|
93 | Function Operator== (lpszText As *Char) As Long
|
---|
94 | If lstrcmp(This, lpszText) = 0 Then
|
---|
95 | Return _System_TRUE
|
---|
96 | Else
|
---|
97 | Return _System_FALSE
|
---|
98 | End If
|
---|
99 | End Function
|
---|
100 |
|
---|
101 | Function Operator<> (ByRef objString As String) As Long
|
---|
102 | Return lstrcmp(This, objString)
|
---|
103 | End Function
|
---|
104 |
|
---|
105 | Function Operator<> (lpszText As *Char) As Long
|
---|
106 | Return lstrcmp(This, lpszText)
|
---|
107 | End Function
|
---|
108 |
|
---|
109 | Function Operator< (ByRef objString As String) As Long
|
---|
110 | If lstrcmp(This, objString) < 0 Then
|
---|
111 | Return _System_TRUE
|
---|
112 | Else
|
---|
113 | Return _System_FALSE
|
---|
114 | End If
|
---|
115 | End Function
|
---|
116 |
|
---|
117 | Function Operator< (lpszText As *Char) As Long
|
---|
118 | If lstrcmp(This, lpszText) < 0 Then
|
---|
119 | Return _System_TRUE
|
---|
120 | Else
|
---|
121 | Return _System_FALSE
|
---|
122 | End If
|
---|
123 | End Function
|
---|
124 |
|
---|
125 | Function Operator> (ByRef objString As String) As Long
|
---|
126 | If lstrcmp(This, objString) > 0 Then
|
---|
127 | Return _System_TRUE
|
---|
128 | Else
|
---|
129 | Return _System_FALSE
|
---|
130 | End If
|
---|
131 | End Function
|
---|
132 |
|
---|
133 | Function Operator> (lpszText As *Char) As Long
|
---|
134 | If lstrcmp(This, lpszText) > 0 Then
|
---|
135 | Return _System_TRUE
|
---|
136 | Else
|
---|
137 | Return _System_FALSE
|
---|
138 | End If
|
---|
139 | End Function
|
---|
140 |
|
---|
141 | Function Operator<= (ByRef objString As String) As Long
|
---|
142 | If lstrcmp(This, objString) <= 0 Then
|
---|
143 | Return _System_TRUE
|
---|
144 | Else
|
---|
145 | Return _System_FALSE
|
---|
146 | End If
|
---|
147 | End Function
|
---|
148 |
|
---|
149 | Function Operator<= (lpszText As *Char) As Long
|
---|
150 | If lstrcmp(This, lpszText) <= 0 Then
|
---|
151 | Return _System_TRUE
|
---|
152 | Else
|
---|
153 | Return _System_FALSE
|
---|
154 | End If
|
---|
155 | End Function
|
---|
156 |
|
---|
157 | Function Operator>= (ByRef objString As String) As Long
|
---|
158 | If lstrcmp(This, objString) => 0 Then
|
---|
159 | Return _System_TRUE
|
---|
160 | Else
|
---|
161 | Return _System_FALSE
|
---|
162 | End If
|
---|
163 | End Function
|
---|
164 |
|
---|
165 | Function Operator>= (lpszText As *Char) As Long
|
---|
166 | If lstrcmp(This, lpszText) => 0 Then
|
---|
167 | Return _System_TRUE
|
---|
168 | Else
|
---|
169 | Return _System_FALSE
|
---|
170 | End If
|
---|
171 | End Function
|
---|
172 |
|
---|
173 | Function StrPtr() As *Char
|
---|
174 | Return Chars
|
---|
175 | End Function
|
---|
176 |
|
---|
177 | Sub ReSize(allocLength As Long)
|
---|
178 | If allocLength < 0 Then Exit Sub
|
---|
179 | If allocLength > m_Length Then
|
---|
180 | Dim oldLength As Long
|
---|
181 | oldLength = m_Length
|
---|
182 | If AllocStringBuffer(allocLength) <> 0 Then
|
---|
183 | ZeroMemory(VarPtr(Chars[oldLength]), SizeOf (Char) * (m_Length - oldLength + 1))
|
---|
184 | End If
|
---|
185 | Else
|
---|
186 | m_Length = allocLength
|
---|
187 | Chars[m_Length] = 0
|
---|
188 | End If
|
---|
189 | End Sub
|
---|
190 |
|
---|
191 | Sub ReSize(allocLength As Long, c As Char)
|
---|
192 | If allocLength < 0 Then
|
---|
193 | Exit Sub
|
---|
194 | ElseIf allocLength > m_Length Then
|
---|
195 | Dim oldLength As Long
|
---|
196 | oldLength = m_Length
|
---|
197 | If AllocStringBuffer(allocLength) <> 0 Then
|
---|
198 | Dim p = VarPtr(Chars[oldLength]) As *Char
|
---|
199 | Dim fillLen = m_Length - oldLength
|
---|
200 | Dim i As Long
|
---|
201 | For i = 0 To ELM(fillLen)
|
---|
202 | p[i] = c
|
---|
203 | Next
|
---|
204 | End If
|
---|
205 | Else
|
---|
206 | m_Length = allocLength
|
---|
207 | End If
|
---|
208 | Chars[m_Length] = 0
|
---|
209 | End Sub
|
---|
210 |
|
---|
211 | Sub Assign(text As *Char, textLength As Long)
|
---|
212 | If text = Chars Then Exit Sub
|
---|
213 | If AllocStringBuffer(textLength) <> 0 Then
|
---|
214 | memcpy(Chars, text, SizeOf (Char) * textLength)
|
---|
215 | Chars[m_Length] = 0
|
---|
216 | End If
|
---|
217 | End Sub
|
---|
218 |
|
---|
219 | Sub Assign(ByRef objString As String)
|
---|
220 | Assign(objString.Chars, objString.m_Length)
|
---|
221 | End Sub
|
---|
222 |
|
---|
223 | Sub Assign(text As *Char)
|
---|
224 | If text Then
|
---|
225 | Assign(text, lstrlen(text))
|
---|
226 | Else
|
---|
227 | 'Chars=_System_realloc(Chars,1)
|
---|
228 | Chars[0] = 0
|
---|
229 | m_Length = 0
|
---|
230 | End If
|
---|
231 | End Sub
|
---|
232 |
|
---|
233 | Sub Append(text As *Char, textLength As Long)
|
---|
234 | Dim prevLen As Long
|
---|
235 | prevLen = m_Length
|
---|
236 | If AllocStringBuffer(m_Length + textLength) <> 0 Then
|
---|
237 | memcpy(VarPtr(Chars[prevLen]), text, SizeOf (Char) * textLength)
|
---|
238 | Chars[m_Length] = 0
|
---|
239 | End If
|
---|
240 | End Sub
|
---|
241 |
|
---|
242 | Sub Append(text As *Char)
|
---|
243 | Append(text, lstrlen(text))
|
---|
244 | End Sub
|
---|
245 |
|
---|
246 | Sub Append(ByRef str As String)
|
---|
247 | Append(str.Chars, str.m_Length)
|
---|
248 | End Sub
|
---|
249 |
|
---|
250 | Function Concat(lpszText As *Char, textLength As Long) As String
|
---|
251 | Dim tempString As String
|
---|
252 | With tempString
|
---|
253 | .AllocStringBuffer(This.m_Length + textLength)
|
---|
254 | memcpy(.Chars, This.Chars, SizeOf (Char) * This.m_Length)
|
---|
255 | memcpy(VarPtr(.Chars[This.m_Length]), lpszText, SizeOf (Char) * textLength)
|
---|
256 | .Chars[.m_Length] = 0
|
---|
257 | End With
|
---|
258 | Return tempString
|
---|
259 | End Function
|
---|
260 |
|
---|
261 | Function Contains(ByRef objString As String) As BOOL
|
---|
262 | If IndexOf(objString, 0, m_Length) >= 0 Then
|
---|
263 | Return _System_TRUE
|
---|
264 | Else
|
---|
265 | Return _System_FALSE
|
---|
266 | End If
|
---|
267 | End Function
|
---|
268 |
|
---|
269 | Function Contains(lpszText As *Char) As BOOL
|
---|
270 | If IndexOf(lpszText, 0, m_Length) >= 0 Then
|
---|
271 | Return _System_TRUE
|
---|
272 | Else
|
---|
273 | Return _System_FALSE
|
---|
274 | End If
|
---|
275 | End Function
|
---|
276 |
|
---|
277 | Function IndexOf(lpszText As *Char) As Long
|
---|
278 | Return IndexOf(lpszText, 0, m_Length)
|
---|
279 | End Function
|
---|
280 |
|
---|
281 | Function IndexOf(lpszText As *Char, startIndex As Long) As Long
|
---|
282 | Return IndexOf(lpszText, startIndex, m_Length - startIndex)
|
---|
283 | End Function
|
---|
284 |
|
---|
285 | Function IndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
|
---|
286 | Dim length As Long
|
---|
287 | length = lstrlen(lpszText)
|
---|
288 |
|
---|
289 | If startIndex < 0 Then Return -1
|
---|
290 | If count < 1 Or count + startIndex > m_Length Then Return -1
|
---|
291 | If length > m_Length Then Return -1
|
---|
292 |
|
---|
293 | If length = 0 Then Return startIndex
|
---|
294 |
|
---|
295 | Dim i As Long, j As Long
|
---|
296 | For i = startIndex To startIndex + count - 1
|
---|
297 | For j = 0 To length - 1
|
---|
298 | If Chars[i + j] = lpszText[j] Then
|
---|
299 | If j = length - 1 Then Return i
|
---|
300 | Else
|
---|
301 | Exit For
|
---|
302 | End If
|
---|
303 | Next
|
---|
304 | Next
|
---|
305 | Return -1
|
---|
306 | End Function
|
---|
307 |
|
---|
308 | Function LastIndexOf(lpszText As *Char) As Long
|
---|
309 | Return LastIndexOf(lpszText, m_Length - 1, m_Length)
|
---|
310 | End Function
|
---|
311 |
|
---|
312 | Function LastIndexOf(lpszText As *Char, startIndex As Long) As Long
|
---|
313 | Return LastIndexOf(lpszText As *Char, startIndex, startIndex + 1)
|
---|
314 | End Function
|
---|
315 |
|
---|
316 | Function LastIndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
|
---|
317 | Dim length As Long
|
---|
318 | length = lstrlen(lpszText)
|
---|
319 |
|
---|
320 | If startIndex < 0 Or startIndex > m_Length - 1 Then Return -1
|
---|
321 | If count < 1 Or count > startIndex + 2 Then Return -1
|
---|
322 | If length > m_Length Then Return -1
|
---|
323 |
|
---|
324 | If length = 0 Then Return startIndex
|
---|
325 |
|
---|
326 | Dim i As Long, j As Long
|
---|
327 | For i = startIndex To startIndex - count + 1 Step -1
|
---|
328 | For j = length - 1 To 0 Step -1
|
---|
329 | If Chars[i + j] = lpszText[j] Then
|
---|
330 | If j = 0 Then Return i
|
---|
331 | Else
|
---|
332 | Exit For
|
---|
333 | End If
|
---|
334 | Next
|
---|
335 | Next
|
---|
336 | Return -1
|
---|
337 | End Function
|
---|
338 |
|
---|
339 | Function StartsWith(lpszText As *Char) As BOOL
|
---|
340 | If IndexOf(lpszText) = 0 Then
|
---|
341 | Return _System_TRUE
|
---|
342 | Else
|
---|
343 | Return _System_FALSE
|
---|
344 | End If
|
---|
345 | End Function
|
---|
346 |
|
---|
347 | Function EndsWith(lpszText As *Char) As BOOL
|
---|
348 | If LastIndexOf(lpszText) = m_Length - lstrlen(lpszText) Then
|
---|
349 | Return _System_TRUE
|
---|
350 | Else
|
---|
351 | Return _System_FALSE
|
---|
352 | End If
|
---|
353 | End Function
|
---|
354 |
|
---|
355 | Function Insert(startIndex As Long, text As *Char) As Long
|
---|
356 | Dim length As Long
|
---|
357 | length = lstrlen(text)
|
---|
358 |
|
---|
359 | If startIndex < 0 Or startIndex > m_Length Then Return -1
|
---|
360 |
|
---|
361 | Dim newChars As *Char
|
---|
362 | newChars = _System_malloc(SizeOf (Char) * (length + m_Length + 1))
|
---|
363 | If newChars = 0 Then Return -1
|
---|
364 |
|
---|
365 | memcpy(newChars, Chars, SizeOf (Char) * startIndex)
|
---|
366 | memcpy(VarPtr(newChars[startIndex]), text, SizeOf (Char) * length)
|
---|
367 | memcpy(VarPtr(newChars[startIndex + length]), VarPtr(Chars[startIndex]), SizeOf (Char) * (m_Length - startIndex + 1))
|
---|
368 |
|
---|
369 | _System_free(Chars)
|
---|
370 | Chars = newChars
|
---|
371 | m_Length = length + m_Length
|
---|
372 | Return m_Length
|
---|
373 | End Function
|
---|
374 |
|
---|
375 | Function SubString(startIndex As Long) As String
|
---|
376 | Return SubString(startIndex, m_Length - startIndex)
|
---|
377 | End Function
|
---|
378 |
|
---|
379 | Function SubString(startIndex As Long, length As Long) As String
|
---|
380 | If startIndex < 0 Or length <= 0 Then Return ""
|
---|
381 | If startIndex + length > m_Length Then Return ""
|
---|
382 |
|
---|
383 | Dim temp As String
|
---|
384 | temp.AllocStringBuffer(length)
|
---|
385 | memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf (Char) * length)
|
---|
386 | Chars[m_Length] = 0
|
---|
387 | Return temp
|
---|
388 | End Function
|
---|
389 |
|
---|
390 | Function Remove(startIndex As Long) As Long
|
---|
391 | If startIndex < 0 Or startIndex > m_Length Then Return -1
|
---|
392 | Chars[startIndex] = 0
|
---|
393 | m_Length = startIndex
|
---|
394 | Return m_Length
|
---|
395 | End Function
|
---|
396 |
|
---|
397 | Function Remove(startIndex As Long, count As Long) As Long
|
---|
398 | If startIndex < 0 Or count < 0 Then Return -1
|
---|
399 | If startIndex + count > m_Length Then Return -1
|
---|
400 |
|
---|
401 | Dim newChars As *Char
|
---|
402 | newChars = _System_malloc(SizeOf (Char) * (m_Length - count + 1))
|
---|
403 | If newChars = 0 Then Return -1
|
---|
404 |
|
---|
405 | memcpy(newChars, Chars, SizeOf (Char) * startIndex)
|
---|
406 | memcpy(VarPtr(newChars[startIndex]), VarPtr(Chars[startIndex + count]), SizeOf (Char) * (m_Length - startIndex - count))
|
---|
407 | newChars[m_Length - count] = 0
|
---|
408 |
|
---|
409 | _System_free(Chars)
|
---|
410 | Chars = newChars
|
---|
411 | m_Length = m_Length - count
|
---|
412 | Return m_Length
|
---|
413 | End Function
|
---|
414 |
|
---|
415 | Function IsNullOrEmpty() As BOOL
|
---|
416 | If m_Length = 0 Then
|
---|
417 | Return _System_TRUE
|
---|
418 | Else
|
---|
419 | Return _System_FALSE
|
---|
420 | End If
|
---|
421 | End Function
|
---|
422 |
|
---|
423 |
|
---|
424 | Sub Replace(oldChar As Char, newChar As Char)
|
---|
425 | Dim i As Long
|
---|
426 | For i = 0 To ELM(m_Length)
|
---|
427 | If Chars[i] = oldChar Then
|
---|
428 | Chars[i] = newChar
|
---|
429 | End If
|
---|
430 | Next
|
---|
431 | End Sub
|
---|
432 |
|
---|
433 | Sub Replace(ByRef oldStr As String, ByRef newStr As String)
|
---|
434 | Replace(oldStr, oldStr.m_Length, newStr, newStr.m_Length)
|
---|
435 | End Sub
|
---|
436 |
|
---|
437 | Sub Replace(oldStr As PCSTR, newStr As PCSTR)
|
---|
438 | Replace(oldStr, lstrlen(oldStr), newStr, lstrlen(newStr))
|
---|
439 | End Sub
|
---|
440 |
|
---|
441 | Sub Replace(oldStr As PCSTR, oldLen As Long, newStr As PCSTR, newLen As Long)
|
---|
442 | Dim tempString As String
|
---|
443 | With tempString
|
---|
444 | Dim current = 0 As Long
|
---|
445 | Do
|
---|
446 | Dim pos As Long
|
---|
447 | pos = IndexOf(oldStr, current)
|
---|
448 | If pos = -1 Then
|
---|
449 | Exit Do
|
---|
450 | End If
|
---|
451 | .Append(Chars + current, pos - current)
|
---|
452 | .Append(newStr, newLen)
|
---|
453 | current = pos + oldLen
|
---|
454 | Loop
|
---|
455 | .Append(Chars + current, m_Length - current)
|
---|
456 | End With
|
---|
457 | Swap(tempString)
|
---|
458 | End Sub
|
---|
459 |
|
---|
460 | Sub ToLower()
|
---|
461 | Dim i As Long
|
---|
462 | For i = 0 To m_Length
|
---|
463 | Chars[i] = _System_ASCII_ToLower(Chars[i])
|
---|
464 | Next
|
---|
465 | End Sub
|
---|
466 |
|
---|
467 | Sub ToUpper()
|
---|
468 | Dim i As Long
|
---|
469 | For i = 0 To m_Length
|
---|
470 | Chars[i] = _System_ASCII_ToUpper(Chars[i])
|
---|
471 | Next
|
---|
472 | End Sub
|
---|
473 |
|
---|
474 | Sub Swap(ByRef x As String)
|
---|
475 | Dim tempLen As Long
|
---|
476 | Dim tempChars As PSTR
|
---|
477 | tempLen = x.m_Length
|
---|
478 | tempChars = x.Chars
|
---|
479 | x.m_Length = This.m_Length
|
---|
480 | x.Chars = This.Chars
|
---|
481 | This.m_Length = tempLen
|
---|
482 | This.Chars = tempChars
|
---|
483 | End Sub
|
---|
484 |
|
---|
485 | Override Function ToString() As String
|
---|
486 | Return This
|
---|
487 | End Function
|
---|
488 |
|
---|
489 | Private
|
---|
490 | ' メモリ確保に失敗すると元の文字列は失われない。(例外安全でいう強い保障)
|
---|
491 | Function AllocStringBuffer(textLength As Long) As *Char
|
---|
492 | If textLength < 0 Then
|
---|
493 | Return 0
|
---|
494 | ElseIf textLength > m_Length Then
|
---|
495 | AllocStringBuffer = _System_realloc(Chars, SizeOf(Char) * (textLength + 1))
|
---|
496 | If AllocStringBuffer <> 0 Then
|
---|
497 | m_Length = textLength
|
---|
498 | Chars = AllocStringBuffer
|
---|
499 | End If
|
---|
500 | Else
|
---|
501 | m_Length = textLength
|
---|
502 | AllocStringBuffer = Chars
|
---|
503 | End If
|
---|
504 | End Function
|
---|
505 |
|
---|
506 | End Class
|
---|
507 |
|
---|