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 WCHAR = 2バイト(UTF-16符号単位)、1 Byte = 1バイトの単位で指定する。
|
---|
16 |
|
---|
17 | */
|
---|
18 | Class Encoding
|
---|
19 | Implements ICloneable
|
---|
20 | Public
|
---|
21 | /*!
|
---|
22 | @brief 簡易的複製を作成する。
|
---|
23 | */
|
---|
24 | Virtual Function Clone() As Object
|
---|
25 | Clone = This
|
---|
26 | End Function
|
---|
27 |
|
---|
28 | ' Override Function Equals(y As Object) As Boolean
|
---|
29 | ' End Function
|
---|
30 |
|
---|
31 | ' Override Function GetHashCode() As Long
|
---|
32 | ' End Function
|
---|
33 |
|
---|
34 | Public
|
---|
35 | Sub Encoding()
|
---|
36 | End Sub
|
---|
37 |
|
---|
38 | /*!
|
---|
39 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
40 | @param[in] src 対象文字列
|
---|
41 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
42 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
43 | @date 2007/12/08
|
---|
44 | */
|
---|
45 | Function GetBytesCount(src As *WCHAR, srcCount As Long) As Long
|
---|
46 | If src = 0 Then
|
---|
47 | Throw New ArgumentNullException("src")
|
---|
48 | ElseIf srcCount < 0 Then
|
---|
49 | Throw New ArgumentOutOfRangeException("srcCount")
|
---|
50 | End If
|
---|
51 | Return GetBytesCountCore(src, srcCount)
|
---|
52 | End Function
|
---|
53 | #ifdef UNICODE
|
---|
54 | /*!
|
---|
55 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
56 | @param[in] src 対象文字列
|
---|
57 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
58 | @date 2007/12/08
|
---|
59 | */
|
---|
60 | Function GetBytesCount(src As String) As Long
|
---|
61 | If ActiveBasic.IsNothing(src) Then
|
---|
62 | Throw New ArgumentNullException("src")
|
---|
63 | End If
|
---|
64 | Return GetBytesCountCore(StrPtr(src), src.Length)
|
---|
65 | End Function
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | Protected
|
---|
69 | /*!
|
---|
70 | @brief GetBytesCountの実装を行う。
|
---|
71 | @param[in] src 対象文字列
|
---|
72 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
73 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
74 | @date 2007/12/08
|
---|
75 | */
|
---|
76 | Abstract Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
|
---|
77 | Public
|
---|
78 | /*!
|
---|
79 | @brief 符号化する。
|
---|
80 | @param[in] src 入力
|
---|
81 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
82 | @param[out] dst 出力
|
---|
83 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
84 | @return dstに書き込まれた要素数
|
---|
85 | @date 2007/12/08
|
---|
86 | */
|
---|
87 | Function GetBytes(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
|
---|
88 | If src = 0 Then
|
---|
89 | Throw New ArgumentNullException("src")
|
---|
90 | ElseIf dst = 0 Then
|
---|
91 | Throw New ArgumentNullException("dst")
|
---|
92 | ElseIf srcCount < 0 Then
|
---|
93 | Throw New ArgumentOutOfRangeException("srcCount")
|
---|
94 | ElseIf dstCount < 0 Then
|
---|
95 | Throw New ArgumentOutOfRangeException("dstCount")
|
---|
96 | End If
|
---|
97 |
|
---|
98 | Return GetBytesCore(src, srcCount, dst, dstCount)
|
---|
99 | End Function
|
---|
100 |
|
---|
101 | Protected
|
---|
102 | /*!
|
---|
103 | @brief GetBytesの処理を行う。
|
---|
104 | @param[in] src 入力
|
---|
105 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
106 | @param[out] dst 出力
|
---|
107 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
108 | @return dstに書き込まれた要素数
|
---|
109 | @exception ArgumentException バッファの大きさが足りない
|
---|
110 | @date 2007/12/08
|
---|
111 | */
|
---|
112 | Abstract Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
|
---|
113 | Public
|
---|
114 | /*!
|
---|
115 | @brief 復号して得られる文字列の長さを計算する。
|
---|
116 | @param[in] src 対象文字列
|
---|
117 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
118 | @return 復号して得られる文字列の長さ(要素数単位)
|
---|
119 | @date 2007/12/08
|
---|
120 | */
|
---|
121 | Function GetCharsCount(src As *Byte, srcCount As Long) As Long
|
---|
122 | If src = 0 Then
|
---|
123 | Throw New ArgumentNullException("src")
|
---|
124 | ElseIf srcCount < 0 Then
|
---|
125 | Throw New ArgumentOutOfRangeException("srcCount")
|
---|
126 | End If
|
---|
127 | Return GetCharsCountCore(src, srcCount)
|
---|
128 | End Function
|
---|
129 |
|
---|
130 | Protected
|
---|
131 | /*!
|
---|
132 | @brief GetCharsCountの処理を行う。
|
---|
133 | @param[in] src 対象文字列
|
---|
134 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
135 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
136 | @date 2007/12/08
|
---|
137 | */
|
---|
138 | Abstract Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
|
---|
139 | Public
|
---|
140 | /*!
|
---|
141 | @brief 復号する。
|
---|
142 | @param[in] src 入力
|
---|
143 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
144 | @param[out] dst 出力
|
---|
145 | @param[in] dstCount srcのバッファの大きさ(要素数単位)
|
---|
146 | @return dstに書き込まれた要素数
|
---|
147 | @date 2007/12/08
|
---|
148 | */
|
---|
149 | Function GetChars(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
|
---|
150 | If dst = 0 Then
|
---|
151 | Throw New ArgumentNullException("dst")
|
---|
152 | ElseIf src = 0 Then
|
---|
153 | Throw New ArgumentNullException("src")
|
---|
154 | ElseIf dstCount < 0 Then
|
---|
155 | Throw New ArgumentOutOfRangeException("dstCount")
|
---|
156 | ElseIf srcCount < 0 Then
|
---|
157 | Throw New ArgumentOutOfRangeException("srcCount")
|
---|
158 | End If
|
---|
159 |
|
---|
160 | Return GetCharsCore(src, srcCount, dst, dstCount)
|
---|
161 | End Function
|
---|
162 |
|
---|
163 | Protected
|
---|
164 | /*!
|
---|
165 | @brief GetCharsの処理を行う。
|
---|
166 | @param[in] src 入力
|
---|
167 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
168 | @param[out] dst 出力
|
---|
169 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
170 | @return dstに書き込まれた要素数
|
---|
171 | @exception ArgumentException バッファの大きさが足りない
|
---|
172 | @date 2007/12/08
|
---|
173 | */
|
---|
174 | Abstract Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
|
---|
175 | Public
|
---|
176 | #ifdef UNICODE
|
---|
177 | /*!
|
---|
178 | @brief 復号し、Stringで結果を返す。
|
---|
179 | @param[in] src 入力
|
---|
180 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
181 | @return 変換結果の文字列
|
---|
182 | @date 2007/12/08
|
---|
183 | */
|
---|
184 | Function GetString(src As *Byte, srcCount As Long) As String
|
---|
185 | If src = 0 Then
|
---|
186 | Throw New ArgumentNullException("src")
|
---|
187 | ElseIf srcCount < 0 Then
|
---|
188 | Throw New ArgumentOutOfRangeException("srcCount")
|
---|
189 | End If
|
---|
190 | GetString = getStringCore(src, srcCount)
|
---|
191 | End Function
|
---|
192 |
|
---|
193 | Private
|
---|
194 | Function getStringCore(src As *Byte, srcCount As Long) As String
|
---|
195 | Dim sb = New StringBuilder
|
---|
196 | Dim bufCount = GetMaxCharCount(srcCount)
|
---|
197 | sb.Length = bufCount
|
---|
198 | Dim len = GetCharsCore(src, srcCount, StrPtr(sb), bufCount)
|
---|
199 | sb.Length = len
|
---|
200 | getStringCore = sb.ToString()
|
---|
201 | End Function
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | Public
|
---|
205 | /*!
|
---|
206 | @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
|
---|
207 | */
|
---|
208 | Abstract Function GetMaxByteCount(srcCount As Long) As Long
|
---|
209 |
|
---|
210 | /*!
|
---|
211 | @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
|
---|
212 | */
|
---|
213 | Abstract Function GetMaxCharCount(srcCount As Long) As Long
|
---|
214 |
|
---|
215 | /*!
|
---|
216 | @brief 符号化された文字列の先頭につける識別文字列の取得
|
---|
217 | ようするにUTFのBOM。
|
---|
218 | */
|
---|
219 | Virtual Function GetPreamble() As *Byte
|
---|
220 | Return 0
|
---|
221 | End Function
|
---|
222 |
|
---|
223 | /*!
|
---|
224 | @brief GetPreambleの配列の要素数。
|
---|
225 | */
|
---|
226 | Virtual Function GetPreambleLength() As Long
|
---|
227 | Return 0
|
---|
228 | End Function
|
---|
229 |
|
---|
230 | /*!
|
---|
231 | @brief 与えられたバイト文字列から2文字目の開始地点を探す。
|
---|
232 | @param[in] s (マルチバイト)文字列
|
---|
233 | @param[in] count sの要素数
|
---|
234 | @return 2文字目の開始位置。2文字が存在しない場合、sを返す。
|
---|
235 | */
|
---|
236 | Function NextChar(s As *Byte, count As Long) As *Byte
|
---|
237 | If count = 0 Then
|
---|
238 | NextChar = s
|
---|
239 | ElseIf s = 0 Then
|
---|
240 | Throw New ArgumentNullException("s")
|
---|
241 | ElseIf count < 0 Then
|
---|
242 | Throw New ArgumentException("count")
|
---|
243 | Else
|
---|
244 | NextChar = NextCharCore(s, count)
|
---|
245 | End If
|
---|
246 | End Function
|
---|
247 | Protected
|
---|
248 | /*!
|
---|
249 | @brief NextCharの処理を行う。
|
---|
250 | @param[in] s (マルチバイト)文字列
|
---|
251 | @param[in] count sの要素数
|
---|
252 | @return 2文字目の開始位置。2文字が存在しない場合、sを返す。
|
---|
253 | */
|
---|
254 | Abstract Function NextCharCore(s As *Byte, count As Long) As *Byte
|
---|
255 |
|
---|
256 | Public
|
---|
257 | ' Abstract Function BodyName() As String
|
---|
258 | ' Abstract Function HeaderName() As String
|
---|
259 |
|
---|
260 | /*!
|
---|
261 | @brief コードページの取得。
|
---|
262 | */
|
---|
263 | ' Abstract Function CodePage() As Long
|
---|
264 | /*!
|
---|
265 | @brief 最も厳密に対応するWindowsコードページの取得。
|
---|
266 | */
|
---|
267 | ' Abstract Function WindowsCodePage() As Long
|
---|
268 |
|
---|
269 | Public
|
---|
270 | /*!
|
---|
271 | @brief この符号化形式の名前の取得。
|
---|
272 | */
|
---|
273 | ' Abstract Function EncodingName() As String
|
---|
274 |
|
---|
275 | /*!
|
---|
276 | @brief この符号化形式のIANA登録名の取得。
|
---|
277 | */
|
---|
278 | ' Abstract Function WebName() As String
|
---|
279 |
|
---|
280 | ' Abstract Function IsBrowserDisplay() As Boolean
|
---|
281 | ' Abstract Function IsBrowserSave() As Boolean
|
---|
282 | ' Abstract Function IsMailNewsDisplay() As Boolean
|
---|
283 | ' Abstract Function IsMailNewsSave() As Boolean
|
---|
284 |
|
---|
285 | /*!
|
---|
286 | @brief この符号化形式が、1バイト文字だけを使う(複数バイト文字を使わない)かどうか。
|
---|
287 | */
|
---|
288 | ' Abstract Function IsSingleByte() As Boolean
|
---|
289 |
|
---|
290 | 'GetPreamble
|
---|
291 |
|
---|
292 | /*!
|
---|
293 | @brief ある符号化文字列から別の符号化文字列へ変換する。
|
---|
294 | @param[in] srcEncoding 入力の符号化方式
|
---|
295 | @param[in] dstEncoding 出力の符号化方式
|
---|
296 | @param[in] bytes 入力文字列
|
---|
297 | @param[in] size バイト単位での文字列の長さ
|
---|
298 | @return 出力文字列
|
---|
299 | @exception ArgumentNullException srcEncoding, dstEncoding, bytesの少なくとも1つ以上がNothing/NULLのとき。
|
---|
300 | @exception ArgumentOutOfRangeException sizeが明らかに範囲外(負の値など)のとき。
|
---|
301 | */
|
---|
302 | ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, size As Long) As *Byte
|
---|
303 | ' End Function
|
---|
304 |
|
---|
305 | ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte
|
---|
306 | ' End Function
|
---|
307 |
|
---|
308 | /*!
|
---|
309 | @brief 指定したコードページ用のEncodingインスタンスの取得。
|
---|
310 | */
|
---|
311 | ' Static Function GetEncoding(codepage As Long) As Encoding
|
---|
312 | ' End Function
|
---|
313 | /*!
|
---|
314 | @brief 指定した符号化形式名用のEncodingインスタンスの取得。
|
---|
315 | */
|
---|
316 | ' Static Function GetEncoding(name As String) As Encoding
|
---|
317 | ' End Function
|
---|
318 | /*!
|
---|
319 | @brief システムで標準のANSIコードページ用のEncodingインスタンスの取得。
|
---|
320 | */
|
---|
321 | Static Function Default() As Encoding
|
---|
322 | End Function
|
---|
323 | /*!
|
---|
324 | @brief UTF-8用のEncodingインスタンスの取得。
|
---|
325 | */
|
---|
326 | Static Function UTF8() As Encoding
|
---|
327 | End Function
|
---|
328 | /*!
|
---|
329 | @brief UTF-16LE用のEncodingインスタンスの取得。
|
---|
330 | */
|
---|
331 | Static Function UTF16() As Encoding
|
---|
332 | End Function
|
---|
333 | /*!
|
---|
334 | @brief UTF-16BE用のEncodingインスタンスの取得。
|
---|
335 | */
|
---|
336 | Static Function UTF16BE() As Encoding
|
---|
337 | End Function
|
---|
338 | End Class
|
---|
339 |
|
---|
340 | Enum NormalizationForm
|
---|
341 | FormC
|
---|
342 | FormD
|
---|
343 | FormKC
|
---|
344 | FormKD
|
---|
345 | End Enum
|
---|
346 |
|
---|
347 | Namespace Detail
|
---|
348 |
|
---|
349 | /*!
|
---|
350 | @brief WideCharToMultiByte/MultiByteToWideCharで変換を行うエンコーディング。
|
---|
351 | DBCS/SBCS限定。UTF-8やUTF-7は非対応。
|
---|
352 | */
|
---|
353 | Class WindowsCodePageEncoding
|
---|
354 | Inherits Encoding
|
---|
355 | Public
|
---|
356 | Sub WindowsCodePageEncoding(codepage As DWord)
|
---|
357 | cp = codepage
|
---|
358 | End Sub
|
---|
359 |
|
---|
360 | Override Function GetHashCode() As Long
|
---|
361 | GetHashCode = cp As Long
|
---|
362 | End Function
|
---|
363 |
|
---|
364 | Override Function Equals(x As Object) As Boolean
|
---|
365 | If GetType().Equals(x.GetType()) Then
|
---|
366 | Dim xe = x As WindowsCodePageEncoding
|
---|
367 | Equals = cp = xe.cp
|
---|
368 | Else
|
---|
369 | Equals = False
|
---|
370 | End If
|
---|
371 | End Function
|
---|
372 |
|
---|
373 | /*!
|
---|
374 | @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
|
---|
375 | */
|
---|
376 | Override Function GetMaxByteCount(srcCount As Long) As Long
|
---|
377 | GetMaxByteCount = srcCount * 2
|
---|
378 | End Function
|
---|
379 |
|
---|
380 | /*!
|
---|
381 | @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
|
---|
382 | */
|
---|
383 | Override Function GetMaxCharCount(srcCount As Long) As Long
|
---|
384 | GetMaxCharCount = srcCount
|
---|
385 | End Function
|
---|
386 |
|
---|
387 | Protected
|
---|
388 | Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
|
---|
389 | GetBytesCountCore = WideCharToMultiByte(cp, 0, src, srcCount, 0, 0, 0, 0)
|
---|
390 | If srcCount <> 0 And GetBytesCountCore = 0 Then
|
---|
391 | ActiveBasic.Windows.ThrowWithLastError()
|
---|
392 | End If
|
---|
393 | End Function
|
---|
394 |
|
---|
395 | Override Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
|
---|
396 | GetBytesCore = WideCharToMultiByte(cp, 0, src, srcCount, dst As PCSTR, dstCount, 0, 0)
|
---|
397 | If srcCount <> 0 And GetBytesCore = 0 Then
|
---|
398 | ActiveBasic.Windows.ThrowWithLastError()
|
---|
399 | End If
|
---|
400 | End Function
|
---|
401 |
|
---|
402 | Override Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
|
---|
403 | GetCharsCountCore = MultiByteToWideChar(cp, 0, src As PCSTR, srcCount, 0, 0)
|
---|
404 | If srcCount <> 0 And GetCharsCountCore = 0 Then
|
---|
405 | ActiveBasic.Windows.ThrowWithLastError()
|
---|
406 | End If
|
---|
407 | End Function
|
---|
408 |
|
---|
409 | Override Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
|
---|
410 | GetCharsCore = MultiByteToWideChar(cp, 0, src As PCSTR, srcCount, dst, dstCount)
|
---|
411 | If srcCount <> 0 And GetCharsCore = 0 Then
|
---|
412 | ActiveBasic.Windows.ThrowWithLastError()
|
---|
413 | End If
|
---|
414 | End Function
|
---|
415 |
|
---|
416 | Override Function NextCharCore(s As *Byte, count As Long) As *Byte
|
---|
417 | NextCharCore = CharNextExA(cp As Word, s As PCSTR, 0) As *Byte
|
---|
418 | End Function
|
---|
419 |
|
---|
420 | Private
|
---|
421 | cp As DWord
|
---|
422 | End Class
|
---|
423 |
|
---|
424 | End Namespace
|
---|
425 |
|
---|
426 | End Namespace 'Text
|
---|
427 | End Namespace 'System
|
---|