[411] | 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 | なお、このクラスで、文字列の長さやバッファの大きさを指定するときには、
|
---|
[653] | 15 | 1 WCHAR = 2バイト(UTF-16符号単位)、1 Byte = 1バイトの単位で指定する。
|
---|
[411] | 16 |
|
---|
| 17 | */
|
---|
| 18 | Class Encoding
|
---|
| 19 | Implements ICloneable
|
---|
| 20 | Public
|
---|
| 21 | /*!
|
---|
| 22 | @brief 簡易的複製を作成する。
|
---|
| 23 | */
|
---|
[653] | 24 | Virtual Function Clone() As Object
|
---|
| 25 | Clone = This
|
---|
| 26 | End Function
|
---|
[411] | 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 符号化して得られる文字列の長さを計算する。
|
---|
[653] | 40 | @param[in] src 対象文字列
|
---|
[673] | 41 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
| 42 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
[411] | 43 | @date 2007/12/08
|
---|
| 44 | */
|
---|
[653] | 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")
|
---|
[411] | 50 | End If
|
---|
[653] | 51 | Return GetBytesCountCore(src, srcCount)
|
---|
[411] | 52 | End Function
|
---|
[497] | 53 | #ifdef UNICODE
|
---|
[411] | 54 | /*!
|
---|
| 55 | @brief 符号化して得られる文字列の長さを計算する。
|
---|
[653] | 56 | @param[in] src 対象文字列
|
---|
[673] | 57 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
[411] | 58 | @date 2007/12/08
|
---|
| 59 | */
|
---|
[653] | 60 | Function GetBytesCount(src As String) As Long
|
---|
| 61 | If ActiveBasic.IsNothing(src) Then
|
---|
| 62 | Throw New ArgumentNullException("src")
|
---|
[411] | 63 | End If
|
---|
[653] | 64 | Return GetBytesCountCore(StrPtr(src), src.Length)
|
---|
[411] | 65 | End Function
|
---|
| 66 | #endif
|
---|
[653] | 67 |
|
---|
[673] | 68 | Protected
|
---|
[411] | 69 | /*!
|
---|
[653] | 70 | @brief GetBytesCountの実装を行う。
|
---|
| 71 | @param[in] src 対象文字列
|
---|
[673] | 72 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
| 73 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
[411] | 74 | @date 2007/12/08
|
---|
| 75 | */
|
---|
[673] | 76 | Abstract Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
|
---|
[411] | 77 | Public
|
---|
| 78 | /*!
|
---|
| 79 | @brief 符号化する。
|
---|
[653] | 80 | @param[in] src 入力
|
---|
[673] | 81 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
[653] | 82 | @param[out] dst 出力
|
---|
[673] | 83 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
| 84 | @return dstに書き込まれた要素数
|
---|
[411] | 85 | @date 2007/12/08
|
---|
| 86 | */
|
---|
[653] | 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")
|
---|
[411] | 96 | End If
|
---|
| 97 |
|
---|
[653] | 98 | Return GetBytesCore(src, srcCount, dst, dstCount)
|
---|
[411] | 99 | End Function
|
---|
| 100 |
|
---|
[673] | 101 | Protected
|
---|
[411] | 102 | /*!
|
---|
| 103 | @brief GetBytesの処理を行う。
|
---|
[653] | 104 | @param[in] src 入力
|
---|
[673] | 105 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
[653] | 106 | @param[out] dst 出力
|
---|
[673] | 107 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
| 108 | @return dstに書き込まれた要素数
|
---|
[411] | 109 | @exception ArgumentException バッファの大きさが足りない
|
---|
| 110 | @date 2007/12/08
|
---|
| 111 | */
|
---|
[673] | 112 | Abstract Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
|
---|
[411] | 113 | Public
|
---|
| 114 | /*!
|
---|
| 115 | @brief 復号して得られる文字列の長さを計算する。
|
---|
[653] | 116 | @param[in] src 対象文字列
|
---|
[673] | 117 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
| 118 | @return 復号して得られる文字列の長さ(要素数単位)
|
---|
[411] | 119 | @date 2007/12/08
|
---|
| 120 | */
|
---|
[653] | 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")
|
---|
[411] | 126 | End If
|
---|
[653] | 127 | Return GetCharsCountCore(src, srcCount)
|
---|
[411] | 128 | End Function
|
---|
[653] | 129 |
|
---|
[673] | 130 | Protected
|
---|
[411] | 131 | /*!
|
---|
[653] | 132 | @brief GetCharsCountの処理を行う。
|
---|
| 133 | @param[in] src 対象文字列
|
---|
[673] | 134 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
| 135 | @return 符号化して得られる文字列の長さ(要素数単位)
|
---|
[411] | 136 | @date 2007/12/08
|
---|
| 137 | */
|
---|
[673] | 138 | Abstract Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
|
---|
[411] | 139 | Public
|
---|
| 140 | /*!
|
---|
| 141 | @brief 復号する。
|
---|
[653] | 142 | @param[in] src 入力
|
---|
[673] | 143 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
[653] | 144 | @param[out] dst 出力
|
---|
[673] | 145 | @param[in] dstCount srcのバッファの大きさ(要素数単位)
|
---|
| 146 | @return dstに書き込まれた要素数
|
---|
[411] | 147 | @date 2007/12/08
|
---|
| 148 | */
|
---|
[653] | 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")
|
---|
[411] | 158 | End If
|
---|
| 159 |
|
---|
[653] | 160 | Return GetCharsCore(src, srcCount, dst, dstCount)
|
---|
[411] | 161 | End Function
|
---|
| 162 |
|
---|
[673] | 163 | Protected
|
---|
[411] | 164 | /*!
|
---|
| 165 | @brief GetCharsの処理を行う。
|
---|
[653] | 166 | @param[in] src 入力
|
---|
[673] | 167 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
[653] | 168 | @param[out] dst 出力
|
---|
[673] | 169 | @param[in] dstCount dstのバッファの大きさ(要素数単位)
|
---|
| 170 | @return dstに書き込まれた要素数
|
---|
[411] | 171 | @exception ArgumentException バッファの大きさが足りない
|
---|
| 172 | @date 2007/12/08
|
---|
| 173 | */
|
---|
[673] | 174 | Abstract Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
|
---|
[411] | 175 | Public
|
---|
[497] | 176 | #ifdef UNICODE
|
---|
[411] | 177 | /*!
|
---|
| 178 | @brief 復号し、Stringで結果を返す。
|
---|
[653] | 179 | @param[in] src 入力
|
---|
[673] | 180 | @param[in] srcCount srcの長さ(要素数単位)
|
---|
[411] | 181 | @return 変換結果の文字列
|
---|
| 182 | @date 2007/12/08
|
---|
| 183 | */
|
---|
[653] | 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")
|
---|
[411] | 189 | End If
|
---|
[653] | 190 | GetString = getStringCore(src, srcCount)
|
---|
[411] | 191 | End Function
|
---|
[653] | 192 |
|
---|
[411] | 193 | Private
|
---|
[653] | 194 | Function getStringCore(src As *Byte, srcCount As Long) As String
|
---|
[411] | 195 | Dim sb = New StringBuilder
|
---|
[653] | 196 | Dim bufCount = GetMaxCharCount(srcCount)
|
---|
| 197 | sb.Length = bufCount
|
---|
| 198 | Dim len = GetCharsCore(src, srcCount, StrPtr(sb), bufCount)
|
---|
[411] | 199 | sb.Length = len
|
---|
[653] | 200 | getStringCore = sb.ToString()
|
---|
[411] | 201 | End Function
|
---|
| 202 | #endif
|
---|
| 203 |
|
---|
| 204 | Public
|
---|
| 205 | /*!
|
---|
| 206 | @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
|
---|
| 207 | */
|
---|
[653] | 208 | Abstract Function GetMaxByteCount(srcCount As Long) As Long
|
---|
[411] | 209 |
|
---|
| 210 | /*!
|
---|
| 211 | @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
|
---|
| 212 | */
|
---|
[653] | 213 | Abstract Function GetMaxCharCount(srcCount As Long) As Long
|
---|
[411] | 214 |
|
---|
| 215 | /*!
|
---|
| 216 | @brief 符号化された文字列の先頭につける識別文字列の取得
|
---|
[653] | 217 | ようするにUTFのBOM。
|
---|
[411] | 218 | */
|
---|
| 219 | Virtual Function GetPreamble() As *Byte
|
---|
| 220 | Return 0
|
---|
| 221 | End Function
|
---|
| 222 |
|
---|
| 223 | /*!
|
---|
[673] | 224 | @brief GetPreambleの配列の要素数。
|
---|
[411] | 225 | */
|
---|
| 226 | Virtual Function GetPreambleLength() As Long
|
---|
| 227 | Return 0
|
---|
| 228 | End Function
|
---|
| 229 |
|
---|
[673] | 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
|
---|
[653] | 257 | ' Abstract Function BodyName() As String
|
---|
| 258 | ' Abstract Function HeaderName() As String
|
---|
[411] | 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 | */
|
---|
[653] | 273 | ' Abstract Function EncodingName() As String
|
---|
[411] | 274 |
|
---|
| 275 | /*!
|
---|
| 276 | @brief この符号化形式のIANA登録名の取得。
|
---|
| 277 | */
|
---|
[653] | 278 | ' Abstract Function WebName() As String
|
---|
[411] | 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 | */
|
---|
[653] | 288 | ' Abstract Function IsSingleByte() As Boolean
|
---|
[411] | 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 | */
|
---|
[653] | 302 | ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, size As Long) As *Byte
|
---|
| 303 | ' End Function
|
---|
[411] | 304 |
|
---|
[653] | 305 | ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte
|
---|
| 306 | ' End Function
|
---|
[411] | 307 |
|
---|
| 308 | /*!
|
---|
| 309 | @brief 指定したコードページ用のEncodingインスタンスの取得。
|
---|
| 310 | */
|
---|
[653] | 311 | ' Static Function GetEncoding(codepage As Long) As Encoding
|
---|
[411] | 312 | ' End Function
|
---|
| 313 | /*!
|
---|
| 314 | @brief 指定した符号化形式名用のEncodingインスタンスの取得。
|
---|
| 315 | */
|
---|
[653] | 316 | ' Static Function GetEncoding(name As String) As Encoding
|
---|
[411] | 317 | ' End Function
|
---|
| 318 | /*!
|
---|
[653] | 319 | @brief システムで標準のANSIコードページ用のEncodingインスタンスの取得。
|
---|
[411] | 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 |
|
---|
[653] | 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 |
|
---|
[411] | 373 | /*!
|
---|
[653] | 374 | @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
|
---|
[411] | 375 | */
|
---|
[653] | 376 | Override Function GetMaxByteCount(srcCount As Long) As Long
|
---|
| 377 | GetMaxByteCount = srcCount * 2
|
---|
[411] | 378 | End Function
|
---|
| 379 |
|
---|
| 380 | /*!
|
---|
[653] | 381 | @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
|
---|
[411] | 382 | */
|
---|
[653] | 383 | Override Function GetMaxCharCount(srcCount As Long) As Long
|
---|
| 384 | GetMaxCharCount = srcCount
|
---|
| 385 | End Function
|
---|
| 386 |
|
---|
[411] | 387 | Protected
|
---|
[673] | 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
|
---|
[411] | 394 |
|
---|
[673] | 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()
|
---|
[653] | 399 | End If
|
---|
[673] | 400 | End Function
|
---|
[653] | 401 |
|
---|
[673] | 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
|
---|
[653] | 407 | End Function
|
---|
| 408 |
|
---|
[673] | 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()
|
---|
[655] | 413 | End If
|
---|
[673] | 414 | End Function
|
---|
[653] | 415 |
|
---|
[673] | 416 | Override Function NextCharCore(s As *Byte, count As Long) As *Byte
|
---|
| 417 | NextCharCore = CharNextExA(cp As Word, s As PCSTR, 0) As *Byte
|
---|
[653] | 418 | End Function
|
---|
| 419 |
|
---|
| 420 | Private
|
---|
| 421 | cp As DWord
|
---|
[411] | 422 | End Class
|
---|
| 423 |
|
---|
[653] | 424 | End Namespace
|
---|
| 425 |
|
---|
[411] | 426 | End Namespace 'Text
|
---|
| 427 | End Namespace 'System
|
---|