Changeset 673 for branch/egtra-stream-without-en_dec/Classes/System/Text
- Timestamp:
- Jan 5, 2009, 1:40:11 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/egtra-stream-without-en_dec/Classes/System/Text/Encoding.ab
r655 r673 39 39 @brief 符号化して得られる文字列の長さを計算する。 40 40 @param[in] src 対象文字列 41 @param[in] srcCount srcの長さ 42 @return 符号化して得られる文字列の長さ 41 @param[in] srcCount srcの長さ(要素数単位) 42 @return 符号化して得られる文字列の長さ(要素数単位) 43 43 @date 2007/12/08 44 44 */ … … 55 55 @brief 符号化して得られる文字列の長さを計算する。 56 56 @param[in] src 対象文字列 57 @return 符号化して得られる文字列の長さ 57 @return 符号化して得られる文字列の長さ(要素数単位) 58 58 @date 2007/12/08 59 59 */ … … 66 66 #endif 67 67 68 Pr ivate68 Protected 69 69 /*! 70 70 @brief GetBytesCountの実装を行う。 71 71 @param[in] src 対象文字列 72 @param[in] srcCount srcの長さ 73 @return 符号化して得られる文字列の長さ 74 @date 2007/12/08 75 */ 76 Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long 77 Dim enc = GetEncoder() 78 GetBytesCountCore = enc.GetBytesCount(src, srcCount, True) 79 End Function 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 80 77 Public 81 78 /*! 82 79 @brief 符号化する。 83 80 @param[in] src 入力 84 @param[in] srcCount srcの長さ 81 @param[in] srcCount srcの長さ(要素数単位) 85 82 @param[out] dst 出力 86 @param[in] dstCount dstのバッファの大きさ 87 @return dstに書き込まれた バイト数83 @param[in] dstCount dstのバッファの大きさ(要素数単位) 84 @return dstに書き込まれた要素数 88 85 @date 2007/12/08 89 86 */ … … 102 99 End Function 103 100 104 Pr ivate101 Protected 105 102 /*! 106 103 @brief GetBytesの処理を行う。 107 104 @param[in] src 入力 108 @param[in] srcCount srcの長さ 105 @param[in] srcCount srcの長さ(要素数単位) 109 106 @param[out] dst 出力 110 @param[in] dstCount dstのバッファの大きさ 111 @return dstに書き込まれた バイト数107 @param[in] dstCount dstのバッファの大きさ(要素数単位) 108 @return dstに書き込まれた要素数 112 109 @exception ArgumentException バッファの大きさが足りない 113 110 @date 2007/12/08 114 111 */ 115 Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long 116 GetBytesCore = GetEncoder().GetBytes(src, srcCount, dst, dstCount, True) 117 End Function 112 Abstract Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long 118 113 Public 119 114 /*! 120 115 @brief 復号して得られる文字列の長さを計算する。 121 116 @param[in] src 対象文字列 122 @param[in] srcCount srcの長さ 123 @return 復号して得られる文字列の長さ 117 @param[in] srcCount srcの長さ(要素数単位) 118 @return 復号して得られる文字列の長さ(要素数単位) 124 119 @date 2007/12/08 125 120 */ … … 133 128 End Function 134 129 135 Pr ivate130 Protected 136 131 /*! 137 132 @brief GetCharsCountの処理を行う。 138 133 @param[in] src 対象文字列 139 @param[in] srcCount srcの長さ 140 @return 符号化して得られる文字列の長さ 141 @date 2007/12/08 142 */ 143 Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long 144 Dim dec = GetDecoder() 145 GetCharsCountCore = dec.GetCharsCount(src, srcCount, True) 146 End Function 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 147 139 Public 148 140 /*! 149 141 @brief 復号する。 150 142 @param[in] src 入力 151 @param[in] srcCount srcの長さ 143 @param[in] srcCount srcの長さ(要素数単位) 152 144 @param[out] dst 出力 153 @param[in] dstCount srcのバッファの大きさ 154 @return dstに書き込まれた バイト数145 @param[in] dstCount srcのバッファの大きさ(要素数単位) 146 @return dstに書き込まれた要素数 155 147 @date 2007/12/08 156 148 */ … … 169 161 End Function 170 162 171 Pr ivate163 Protected 172 164 /*! 173 165 @brief GetCharsの処理を行う。 174 166 @param[in] src 入力 175 @param[in] srcCount srcの長さ 167 @param[in] srcCount srcの長さ(要素数単位) 176 168 @param[out] dst 出力 177 @param[in] dstCount dstのバッファの大きさ 178 @return dstに書き込まれた バイト数169 @param[in] dstCount dstのバッファの大きさ(要素数単位) 170 @return dstに書き込まれた要素数 179 171 @exception ArgumentException バッファの大きさが足りない 180 172 @date 2007/12/08 181 173 */ 182 Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long 183 GetCharsCore = GetDecoder().GetChars(src, srcCount, dst, dstCount, True) 184 End Function 174 Abstract Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long 185 175 Public 186 176 #ifdef UNICODE … … 188 178 @brief 復号し、Stringで結果を返す。 189 179 @param[in] src 入力 190 @param[in] srcCount srcの長さ 180 @param[in] srcCount srcの長さ(要素数単位) 191 181 @return 変換結果の文字列 192 182 @date 2007/12/08 … … 214 204 Public 215 205 /*! 216 @brief 符号器を取得する。217 */218 Abstract Function GetDecoder() As Decoder219 220 /*!221 @brief 復号器を取得する。222 */223 Abstract Function GetEncoder() As Encoder224 225 /*!226 206 @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。 227 207 */ … … 242 222 243 223 /*! 244 @brief GetPreambleの配列の要素数 224 @brief GetPreambleの配列の要素数。 245 225 */ 246 226 Virtual Function GetPreambleLength() As Long … … 248 228 End Function 249 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 250 257 ' Abstract Function BodyName() As String 251 258 ' Abstract Function HeaderName() As String … … 331 338 End Class 332 339 333 /*!334 @brief 復号を行うクラス335 @date 2007/12/19336 @auther Egtra337 */338 Class Decoder339 Public340 /*!341 @brief 変換する342 @param[in] src 入力343 @param[in] srcCount 入力要素数344 @param[out] dst 出力345 @param[in] dstCount 出力要素数346 @param[in] flush 終了後に内部状態を初期化するかどうか347 @param[out] srcUsed 使用された入力の要素数348 @param[out] dstUsed 出力の内、実際に書き込まれた要素数349 @param[out] completed 入力の全ての文字が変換に使われたかどうか350 */351 Sub Convert(src As *Byte, srcCount As Long,352 dst As *WCHAR, dstCount As Long, flush As Boolean,353 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean)354 355 If src = 0 And srcCount > 0 Then356 Throw New ArgumentNullException("src")357 ElseIf srcCount < 0 Then358 Throw New ArgumentOutOfRangeException("srcCount")359 ElseIf dst = 0 Then360 Throw New ArgumentNullException("dst")361 ElseIf dstCount < 0 Then362 Throw New ArgumentOutOfRangeException("dstCount")363 End If364 ConvertCore(src, srcCount, dst, dstCount, flush, srcUsed, dstUsed, completed)365 End Sub366 367 /*!368 @brief 変換する369 @param[in] src 入力370 @param[in] srcCount 入力要素数371 @param[out] dst 出力372 @param[in] dstCount 出力要素数373 @param[in] flush 終了後に内部状態を初期化するかどうか374 @return 出力の内、実際に書き込まれた要素数375 */376 Function GetChars(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean) As Long377 Dim srcUsed As Long378 Dim completed As Boolean379 Convert(src, srcCount, dst, dstCount, flush, srcUsed, GetChars, completed)380 If srcUsed < srcCount Then381 Throw New ArgumentException("src", "buffer is too small")382 End If383 End Function384 385 /*!386 @brief 変換すると何文字になるか数える387 @param[in] src 入力388 @param[in] srcCount 入力要素数389 @param[in] flush 終了後に内部状態を初期化するとして計算するかどうか390 @return 必要な文字数391 */392 Function GetCharsCount(src As *Byte, srcCount As Long, flush As Boolean) As Long393 If src = 0 Then394 Throw New ArgumentNullException("src")395 ElseIf srcCount < 0 Then396 Throw New ArgumentOutOfRangeException("srcCount")397 End If398 GetCharsCountCore(src, srcCount, flush)399 End Function400 401 /*!402 @brief 内部状態を初期状態に戻す403 */404 Virtual Sub Reset()405 End Sub406 407 Protected408 /*!409 @brief 実際に変換する410 @param[in] src 入力411 @param[in] srcCount 入力要素数412 @param[out] dst 出力413 @param[in] dstCount 出力要素数414 @param[in] flush 終了後に内部状態を初期化するかどうか415 @param[out] dstUsed 使用された入力の要素数416 @param[out] srcUsed 出力の内、実際に書き込まれた要素数417 @param[out] completed 入力の全ての文字が変換に使われたかどうか418 */419 Abstract Sub ConvertCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean,420 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean)421 422 /*!423 @brief 変換すると何文字になるか数える424 @param[in] src 入力425 @param[in] srcCount 入力要素数426 @param[in] flush 終了後に内部状態を初期化するとして計算するかどうか427 @return 必要な文字数428 */429 Abstract Function GetCharsCountCore(src As *Byte, srcCount As Long, flush As Boolean) As Long430 End Class431 432 /*!433 @brief 符号化を行うクラス434 @date 2007/12/19435 @auther Egtra436 */437 Class Encoder438 Public439 /*!440 @brief 変換する441 @param[in] src 入力442 @param[in] srcCount 入力要素数443 @param[out] dst 出力444 @param[in] dstCount 出力要素数445 @param[in] flush 終了後に内部状態を初期化するかどうか446 @param[out] srcUsed 使用された入力の要素数447 @param[out] dstUsed 出力の内、実際に書き込まれた要素数448 @param[out] completed 入力の全ての文字が変換に使われたかどうか449 */450 Sub Convert(src As *WCHAR, srcCount As Long,451 dst As *Byte, dstCount As Long, flush As Boolean,452 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean)453 454 If src = 0 And srcCount > 0 Then455 Throw New ArgumentNullException("src")456 ElseIf srcCount < 0 Then457 Throw New ArgumentOutOfRangeException("srcCount")458 ElseIf dst = 0 Then459 Throw New ArgumentNullException("dst")460 ElseIf dstCount < 0 Then461 Throw New ArgumentOutOfRangeException("dstCount")462 End If463 ConvertCore(src, srcCount, dst, dstCount, flush, srcUsed, dstUsed, completed)464 End Sub465 466 /*!467 @brief 変換する468 @param[in] src 入力469 @param[in] srcCount 入力要素数470 @param[out] dst 出力471 @param[in] dstCount 出力要素数472 @param[in] flush 終了後に内部状態を初期化するかどうか473 @return 出力の内、実際に書き込まれた要素数474 */475 Function GetBytes(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long, flush As Boolean) As Long476 Dim srcUsed As Long477 Dim completed As Boolean478 Convert(src, srcCount, dst, dstCount, flush, srcUsed, GetBytes, completed)479 If srcUsed < srcCount Then480 Throw New ArgumentException("src", "buffer is too small")481 End If482 End Function483 484 /*!485 @brief 変換すると何文字になるか数える486 @param[in] src 入力487 @param[in] srcCount 入力要素数488 @param[in] flush 終了後に内部状態を初期化するとして計算するかどうか489 @return 必要な文字数490 */491 Function GetBytesCount(src As *WCHAR, srcCount As Long, flush As Boolean) As Long492 If src = 0 Then493 Throw New ArgumentNullException("src")494 ElseIf srcCount < 0 Then495 Throw New ArgumentOutOfRangeException("srcCount")496 End If497 GetBytesCountCore(src, srcCount, flush)498 End Function499 500 /*!501 @brief 内部状態を初期状態に戻す502 */503 Virtual Sub Reset()504 End Sub505 506 Protected507 /*!508 @brief 実際に変換する509 @param[in] src 入力510 @param[in] srcCount 入力要素数511 @param[out] dst 出力512 @param[in] dstCount 出力要素数513 @param[in] flush 終了後に内部状態を初期化するかどうか514 @param[out] dstUsed 使用された入力の要素数515 @param[out] srcUsed 出力の内、実際に書き込まれた要素数516 @param[out] completed 入力の全ての文字が変換に使われたかどうか517 */518 Abstract Sub ConvertCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long, flush As Boolean,519 ByRef dstUsed As Long, ByRef srcUsed As Long, ByRef completed As Boolean)520 521 /*!522 @brief 変換すると何文字になるか数える523 @param[in] src 入力524 @param[in] srcCount 入力要素数525 @param[in] flush 終了後に内部状態を初期化するとして計算するかどうか526 @return 必要な文字数527 */528 Abstract Function GetBytesCountCore(src As *WCHAR, srcCount As Long, flush As Boolean) As Long529 End Class530 531 340 Enum NormalizationForm 532 341 FormC … … 549 358 End Sub 550 359 551 /*!552 @brief 符号器を取得する。553 */554 Override Function GetDecoder() As Decoder555 GetDecoder = New WindowsCodePageDecoder(cp)556 End Function557 558 /*!559 @brief 復号器を取得する。560 */561 Override Function GetEncoder() As Encoder562 GetEncoder = New WindowsCodePageEncoder(cp)563 End Function564 565 360 Override Function GetHashCode() As Long 566 361 GetHashCode = cp As Long … … 590 385 End Function 591 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 592 420 Private 593 421 cp As DWord 594 422 End Class 595 423 596 Class WindowsCodePageEncoder597 Inherits Encoder598 Public599 Sub WindowsCodePageEncoder(codepage As DWord)600 cp = codepage601 End Sub602 603 Override Sub Reset()604 nextByte = 0605 End Sub606 607 Protected608 Override Sub ConvertCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long, flush As Boolean,609 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean)610 611 Dim srcPos = 0 As Long612 Dim dstPos = 0 As Long613 If dstCount > 0 And nextByte <> 0 Then614 dst[0] = nextByte615 nextByte = 0616 dstPos++617 End If618 While srcPos < srcCount And dstPos < srcCount619 Dim buf[1] As CHAR620 Dim len = WideCharToMultiByte(cp, WC_COMPOSITECHECK Or WC_DEFAULTCHAR, VarPtr(src[srcPos]), 1, buf, Len(buf), 0, 0)621 If len = 0 Then622 ActiveBasic.Windows.ThrowWithLastError()623 End If624 dst[dstPos] = buf[0] As Byte625 If len = 2 Then626 If dstCount = 1 Then627 nextByte = buf[1] As Byte628 Exit While629 End If630 dstPos++631 dst[dstPos] = buf[1] As Byte632 nextByte = 0633 End If634 srcPos++635 dstPos++636 Wend637 srcUsed = srcPos638 dstUsed = dstPos639 completed = (srcPos = srcCount And dstPos = srcCount And nextByte = 0)640 End Sub641 642 Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long, flush As Boolean) As Long643 GetBytesCountCore = srcCount * 2 + 1 '暫定644 End Function645 646 Private647 cp As DWord648 nextByte As Byte649 End Class650 651 Class WindowsCodePageDecoder652 Inherits Decoder653 Public654 Sub WindowsCodePageDecoder(codepage As DWord)655 cp = codepage656 End Sub657 658 Protected659 Override Sub ConvertCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean,660 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean)661 662 Dim srcPos = 0 As Long663 Dim dstPos = 0 As Long664 If dstCount > 0 And nextByte <> 0 Then665 Dim buf[1] As CHAR666 buf[0] = nextByte As CHAR667 buf[1] = src[1] As CHAR668 Dim len = MultiByteToWideChar(cp, 0, buf, Len(buf), VarPtr(dst[dstPos]), 10)669 If len = 0 Then670 ActiveBasic.Windows.ThrowWithLastError()671 End If672 srcPos++673 dstPos++674 nextByte = 0675 End If676 While srcPos < srcCount And dstPos < srcCount677 Dim srcCharSize = 1 As Long678 If IsDBCSLeadByteEx(cp, src[srcPos]) Then679 srcCharSize = 2680 If srcPos + 1 = srcCount Then681 nextByte = src[srcPos]682 Exit While683 End If684 End If685 '将来的には行毎に変換しMB_USEGLYPHCHARSを使うようにしたい。686 Dim len = MultiByteToWideChar(cp, 0, VarPtr(src[srcPos]) As *CHAR, srcCharSize, VarPtr(dst[dstPos]), 1)687 If len = 0 Then688 ActiveBasic.Windows.ThrowWithLastError()689 End If690 srcPos += srcCharSize691 dstPos++692 Wend693 srcUsed = srcPos694 dstUsed = dstPos695 completed = (srcPos = srcCount And dstPos = srcCount And nextByte = 0)696 End Sub697 698 Override Function GetCharsCountCore(src As *Byte, srcCount As Long, flush As Boolean) As Long699 GetCharsCountCore = srcCount + 1 '暫定700 End Function701 702 Private703 cp As DWord704 nextByte As Byte705 End Class706 707 424 End Namespace 708 425
Note:
See TracChangeset
for help on using the changeset viewer.