Changeset 653 for trunk


Ignore:
Timestamp:
Nov 3, 2008, 11:42:22 PM (15 years ago)
Author:
イグトランス (egtra)
Message:

TextWriter及びその派生クラスをEncodingを用いるように変更。UTF8EncodingをEncodingの変更に追従させていないので、それ関連を一時的にコメントアウト。

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  
    11Imports System.Text
    22Imports System.Text.Detail
    3 
     3/*
    44Namespace EncodingTest
    55
     
    4040
    4141EncodingTest.TestMain()
     42*/
  • trunk/ab5.0/ablib/src/Classes/System/IO/BinaryReader.ab

    r553 r653  
    1 NameSpace System
    2 NameSpace IO
     1Namespace System
     2Namespace IO
    33
    44Class BinaryReader
     
    1313    Sub BinaryReader(input As Stream)
    1414        stream = input
    15         enc = New System.Text.UTF8Encoding()
     15'       enc = New System.Text.UTF8Encoding()
    1616    End Sub
    1717    /*!
  • trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab

    r627 r653  
    4141
    4242    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
    10959    End Sub
    11060
     
    11262    Override Sub Dispose(disposing As Boolean)
    11363        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()
    11866            s.Dispose()
    11967        End If
     
    12371    Sub init(stream As Stream)
    12472        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()
    12677    End Sub
    12778
    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
    12992    s As System.IO.Stream
    13093End Class
  • trunk/ab5.0/ablib/src/Classes/System/IO/TextWriter.ab

    r523 r653  
    1010*/
    1111Class TextWriter
     12    Implements System.IDisposable
    1213Public
    1314    Virtual Sub ~TextWriter()
     
    2627    End Sub
    2728
    28     Sub TextWriter()
    29         newLine = Environment.NewLine
    30     End Sub
    31 
    3229    Virtual Sub Flush()
    3330    End Sub
    3431
    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
    3838    End Sub
    3939   
    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)
    4246    End Sub
    4347
    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)
    5850    End Sub
    5951
    60     Virtual Sub Write(x As DWord)
    61         Write(Str$(x))
     52    Sub Write(x As Word)
     53        buf.Append(x)
    6254    End Sub
    6355
    64     Virtual Sub Write(x As Long)
    65         Write(Str$(x))
     56    Sub Write(x As Integer)
     57        buf.Append(x)
    6658    End Sub
    6759
    68     Virtual Sub Write(x As QWord)
    69         Write(Str$(x))
     60    Sub Write(x As DWord)
     61        buf.Append(x)
    7062    End Sub
    7163
    72     Virtual Sub Write(x As Int64)
    73         Write(Str$(x))
     64    Sub Write(x As Long)
     65        buf.Append(x)
    7466    End Sub
    7567
    76     Virtual Sub Write(x As Single)
    77         Write(Str$(x))
     68    Sub Write(x As QWord)
     69        buf.Append(x)
    7870    End Sub
    7971
    80     Virtual Sub Write(x As Double)
    81         Write(Str$(x))
     72    Sub Write(x As Int64)
     73        buf.Append(x)
    8274    End Sub
    8375
    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)
    8686    End Sub
    8787
    8888    Sub WriteLine()
    89         Write(newLine)
     89        Write(Environment.NewLine)
    9090    End Sub
    9191
    92     Sub WriteLine(s As String)
    93         Write(s)
     92    Sub WriteLine(x As String)
     93        Write(x)
    9494        WriteLine()
    9595    End Sub
    9696
    9797    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)
    10499        WriteLine()
    105100    End Sub
    106101
    107102    Sub WriteLine(x As Byte)
    108         Write(Str$(x))
     103        Write(x)
    109104        WriteLine()
    110105    End Sub
    111 #ifdef UNICODE
     106   
    112107    Sub WriteLine(x As SByte)
    113         Write(Str$(x))
     108        Write(x)
    114109        WriteLine()
    115110    End Sub
    116 #else
     111
    117112    Sub WriteLine(x As Word)
    118         Write(Str$(x))
     113        Write(x)
    119114        WriteLine()
    120115    End Sub
    121 #endif
     116
    122117    Sub WriteLine(x As Integer)
    123         Write(Str$(x))
     118        Write(x)
    124119        WriteLine()
    125120    End Sub
    126121
    127122    Sub WriteLine(x As DWord)
    128         Write(Str$(x))
     123        Write(x)
    129124        WriteLine()
    130125    End Sub
    131126
    132127    Sub WriteLine(x As Long)
    133         Write(Str$(x))
     128        Write(x)
    134129        WriteLine()
    135130    End Sub
    136131
    137132    Sub WriteLine(x As QWord)
    138         Write(Str$(x))
     133        Write(x)
    139134        WriteLine()
    140135    End Sub
    141136
    142137    Sub WriteLine(x As Int64)
    143         Write(Str$(x))
     138        Write(x)
    144139        WriteLine()
    145140    End Sub
    146141
    147142    Sub WriteLine(x As Single)
    148         Write(Str$(x))
     143        Write(x)
    149144        WriteLine()
    150145    End Sub
    151146
    152147    Sub WriteLine(x As Double)
    153         Write(Str$(x))
     148        Write(x)
    154149        WriteLine()
    155150    End Sub
    156151
    157152    Sub WriteLine(x As Object)
    158         Write(x.ToString)
     153        Write(x)
    159154        WriteLine()
    160155    End Sub
    161 
    162     /*
    163     @brief 改行文字の設定
    164     @date 2007/03/05
    165     @auther Egtra
    166     */
    167     Sub NewLine(n As String)
    168         newLine = n
    169     End Sub
    170     /*
    171     @brief 改行文字の取得
    172     @date 2007/03/05
    173     @auther Egtra
    174     */
    175     Function NewLine() As String
    176     End Function
    177156
    178157    Static Function Synchronized(writer As TextWriter) As TextWriter
     
    180159        Return writer
    181160    End Function
     161
    182162Protected
     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
    183178    Virtual Sub Dispose(disposing As Boolean)
     179        If disposing Then
     180            Flush()
     181            buf = Nothing
     182        End If
    184183    End Sub
    185184
    186185Private
    187     newLine As String
     186    buf As Text.StringBuilder
    188187End Class
    189188
  • trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab

    r581 r653  
    1313
    1414なお、このクラスで、文字列の長さやバッファの大きさを指定するときには、
    15 1 chars = 2バイト(UTF-16符号単位)、1 bytes = 1バイトの単位で指定する。
     151 WCHAR = 2バイト(UTF-16符号単位)、1 Byte = 1バイトの単位で指定する。
    1616
    1717*/
     
    2222    @brief  簡易的複製を作成する。
    2323    */
    24     Abstract Function Clone() As Object
     24    Virtual Function Clone() As Object
     25        Clone = This
     26    End Function
    2527
    2628'   Override Function Equals(y As Object) As Boolean
     
    3234Public
    3335    Sub Encoding()
    34         decoderFallback = New DecoderReplacementFallback
    3536    End Sub
    3637
    3738    /*!
    3839    @brief  符号化して得られる文字列の長さを計算する。
    39     @param[in] s    対象文字列
    40     @param[in] n    sの長さ
     40    @param[in] src  対象文字列
     41    @param[in] srcCount srcの長さ
    4142    @return 符号化して得られる文字列の長さ
    4243    @date   2007/12/08
    4344    */
    44     Function GetBytesCount(s As *WCHAR, n As Long) As Long
    45         If s = 0 Then
    46             Throw New ArgumentNullException("Encoding.GetBytesCount: An argument is null value.", "s")
    47         ElseIf n < 0 Then
    48             Throw New ArgumentOutOfRangeException("Encoding.GetBytesCount: An argument is out of range value.", "n")
    49         End If
    50         Return GetBytesCountCore(s, n)
     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)
    5152    End Function
    5253#ifdef UNICODE
    5354    /*!
    5455    @brief  符号化して得られる文字列の長さを計算する。
    55     @param[in] s    対象文字列
     56    @param[in] src  対象文字列
    5657    @return 符号化して得られる文字列の長さ
    5758    @date   2007/12/08
    5859    */
    59     Function GetBytesCount(s As String) As Long
    60         If ActiveBasic.IsNothing(s) Then
    61             Throw New ArgumentNullException("Encoding.GetBytesCount: An argument is null value.", "s")
    62         End If
    63         Return GetBytesCountCore(StrPtr(s), s.Length)
     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)
    6465    End Function
    6566#endif
    66     /*!
    67     @brief  符号化して得られる文字列の長さを計算する。
    68     @param[in] s    対象文字列
    69     @param[in] index    開始位置
    70     @param[in] count    符号化する文字の数
     67
     68Private
     69    /*!
     70    @brief  GetBytesCountの実装を行う。
     71    @param[in] src  対象文字列
     72    @param[in] srcCount srcの長さ
    7173    @return 符号化して得られる文字列の長さ
    7274    @date   2007/12/08
    7375    */
    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
     80Public
     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
     104Private
     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
     118Public
     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
     135Private
     136    /*!
     137    @brief  GetCharsCountの処理を行う。
     138    @param[in] src  対象文字列
     139    @param[in] srcCount srcの長さ
    89140    @return 符号化して得られる文字列の長さ
    90141    @date   2007/12/08
    91142    */
    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
     147Public
     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
     171Private
     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
     185Public
    141186#ifdef UNICODE
    142187    /*!
    143     @brief  符号化する
    144     @param[in] s    入力
    145     @param[in] index    sの開始位置
    146     @param[in] count    符号化する文字の数
    147     @param[out] bytes   出力
    148     @param[in] byteCount    bytesのバッファの大きさ
    149     @return bytesに書き込まれたバイト数
    150     @date   2007/12/08
    151     */
    152     Function GetBytes(s As String, index As Long, count As Long, bytes As *Byte, byteCount As Long) As Long
    153         If ActiveBasic.IsNothing(s) Then
    154             Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "chars")
    155         ElseIf bytes = 0 Then
    156             Throw New ArgumentNullException("Encoding.GetBytes: An argument is null value.", "bytes")
    157         ElseIf index < 0 Or index >= s.Length Then
    158             Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "index")
    159         ElseIf count < 0 Or index + count >= s.Length Then
    160             Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "count")
    161         ElseIf byteCount < 0 Then
    162             Throw New ArgumentOutOfRangeException("Encoding.GetBytes: An argument is out of range value.", "byteCount")
    163         End If
    164         Dim p = StrPtr(s)
    165         Return GetBytesCore(VarPtr(p[index]), count, bytes, byteCount)
     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
     203Private
     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()
    166211    End Function
    167212#endif
    168 Protected
    169     /*!
    170     @brief  GetBytesの処理を行う。
    171     @param[in] chars    入力
    172     @param[in] charCount    charsの長さ
    173     @param[out] bytes   出力
    174     @param[in] byteCount    bytesのバッファの大きさ
    175     @return bytesに書き込まれたバイト数
    176     @exception ArgumentException    バッファの大きさが足りない
    177     @exception EncoderFallbackException フォールバックが発生した
    178     @date   2007/12/08
    179     */
    180     Abstract Function GetBytesCore(chars As *WCHAR, charCount As Long, bytes As *Byte, byteCount As Long) As Long
    181 
    182 Public
    183     /*!
    184     @brief  復号して得られる文字列の長さを計算する。
    185     @param[in] s    対象文字列
    186     @param[in] n    sの長さ
    187     @return 復号して得られる文字列の長さ
    188     @date   2007/12/08
    189     */
    190     Function GetCharsCount(s As *Byte, n As Long) As Long
    191         If s = 0 Then
    192             Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")
    193         ElseIf n < 0 Then
    194             Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "n")
    195         End If
    196         Return GetCharsCountCore(s, n)
    197     End Function
    198     /*!
    199     @brief  復号して得られる文字列の長さを計算する。
    200     @param[in] s    対象文字列
    201     @param[in] index    開始位置
    202     @param[in] count    符号化する文字の数
    203     @return 符号化して得られる文字列の長さ
    204     @date   2007/12/08
    205     */
    206     Function GetCharsCount(s As *Byte, index As Long, count As Long) As Long
    207         If s = 0 Then
    208             Throw New ArgumentNullException("Encoding.GetCharsCount: An argument is null value.", "s")
    209         ElseIf index < 0 Then
    210             Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "index")
    211         ElseIf count < 0 Then
    212             Throw New ArgumentOutOfRangeException("Encoding.GetCharsCount: An argument is out of range value.", "count")
    213         End If
    214         Return GetCharsCountCore(VarPtr(s[index]), count)
    215     End Function
    216 Protected
    217     /*!
    218     @brief  GetCharsCountの処理を行う。
    219     @param[in] s    対象文字列
    220     @param[in] n    sの長さ
    221     @return 符号化して得られる文字列の長さ
    222     @date   2007/12/08
    223     */
    224     Abstract Function GetCharsCountCore(s As *Byte, n As Long) As Long
    225     /*!
    226     */
    227 Public
    228     /*!
    229     @brief  復号する。
    230     @param[in] bytes    入力
    231     @param[in] byteCount    charsの長さ
    232     @param[out] chars   出力
    233     @param[in] charCount    bytesのバッファの大きさ
    234     @return bytesに書き込まれたバイト数
    235     @date   2007/12/08
    236     */
    237     Function GetChars(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long
    238         If chars = 0 Then
    239             Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")
    240         ElseIf bytes = 0 Then
    241             Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")
    242         ElseIf charCount < 0 Then
    243             Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "charCount")
    244         ElseIf byteCount < 0 Then
    245             Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")
    246         End If
    247 
    248         Return GetCharsCore(bytes, byteCount, chars, charCount)
    249     End Function
    250     /*!
    251     @brief  復号する。
    252     @param[in] bytes    入力
    253     @param[in] index    charsの開始位置
    254     @param[in] count    符号化する文字の数
    255     @param[out] chars   出力
    256     @param[in] charCount    bytesのバッファの大きさ
    257     @return bytesに書き込まれたバイト数
    258     @date   2007/12/08
    259     */
    260     Function GetChars(bytes As *Byte, index As Long, count As Long, chars As *WCHAR, charCount As Long) As Long
    261         If chars = 0 Then
    262             Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "chars")
    263         ElseIf bytes = 0 Then
    264             Throw New ArgumentNullException("Encoding.GetChars: An argument is null value.", "bytes")
    265         ElseIf index < 0 Then
    266             Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "index")
    267         ElseIf count < 0 Then
    268             Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "count")
    269         ElseIf charCount < 0 Then
    270             Throw New ArgumentOutOfRangeException("Encoding.GetChars: An argument is out of range value.", "byteCount")
    271         End If
    272 
    273         Return GetCharsCore(VarPtr(bytes[index]), count, chars, charCount)
    274     End Function
    275 Protected
    276     /*!
    277     @brief  GetCharsの処理を行う。
    278     @param[in] bytes    入力
    279     @param[in] byteCount    charsの長さ
    280     @param[out] chars   出力
    281     @param[in] charCount    bytesのバッファの大きさ
    282     @return bytesに書き込まれたバイト数
    283     @exception ArgumentException    バッファの大きさが足りない
    284     @exception EncoderFallbackException フォールバックが発生した
    285     @date   2007/12/08
    286     */
    287     Abstract Function GetCharsCore(bytes As *Byte, byteCount As Long, chars As *WCHAR, charCount As Long) As Long
    288 Public
    289 #ifdef UNICODE
    290     /*!
    291     @brief  復号し、Stringで結果を返す。
    292     @param[in] bytes    入力
    293     @param[in] byteCount    charsの長さ
    294     @return 変換結果の文字列
    295     @date   2007/12/08
    296     */
    297     Function GetString(bytes As *Byte, byteCount As Long) As String
    298         If bytes = 0 Then
    299             Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")
    300         ElseIf byteCount < 0 Then
    301             Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "byteCount")
    302         End If
    303         Return getStringCore(bytes, byteCount)
    304     End Function
    305     /*!
    306     @brief  復号し、Stringで結果を返す。
    307     @param[in] bytes    入力
    308     @param[in] index    charsの開始位置
    309     @param[in] count    符号化する文字の数
    310     @return 変換結果の文字列
    311     @date   2007/12/08
    312     */
    313     Function GetString(bytes As *Byte, index As Long, count As Long) As String
    314         If bytes = 0 Then
    315             Throw New ArgumentNullException("Encoding.GetString: An argument is null value.", "bytes")
    316         ElseIf index < 0 Then
    317             Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "index")
    318         ElseIf count < 0 Then
    319             Throw New ArgumentOutOfRangeException("Encoding.GetString: An argument is out of range value.", "count")
    320         End If
    321         Return getStringCore(VarPtr(bytes[index]), count)
    322     End Function
    323 Private
    324 
    325     Function getStringCore(bytes As *Byte, byteCount As Long) As String
    326         Dim sb = New StringBuilder
    327         Dim bufSize = GetMaxCharCount(byteCount)
    328         sb.Length = bufSize
    329         Dim len = GetCharsCore(bytes, byteCount, StrPtr(sb), bufSize)
    330         sb.Length = len
    331         getStringCore = sb.ToString
    332     End Function
    333 #endif
    334213
    335214Public
     
    347226    @brief  ある長さの文字列を符号化して得られるバイト列の最大の長さを返す。
    348227    */
    349     Abstract Function GetMaxByteCount(charCount As Long) As Long
     228    Abstract Function GetMaxByteCount(srcCount As Long) As Long
    350229
    351230    /*!
    352231    @brief  ある長さのバイト列を復号して得られる文字列の最大の長さを返す。
    353232    */
    354     Abstract Function GetMaxCharCount(charCount As Long) As Long
     233    Abstract Function GetMaxCharCount(srcCount As Long) As Long
    355234
    356235    /*!
    357236    @brief  符号化された文字列の先頭につける識別文字列の取得
    358     ようするにUTFのBOM
     237    ようするにUTFのBOM
    359238    */
    360239    Virtual Function GetPreamble() As *Byte
     
    369248    End Function
    370249
    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
    383252
    384253    /*!
     
    391260'   Abstract Function WindowsCodePage() As Long
    392261
    393     Function DecoderFallback() As DecoderFallback
    394         Return decoderFallback
    395     End Function
    396 
    397     Sub DecoderFallback(f As DecoderFallback)
    398         If ActiveBasic.IsNothing(f) Then
    399             Throw New ArgumentNullException("f")
    400         End If
    401         decoderFallback = f
    402     End Sub
    403 
    404     Function EncoderFallback() As EncoderFallback
    405         Return encoderFallback
    406     End Function
    407 
    408     Sub EncoderFallback(f As EncoderFallback)
    409         If ActiveBasic.IsNothing(f) Then
    410             Throw New ArgumentNullException("f")
    411         End If
    412         encoderFallback = f
    413     End Sub
    414 
    415 Private
    416     decoderFallback As DecoderFallback
    417     encoderFallback As EncoderFallback
    418262Public
    419263    /*!
    420264    @brief  この符号化形式の名前の取得。
    421265    */
    422     Abstract Function EncodingName() As String
     266'   Abstract Function EncodingName() As String
    423267
    424268    /*!
    425269    @brief  この符号化形式のIANA登録名の取得。
    426270    */
    427     Abstract Function WebName() As String
     271'   Abstract Function WebName() As String
    428272
    429273'   Abstract Function IsBrowserDisplay() As Boolean
     
    432276'   Abstract Function IsMailNewsSave() As Boolean
    433277
    434 '   Abstract Function IsReadOnly() Boolean
    435 
    436278    /*!
    437279    @brief  この符号化形式が、1バイト文字だけを使う(複数バイト文字を使わない)かどうか。
    438280    */
    439     Abstract Function IsSingleByte() As Boolean
     281'   Abstract Function IsSingleByte() As Boolean
    440282
    441283    'GetPreamble
     
    450292    @exception ArgumentNullException    srcEncoding, dstEncoding, bytesの少なくとも1つ以上がNothing/NULLのとき。
    451293    @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
    457297   
    458     Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte
    459     End Function
     298'   Static Function Convert(srcEncoding As Encoding, dstEncoding As Encoding, bytes As *Byte, index As Long, count As Long) As *Byte
     299'   End Function
    460300
    461301    /*!
    462302    @brief  指定したコードページ用のEncodingインスタンスの取得。
    463303    */
    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
    467305'   End Function
    468306    /*!
    469307    @brief  指定した符号化形式名用のEncodingインスタンスの取得。
    470308    */
    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
    474310'   End Function
    475 
    476     /*!
    477     @brief  システム既定のANSIコードページ用のEncodingインスタンスの取得。
     311    /*!
     312    @brief  システムで標準のANSIコードページ用のEncodingインスタンスの取得。
    478313    */
    479314    Static Function Default() As Encoding
    480315    End Function
    481316    /*!
    482     @brief  UTF-7用のEncodingインスタンスの取得。
    483     */
    484     Static Function UTF7() As Encoding
    485     End Function
    486     /*!
    487317    @brief  UTF-8用のEncodingインスタンスの取得。
    488318    */
     
    498328    */
    499329    Static Function UTF16BE() As Encoding
    500     End Function
    501     /*!
    502     @brief  UTF-32LE用のEncodingインスタンスの取得。
    503     */
    504     Static Function UTF32() As Encoding
    505330    End Function
    506331End Class
     
    515340    /*!
    516341    @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 出力要素数
    523346    @param[in] flush    終了後に内部状態を初期化するかどうか
    524     @param[out] charsUsed   使用された入力の要素数
    525     @param[out] bytesUsed   出力の内、実際に書き込まれた要素数
     347    @param[out] srcUsed 使用された入力の要素数
     348    @param[out] dstUsed 出力の内、実際に書き込まれた要素数
    526349    @param[out] completed   入力の全ての文字が変換に使われたかどうか
    527350    */
    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)
    567365    End Sub
    568366
    569367    /*!
    570368    @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 出力要素数
    577373    @param[in] flush    終了後に内部状態を初期化するかどうか
    578     @return 出力の内、実際に書き込まれた要素数
    579     */
    580     Function GetChars(bytes As *Byte, byteIndex As Long, byteCount As Long, chars As *WCHAR, charIndex As Long, charCount As Long, flush As Boolean) As Long
    581         Dim bytesUsed As Long
     374    @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
    582378        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
    618407Protected
    619408    /*!
    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 出力要素数
    625414    @param[in] flush    終了後に内部状態を初期化するかどうか
    626     @param[out] charsUsed   使用された入力の要素数
    627     @param[out] bytesUsed   出力の内、実際に書き込まれた要素数
     415    @param[out] dstUsed 使用された入力の要素数
     416    @param[out] srcUsed 出力の内、実際に書き込まれた要素数
    628417    @param[out] completed   入力の全ての文字が変換に使われたかどうか
    629418    */
    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
    635430End Class
    636431
     
    644439    /*!
    645440    @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 出力要素数
    652445    @param[in] flush    終了後に内部状態を初期化するかどうか
    653     @param[out] charsUsed   使用された入力の要素数
    654     @param[out] bytesUsed   出力の内、実際に書き込まれた要素数
     446    @param[out] srcUsed 使用された入力の要素数
     447    @param[out] dstUsed 出力の内、実際に書き込まれた要素数
    655448    @param[out] completed   入力の全ての文字が変換に使われたかどうか
    656449    */
    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)
    696464    End Sub
    697465
    698466    /*!
    699467    @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 出力要素数
    706472    @param[in] flush    終了後に内部状態を初期化するかどうか
    707     @return bytesUsed   出力の内、実際に書き込まれた要素数
    708     */
    709     Function GetBytes(chars As *WCHAR, charIndex As Long, charCount As Long, bytes As *Byte, byteIndex As Long, byteCount As Long, flush As Boolean) As Long
    710         Dim charsUsed As Long
     473    @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
    711477        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
    747506Protected
    748507    /*!
    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 出力要素数
    754513    @param[in] flush    終了後に内部状態を初期化するかどうか
    755     @param[out] bytesUsed   使用された入力の要素数
    756     @param[out] charsUsed   出力の内、実際に書き込まれた要素数
     514    @param[out] dstUsed 使用された入力の要素数
     515    @param[out] srcUsed 出力の内、実際に書き込まれた要素数
    757516    @param[out] completed   入力の全ての文字が変換に使われたかどうか
    758517    */
    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
    764529End Class
    765530
     
    771536End Enum
    772537
    773 Class EncoderFallback
     538Namespace Detail
     539
     540/*!
     541@brief WideCharToMultiByte/MultiByteToWideCharで変換を行うエンコーディング。
     542DBCS/SBCS限定。UTF-8やUTF-7は非対応。
     543*/
     544Class WindowsCodePageEncoding
     545    Inherits Encoding
     546Public
     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
     592Private
     593    cp As DWord
    774594End Class
     595
     596Class WindowsCodePageEncoder
     597    Inherits Encoder
     598Public
     599    Sub WindowsCodePageEncoder(codepage As DWord)
     600        cp = codepage
     601    End Sub
     602
     603    Override Sub Reset()
     604        nextByte = 0
     605    End Sub
     606
     607Protected
     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
     645Private
     646    cp As DWord
     647    nextByte As Byte
     648End Class
     649
     650Class WindowsCodePageDecoder
     651    Inherits Decoder
     652Public
     653    Sub WindowsCodePageDecoder(codepage As DWord)
     654        cp = codepage
     655    End Sub
     656
     657Protected
     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
     665Private
     666    cp As DWord
     667End Class
     668
     669End Namespace
    775670
    776671End Namespace 'Text
  • trunk/ab5.0/ablib/src/Classes/index.ab

    r646 r653  
    1010#require "./ActiveBasic/Windows/Registry.ab"
    1111#require "./ActiveBasic/Windows/Windows.ab"
    12 #require "./ActiveBasic/Windows/UI/WindowHandle.ab"
     12'#require "./ActiveBasic/Windows/UI/WindowHandle.ab"
    1313#require "./ActiveBasic/Windows/Version/Version.ab"
    1414#require "./ActiveBasic/Xml/Parser.ab"
     
    8383#require "./System/IO/TextWriter.ab"
    8484#require "./System/IO/StreamReader.ab"
     85#require "./System/IO/StreamWriter.ab"
    8586#require "./System/IO/StringReader.ab"
    86 #require "./System/IO/StreamWriter.ab"
    87 #require "./System/IO/TextReader.ab"
     87#require "./System/IO/StringWriter.ab"
    8888#require "./System/Media/SystemSound.ab"
    8989#require "./System/Media/SystemSounds.ab"
     
    9393#require "./System/Text/StringBuilder.ab"
    9494#require "./System/Text/Encoding.ab"
    95 #require "./System/Text/UTF8Encoding.ab"
    96 #require "./System/Text/DecoderFallback.ab"
     95'#require "./System/Text/UTF8Encoding.ab"
    9796#require "./System/Threading/AutoResetEvent.ab"
    9897#require "./System/Threading/EventWaitHandle.ab"
Note: See TracChangeset for help on using the changeset viewer.