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 | |
---|
9 | Namespace ActiveBasic |
---|
10 | Namespace Strings |
---|
11 | |
---|
12 | Sub 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 |
---|
18 | End Sub |
---|
19 | |
---|
20 | Sub 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 |
---|
26 | End Sub |
---|
27 | |
---|
28 | Function ChrCopy(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR |
---|
29 | memcpy(dst, src, size * SizeOf (WCHAR)) |
---|
30 | Return dst |
---|
31 | End Function |
---|
32 | |
---|
33 | Function ChrCopy(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR |
---|
34 | memcpy(dst, src, size) |
---|
35 | Return dst |
---|
36 | End Function |
---|
37 | |
---|
38 | Function ChrMove(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR |
---|
39 | MoveMemory(dst, src, size * SizeOf (WCHAR)) |
---|
40 | Return dst |
---|
41 | End Function |
---|
42 | |
---|
43 | Function ChrMove(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR |
---|
44 | MoveMemory(dst, src, size) |
---|
45 | Return dst |
---|
46 | End Function |
---|
47 | |
---|
48 | Function 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] |
---|
57 | End Function |
---|
58 | |
---|
59 | Function 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] |
---|
68 | End Function |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | Function 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 |
---|
81 | End Function |
---|
82 | |
---|
83 | Function 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 |
---|
92 | End Function |
---|
93 | |
---|
94 | Function 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 |
---|
98 | End If |
---|
99 | End Function |
---|
100 | |
---|
101 | Function 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 |
---|
105 | End If |
---|
106 | End Function |
---|
107 | |
---|
108 | Function 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 |
---|
117 | End Function |
---|
118 | |
---|
119 | Function 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 |
---|
128 | End Function |
---|
129 | |
---|
130 | Function 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 |
---|
139 | End Function |
---|
140 | |
---|
141 | Function 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 |
---|
150 | End Function |
---|
151 | |
---|
152 | Function 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 |
---|
174 | End Function |
---|
175 | |
---|
176 | Function 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 |
---|
198 | End Function |
---|
199 | |
---|
200 | Namespace Detail |
---|
201 | Function 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 |
---|
217 | End Function |
---|
218 | |
---|
219 | End Namespace 'Detail |
---|
220 | |
---|
221 | End Namespace 'Strings |
---|
222 | End Namespace 'ActiveBasic |
---|
223 | |
---|
224 | #endif '__ACTIVEBASIC_STRINGS_STRINGS_AB__ |
---|