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

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.