source: Include/Classes/System/String.ab@ 1

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