1 | /*!
|
---|
2 | @file Classes/System/Text/Encoding.ab
|
---|
3 | @brief Encodingクラスとそれに関係するクラスなどの宣言・定義
|
---|
4 | */
|
---|
5 |
|
---|
6 | Namespace System
|
---|
7 | Namespace Text
|
---|
8 |
|
---|
9 | /*!
|
---|
10 | @brief 各種の文字符号化(エンコーディング)を行うためのクラス
|
---|
11 | @date 2007/12/07
|
---|
12 | @auther Egtra
|
---|
13 |
|
---|
14 | なお、このクラスで、文字列の長さやバッファの大きさを指定するときには、
|
---|
15 | 1 chars = 2バイト(UTF-16符号単位)、1 bytes = 1バイトの単位で指定する。
|
---|
16 |
|
---|
17 | */
|
---|
18 | Class Encoding
|
---|
19 | Implements ICloneable
|
---|
20 | Public
|
---|
21 | /*!
|
---|
22 | @brief 簡易的複製を作成する。
|
---|
23 | */
|
---|
24 | Abstract Function Clone() As Object
|
---|
25 |
|
---|
26 | ' Override Function Equals(y As Object) As Boolean
|
---|
27 | ' End Function
|
---|
28 |
|
---|
29 | ' Override Function GetHashCode() As Long
|
---|
30 | ' End Function
|
---|
31 |
|
---|
32 | Public
|
---|
33 | Sub Encoding()
|
---|
34 | decoderFallback = New DecoderReplacementFallback
|
---|
35 | End Sub
|
---|
36 |
|
---|
37 | /*!
|
---|
38 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
39 | @param[in] s 対象文字列
|
---|
40 | @param[in] n sの長さ
|
---|
41 | @return 符号化して得られる文字列の長さ
|
---|
42 | @date 2007/12/08
|
---|
43 | */
|
---|
44 | Function GetBytesCount(s As *WCHAR, n As Long) As Long
|
---|
45 | If s = 0 Then
|
---|
46 | Throw New ArgumentNullException("Encoding.GetBytesCount: An argument is null value.", "s")
|
---|
47 | ElseIf n < 0 Then
|
---|
48 | Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "n")
|
---|
49 | End If
|
---|
50 | Return GetBytesCountCore(s, n)
|
---|
51 | End Function
|
---|
52 | #ifdef UNICODE
|
---|
53 | /*!
|
---|
54 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
55 | @param[in] s 対象文字列
|
---|
56 | @return 符号化して得られる文字列の長さ
|
---|
57 | @date 2007/12/08
|
---|
58 | */
|
---|
59 | Function GetBytesCount(s As String) As Long
|
---|
60 | If ActiveBasic.IsNothing(s) Then
|
---|
61 | Throw New ArgumentNullException("Encoding.GetBytesCount: An argument is null value.", "s")
|
---|
62 | End If
|
---|
63 | Return GetBytesCountCore(StrPtr(s), s.Length)
|
---|
64 | End Function
|
---|
65 | #endif
|
---|
66 | /*!
|
---|
67 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
68 | @param[in] s 対象文字列
|
---|
69 | @param[in] index 開始位置
|
---|
70 | @param[in] count 符号化する文字の数
|
---|
71 | @return 符号化して得られる文字列の長さ
|
---|
72 | @date 2007/12/08
|
---|
73 | */
|
---|
74 | Function GetBytesCount(s As *WCHAR, index As Long, count As Long) As Long
|
---|
75 | If s = 0 Then
|
---|
76 | Throw New ArgumentNullException("Encoding.GetBytesCount: An argument is null value.", "s")
|
---|
77 | ElseIf index < 0 Then
|
---|
78 | Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "index")
|
---|
79 | ElseIf count < 0 Then
|
---|
80 | Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "count")
|
---|
81 | End If
|
---|
82 | Return GetBytesCountCore(VarPtr(s[index]), count)
|
---|
83 | End Function
|
---|
84 | Protected
|
---|
85 | /*!
|
---|
86 | @brief GetBytesCountの実装を行う。
|
---|
87 | @param[in] s 対象文字列
|
---|
88 | @param[in] n sの長さ
|
---|
89 | @return 符号化して得られる文字列の長さ
|
---|
90 | @date 2007/12/08
|
---|
91 | */
|
---|
92 | Abstract Function GetBytesCountCore(s As *WCHAR, n As Long) As Long
|
---|
93 | Public
|
---|
94 | /*!
|
---|
95 | @brief 符号化する。
|
---|
96 | @param[in] chars 入力
|
---|
97 | @param[in] charCount charsの長さ
|
---|
98 | @param[out] bytes 出力
|
---|
99 | @param[in] byteCount bytesのバッファの大きさ
|
---|
100 | @return bytesに書き込まれたバイト数
|
---|
101 | @date 2007/12/08
|
---|
102 | */
|
---|
103 | Function GetBytes(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long) As Long
|
---|
104 | If chars = 0 Then
|
---|
105 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "chars")
|
---|
106 | ElseIf bytes = 0 Then
|
---|
107 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "bytes")
|
---|
108 | ElseIf charCount < 0 Then
|
---|
109 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "charCount")
|
---|
110 | ElseIf byteCount < 0 Then
|
---|
111 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "byteCount")
|
---|
112 | End If
|
---|
113 |
|
---|
114 | Return GetBytesCore(chars, charCount, bytes, byteCount)
|
---|
115 | End Function
|
---|
116 | /*!
|
---|
117 | @brief 符号化する。
|
---|
118 | @param[in] chars 入力
|
---|
119 | @param[in] index charsの開始位置
|
---|
120 | @param[in] count 符号化する文字の数
|
---|
121 | @param[out] bytes 出力
|
---|
122 | @param[in] byteCount bytesのバッファの大きさ
|
---|
123 | @return bytesに書き込まれたバイト数
|
---|
124 | @date 2007/12/08
|
---|
125 | */
|
---|
126 | Function GetBytes(chars As *WCHAR, index As Long, count As Long, bytes As *Byte, byteCount As Long) As Long
|
---|
127 | If chars = 0 Then
|
---|
128 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "chars")
|
---|
129 | ElseIf bytes = 0 Then
|
---|
130 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "bytes")
|
---|
131 | ElseIf index < 0 Then
|
---|
132 | Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "index")
|
---|
133 | ElseIf count < 0 Then
|
---|
134 | Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "count")
|
---|
135 | ElseIf byteCount < 0 Then
|
---|
136 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "byteCount")
|
---|
137 | End If
|
---|
138 |
|
---|
139 | Return GetBytesCore(VarPtr(chars[index]), count, bytes, byteCount)
|
---|
140 | End Function
|
---|
141 | #ifdef UNICODE
|
---|
142 | /*!
|
---|
143 | @brief 符号化する。
|
---|
144 | @param[in] s 入力
|
---|
145 | @param[in] index sの開始位置
|
---|
146 | @param[in] count 符号化する文字の数
|
---|
147 | @param[out] bytes 出力
|
---|
148 | @param[in] byteCount bytesのバッファの大きさ
|
---|
149 | @return bytesに書き込まれたバイト数
|
---|
150 | @date 2007/12/08
|
---|
151 | */
|
---|
152 | Function GetBytes(s As String, index As Long, count As Long, bytes As *Byte, byteCount As Long) As Long
|
---|
153 | If chars = 0 Then
|
---|
154 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "chars")
|
---|
155 | ElseIf bytes = 0 Then
|
---|
156 | Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "bytes")
|
---|
157 | ElseIf index < 0 Or index >= s.Length Then
|
---|
158 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "index")
|
---|
159 | ElseIf count < 0 Or index + count >= s.Length Then
|
---|
160 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "count")
|
---|
161 | ElseIf byteCount < 0 Then
|
---|
162 | Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "byteCount")
|
---|
163 | End If
|
---|
164 | Dim p = StrPtr(s)
|
---|
165 | Return GetBytesCore(VarPtr(p[index]), count, bytes, byteCount)
|
---|
166 | End Function
|
---|
167 | #endif
|
---|
168 | Protected
|
---|
169 | /*!
|
---|
170 | @brief GetBytesの処理を行う。
|
---|
171 | @param[in] chars 入力
|
---|
172 | @param[in] charCount charsの長さ
|
---|
173 | @param[out] bytes 出力
|
---|
174 | @param[in] byteCount bytesのバッファの大きさ
|
---|
175 | @return bytesに書き込まれたバイト数
|
---|
176 | @exception ArgumentException バッファの大きさが足りない
|
---|
177 | @exception EncoderFallbackException フォールバックが発生した
|
---|
178 | @date 2007/12/08
|
---|
179 | */
|
---|
180 | Abstract Function GetBytesCore(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long) As Long
|
---|
181 |
|
---|
182 | Public
|
---|
183 | /*!
|
---|
184 | @brief 復号して得られる文字列の長さを計算する。
|
---|
185 | @param[in] s 対象文字列
|
---|
186 | @param[in] n sの長さ
|
---|
187 | @return 復号して得られる文字列の長さ
|
---|
188 | @date 2007/12/08
|
---|
189 | */
|
---|
190 | Function GetCharsCount(s As *Byte, n As Long) As Long
|
---|
191 | If s = 0 Then
|
---|
192 | Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")
|
---|
193 | ElseIf n < 0 Then
|
---|
194 | Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "n")
|
---|
195 | End If
|
---|
196 | Return GetCharsCountCore(s, n)
|
---|
197 | End Function
|
---|
198 | /*!
|
---|
199 | @brief 復号して得られる文字列の長さを計算する。
|
---|
200 | @param[in] s 対象文字列
|
---|
201 | @param[in] index 開始位置
|
---|
202 | @param[in] count 符号化する文字の数
|
---|
203 | @return 符号化して得られる文字列の長さ
|
---|
204 | @date 2007/12/08
|
---|
205 | */
|
---|
206 | Function GetCharsCount(s As *Byte, index As Long, count As Long) As Long
|
---|
207 | If s = 0 Then
|
---|
208 | Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")
|
---|
209 | ElseIf index < 0 Then
|
---|
210 | Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "index")
|
---|
211 | ElseIf count < 0 Then
|
---|
212 | Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "count")
|
---|
213 | End If
|
---|
214 | Return GetCharsCountCore(VarPtr(s[index]), count)
|
---|
215 | End Function
|
---|
216 | Protected
|
---|
217 | /*!
|
---|
218 | @brief GetCharsCountの処理を行う。
|
---|
219 | @param[in] s 対象文字列
|
---|
220 | @param[in] n sの長さ
|
---|
221 | @return 符号化して得られる文字列の長さ
|
---|
222 | @date 2007/12/08
|
---|
223 | */
|
---|
224 | Abstract Function GetCharsCountCore(s As *Byte, n As Long) As Long
|
---|
225 | /*!
|
---|
226 | */
|
---|
227 | Public
|
---|
228 | /*!
|
---|
229 | @brief 復号する。
|
---|
230 | @param[in] bytes 入力
|
---|
231 | @param[in] byteCount charsの長さ
|
---|
232 | @param[out] chars 出力
|
---|
233 | @param[in] charCount bytesのバッファの大きさ
|
---|
234 | @return bytesに書き込まれたバイト数
|
---|
235 | @date 2007/12/08
|
---|
236 | */
|
---|
237 | Function GetChars(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long
|
---|
238 | If chars = 0 Then
|
---|
239 | Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")
|
---|
240 | ElseIf bytes = 0 Then
|
---|
241 | Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")
|
---|
242 | ElseIf charCount < 0 Then
|
---|
243 | Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "charCount")
|
---|
244 | ElseIf byteCount < 0 Then
|
---|
245 | Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")
|
---|
246 | End If
|
---|
247 |
|
---|
248 | Return GetCharsCore(bytes, byteCount, chars, charCount)
|
---|
249 | End Function
|
---|
250 | /*!
|
---|
251 | @brief 復号する。
|
---|
252 | @param[in] bytes 入力
|
---|
253 | @param[in] index charsの開始位置
|
---|
254 | @param[in] count 符号化する文字の数
|
---|
255 | @param[out] chars 出力
|
---|
256 | @param[in] charCount bytesのバッファの大きさ
|
---|
257 | @return bytesに書き込まれたバイト数
|
---|
258 | @date 2007/12/08
|
---|
259 | */
|
---|
260 | Function GetChars(bytes As *Byte, index As Long, count As Long, chars As *WCHAR, charCount As Long) As Long
|
---|
261 | If chars = 0 Then
|
---|
262 | Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")
|
---|
263 | ElseIf bytes = 0 Then
|
---|
264 | Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")
|
---|
265 | ElseIf index < 0 Then
|
---|
266 | Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "index")
|
---|
267 | ElseIf count < 0 Then
|
---|
268 | Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "count")
|
---|
269 | ElseIf charCount < 0 Then
|
---|
270 | Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")
|
---|
271 | End If
|
---|
272 |
|
---|
273 | Return GetCharsCore(VarPtr(bytes[index]), count, chars, charCount)
|
---|
274 | End Function
|
---|
275 | Protected
|
---|
276 | /*!
|
---|
277 | @brief GetCharsの処理を行う。
|
---|
278 | @param[in] bytes 入力
|
---|
279 | @param[in] byteCount charsの長さ
|
---|
280 | @param[out] chars 出力
|
---|
281 | @param[in] charCount bytesのバッファの大きさ
|
---|
282 | @return bytesに書き込まれたバイト数
|
---|
283 | @exception ArgumentException バッファの大きさが足りない
|
---|
284 | @exception EncoderFallbackException フォールバックが発生した
|
---|
285 | @date 2007/12/08
|
---|
286 | */
|
---|
287 | Abstract Function GetCharsCore(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long
|
---|
288 | Public
|
---|
289 | #ifdef UNICODE
|
---|
290 | /*!
|
---|
291 | @brief 復号し、Stringで結果を返す。
|
---|
292 | @param[in] bytes 入力
|
---|
293 | @param[in] byteCount charsの長さ
|
---|
294 | @return 変換結果の文字列
|
---|
295 | @date 2007/12/08
|
---|
296 | */
|
---|
297 | Function GetString(bytes As *Byte, byteCount As Long) As String
|
---|
298 | If bytes = 0 Then
|
---|
299 | Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")
|
---|
300 | ElseIf byteCount < 0 Then
|
---|
301 | Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "byteCount")
|
---|
302 | End If
|
---|
303 | Return getStringCore(bytes, byteCount)
|
---|
304 | End Function
|
---|
305 | /*!
|
---|
306 | @brief 復号し、Stringで結果を返す。
|
---|
307 | @param[in] bytes 入力
|
---|
308 | @param[in] index charsの開始位置
|
---|
309 | @param[in] count 符号化する文字の数
|
---|
310 | @return 変換結果の文字列
|
---|
311 | @date 2007/12/08
|
---|
312 | */
|
---|
313 | Function GetString(bytes As *Byte, index As Long, count As Long) As String
|
---|
314 | If bytes = 0 Then
|
---|
315 | Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")
|
---|
316 | ElseIf index < 0 Then
|
---|
317 | Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "index")
|
---|
318 | ElseIf count < 0 Then
|
---|
319 | Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "count")
|
---|
320 | End If
|
---|
321 | Return getStringCore(VarPtr(bytes[index]), count)
|
---|
322 | End Function
|
---|
323 | Private
|
---|
324 |
|
---|
325 | Function getStringCore(bytes As *Byte, byteCount As Long) As String
|
---|
326 | Dim sb = New StringBuilder
|
---|
327 | Dim bufSize = GetMaxCharCount(byteCount)
|
---|
328 | sb.Length = bufSize
|
---|
329 | Dim len = GetCharsCore(bytes, byteCount, StrPtr(sb), bufSize)
|
---|
330 | sb.Length = len
|
---|
331 | getStringCore = sb.ToString
|
---|
332 | End Function
|
---|
333 | #endif
|
---|
334 |
|
---|
335 | Public
|
---|
336 | /*!
|
---|
337 | @brief 符号器を取得する。
|
---|
338 | */
|
---|
339 | Abstract Function GetDecoder() As Decoder
|
---|
340 |
|
---|
341 | /*!
|
---|
342 | @brief 復号器を取得する。
|
---|
343 | */
|
---|
344 | Abstract Function GetEncoder() As Encoder
|
---|
345 |
|
---|
346 | /*!
|
---|
347 | @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
|
---|
348 | */
|
---|
349 | Abstract Function GetMaxByteCount(charCount As Long) As Long
|
---|
350 |
|
---|
351 | /*!
|
---|
352 | @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
|
---|
353 | */
|
---|
354 | Abstract Function GetMaxCharCount(charCount As Long) As Long
|
---|
355 |
|
---|
356 | /*!
|
---|
357 | @brief 符号化された文字列の先頭につける識別文字列の取得
|
---|
358 | ようするにUTFのBOM
|
---|
359 | */
|
---|
360 | Virtual Function GetPreamble() As *Byte
|
---|
361 | Return 0
|
---|
362 | End Function
|
---|
363 |
|
---|
364 | /*!
|
---|
365 | @brief GetPreambleの配列の要素数
|
---|
366 | */
|
---|
367 | Virtual Function GetPreambleLength() As Long
|
---|
368 | Return 0
|
---|
369 | End Function
|
---|
370 |
|
---|
371 | /*!
|
---|
372 | @brief 正規化されるかどうか。
|
---|
373 | */
|
---|
374 | Abstract Function IsAlwaysNormalized() As Boolean
|
---|
375 |
|
---|
376 | /*!
|
---|
377 | @brief 指定された方式で正規化されるかどうか。
|
---|
378 | */
|
---|
379 | Abstract Function IsAlwaysNormalized(form As NormalizationForm) As Boolean
|
---|
380 |
|
---|
381 | Abstract Function BodyName() As String
|
---|
382 | Abstract Function HeaderName() As String
|
---|
383 |
|
---|
384 | /*!
|
---|
385 | @brief コードページの取得。
|
---|
386 | */
|
---|
387 | ' Abstract Function CodePage() As Long
|
---|
388 | /*!
|
---|
389 | @brief 最も厳密に対応するWindowsコードページの取得。
|
---|
390 | */
|
---|
391 | ' Abstract Function WindowsCodePage() As Long
|
---|
392 |
|
---|
393 | Function DecoderFallback() As DecoderFallback
|
---|
394 | Return decoderFallback
|
---|
395 | End Function
|
---|
396 |
|
---|
397 | Sub DecoderFallback(f As DecoderFallback)
|
---|
398 | If ActiveBasic.IsNothing(f) Then
|
---|
399 | Throw New ArgumentNullException("f")
|
---|
400 | End If
|
---|
401 | decoderFallback = f
|
---|
402 | End Sub
|
---|
403 |
|
---|
404 | Function EncoderFallback() As EncoderFallback
|
---|
405 | Return encoderFallback
|
---|
406 | End Function
|
---|
407 |
|
---|
408 | Sub EncoderFallback(f As EncoderFallback)
|
---|
409 | If ActiveBasic.IsNothing(f) Then
|
---|
410 | Throw New ArgumentNullException("f")
|
---|
411 | End If
|
---|
412 | encoderFallback = f
|
---|
413 | End Sub
|
---|
414 |
|
---|
415 | Private
|
---|
416 | decoderFallback As DecoderFallback
|
---|
417 | encoderFallback As EncoderFallback
|
---|
418 | Public
|
---|
419 | /*!
|
---|
420 | @brief この符号化形式の名前の取得。
|
---|
421 | */
|
---|
422 | Abstract Function EncodingName() As String
|
---|
423 |
|
---|
424 | /*!
|
---|
425 | @brief この符号化形式のIANA登録名の取得。
|
---|
426 | */
|
---|
427 | Abstract Function WebName() As String
|
---|
428 |
|
---|
429 | ' Abstract Function IsBrowserDisplay() As Boolean
|
---|
430 | ' Abstract Function IsBrowserSave() As Boolean
|
---|
431 | ' Abstract Function IsMailNewsDisplay() As Boolean
|
---|
432 | ' Abstract Function IsMailNewsSave() As Boolean
|
---|
433 |
|
---|
434 | ' Abstract Function IsReadOnly() Boolean
|
---|
435 |
|
---|
436 | /*!
|
---|
437 | @brief この符号化形式が、1バイト文字だけを使う(複数バイト文字を使わない)かどうか。
|
---|
438 | */
|
---|
439 | Abstract Function IsSingleByte() As Boolean
|
---|
440 |
|
---|
441 | 'GetPreamble
|
---|
442 |
|
---|
443 | /*!
|
---|
444 | @brief ある符号化文字列から別の符号化文字列へ変換する。
|
---|
445 | @param[in] srcEncoding 入力の符号化方式
|
---|
446 | @param[in] dstEncoding 出力の符号化方式
|
---|
447 | @param[in] bytes 入力文字列
|
---|
448 | @param[in] size バイト単位での文字列の長さ
|
---|
449 | @return 出力文字列
|
---|
450 | @exception ArgumentNullException srcEncoding, dstEncoding, bytesの少なくとも1つ以上がNothing/NULLのとき。
|
---|
451 | @exception ArgumentOutOfRangeException sizeが明らかに範囲外(負の値など)のとき。
|
---|
452 | @exception DecoderFallbackException
|
---|
453 | @exception EncoderFallbackException
|
---|
454 | */
|
---|
455 | Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, size As Long) As *Byte
|
---|
456 | End Function
|
---|
457 |
|
---|
458 | Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte
|
---|
459 | End Function
|
---|
460 |
|
---|
461 | /*!
|
---|
462 | @brief 指定したコードページ用のEncodingインスタンスの取得。
|
---|
463 | */
|
---|
464 | Static Function GetEncoding(codepage As Long) As Encoding
|
---|
465 | End Function
|
---|
466 | ' Static Function GetEncoding(codepage As Long, encoderFallback As EncoderFallback, decoderFallback As DecoderFallback) As Encoding
|
---|
467 | ' End Function
|
---|
468 | /*!
|
---|
469 | @brief 指定した符号化形式名用のEncodingインスタンスの取得。
|
---|
470 | */
|
---|
471 | Static Function GetEncoding(name As String) As Encoding
|
---|
472 | End Function
|
---|
473 | ' Static Function GetEncoding(name As String, encoderFallback As EncoderFallback, decoderFallback As DecoderFallback) As Encoding
|
---|
474 | ' End Function
|
---|
475 |
|
---|
476 | /*!
|
---|
477 | @brief システム既定のANSIコードページ用のEncodingインスタンスの取得。
|
---|
478 | */
|
---|
479 | Static Function Default() As Encoding
|
---|
480 | End Function
|
---|
481 | /*!
|
---|
482 | @brief UTF-7用のEncodingインスタンスの取得。
|
---|
483 | */
|
---|
484 | Static Function UTF7() As Encoding
|
---|
485 | End Function
|
---|
486 | /*!
|
---|
487 | @brief UTF-8用のEncodingインスタンスの取得。
|
---|
488 | */
|
---|
489 | Static Function UTF8() As Encoding
|
---|
490 | End Function
|
---|
491 | /*!
|
---|
492 | @brief UTF-16LE用のEncodingインスタンスの取得。
|
---|
493 | */
|
---|
494 | Static Function UTF16() As Encoding
|
---|
495 | End Function
|
---|
496 | /*!
|
---|
497 | @brief UTF-16BE用のEncodingインスタンスの取得。
|
---|
498 | */
|
---|
499 | Static Function UTF16BE() As Encoding
|
---|
500 | End Function
|
---|
501 | /*!
|
---|
502 | @brief UTF-32LE用のEncodingインスタンスの取得。
|
---|
503 | */
|
---|
504 | Static Function UTF32() As Encoding
|
---|
505 | End Function
|
---|
506 | End Class
|
---|
507 |
|
---|
508 | /*!
|
---|
509 | @brief 復号を行うクラス
|
---|
510 | @date 2007/12/19
|
---|
511 | @auther Egtra
|
---|
512 | */
|
---|
513 | Class Decoder
|
---|
514 | Public
|
---|
515 | /*!
|
---|
516 | @brief 変換する
|
---|
517 | @param[in] bytes 入力
|
---|
518 | @param[in] byteIndex 入力開始位置
|
---|
519 | @param[in] byteCount 入力要素数
|
---|
520 | @param[out] chars 出力
|
---|
521 | @param[in] charIndex 出力開始位置
|
---|
522 | @param[in] charCount 出力要素数
|
---|
523 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
524 | @param[out] charsUsed 使用された入力の要素数
|
---|
525 | @param[out] bytesUsed 出力の内、実際に書き込まれた要素数
|
---|
526 | @param[out] completed 入力の全ての文字が変換に使われたかどうか
|
---|
527 | */
|
---|
528 | Sub Convert(bytes As *Byte, byteIndex As Long, byteCount As Long,
|
---|
529 | chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean,
|
---|
530 | ByRef bytesUsed As Long, ByRef charsUsed As Long, ByRef completed As Boolean)
|
---|
531 |
|
---|
532 | If bytes = 0 Then
|
---|
533 | Throw New ArgumentNullException("bytes")
|
---|
534 | ElseIf byteIndex < 0 Then
|
---|
535 | Throw New ArgumentOutOfRangeException("byteIndex")
|
---|
536 | ElseIf byteCount < 0 Then
|
---|
537 | Throw New ArgumentOutOfRangeException("byteCount")
|
---|
538 | ElseIf chars = 0 Then
|
---|
539 | Throw New ArgumentNullException("chars")
|
---|
540 | ElseIf charIndex < 0 Then
|
---|
541 | Throw New ArgumentOutOfRangeException("charIndex")
|
---|
542 | ElseIf charCount < 0 Then
|
---|
543 | Throw New ArgumentOutOfRangeException("charCount")
|
---|
544 | End If
|
---|
545 | ConvertCore(VarPtr(bytes[byteIndex]), byteCount, VarPtr(chars[charIndex]), charCount, flush, bytesUsed, charsUsed, completed)
|
---|
546 | End Sub
|
---|
547 |
|
---|
548 | /*!
|
---|
549 | @overload Sub Convert(bytes As *Byte, byteIndex As Long, byteCount As Long,
|
---|
550 | chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean,
|
---|
551 | ByRef bytesUsed As Long, ByRef charsUsed As Long, ByRef completed As Boolean)
|
---|
552 | */
|
---|
553 | Sub Convert(bytes As *Byte, byteCount As Long,
|
---|
554 | chars As *WCHAR, charCount As Long, flush As Boolean,
|
---|
555 | ByRef bytesUsed As Long, ByRef charsUsed As Long, ByRef completed As Boolean)
|
---|
556 |
|
---|
557 | If bytes = 0 Then
|
---|
558 | Throw New ArgumentNullException("bytes")
|
---|
559 | ElseIf byteCount < 0 Then
|
---|
560 | Throw New ArgumentOutOfRangeException("byteCount")
|
---|
561 | ElseIf chars = 0 Then
|
---|
562 | Throw New ArgumentNullException("chars")
|
---|
563 | ElseIf charCount < 0 Then
|
---|
564 | Throw New ArgumentOutOfRangeException("charCount")
|
---|
565 | End If
|
---|
566 | ConvertCore(bytes, byteCount, chars, charCount, flush, bytesUsed, charsUsed, completed)
|
---|
567 | End Sub
|
---|
568 |
|
---|
569 | /*!
|
---|
570 | @brief 変換する
|
---|
571 | @param[in] bytes 入力
|
---|
572 | @param[in] byteIndex 入力開始位置
|
---|
573 | @param[in] byteCount 入力要素数
|
---|
574 | @param[out] chars 出力
|
---|
575 | @param[in] charIndex 出力開始位置
|
---|
576 | @param[in] charCount 出力要素数
|
---|
577 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
578 | @return 出力の内、実際に書き込まれた要素数
|
---|
579 | */
|
---|
580 | Function GetChars(bytes As *Byte, byteIndex As Long, byteCount As Long, chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean) As Long
|
---|
581 | Dim bytesUsed As Long
|
---|
582 | Dim completed As Boolean
|
---|
583 | Convert(bytes, byteIndex, byteCount, chars, charIndex, charCount, flush, bytesUsed, GetChars, completed)
|
---|
584 | End Function
|
---|
585 |
|
---|
586 | /*!
|
---|
587 | @overload Function GetChars(bytes As *Byte, byteIndex As Long, byteCount As Long, chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean) As Long
|
---|
588 | */
|
---|
589 | Function GetChars(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long, flush As Boolean) As Long
|
---|
590 | Dim bytesUsed As Long
|
---|
591 | Dim completed As Boolean
|
---|
592 | Convert(bytes, byteCount, chars, charCount, flush, bytesUsed, GetChars, completed)
|
---|
593 | End Function
|
---|
594 |
|
---|
595 | /*!
|
---|
596 | @overload Function GetChars(bytes As *Byte, byteIndex As Long, byteCount As Long, chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean) As Long
|
---|
597 | */
|
---|
598 | Function GetChars(bytes As *Byte, byteIndex As Long, byteCount As Long, chars As *WCHAR, charIndex As Long, charCount As Long) As Long
|
---|
599 | GetChars = GetChars(bytes, byteIndex, byteCount, chars, charIndex, charCount, False)
|
---|
600 | End Function
|
---|
601 |
|
---|
602 | /*!
|
---|
603 | @brief フォールバックの取得
|
---|
604 | */
|
---|
605 | Function Fallback() As DecoderFallback
|
---|
606 | Return fallback
|
---|
607 | End Function
|
---|
608 |
|
---|
609 | /*!
|
---|
610 | @brief フォールバックの設定
|
---|
611 | */
|
---|
612 | Sub Fallback(newFallback As DecoderFallback)
|
---|
613 | If ActiveBasic.IsNothing(newFallback) Then
|
---|
614 | Throw New ArgumentNullException("newFallback")
|
---|
615 | End If
|
---|
616 | fallback = newFallback
|
---|
617 | End Sub
|
---|
618 | Protected
|
---|
619 | /*!
|
---|
620 | @brief 実際に変換するメソッド
|
---|
621 | @param[in] bytes 入力
|
---|
622 | @param[in] byteCount 入力要素数
|
---|
623 | @param[out] chars 出力
|
---|
624 | @param[in] charCount 出力要素数
|
---|
625 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
626 | @param[out] charsUsed 使用された入力の要素数
|
---|
627 | @param[out] bytesUsed 出力の内、実際に書き込まれた要素数
|
---|
628 | @param[out] completed 入力の全ての文字が変換に使われたかどうか
|
---|
629 | */
|
---|
630 | Abstract Sub ConvertCore(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long, flush As Boolean,
|
---|
631 | ByRef bytesUsed As Long, ByRef charsUsed As Long, ByRef completed As Boolean)
|
---|
632 |
|
---|
633 | Private
|
---|
634 | fallback As DecoderFallback
|
---|
635 | End Class
|
---|
636 |
|
---|
637 | /*!
|
---|
638 | @brief 符号化を行うクラス
|
---|
639 | @date 2007/12/19
|
---|
640 | @auther Egtra
|
---|
641 | */
|
---|
642 | Class Encoder
|
---|
643 | Public
|
---|
644 | /*!
|
---|
645 | @brief 変換する
|
---|
646 | @param[in] chars 入力
|
---|
647 | @param[in] charIndex 入力開始位置
|
---|
648 | @param[in] charCount 入力要素数
|
---|
649 | @param[out] bytes 出力
|
---|
650 | @param[in] byteIndex 出力開始位置
|
---|
651 | @param[in] byteCount 出力要素数
|
---|
652 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
653 | @param[out] charsUsed 使用された入力の要素数
|
---|
654 | @param[out] bytesUsed 出力の内、実際に書き込まれた要素数
|
---|
655 | @param[out] completed 入力の全ての文字が変換に使われたかどうか
|
---|
656 | */
|
---|
657 | Sub Convert(chars As *WCHAR, charIndex As Long, charCount As Long,
|
---|
658 | bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean,
|
---|
659 | ByRef charsUsed As Long, ByRef bytesUsed As Long, ByRef completed As Boolean)
|
---|
660 |
|
---|
661 | If chars = 0 Then
|
---|
662 | Throw New ArgumentNullException("chars")
|
---|
663 | ElseIf charIndex < 0 Then
|
---|
664 | Throw New ArgumentOutOfRangeException("charIndex")
|
---|
665 | ElseIf charCount < 0 Then
|
---|
666 | Throw New ArgumentOutOfRangeException("charCount")
|
---|
667 | ElseIf bytes = 0 Then
|
---|
668 | Throw New ArgumentNullException("bytes")
|
---|
669 | ElseIf byteIndex < 0 Then
|
---|
670 | Throw New ArgumentOutOfRangeException("byteIndex")
|
---|
671 | ElseIf byteCount < 0 Then
|
---|
672 | Throw New ArgumentOutOfRangeException("byteCount")
|
---|
673 | End If
|
---|
674 | ConvertCore(VarPtr(chars[charIndex]), charCount, VarPtr(bytes[byteIndex]), byteCount, flush, charsUsed, bytesUsed, completed)
|
---|
675 | End Sub
|
---|
676 |
|
---|
677 | /*!
|
---|
678 | @overload Sub Convert(chars As *WCHAR, charIndex As Long, charCount As Long,
|
---|
679 | bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean,
|
---|
680 | ByRef charsUsed As Long, ByRef bytesUsed As Long, ByRef completed As Boolean)
|
---|
681 | */
|
---|
682 | Sub Convert(chars As *WCHAR, charCount As Long,
|
---|
683 | bytes As *Byte, byteCount As Long, flush As Boolean,
|
---|
684 | ByRef charsUsed As Long, ByRef bytesUsed As Long, ByRef completed As Boolean)
|
---|
685 |
|
---|
686 | If chars = 0 Then
|
---|
687 | Throw New ArgumentNullException("chars")
|
---|
688 | ElseIf charCount < 0 Then
|
---|
689 | Throw New ArgumentOutOfRangeException("charCount")
|
---|
690 | ElseIf bytes = 0 Then
|
---|
691 | Throw New ArgumentNullException("bytes")
|
---|
692 | ElseIf byteCount < 0 Then
|
---|
693 | Throw New ArgumentOutOfRangeException("byteCount")
|
---|
694 | End If
|
---|
695 | ConvertCore(chars, charCount, bytes, byteCount, flush, charsUsed, bytesUsed, completed)
|
---|
696 | End Sub
|
---|
697 |
|
---|
698 | /*!
|
---|
699 | @brief 変換する
|
---|
700 | @param[in] chars 入力
|
---|
701 | @param[in] charIndex 入力開始位置
|
---|
702 | @param[in] charCount 入力要素数
|
---|
703 | @param[out] bytes 出力
|
---|
704 | @param[in] byteIndex 出力開始位置
|
---|
705 | @param[in] byteCount 出力要素数
|
---|
706 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
707 | @return bytesUsed 出力の内、実際に書き込まれた要素数
|
---|
708 | */
|
---|
709 | Function GetBytes(chars As *WCHAR, charIndex As Long, charCount As Long, bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean) As Long
|
---|
710 | Dim charsUsed As Long
|
---|
711 | Dim completed As Boolean
|
---|
712 | Convert(chars, charIndex, charCount, bytes, byteIndex, byteCount, flush, charsUsed, GetBytes, completed)
|
---|
713 | End Function
|
---|
714 |
|
---|
715 | /*!
|
---|
716 | @overload Function GetBytes(chars As *WCHAR, charIndex As Long, charCount As Long, bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean) As Long
|
---|
717 | */
|
---|
718 | Function GetBytes(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long, flush As Boolean) As Long
|
---|
719 | Dim charsUsed As Long
|
---|
720 | Dim completed As Boolean
|
---|
721 | Convert(chars, charCount, bytes, byteCount, flush, charsUsed, GetBytes, completed)
|
---|
722 | End Function
|
---|
723 |
|
---|
724 | /*!
|
---|
725 | @overload Function GetBytes(chars As *WCHAR, charIndex As Long, charCount As Long, bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean) As Long
|
---|
726 | */
|
---|
727 | Function GetBytes(chars As *WCHAR, charIndex As Long, charCount As Long, bytes As *Byte, byteIndex As Long, byteCount As Long) As Long
|
---|
728 | GetBytes = GetBytes(chars, charIndex, charCount, bytes, byteIndex, byteCount, False)
|
---|
729 | End Function
|
---|
730 |
|
---|
731 | /*!
|
---|
732 | @brief フォールバックの取得
|
---|
733 | */
|
---|
734 | Function Fallback() As EncoderFallback
|
---|
735 | Return fallback
|
---|
736 | End Function
|
---|
737 |
|
---|
738 | /*!
|
---|
739 | @brief フォールバックの設定
|
---|
740 | */
|
---|
741 | Sub Fallback(newFallback As EncoderFallback)
|
---|
742 | If ActiveBasic.IsNothing(newFallback) Then
|
---|
743 | Throw New ArgumentNullException("newFallback")
|
---|
744 | End If
|
---|
745 | fallback = newFallback
|
---|
746 | End Sub
|
---|
747 | Protected
|
---|
748 | /*!
|
---|
749 | @brief 実際に変換するメソッド
|
---|
750 | @param[in] chars 入力
|
---|
751 | @param[in] charCount 入力要素数
|
---|
752 | @param[out] bytes 出力
|
---|
753 | @param[in] byteCount 出力要素数
|
---|
754 | @param[in] flush 終了後に内部状態を初期化するかどうか
|
---|
755 | @param[out] bytesUsed 使用された入力の要素数
|
---|
756 | @param[out] charsUsed 出力の内、実際に書き込まれた要素数
|
---|
757 | @param[out] completed 入力の全ての文字が変換に使われたかどうか
|
---|
758 | */
|
---|
759 | Abstract Sub ConvertCore(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long, flush As Boolean,
|
---|
760 | ByRef bytesUsed As Long, ByRef charsUsed As Long, ByRef completed As Boolean)
|
---|
761 |
|
---|
762 | Private
|
---|
763 | fallback As EncoderFallback
|
---|
764 | End Class
|
---|
765 |
|
---|
766 | Enum NormalizationForm
|
---|
767 | FormC
|
---|
768 | FormD
|
---|
769 | FormKC
|
---|
770 | FormKD
|
---|
771 | End Enum
|
---|
772 |
|
---|
773 | Class EncoderFallback
|
---|
774 | End Class
|
---|
775 |
|
---|
776 | End Namespace 'Text
|
---|
777 | End Namespace 'System
|
---|