source: trunk/Include/Classes/ActiveBasic/Strings/Strings.ab@ 385

Last change on this file since 385 was 385, checked in by イグトランス (egtra), 16 years ago

例外クラスの実装。ExceptionTestでSystem.Exceptionを使用するようにした。
StringBuilderでコメント化されていた例外を投げる処理を有効にした(除OutOfMemory)。
Str$の実装にSPrintfなどを使用するようにした。
毎回Object.ReferenceEquals(xxx, Nothing)と打つのが面倒なので、IsNothingを導入。

File size: 4.8 KB
Line 
1'Classes/ActiveBasic/Strings/Strings.ab
2
3#ifndef __ACTIVEBASIC_STRINGS_STRINGS_AB__
4#define __ACTIVEBASIC_STRINGS_STRINGS_AB__
5
6#require <Classes/System/Math.ab>
7#require <Classes/System/Collections/ArrayList.ab>
8
9Namespace ActiveBasic
10Namespace Strings
11
12Sub ChrFill(p As PWSTR, n As SIZE_T, c As WCHAR)
13 Dim i = 0 As SIZE_T
14 While i <> n
15 p[i] = c
16 i++
17 Wend
18End Sub
19
20Sub ChrFill(p As PSTR, n As SIZE_T, c As SByte)
21 Dim i = 0 As SIZE_T
22 While i <> n
23 p[i] = c
24 i++
25 Wend
26End Sub
27
28Function ChrCopy(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR
29 memcpy(dst, src, size * SizeOf (WCHAR))
30 Return dst
31End Function
32
33Function ChrCopy(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR
34 memcpy(dst, src, size)
35 Return dst
36End Function
37
38Function ChrMove(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR
39 MoveMemory(dst, src, size * SizeOf (WCHAR))
40 Return dst
41End Function
42
43Function ChrMove(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR
44 MoveMemory(dst, src, size)
45 Return dst
46End Function
47
48Function StrCmp(s1 As PCWSTR, s2 As PCWSTR) As Long
49 Dim i = 0 As SIZE_T
50 While s1[i] = s2[i]
51 If s1[i] = 0 Then
52 Exit While
53 End If
54 i++
55 Wend
56 Return s1[i] As Long - s2[i]
57End Function
58
59Function StrCmp(s1 As PCSTR, s2 As PCSTR) As Long
60 Dim i = 0 As SIZE_T
61 While s1[i] = s2[i]
62 If s1[i] = 0 Then
63 Exit While
64 End If
65 i++
66 Wend
67 Return s1[i] As Long - s2[i]
68End Function
69
70
71
72Function ChrCmp(s1 As PCWSTR, s2 As PCWSTR, size As SIZE_T) As Long
73 Dim i = 0 As SIZE_T
74 While i <> size 'Forではsize = 0のときにまずい
75 ChrCmp = s1[i] As Long - s2[i]
76 If ChrCmp <> 0 Then
77 Exit Function
78 End If
79 i++
80 Wend
81End Function
82
83Function ChrCmp(s1 As PCSTR, s2 As PCSTR, size As SIZE_T) As Long
84 Dim i = 0 As SIZE_T
85 While i <> size
86 ChrCmp = s1[i] As Long - s2[i]
87 If ChrCmp <> 0 Then
88 Exit Function
89 End If
90 i++
91 Wend
92End Function
93
94Function ChrCmp(s1 As PCWSTR, size1 As SIZE_T, s2 As PCWSTR, size2 As SIZE_T) As Long
95 ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
96 If ChrCmp = 0 Then
97 ChrCmp = (( size1 - size2 ) As LONG_PTR) As Long
98 End If
99End Function
100
101Function ChrCmp(s1 As PCSTR, size1 As SIZE_T, s2 As PCSTR, size2 As SIZE_T) As Long
102 ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
103 If ChrCmp = 0 Then
104 ChrCmp = (( size1 - size2 ) As LONG_PTR) As Long
105 End If
106End Function
107
108Function ChrPBrk(str As PCWSTR, cStr As SIZE_T, chars As PCWSTR, cChars As SIZE_T) As SIZE_T
109 Dim i = 0 As SIZE_T
110 While i <> cStr
111 If ChrFind(chars, cChars, str[i]) <> -1 Then
112 Return i
113 End If
114 i++
115 Wend
116 Return -1 As SIZE_T
117End Function
118
119Function ChrPBrk(str As PCSTR, cStr As SIZE_T, Chars As PCSTR, cChars As SIZE_T) As SIZE_T
120 Dim i = 0 As SIZE_T
121 While i <> cStr
122 If ChrFind(Chars, cChars, str[i]) <> -1 Then
123 Return i
124 End If
125 i++
126 Wend
127 Return -1 As SIZE_T
128End Function
129
130Function ChrFind(s As PCWSTR, size As SIZE_T, c As WCHAR) As SIZE_T
131 Dim i = 0 As SIZE_T
132 While i <> size
133 If s[i] = c Then
134 Return i
135 End If
136 i++
137 Wend
138 Return -1 As SIZE_T
139End Function
140
141Function ChrFind(s As PCSTR, size As SIZE_T, c As CHAR) As SIZE_T
142 Dim i = 0 As SIZE_T
143 While i <> size
144 If s[i] = c Then
145 Return i
146 End If
147 i++
148 Wend
149 Return -1 As SIZE_T
150End Function
151
152Function ChrFind(s1 As PCWSTR, len1 As SIZE_T, s2 As PCWSTR, len2 As SIZE_T) As SIZE_T
153 If len2 = 0 Then
154 'ChrFind = 0
155 Exit Function
156 End If
157 Do
158 Dim prev = ChrFind
159 ChrFind = ChrFind(VarPtr(s1[prev]), (len1 - prev) As SIZE_T, s2[0])
160 If ChrFind = -1 As SIZE_T Then
161 Exit Function
162 End If
163 ChrFind += prev
164
165 If ChrCmp(VarPtr(s1[ChrFind]), s2, len2) = 0 Then
166 Exit Function
167 End If
168 ChrFind++
169 If ChrFind = len1 Then
170 ChrFind = -1
171 Exit Function
172 End If
173 Loop
174End Function
175
176Function ChrFind(s1 As PCSTR, len1 As SIZE_T, s2 As PCSTR, len2 As SIZE_T) As SIZE_T
177 If len2 = 0 Then
178 'ChrFind = 0
179 Exit Function
180 End If
181 Do
182 Dim prev = ChrFind
183 ChrFind = ChrFind(VarPtr(s1[prev]), (len1 - prev) As SIZE_T, s2[0])
184 If ChrFind = -1 As SIZE_T Then
185 Exit Function
186 End If
187 ChrFind += prev
188
189 If ChrCmp(VarPtr(s1[ChrFind]), s2, len2) = 0 Then
190 Exit Function
191 End If
192 ChrFind++
193 If ChrFind = len1 Then
194 ChrFind = -1
195 Exit Function
196 End If
197 Loop
198End Function
199
200Namespace Detail
201Function Split(s As String, c As StrChar) As System.Collections.ArrayList
202 Split = New System.Collections.ArrayList
203
204 Dim last = 0 As Long
205 Do
206 Dim i = s.IndexOf(c, last)
207 If i < 0 Then
208 Split.Add(s.Substring(last, s.Length - last))
209 Exit Function
210 End If
211 Split.Add(s.Substring(last, i - last))
212 last = i + 1
213 If last > s.Length Then
214 Split.Add(System.String.Empty)
215 End If
216 Loop
217End Function
218
219End Namespace 'Detail
220
221End Namespace 'Strings
222End Namespace 'ActiveBasic
223
224#endif '__ACTIVEBASIC_STRINGS_STRINGS_AB__
Note: See TracBrowser for help on using the repository browser.