Ignore:
Timestamp:
Jan 5, 2009, 1:40:11 AM (16 years ago)
Author:
イグトランス (egtra)
Message:

Encoder/Decoderを排除した実装(UTF8Encodingは修正していません)。

Location:
branch/egtra-stream-without-en_dec/Classes/System/IO
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branch/egtra-stream-without-en_dec/Classes/System/IO/StreamReader.ab

    r666 r673  
    5858    */
    5959    Override Function Underflow() As Boolean
    60         Dim wcBuf[4095] As WCHAR
    61         Dim mbBuf[4095] As SByte
    62         Dim pNext = mbBuf As PSTR
    63         Dim mbBufSize = Len(buf)
    64         If leadByte <> 0 Then
    65             pNext[0] = leadByte
    66             leadByte = 0
    67             pNext++
    68             mbBufSize--
    69         End If
    70         Dim mbLen = s.Read(pNext As *Byte, 0, mbBufSize)
    71         If mbLen = 0 Then
     60        Dim nextRead As *Byte
     61        Dim readSize = s.Read(mbBuf + bufSize, 0, bufCapacity - bufSize)
     62        If readSize = 0 Then
     63            If bufSize <> 0 Then
     64#ifdef UNCODE
     65                Buffer.Append(&hfffd As WCHAR)
     66#else
     67                Buffer.Append("?")
     68#endif
     69            End If
    7270            Underflow = False
    7371            Exit Function
    7472        End If
     73        Dim mbLen = bufSize + readSize
     74        '文字全体が揃っている部分を抽出する。
     75        Dim pNext = mbBuf
    7576        Do
    76             Dim q = CharNextExA(cp, pNext, 0)
     77            Dim q = e.NextChar(pNext, ((mbBuf + mbLen) - pNext) As Long)
    7778            If q = pNext Then
    7879                Exit Do
    7980            End If
    80             pNext = q As PSTR
     81            pNext = q
    8182        Loop
    82         If pNext <> mbBuf + mbLen Then
    83             leadByte = mbBuf[mbLen - 1]
     83        '文字全体が揃っている部分を変換しTextReaderのバッファに送る。
     84        Dim wcBufCapacity = e.GetMaxCharCount(mbLen)
     85        Dim wcBuf = GC_malloc_atomic(wcBufCapacity * SizeOf (WCHAR)) As *WCHAR
     86        Dim wcLen = e.GetChars(mbBuf, (pNext - mbBuf) As Long, wcBuf, wcBufCapacity)
     87        Dim appendStr As PCTSTR
     88        Dim appendLen = GetStr(wcBuf, wcLen As SIZE_T, appendStr)
     89        Dim readerBuffer = Buffer
     90        readerBuffer.Append(appendStr, 0, appendLen)
     91        'マルチバイトの揃っていない部分を手前に連れてくる。
     92        bufSize = ((mbBuf + mbLen) - pNext) As Long
     93        If bufSize <> 0 Then
     94            memmove(mbBuf, mbBuf + mbLen, bufSize)
    8495        End If
    85         Dim wcLen = MultiByteToWideChar(cp, 0, mbBuf, (pNext - mbBuf) As Long, wcBuf, 4095)
    86         Dim s = New String(wcBuf, wcLen)
    87         Dim buf = Buffer
    88         buf.Append(s)
    8996        Underflow = True
    9097    End Function
     
    97104    Sub init(str As Stream)
    98105        s = str
    99         cp  = CP_ACP '暫定。
    100         leadByte = 0
     106        e = New Text.Detail.WindowsCodePageEncoding(CP_ACP) '暫定。
     107        bufCapacity = 8192
     108        mbBuf = GC_malloc_atomic(bufCapacity) As *Byte
     109        bufSize = 0
    101110    End Sub
    102111
    103112    s As Stream
    104     cp As Word
    105     leadByte As Byte
     113    e As Text.Encoding
     114    mbBuf As *Byte
     115    bufSize As Long
     116    bufCapacity As Long
    106117End Class
    107118
  • branch/egtra-stream-without-en_dec/Classes/System/IO/StreamWriter.ab

    r653 r673  
    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        softFlush()
     44        s.Flush()
     45    End Sub
     46
     47    Override Sub WriteLine()
     48        Super.WriteLine()
     49        softFlush()
    5950    End Sub
    6051
     
    6354        If disposing Then
    6455            Flush()
    65             flushLast()
    6656            s.Dispose()
    6757        End If
     
    7363        buf = New Text.StringBuilder(4096)
    7464        '暫定。正式版ではUTF-8を標準とする。
    75         encoding = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
    76         encoder = encoding.GetEncoder()
     65        e = New Text.Detail.WindowsCodePageEncoding(CP_ACP)
    7766    End Sub
    7867
    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
     68    Sub softFlush()
     69        Dim buf = Buffer()
     70        Dim pws As PCWSTR
     71        Dim wcSize = GetWCStr(buf.ToString(), pws)
     72        Dim mbMaxSize = e.GetMaxByteCount(wcSize)
     73        Dim mbBuf = GC_malloc_atomic(mbMaxSize) As *Byte
     74        Dim mbBufSize = e.GetBytes(pws, wcSize, mbBuf, mbMaxSize)
     75        s.Write(mbBuf, 0, mbBufSize)
    8876    End Sub
    8977
    90     encoding As Text.Encoding
    91     encoder As Text.Encoder
     78    e As Text.Encoding
    9279    s As System.IO.Stream
    9380End Class
  • branch/egtra-stream-without-en_dec/Classes/System/IO/TextWriter.ab

    r665 r673  
    8686    End Sub
    8787
    88     Sub WriteLine()
     88    Virtual Sub WriteLine()
    8989        Write(Environment.NewLine)
    9090    End Sub
Note: See TracChangeset for help on using the changeset viewer.