Changeset 653 for trunk/ab5.0
- Timestamp:
- Nov 3, 2008, 11:42:22 PM (16 years ago)
- Location:
- trunk/ab5.0/ablib
- Files:
-
- 1 added
- 1 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/TestCase/SimpleTestCase/EncodingTest.ab
r411 r653 1 1 Imports System.Text 2 2 Imports System.Text.Detail 3 3 /* 4 4 Namespace EncodingTest 5 5 … … 40 40 41 41 EncodingTest.TestMain() 42 */ -
trunk/ab5.0/ablib/src/Classes/System/IO/BinaryReader.ab
r553 r653 1 Name Space System2 Name Space IO1 Namespace System 2 Namespace IO 3 3 4 4 Class BinaryReader … … 13 13 Sub BinaryReader(input As Stream) 14 14 stream = input 15 enc = New System.Text.UTF8Encoding()15 ' enc = New System.Text.UTF8Encoding() 16 16 End Sub 17 17 /*! -
trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab
r627 r653 41 41 42 42 Override Sub Flush() 43 Dim len = buf.Length 44 If len > 0 Then 45 s.Write(StrPtr(buf) As *Byte, 0, len) 46 buf.Length = 0 47 End If 48 End Sub 49 50 Override Sub Write(str As String) 51 buf.Append(str) 52 Dim len = buf.Length 53 If len >= 2048 Then 54 s.Write(StrPtr(buf) As *Byte, 0, len) 55 buf.Length = 0 56 End If 57 End Sub 58 59 Override Sub Write(x As Boolean) 60 buf.Append(x) 61 End Sub 62 63 Override Sub Write(x As Char) 64 buf.Append(x) 65 End Sub 66 67 Override Sub Write(x As Byte) 68 buf.Append(x) 69 End Sub 70 #ifdef UNICODE 71 Override Sub Write(x As SByte) 72 buf.Append(x) 73 End Sub 74 #else 75 Override Sub Write(x As Word) 76 buf.Append(x) 77 End Sub 78 #endif 79 Override Sub Write(x As Integer) 80 buf.Append(x) 81 End Sub 82 83 Override Sub Write(x As DWord) 84 buf.Append(x) 85 End Sub 86 87 Override Sub Write(x As Long) 88 buf.Append(x) 89 End Sub 90 91 Override Sub Write(x As QWord) 92 buf.Append(x) 93 End Sub 94 95 Override Sub Write(x As Int64) 96 buf.Append(x) 97 End Sub 98 99 Override Sub Write(x As Single) 100 buf.Append(x) 101 End Sub 102 103 Override Sub Write(x As Double) 104 buf.Append(x) 105 End Sub 106 107 Override Sub Write(x As Object) 108 Write(x.ToString) 43 Dim buf = Buffer() 44 Dim pws As PCWSTR 45 Dim size = GetWCStr(buf.ToString(), pws) 46 Dim pwsEnd = VarPtr(pws[size]) 47 Dim charConverted As Long 48 Dim byteBuf[4095] As Byte 49 Dim byteSize As Long 50 Dim completed As Boolean 51 Do 52 Dim converted As Long 53 encoder.Convert(pws, size, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed) 54 s.Write(byteBuf, 0, byteSize) 55 pws = VarPtr(pws[charConverted]) 56 size -= charConverted 57 Loop Until pws = pwsEnd 58 buf.Length = 0 109 59 End Sub 110 60 … … 112 62 Override Sub Dispose(disposing As Boolean) 113 63 If disposing Then 114 Dim len = buf.Length 115 If len > 0 Then 116 s.Write(StrPtr(buf) As *Byte, 0, len) 117 End If 64 Flush() 65 flushLast() 118 66 s.Dispose() 119 67 End If … … 123 71 Sub init(stream As Stream) 124 72 s = stream 125 buf = New System.Text.StringBuilder(4096) 73 buf = New Text.StringBuilder(4096) 74 '暫定。正式版ではUTF-8を標準とする。 75 encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP) 76 encoder = encoding.GetEncoder() 126 77 End Sub 127 78 128 buf As Text.StringBuilder 79 Sub flushLast() 80 Dim charConverted As Long 81 Dim byteBuf[63] As Byte 82 Dim byteSize As Long 83 Dim completed As Boolean 84 Do 85 encoder.Convert(0, 0, byteBuf, Len(byteBuf), False, charConverted, byteSize, completed) 86 s.Write(byteBuf, 0, byteSize) 87 Loop Until completed 88 End Sub 89 90 encoding As Text.Encoding 91 encoder As Text.Encoder 129 92 s As System.IO.Stream 130 93 End Class -
trunk/ab5.0/ablib/src/Classes/System/IO/TextWriter.ab
r523 r653 10 10 */ 11 11 Class TextWriter 12 Implements System.IDisposable 12 13 Public 13 14 Virtual Sub ~TextWriter() … … 26 27 End Sub 27 28 28 Sub TextWriter()29 newLine = Environment.NewLine30 End Sub31 32 29 Virtual Sub Flush() 33 30 End Sub 34 31 35 Abstract Sub Write(s As String) 36 Virtual Sub Write(x As Boolean) 37 Write(Str$(x)) 32 'これ以外全てのWrite/WriteLineは、最終的にこのWriteを呼ぶ。 33 Sub Write(x As String) 34 buf.Append(x) 35 If buf.Length >= 4096 Then 36 Flush() 37 End If 38 38 End Sub 39 39 40 Virtual Sub Write(x As Char) 41 Write(Chr$(x)) 40 Sub Write(x As Boolean) 41 buf.Append(x) 42 End Sub 43 44 Sub Write(x As SByte) 45 buf.Append(x) 42 46 End Sub 43 47 44 Virtual Sub Write(x As Byte) 45 Write(Str$(x)) 46 End Sub 47 #ifdef UNICODE 48 Virtual Sub Write(x As SByte) 49 Write(Str$(x)) 50 End Sub 51 #else 52 Virtual Sub Write(x As Word) 53 Write(Str$(x)) 54 End Sub 55 #endif 56 Virtual Sub Write(x As Integer) 57 Write(Str$(x)) 48 Sub Write(x As Byte) 49 buf.Append(x) 58 50 End Sub 59 51 60 Virtual Sub Write(x As DWord)61 Write(Str$(x))52 Sub Write(x As Word) 53 buf.Append(x) 62 54 End Sub 63 55 64 Virtual Sub Write(x As Long)65 Write(Str$(x))56 Sub Write(x As Integer) 57 buf.Append(x) 66 58 End Sub 67 59 68 Virtual Sub Write(x As QWord)69 Write(Str$(x))60 Sub Write(x As DWord) 61 buf.Append(x) 70 62 End Sub 71 63 72 Virtual Sub Write(x As Int64)73 Write(Str$(x))64 Sub Write(x As Long) 65 buf.Append(x) 74 66 End Sub 75 67 76 Virtual Sub Write(x As Single)77 Write(Str$(x))68 Sub Write(x As QWord) 69 buf.Append(x) 78 70 End Sub 79 71 80 Virtual Sub Write(x As Double)81 Write(Str$(x))72 Sub Write(x As Int64) 73 buf.Append(x) 82 74 End Sub 83 75 84 Virtual Sub Write(x As Object) 85 Write(x.ToString) 76 Sub Write(x As Single) 77 buf.Append(x) 78 End Sub 79 80 Sub Write(x As Double) 81 buf.Append(x) 82 End Sub 83 84 Sub Write(x As Object) 85 buf.Append(x) 86 86 End Sub 87 87 88 88 Sub WriteLine() 89 Write( newLine)89 Write(Environment.NewLine) 90 90 End Sub 91 91 92 Sub WriteLine( sAs String)93 Write( s)92 Sub WriteLine(x As String) 93 Write(x) 94 94 WriteLine() 95 95 End Sub 96 96 97 97 Sub WriteLine(x As Boolean) 98 Write(Str$(x)) 99 WriteLine() 100 End Sub 101 102 Sub WriteLine(x As Char) 103 Write(Chr$(x)) 98 Write(x) 104 99 WriteLine() 105 100 End Sub 106 101 107 102 Sub WriteLine(x As Byte) 108 Write( Str$(x))103 Write(x) 109 104 WriteLine() 110 105 End Sub 111 #ifdef UNICODE 106 112 107 Sub WriteLine(x As SByte) 113 Write( Str$(x))108 Write(x) 114 109 WriteLine() 115 110 End Sub 116 #else 111 117 112 Sub WriteLine(x As Word) 118 Write( Str$(x))113 Write(x) 119 114 WriteLine() 120 115 End Sub 121 #endif 116 122 117 Sub WriteLine(x As Integer) 123 Write( Str$(x))118 Write(x) 124 119 WriteLine() 125 120 End Sub 126 121 127 122 Sub WriteLine(x As DWord) 128 Write( Str$(x))123 Write(x) 129 124 WriteLine() 130 125 End Sub 131 126 132 127 Sub WriteLine(x As Long) 133 Write( Str$(x))128 Write(x) 134 129 WriteLine() 135 130 End Sub 136 131 137 132 Sub WriteLine(x As QWord) 138 Write( Str$(x))133 Write(x) 139 134 WriteLine() 140 135 End Sub 141 136 142 137 Sub WriteLine(x As Int64) 143 Write( Str$(x))138 Write(x) 144 139 WriteLine() 145 140 End Sub 146 141 147 142 Sub WriteLine(x As Single) 148 Write( Str$(x))143 Write(x) 149 144 WriteLine() 150 145 End Sub 151 146 152 147 Sub WriteLine(x As Double) 153 Write( Str$(x))148 Write(x) 154 149 WriteLine() 155 150 End Sub 156 151 157 152 Sub WriteLine(x As Object) 158 Write(x .ToString)153 Write(x) 159 154 WriteLine() 160 155 End Sub 161 162 /*163 @brief 改行文字の設定164 @date 2007/03/05165 @auther Egtra166 */167 Sub NewLine(n As String)168 newLine = n169 End Sub170 /*171 @brief 改行文字の取得172 @date 2007/03/05173 @auther Egtra174 */175 Function NewLine() As String176 End Function177 156 178 157 Static Function Synchronized(writer As TextWriter) As TextWriter … … 180 159 Return writer 181 160 End Function 161 182 162 Protected 163 Sub TextWriter() 164 buf = New Text.StringBuilder 165 End Sub 166 167 Sub TextWriter(buffer As Text.StringBuilder) 168 If ActiveBasic.IsNothing(buffer) Then 169 Throw New ArgumentNullException("buffer") 170 End If 171 buf = buffer 172 End Sub 173 174 Function Buffer() As Text.StringBuilder 175 Buffer = buf 176 End Function 177 183 178 Virtual Sub Dispose(disposing As Boolean) 179 If disposing Then 180 Flush() 181 buf = Nothing 182 End If 184 183 End Sub 185 184 186 185 Private 187 newLine As String186 buf As Text.StringBuilder 188 187 End Class 189 188 -
trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab
r581 r653 13 13 14 14 なお、このクラスで、文字列の長さやバッファの大きさを指定するときには、 15 1 chars = 2バイト(UTF-16符号単位)、1 bytes= 1バイトの単位で指定する。15 1 WCHAR = 2バイト(UTF-16符号単位)、1 Byte = 1バイトの単位で指定する。 16 16 17 17 */ … … 22 22 @brief 簡易的複製を作成する。 23 23 */ 24 Abstract Function Clone() As Object 24 Virtual Function Clone() As Object 25 Clone = This 26 End Function 25 27 26 28 ' Override Function Equals(y As Object) As Boolean … … 32 34 Public 33 35 Sub Encoding() 34 decoderFallback = New DecoderReplacementFallback35 36 End Sub 36 37 37 38 /*! 38 39 @brief 符号化して得られる文字列の長さを計算する。 39 @param[in] s 対象文字列40 @param[in] n sの長さ40 @param[in] src 対象文字列 41 @param[in] srcCount srcの長さ 41 42 @return 符号化して得られる文字列の長さ 42 43 @date 2007/12/08 43 44 */ 44 Function GetBytesCount(s As *WCHAR, nAs Long) As Long45 If s = 0 Then46 Throw New ArgumentNullException(" Encoding.GetBytesCount: An argument is null value.", "s")47 ElseIf n< 0 Then48 Throw New ArgumentOutOfRangeException(" Encoding.GetBytesCount: An argument is out of range value.", "n")49 End If 50 Return GetBytesCountCore(s , n)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) 51 52 End Function 52 53 #ifdef UNICODE 53 54 /*! 54 55 @brief 符号化して得られる文字列の長さを計算する。 55 @param[in] s 対象文字列56 @param[in] src 対象文字列 56 57 @return 符号化して得られる文字列の長さ 57 58 @date 2007/12/08 58 59 */ 59 Function GetBytesCount(s As String) As Long60 If ActiveBasic.IsNothing(s ) Then61 Throw New ArgumentNullException(" Encoding.GetBytesCount: An argument is null value.", "s")62 End If 63 Return GetBytesCountCore(StrPtr(s ), s.Length)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) 64 65 End Function 65 66 #endif 66 /*! 67 @brief 符号化して得られる文字列の長さを計算する。 68 @param[in] s 対象文字列 69 @param[in] index 開始位置 70 @param[in] count 符号化する文字の数 67 68 Private 69 /*! 70 @brief GetBytesCountの実装を行う。 71 @param[in] src 対象文字列 72 @param[in] srcCount srcの長さ 71 73 @return 符号化して得られる文字列の長さ 72 74 @date 2007/12/08 73 75 */ 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の長さ 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 80 Public 81 /*! 82 @brief 符号化する。 83 @param[in] src 入力 84 @param[in] srcCount srcの長さ 85 @param[out] dst 出力 86 @param[in] dstCount dstのバッファの大きさ 87 @return dstに書き込まれたバイト数 88 @date 2007/12/08 89 */ 90 Function GetBytes(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long 91 If src = 0 Then 92 Throw New ArgumentNullException("src") 93 ElseIf dst = 0 Then 94 Throw New ArgumentNullException("dst") 95 ElseIf srcCount < 0 Then 96 Throw New ArgumentOutOfRangeException("srcCount") 97 ElseIf dstCount < 0 Then 98 Throw New ArgumentOutOfRangeException("dstCount") 99 End If 100 101 Return GetBytesCore(src, srcCount, dst, dstCount) 102 End Function 103 104 Private 105 /*! 106 @brief GetBytesの処理を行う。 107 @param[in] src 入力 108 @param[in] srcCount srcの長さ 109 @param[out] dst 出力 110 @param[in] dstCount dstのバッファの大きさ 111 @return dstに書き込まれたバイト数 112 @exception ArgumentException バッファの大きさが足りない 113 @date 2007/12/08 114 */ 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 118 Public 119 /*! 120 @brief 復号して得られる文字列の長さを計算する。 121 @param[in] src 対象文字列 122 @param[in] srcCount srcの長さ 123 @return 復号して得られる文字列の長さ 124 @date 2007/12/08 125 */ 126 Function GetCharsCount(src As *Byte, srcCount As Long) As Long 127 If src = 0 Then 128 Throw New ArgumentNullException("src") 129 ElseIf srcCount < 0 Then 130 Throw New ArgumentOutOfRangeException("srcCount") 131 End If 132 Return GetCharsCountCore(src, srcCount) 133 End Function 134 135 Private 136 /*! 137 @brief GetCharsCountの処理を行う。 138 @param[in] src 対象文字列 139 @param[in] srcCount srcの長さ 89 140 @return 符号化して得られる文字列の長さ 90 141 @date 2007/12/08 91 142 */ 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 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 147 Public 148 /*! 149 @brief 復号する。 150 @param[in] src 入力 151 @param[in] srcCount srcの長さ 152 @param[out] dst 出力 153 @param[in] dstCount srcのバッファの大きさ 154 @return dstに書き込まれたバイト数 155 @date 2007/12/08 156 */ 157 Function GetChars(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long 158 If dst = 0 Then 159 Throw New ArgumentNullException("dst") 160 ElseIf src = 0 Then 161 Throw New ArgumentNullException("src") 162 ElseIf dstCount < 0 Then 163 Throw New ArgumentOutOfRangeException("dstCount") 164 ElseIf srcCount < 0 Then 165 Throw New ArgumentOutOfRangeException("srcCount") 166 End If 167 168 Return GetCharsCore(src, srcCount, dst, dstCount) 169 End Function 170 171 Private 172 /*! 173 @brief GetCharsの処理を行う。 174 @param[in] src 入力 175 @param[in] srcCount srcの長さ 176 @param[out] dst 出力 177 @param[in] dstCount dstのバッファの大きさ 178 @return dstに書き込まれたバイト数 179 @exception ArgumentException バッファの大きさが足りない 180 @date 2007/12/08 181 */ 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 185 Public 141 186 #ifdef UNICODE 142 187 /*! 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/08151 */152 Function GetBytes(s As String, index As Long, count As Long, bytes As *Byte, byteCount As Long) As Long153 If ActiveBasic.IsNothing(s) Then154 Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "chars")155 ElseIf bytes = 0 Then156 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 Then160 Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "count")161 ElseIf byteCount < 0 Then162 Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "byteCount")163 End If164 Dim p = StrPtr(s)165 Return GetBytesCore(VarPtr(p[index]), count, bytes, byteCount)188 @brief 復号し、Stringで結果を返す。 189 @param[in] src 入力 190 @param[in] srcCount srcの長さ 191 @return 変換結果の文字列 192 @date 2007/12/08 193 */ 194 Function GetString(src As *Byte, srcCount As Long) As String 195 If src = 0 Then 196 Throw New ArgumentNullException("src") 197 ElseIf srcCount < 0 Then 198 Throw New ArgumentOutOfRangeException("srcCount") 199 End If 200 GetString = getStringCore(src, srcCount) 201 End Function 202 203 Private 204 Function getStringCore(src As *Byte, srcCount As Long) As String 205 Dim sb = New StringBuilder 206 Dim bufCount = GetMaxCharCount(srcCount) 207 sb.Length = bufCount 208 Dim len = GetCharsCore(src, srcCount, StrPtr(sb), bufCount) 209 sb.Length = len 210 getStringCore = sb.ToString() 166 211 End Function 167 212 #endif 168 Protected169 /*!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/08179 */180 Abstract Function GetBytesCore(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long) As Long181 182 Public183 /*!184 @brief 復号して得られる文字列の長さを計算する。185 @param[in] s 対象文字列186 @param[in] n sの長さ187 @return 復号して得られる文字列の長さ188 @date 2007/12/08189 */190 Function GetCharsCount(s As *Byte, n As Long) As Long191 If s = 0 Then192 Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")193 ElseIf n < 0 Then194 Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "n")195 End If196 Return GetCharsCountCore(s, n)197 End Function198 /*!199 @brief 復号して得られる文字列の長さを計算する。200 @param[in] s 対象文字列201 @param[in] index 開始位置202 @param[in] count 符号化する文字の数203 @return 符号化して得られる文字列の長さ204 @date 2007/12/08205 */206 Function GetCharsCount(s As *Byte, index As Long, count As Long) As Long207 If s = 0 Then208 Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")209 ElseIf index < 0 Then210 Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "index")211 ElseIf count < 0 Then212 Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "count")213 End If214 Return GetCharsCountCore(VarPtr(s[index]), count)215 End Function216 Protected217 /*!218 @brief GetCharsCountの処理を行う。219 @param[in] s 対象文字列220 @param[in] n sの長さ221 @return 符号化して得られる文字列の長さ222 @date 2007/12/08223 */224 Abstract Function GetCharsCountCore(s As *Byte, n As Long) As Long225 /*!226 */227 Public228 /*!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/08236 */237 Function GetChars(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long238 If chars = 0 Then239 Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")240 ElseIf bytes = 0 Then241 Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")242 ElseIf charCount < 0 Then243 Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "charCount")244 ElseIf byteCount < 0 Then245 Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")246 End If247 248 Return GetCharsCore(bytes, byteCount, chars, charCount)249 End Function250 /*!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/08259 */260 Function GetChars(bytes As *Byte, index As Long, count As Long, chars As *WCHAR, charCount As Long) As Long261 If chars = 0 Then262 Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")263 ElseIf bytes = 0 Then264 Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")265 ElseIf index < 0 Then266 Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "index")267 ElseIf count < 0 Then268 Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "count")269 ElseIf charCount < 0 Then270 Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")271 End If272 273 Return GetCharsCore(VarPtr(bytes[index]), count, chars, charCount)274 End Function275 Protected276 /*!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/08286 */287 Abstract Function GetCharsCore(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long288 Public289 #ifdef UNICODE290 /*!291 @brief 復号し、Stringで結果を返す。292 @param[in] bytes 入力293 @param[in] byteCount charsの長さ294 @return 変換結果の文字列295 @date 2007/12/08296 */297 Function GetString(bytes As *Byte, byteCount As Long) As String298 If bytes = 0 Then299 Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")300 ElseIf byteCount < 0 Then301 Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "byteCount")302 End If303 Return getStringCore(bytes, byteCount)304 End Function305 /*!306 @brief 復号し、Stringで結果を返す。307 @param[in] bytes 入力308 @param[in] index charsの開始位置309 @param[in] count 符号化する文字の数310 @return 変換結果の文字列311 @date 2007/12/08312 */313 Function GetString(bytes As *Byte, index As Long, count As Long) As String314 If bytes = 0 Then315 Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")316 ElseIf index < 0 Then317 Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "index")318 ElseIf count < 0 Then319 Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "count")320 End If321 Return getStringCore(VarPtr(bytes[index]), count)322 End Function323 Private324 325 Function getStringCore(bytes As *Byte, byteCount As Long) As String326 Dim sb = New StringBuilder327 Dim bufSize = GetMaxCharCount(byteCount)328 sb.Length = bufSize329 Dim len = GetCharsCore(bytes, byteCount, StrPtr(sb), bufSize)330 sb.Length = len331 getStringCore = sb.ToString332 End Function333 #endif334 213 335 214 Public … … 347 226 @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。 348 227 */ 349 Abstract Function GetMaxByteCount( charCount As Long) As Long228 Abstract Function GetMaxByteCount(srcCount As Long) As Long 350 229 351 230 /*! 352 231 @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。 353 232 */ 354 Abstract Function GetMaxCharCount( charCount As Long) As Long233 Abstract Function GetMaxCharCount(srcCount As Long) As Long 355 234 356 235 /*! 357 236 @brief 符号化された文字列の先頭につける識別文字列の取得 358 ようするにUTFのBOM 237 ようするにUTFのBOM。 359 238 */ 360 239 Virtual Function GetPreamble() As *Byte … … 369 248 End Function 370 249 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 250 ' Abstract Function BodyName() As String 251 ' Abstract Function HeaderName() As String 383 252 384 253 /*! … … 391 260 ' Abstract Function WindowsCodePage() As Long 392 261 393 Function DecoderFallback() As DecoderFallback394 Return decoderFallback395 End Function396 397 Sub DecoderFallback(f As DecoderFallback)398 If ActiveBasic.IsNothing(f) Then399 Throw New ArgumentNullException("f")400 End If401 decoderFallback = f402 End Sub403 404 Function EncoderFallback() As EncoderFallback405 Return encoderFallback406 End Function407 408 Sub EncoderFallback(f As EncoderFallback)409 If ActiveBasic.IsNothing(f) Then410 Throw New ArgumentNullException("f")411 End If412 encoderFallback = f413 End Sub414 415 Private416 decoderFallback As DecoderFallback417 encoderFallback As EncoderFallback418 262 Public 419 263 /*! 420 264 @brief この符号化形式の名前の取得。 421 265 */ 422 Abstract Function EncodingName() As String266 ' Abstract Function EncodingName() As String 423 267 424 268 /*! 425 269 @brief この符号化形式のIANA登録名の取得。 426 270 */ 427 Abstract Function WebName() As String271 ' Abstract Function WebName() As String 428 272 429 273 ' Abstract Function IsBrowserDisplay() As Boolean … … 432 276 ' Abstract Function IsMailNewsSave() As Boolean 433 277 434 ' Abstract Function IsReadOnly() Boolean435 436 278 /*! 437 279 @brief この符号化形式が、1バイト文字だけを使う(複数バイト文字を使わない)かどうか。 438 280 */ 439 Abstract Function IsSingleByte() As Boolean281 ' Abstract Function IsSingleByte() As Boolean 440 282 441 283 'GetPreamble … … 450 292 @exception ArgumentNullException srcEncoding, dstEncoding, bytesの少なくとも1つ以上がNothing/NULLのとき。 451 293 @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 294 */ 295 ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, size As Long) As *Byte 296 ' End Function 457 297 458 Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte459 End Function298 ' Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte 299 ' End Function 460 300 461 301 /*! 462 302 @brief 指定したコードページ用のEncodingインスタンスの取得。 463 303 */ 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 304 ' Static Function GetEncoding(codepage As Long) As Encoding 467 305 ' End Function 468 306 /*! 469 307 @brief 指定した符号化形式名用のEncodingインスタンスの取得。 470 308 */ 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 309 ' Static Function GetEncoding(name As String) As Encoding 474 310 ' End Function 475 476 /*! 477 @brief システム既定のANSIコードページ用のEncodingインスタンスの取得。 311 /*! 312 @brief システムで標準のANSIコードページ用のEncodingインスタンスの取得。 478 313 */ 479 314 Static Function Default() As Encoding 480 315 End Function 481 316 /*! 482 @brief UTF-7用のEncodingインスタンスの取得。483 */484 Static Function UTF7() As Encoding485 End Function486 /*!487 317 @brief UTF-8用のEncodingインスタンスの取得。 488 318 */ … … 498 328 */ 499 329 Static Function UTF16BE() As Encoding 500 End Function501 /*!502 @brief UTF-32LE用のEncodingインスタンスの取得。503 */504 Static Function UTF32() As Encoding505 330 End Function 506 331 End Class … … 515 340 /*! 516 341 @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 出力要素数 342 @param[in] src 入力 343 @param[in] srcCount 入力要素数 344 @param[out] dst 出力 345 @param[in] dstCount 出力要素数 523 346 @param[in] flush 終了後に内部状態を初期化するかどうか 524 @param[out] charsUsed 使用された入力の要素数525 @param[out] bytesUsed 出力の内、実際に書き込まれた要素数347 @param[out] srcUsed 使用された入力の要素数 348 @param[out] dstUsed 出力の内、実際に書き込まれた要素数 526 349 @param[out] completed 入力の全ての文字が変換に使われたかどうか 527 350 */ 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) 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 Then 356 Throw New ArgumentNullException("src") 357 ElseIf srcCount < 0 Then 358 Throw New ArgumentOutOfRangeException("srcCount") 359 ElseIf dst = 0 Then 360 Throw New ArgumentNullException("dst") 361 ElseIf dstCount < 0 Then 362 Throw New ArgumentOutOfRangeException("dstCount") 363 End If 364 ConvertCore(src, srcCount, dst, dstCount, flush, srcUsed, dstUsed, completed) 567 365 End Sub 568 366 569 367 /*! 570 368 @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 出力要素数 369 @param[in] src 入力 370 @param[in] srcCount 入力要素数 371 @param[out] dst 出力 372 @param[in] dstCount 出力要素数 577 373 @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 Long581 Dim bytesUsed As Long374 @return 出力の内、実際に書き込まれた要素数 375 */ 376 Function GetChars(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean) As Long 377 Dim srcUsed As Long 582 378 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 379 Convert(src, srcCount, dst, dstCount, flush, srcUsed, GetChars, completed) 380 If srcUsed < srcCount Then 381 Throw New ArgumentException("src", "buffer is too small") 382 End If 383 End Function 384 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 Long 393 If src = 0 Then 394 Throw New ArgumentNullException("src") 395 ElseIf srcCount < 0 Then 396 Throw New ArgumentOutOfRangeException("srcCount") 397 End If 398 GetCharsCountCore(src, srcCount, flush) 399 End Function 400 401 /*! 402 @brief 内部状態を初期状態に戻す 403 */ 404 Virtual Sub Reset() 405 End Sub 406 618 407 Protected 619 408 /*! 620 @brief 実際に変換する メソッド621 @param[in] bytes入力622 @param[in] byteCount 入力要素数623 @param[out] chars出力624 @param[in] charCount 出力要素数409 @brief 実際に変換する 410 @param[in] src 入力 411 @param[in] srcCount 入力要素数 412 @param[out] dst 出力 413 @param[in] dstCount 出力要素数 625 414 @param[in] flush 終了後に内部状態を初期化するかどうか 626 @param[out] charsUsed 使用された入力の要素数627 @param[out] bytesUsed 出力の内、実際に書き込まれた要素数415 @param[out] dstUsed 使用された入力の要素数 416 @param[out] srcUsed 出力の内、実際に書き込まれた要素数 628 417 @param[out] completed 入力の全ての文字が変換に使われたかどうか 629 418 */ 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 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 Long 635 430 End Class 636 431 … … 644 439 /*! 645 440 @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 出力要素数 441 @param[in] src 入力 442 @param[in] srcCount 入力要素数 443 @param[out] dst 出力 444 @param[in] dstCount 出力要素数 652 445 @param[in] flush 終了後に内部状態を初期化するかどうか 653 @param[out] charsUsed 使用された入力の要素数654 @param[out] bytesUsed 出力の内、実際に書き込まれた要素数446 @param[out] srcUsed 使用された入力の要素数 447 @param[out] dstUsed 出力の内、実際に書き込まれた要素数 655 448 @param[out] completed 入力の全ての文字が変換に使われたかどうか 656 449 */ 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) 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 Then 455 Throw New ArgumentNullException("src") 456 ElseIf srcCount < 0 Then 457 Throw New ArgumentOutOfRangeException("srcCount") 458 ElseIf dst = 0 Then 459 Throw New ArgumentNullException("dst") 460 ElseIf dstCount < 0 Then 461 Throw New ArgumentOutOfRangeException("dstCount") 462 End If 463 ConvertCore(src, srcCount, dst, dstCount, flush, srcUsed, dstUsed, completed) 696 464 End Sub 697 465 698 466 /*! 699 467 @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 出力要素数 468 @param[in] src 入力 469 @param[in] srcCount 入力要素数 470 @param[out] dst 出力 471 @param[in] dstCount 出力要素数 706 472 @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 Long710 Dim charsUsed As Long473 @return 出力の内、実際に書き込まれた要素数 474 */ 475 Function GetBytes(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long, flush As Boolean) As Long 476 Dim srcUsed As Long 711 477 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 478 Convert(src, srcCount, dst, dstCount, flush, srcUsed, GetBytes, completed) 479 If srcUsed < srcCount Then 480 Throw New ArgumentException("src", "buffer is too small") 481 End If 482 End Function 483 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 Long 492 If src = 0 Then 493 Throw New ArgumentNullException("src") 494 ElseIf srcCount < 0 Then 495 Throw New ArgumentOutOfRangeException("srcCount") 496 End If 497 GetBytesCountCore(src, srcCount, flush) 498 End Function 499 500 /*! 501 @brief 内部状態を初期状態に戻す 502 */ 503 Virtual Sub Reset() 504 End Sub 505 747 506 Protected 748 507 /*! 749 @brief 実際に変換する メソッド750 @param[in] chars入力751 @param[in] charCount 入力要素数752 @param[out] bytes出力753 @param[in] byteCount 出力要素数508 @brief 実際に変換する 509 @param[in] src 入力 510 @param[in] srcCount 入力要素数 511 @param[out] dst 出力 512 @param[in] dstCount 出力要素数 754 513 @param[in] flush 終了後に内部状態を初期化するかどうか 755 @param[out] bytesUsed 使用された入力の要素数756 @param[out] charsUsed 出力の内、実際に書き込まれた要素数514 @param[out] dstUsed 使用された入力の要素数 515 @param[out] srcUsed 出力の内、実際に書き込まれた要素数 757 516 @param[out] completed 入力の全ての文字が変換に使われたかどうか 758 517 */ 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 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 Long 764 529 End Class 765 530 … … 771 536 End Enum 772 537 773 Class EncoderFallback 538 Namespace Detail 539 540 /*! 541 @brief WideCharToMultiByte/MultiByteToWideCharで変換を行うエンコーディング。 542 DBCS/SBCS限定。UTF-8やUTF-7は非対応。 543 */ 544 Class WindowsCodePageEncoding 545 Inherits Encoding 546 Public 547 Sub WindowsCodePageEncoding(codepage As DWord) 548 cp = codepage 549 End Sub 550 551 /*! 552 @brief 符号器を取得する。 553 */ 554 Override Function GetDecoder() As Decoder 555 GetDecoder = New WindowsCodePageDecoder(cp) 556 End Function 557 558 /*! 559 @brief 復号器を取得する。 560 */ 561 Override Function GetEncoder() As Encoder 562 GetEncoder = New WindowsCodePageEncoder(cp) 563 End Function 564 565 Override Function GetHashCode() As Long 566 GetHashCode = cp As Long 567 End Function 568 569 Override Function Equals(x As Object) As Boolean 570 If GetType().Equals(x.GetType()) Then 571 Dim xe = x As WindowsCodePageEncoding 572 Equals = cp = xe.cp 573 Else 574 Equals = False 575 End If 576 End Function 577 578 /*! 579 @brief ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。 580 */ 581 Override Function GetMaxByteCount(srcCount As Long) As Long 582 GetMaxByteCount = srcCount * 2 583 End Function 584 585 /*! 586 @brief ある長さのバイト列を復号して得られる文字列の最大の長さを返す。 587 */ 588 Override Function GetMaxCharCount(srcCount As Long) As Long 589 GetMaxCharCount = srcCount 590 End Function 591 592 Private 593 cp As DWord 774 594 End Class 595 596 Class WindowsCodePageEncoder 597 Inherits Encoder 598 Public 599 Sub WindowsCodePageEncoder(codepage As DWord) 600 cp = codepage 601 End Sub 602 603 Override Sub Reset() 604 nextByte = 0 605 End Sub 606 607 Protected 608 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 Long 612 Dim dstPos = 0 As Long 613 If dstCount > 0 And nextByte <> 0 Then 614 dst[0] = nextByte 615 nextByte = 0 616 dstPos++ 617 End If 618 While srcPos < srcCount And dstPos < srcCount 619 Dim buf[1] As CHAR 620 Dim len = WideCharToMultiByte(cp, WC_COMPOSITECHECK Or WC_DEFAULTCHAR, VarPtr(src[srcPos]), 1, buf, Len(buf), 0, 0) 621 If len = 0 Then 622 ActiveBasic.Windows.ThrowWithLastError() 623 End If 624 dst[dstPos] = buf[0] As Byte 625 If len = 2 Then 626 If dstCount = 1 Then 627 nextByte = buf[1] As Byte 628 Exit While 629 End If 630 dstPos++ 631 dst[dstPos] = buf[1] As Byte 632 nextByte = 0 633 End If 634 srcPos++ 635 dstPos++ 636 Wend 637 srcUsed = srcPos 638 dstUsed = dstPos 639 completed = (srcPos = srcCount And dstPos = srcCount And nextByte = 0) 640 End Sub 641 642 Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long, flush As Boolean) As Long 643 End Function 644 645 Private 646 cp As DWord 647 nextByte As Byte 648 End Class 649 650 Class WindowsCodePageDecoder 651 Inherits Decoder 652 Public 653 Sub WindowsCodePageDecoder(codepage As DWord) 654 cp = codepage 655 End Sub 656 657 Protected 658 Override Sub ConvertCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long, flush As Boolean, 659 ByRef srcUsed As Long, ByRef dstUsed As Long, ByRef completed As Boolean) 660 End Sub 661 662 Override Function GetCharsCountCore(src As *Byte, srcCount As Long, flush As Boolean) As Long 663 End Function 664 665 Private 666 cp As DWord 667 End Class 668 669 End Namespace 775 670 776 671 End Namespace 'Text -
trunk/ab5.0/ablib/src/Classes/index.ab
r646 r653 10 10 #require "./ActiveBasic/Windows/Registry.ab" 11 11 #require "./ActiveBasic/Windows/Windows.ab" 12 #require "./ActiveBasic/Windows/UI/WindowHandle.ab"12 '#require "./ActiveBasic/Windows/UI/WindowHandle.ab" 13 13 #require "./ActiveBasic/Windows/Version/Version.ab" 14 14 #require "./ActiveBasic/Xml/Parser.ab" … … 83 83 #require "./System/IO/TextWriter.ab" 84 84 #require "./System/IO/StreamReader.ab" 85 #require "./System/IO/StreamWriter.ab" 85 86 #require "./System/IO/StringReader.ab" 86 #require "./System/IO/StreamWriter.ab" 87 #require "./System/IO/TextReader.ab" 87 #require "./System/IO/StringWriter.ab" 88 88 #require "./System/Media/SystemSound.ab" 89 89 #require "./System/Media/SystemSounds.ab" … … 93 93 #require "./System/Text/StringBuilder.ab" 94 94 #require "./System/Text/Encoding.ab" 95 #require "./System/Text/UTF8Encoding.ab" 96 #require "./System/Text/DecoderFallback.ab" 95 '#require "./System/Text/UTF8Encoding.ab" 97 96 #require "./System/Threading/AutoResetEvent.ab" 98 97 #require "./System/Threading/EventWaitHandle.ab"
Note:
See TracChangeset
for help on using the changeset viewer.