Changeset 682


Ignore:
Timestamp:
Feb 16, 2009, 4:26:24 PM (16 years ago)
Author:
イグトランス (egtra)
Message:

UTF8Encodingクラスをとりあえず使える状態に。ただし、BOM出力はまだ不可能。
(#231)

Location:
trunk/ab5.0/ablib
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/TestCase/SimpleTestCase/EncodingTest.ab

    r653 r682  
    11Imports System.Text
    22Imports System.Text.Detail
    3 /*
     3
    44Namespace EncodingTest
    55
     
    2727
    2828    Dim utf8 = New UTF8Encoding
    29     Dim e = utf8.GetEncoder
    30     Dim d = utf8.GetDecoder
     29    Dim e = utf8.GetEncoder()
     30    Dim d = utf8.GetDecoder()
    3131    Dim b[256] As Byte
    3232    Dim u16[256] As WCHAR
    33     e.GetBytes(chars, Len(chars) \ SizeOf (WCHAR), b, Len(b), False)
    34     UnitTest("UTF8 Encode", memcmp(b, utf8bytes, Len(utf8bytes)) = 0)
    35     d.GetChars(utf8bytes, Len(utf8bytes), u16, Len(u16) \ SizeOf (WCHAR), False)
    36     UnitTest("UTF8 Decode", memcmp(u16, chars, Len(chars)) = 0)
     33    Dim len As Long
     34
     35    len = utf8.GetBytesCount(chars, Len(chars) \ SizeOf(WCHAR))
     36    UnitTest("UTF-8 GetByteCount", len = Len(utf8bytes))
     37    utf8.GetBytes(chars, Len(chars) \ SizeOf (WCHAR), b, Len(b))
     38    UnitTest("UTF-8 Encode", memcmp(b, utf8bytes, Len(utf8bytes)) = 0)
     39
     40    len = utf8.GetCharsCount(utf8bytes, Len(utf8bytes))
     41    UnitTest("UTF-8 GetCharCount", len = Len(chars) \ SizeOf(WCHAR))
     42    utf8.GetChars(utf8bytes, Len(utf8bytes), u16, Len(u16) \ SizeOf (WCHAR))
     43    UnitTest("UTF-8 Decode", memcmp(u16, chars, Len(chars)) = 0)
    3744End Sub
    3845
     
    4047
    4148EncodingTest.TestMain()
    42 */
     49
  • trunk/ab5.0/ablib/src/Classes/System/Exception.ab

    r602 r682  
    1414Const AB_E_PLATFORMNOTSUPPORTED = &h80041539 '80131539
    1515Const AB_E_KEYNOTFOUND = &h80041577 '80131577
     16Const AB_E_ARITHMETIC = &h80040216 '80070216
     17Const AB_E_OVERFLOW = &h80041516 '80131516
    1618
    1719End Namespace
     
    3840MulticastNotSupportedException
    3941NullReferenceException
    40 OverflowException
    4142RankException
    4243StackOverflowException
     
    712713End Class
    713714
     715/*!
     716@brief  算術演算例外
     717@author Egtra
     718@date   2009/02/16
     719*/
     720Class ArithmeticException
     721    Inherits SystemException
     722Public
     723    /*!
     724    @biref  コンストラクタ
     725    */
     726    Sub ArithmeticException()
     727        SystemException("ArithmeticException", Nothing)
     728        HResult = ActiveBasic.AB_E_ARITHMETIC
     729    End Sub
     730    /*!
     731    @biref  コンストラクタ
     732    @param[in] message  エラーメッセージ
     733    */
     734    Sub ArithmeticException(message As String)
     735        SystemException(message, Nothing)
     736        HResult = ActiveBasic.AB_E_ARITHMETIC
     737    End Sub
     738    /*!
     739    @biref  コンストラクタ
     740    @param[in] message  エラーメッセージ
     741    @param[in] innerException   内部例外
     742    */
     743    Sub ArithmeticException(message As String, innerException As Exception)
     744        SystemException(message, innerException)
     745        HResult = ActiveBasic.AB_E_ARITHMETIC
     746    End Sub
     747End Class
     748
     749/*!
     750@brief  オーバーフロー例外
     751@author Egtra
     752@date   2009/02/16
     753*/
     754Class OverflowException
     755    Inherits ArithmeticException
     756Public
     757    /*!
     758    @biref  コンストラクタ
     759    */
     760    Sub OverflowException()
     761        ArithmeticException("OverflowException", Nothing)
     762        HResult = ActiveBasic.AB_E_OVERFLOW
     763    End Sub
     764    /*!
     765    @biref  コンストラクタ
     766    @param[in] message  エラーメッセージ
     767    */
     768    Sub OverflowException(message As String)
     769        ArithmeticException(message, Nothing)
     770        HResult = ActiveBasic.AB_E_OVERFLOW
     771    End Sub
     772    /*!
     773    @biref  コンストラクタ
     774    @param[in] message  エラーメッセージ
     775    @param[in] innerException   内部例外
     776    */
     777    Sub OverflowException(message As String, innerException As Exception)
     778        ArithmeticException(message, innerException)
     779        HResult = ActiveBasic.AB_E_OVERFLOW
     780    End Sub
     781End Class
    714782
    715783End Namespace
  • trunk/ab5.0/ablib/src/Classes/System/IO/MemoryStream.ab

    r617 r682  
    339339    End Function*/
    340340
     341    Function ToPointer() As VoidPtr
     342        ToPointer = pointer
     343    End Function
     344
    341345    /*!
    342346    @brief  ストリームの内容全体をまるごと別のストリームに書き込みます。
  • trunk/ab5.0/ablib/src/Classes/System/IO/Stream.ab

    r609 r682  
    155155End Class
    156156
     157/*!
     158@brief 書き込みバイト数を記憶するヌルストリーム
     159Encoding関連で使用
     160@date 2009/02/16
     161@auther Egtra
     162*/
     163Class WriteCountingNullStream
     164    Inherits NullStream
     165Public
     166    Sub WriteCountingNullStream()
     167        writeCount = 0
     168    End Sub
     169
     170    Override Sub Write(buffer As *Byte, offset As Long, count As Long)
     171        If count < 0 Then
     172            Throw New ArgumentOutOfRangeException("count")
     173        End If
     174        writeCount += count
     175    End Sub
     176
     177    Override Sub WriteByte(b As Byte)
     178        writeCount++
     179    End Sub
     180
     181    /*!
     182    @brief 書き込まれたバイト数を返す。
     183    */
     184    Function WriteCount() As Long
     185        WriteCount = writeCount
     186    End Function
     187
     188Private
     189    writeCount As Long
     190End Class
     191
    157192End Namespace
    158193
  • trunk/ab5.0/ablib/src/Classes/System/IO/StreamWriter.ab

    r676 r682  
    2020    */
    2121    Sub StreamWriter(path As String)
    22         init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
     22        init(New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None), Nothing)
    2323    End Sub
    2424
     
    2828    */
    2929    Sub StreamWriter(stream As Stream)
    30         init(stream)
     30        init(stream, Nothing)
     31    End Sub
     32
     33    /*
     34    @date 2009/02/16
     35    @auther Egtra
     36    */
     37    Sub StreamWriter(stream As Stream, encoding As Text.Encoding)
     38        init(stream, encoding)
    3139    End Sub
    3240
     
    8391
    8492Private
    85     Sub init(stream As Stream)
     93    Sub init(stream As Stream, e As Text.Encoding)
    8694        s = stream
    8795        buf = New Text.StringBuilder(4096)
    88         '暫定。正式版ではUTF-8を標準とする。
    89         encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
     96        If ActiveBasic.IsNothing(e) Then
     97            '暫定。正式版ではUTF-8を標準とする。
     98            encoder = New Text.Detail.WindowsCodePageEncoder(CP_ACP)
     99        Else
     100            encoder = e.GetEncoder()
     101        End If
    90102    End Sub
    91103
  • trunk/ab5.0/ablib/src/Classes/System/Text/Encoding.ab

    r676 r682  
    7474    @date   2007/12/08
    7575    */
    76     Abstract Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
     76    Virtual Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
     77        Dim s = New IO.Detail.WriteCountingNullStream
     78        GetEncoder().Encode(src, srcCount, s, True)
     79        GetBytesCountCore = s.WriteCount
     80        If GetBytesCountCore < 0 Then
     81            Throw New OverflowException
     82        End If
     83    End Function
    7784Public
    7885    /*!
     
    110117    @date   2007/12/08
    111118    */
    112     Abstract Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
     119    Virtual Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
     120        Dim s = New IO.MemoryStream
     121        GetEncoder().Encode(src, srcCount, s, True)
     122        Dim size = s.Position()
     123        If size > (dstCount As QWord) Then
     124            Throw New ArgumentException("dstCount")
     125        End If
     126        GetBytesCore = size As Long
     127        memcpy(dst, s.ToPointer(), size As SIZE_T)
     128    End Function
    113129Public
    114130    /*!
     
    136152    @date   2007/12/08
    137153    */
    138     Abstract Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
     154    Virtual Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
     155        Dim s = New IO.MemoryStream(src, srcCount)
     156        Dim l = New Collections.Generic.List<WCHAR>
     157        GetDecoder().Decode(l, s)
     158        GetCharsCountCore = l.Count
     159    End Function
    139160Public
    140161    /*!
     
    172193    @date   2007/12/08
    173194    */
    174     Abstract Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
     195    Virtual Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
     196        Dim s = New IO.MemoryStream(src, srcCount)
     197        Dim l = New Collections.Generic.List<WCHAR>
     198        GetDecoder().Decode(l, s)
     199        If l.Count > dstCount Then
     200            Throw New ArgumentException("dstCount")
     201        End If
     202        Dim p As *WCHAR
     203        p = l
     204        memcpy(dst, p, (l.Count As SIZE_T) * 2)
     205    End Function
    175206Public
    176207#ifdef UNICODE
  • trunk/ab5.0/ablib/src/Classes/System/Text/UTF8Encoding.ab

    r676 r682  
    111111                            dst.Add((&hDC00 Or (buffer And &h3FF)) As WCHAR)
    112112                        Else
    113                             '最短形式でないもの、4バイト形式で10FFFFを超えるコードポイントのもの
     113                            '最短形式でないもの、または4バイト形式で10FFFFを超えるコードポイントのもの
    114114                            dst.Add(&hfffd As WCHAR)
    115115                        End If
     
    196196    End Function
    197197*/
    198 Protected
    199     Override Function GetBytesCountCore(src As *WCHAR, srcCount As Long) As Long
    200     End Function
    201 
    202     Override Function GetBytesCore(src As *WCHAR, srcCount As Long, dst As *Byte, dstCount As Long) As Long
    203     End Function
    204 
    205     Override Function GetCharsCountCore(src As *Byte, srcCount As Long) As Long
    206     End Function
    207 
    208     Override Function GetCharsCore(src As *Byte, srcCount As Long, dst As *WCHAR, dstCount As Long) As Long
    209     End Function
    210198
    211199Private
Note: See TracChangeset for help on using the changeset viewer.