Ignore:
Timestamp:
Jan 13, 2009, 2:01:38 AM (15 years ago)
Author:
イグトランス (egtra)
Message:

#231に関連して、エンコーディング周りを見直し、Encoder/Decoderをストリーム用に特化。
UTF8Encodingをコンパイル可能にし、ビルドに含めるようにした。ただし、実装が不完全なためテストは不可。
(#231)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab

    r653 r676  
    4141
    4242    Override Sub Flush()
    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
     43        Flush(False)
     44    End Sub
     45
     46    Sub Flush(last As Boolean)
     47#ifdef UNICODE
     48        encoder.Encode(StrPtr(buf), buf.Length As SIZE_T, s, last)
     49        buf.Remove(0, buf.Length As SIZE_T)
     50#else
     51        Dim p = StrPtr(buf)
     52        Dim i = 0 As SIZE_T
     53        Dim mbLen = 0 As SIZE_T
     54        While i <= buf.Length
     55            mbLen = i
     56            'ヌル文字で終わっていないので、CharNextは使えない
     57            If IsDBCSLeadByte(p[i]) Then
     58                i += 2
     59            Else
     60                i += 1
     61            End If
     62        Wend
     63        Dim wcBuf As PCWSTR
     64        Dim wcLen = GetStr(p, mbLen, wcBuf)
     65        encoder.Encode(wcBuf, wcLen, s, last)
     66        buf.Remove(0, mbLen)
     67        If last Then
     68            If buf.Length <> 0 Then
     69                s.WriteByte(Asc("?"))
     70                buf.Length = 0 
     71            End If
     72        End If
     73#endif
    5974    End Sub
    6075
     
    6277    Override Sub Dispose(disposing As Boolean)
    6378        If disposing Then
    64             Flush()
    65             flushLast()
     79            Flush(True)
    6680            s.Dispose()
    6781        End If
     
    7387        buf = New Text.StringBuilder(4096)
    7488        '暫定。正式版ではUTF-8を標準とする。
    75         encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
    76         encoder = encoding.GetEncoder()
     89        encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
    7790    End Sub
    7891
    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
    9192    encoder As Text.Encoder
    9293    s As System.IO.Stream
Note: See TracChangeset for help on using the changeset viewer.