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

Last change on this file since 30 was 30, checked in by OverTaker, 17 years ago

Chars,Lengthをprivateメンバに変更

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