Changeset 385


Ignore:
Timestamp:
Nov 20, 2007, 12:04:07 AM (16 years ago)
Author:
イグトランス (egtra)
Message:

例外クラスの実装。ExceptionTestでSystem.Exceptionを使用するようにした。
StringBuilderでコメント化されていた例外を投げる処理を有効にした(除OutOfMemory)。
Str$の実装にSPrintfなどを使用するようにした。
毎回Object.ReferenceEquals(xxx, Nothing)と打つのが面倒なので、IsNothingを導入。

Location:
trunk
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/ActiveBasic/Strings/SPrintF.ab

    r384 r385  
    9090    Zero = &h4
    9191    '! 左揃え、-。フィールド内で左揃えにする。
    92     Left = &h8
     92    LeftSide = &h8
    9393    /*! 代替表記、#。
    9494    <ul>
     
    867867        If .Length < field Then
    868868            Dim embeddedSize = field - .Length
    869             If flags And Left Then
     869            If flags And LeftSide Then
    870870                .Append(&h20, embeddedSize)
    871871            Else
     
    898898    Dim sb = New System.Text.StringBuilder(
    899899        x, 0, System.Math.Min(x.Length As DWord, d) As Long, field)
    900     AdjustFieldWidth(sb, field, flags And Left)
     900    AdjustFieldWidth(sb, field, flags And LeftSide)
    901901    FormatString = sb.ToString()
    902902End Function
     
    915915    Dim sb = New System.Text.StringBuilder(field + 1)
    916916    sb.Append(x)
    917     AdjustFieldWidth(sb, field, flags And Left)
     917    AdjustFieldWidth(sb, field, flags And LeftSide)
    918918    FormatCharacter = sb.ToString()
    919919End Function
     
    10941094                flags Or= Sign
    10951095            Case &h2d '-
    1096                 flags Or = Left
     1096                flags Or = LeftSide
    10971097            Case &h26 '&
    10981098                flags Or= BPrefix
     
    11221122    If ReadInt(fmt, params, paramsCount, t) Then
    11231123        If t < 0 Then
    1124             flags Or= Left
     1124            flags Or= LeftSide
    11251125            fieldWidth = -t As DWord
    11261126        Else
  • trunk/Include/Classes/ActiveBasic/Strings/Strings.ab

    r383 r385  
    9595    ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
    9696    If ChrCmp = 0 Then
    97         ChrCmp = ( size1 - size2 ) As Long
     97        ChrCmp = (( size1 - size2 ) As LONG_PTR) As Long
    9898    End If
    9999End Function
     
    102102    ChrCmp = ChrCmp(s1, s2, System.Math.Min(size1, size2))
    103103    If ChrCmp = 0 Then
    104         ChrCmp = ( size1 - size2 ) As Long
     104        ChrCmp = (( size1 - size2 ) As LONG_PTR) As Long
    105105    End If
    106106End Function
  • trunk/Include/Classes/System/Text/StringBuilder.ab

    r383 r385  
    120120
    121121    Function Append(s As String, startIndex As Long, count As Long) As StringBuilder
    122         If s = 0 Then
    123             If startIndex = 0 And count = 0 Then
    124                 Return This
    125             Else
    126                 'Throw ArgumentNullException
    127             End If
    128         End If
    129         StringBuilder.rangeCheck2(s.Length, startIndex, count)
    130         appendCore(s, startIndex, count)
    131         Return This
     122        Return Append(StrPtr(s), startIndex, count)
    132123    End Function
    133124
     
    137128                Return This
    138129            Else
    139                 'Throw ArgumentNullException
     130                Throw New ArgumentNullException("StringBuilder.Append: An argument was null", "s")
    140131            End If
    141132        ElseIf startIndex < 0 Or count < 0 Then
    142             'Throw ArgumentOutOfRangeException
     133            Throw New ArgumentOutOfRangeException("StringBuilder.Append: One or more arguments have out of range value.", "startIndex or count or both")
    143134        End If
    144135        appendCore(s, startIndex, count)
     
    178169    Const Sub CopyTo(sourceIndex As Long, ByRef dest[] As StrChar, destIndex As Long, count As Long)
    179170        If dest = 0 Then
    180             'Throw ArgumentNullException
     171            Throw New ArgumentNullException("StringBuilder.CopyTo: An argument was null", "sourceIndex")
    181172        ElseIf size < sourceIndex + count Or sourceIndex < 0 Or destIndex < 0 Or count < 0 Then
    182             'Throw ArgumentOutOfRangeException
     173            Throw New ArgumentOutOfRangeException("StringBuilder.CopyTo: One or more arguments have out of range value.", "startIndex or count or both")
    183174        End If
    184175
     
    188179    Function EnsureCapacity(c As Long) As Long
    189180        If c < 0 Or c > MaxCapacity Then
    190             'Throw ArgumentOutOfRangeException
     181            Throw New ArgumentOutOfRangeException("StringBuilder.Append: An argument was out of range value.", "c")
    191182        ElseIf c > Capacity Then
    192183            Dim p = GC_malloc_atomic((c + 1) * SizeOf (StrChar)) As *StrChar
     
    300291        rangeCheck(index)
    301292        If n < 0 Then
    302             'Throw New ArgumentOutOfRangeException
    303             Debug
     293            Throw New ArgumentOutOfRangeException("StringBuilder.Insert: An argument was out of range value.", "n")
    304294        End If
    305295        Dim len = x.Length
     
    308298        EnsureCapacity(newSize)
    309299        separateBuffer()
    310 
    311         ' TODO: fix me!(定義されていない変数iが使われています)
    312         'ActiveBasic.Strings.ChrMove(VarPtr(chars[i + lenTotal]), VarPtr(chars[i]), (size - index) As SIZE_T)
    313300
    314301        Dim i As Long
     
    320307    End Function
    321308
    322 
    323309    Function Insert(i As Long, x As *StrChar, index As Long, count As Long) As StringBuilder
    324310        rangeCheck(i)
    325311        If x = 0 Then
    326             'Throw New ArgumentNullException
    327             Debug
     312            Throw New ArgumentNullException("StringBuilder.Insert: An argument was null", "x")
    328313        ElseIf index < 0 Or count < 0 Then
    329             'Throw New ArgumentNullException
    330             Debug
     314            Throw New ArgumentOutOfRangeException("StringBuilder.Append: One or more arguments have out of range value.", "index or count or both")
    331315        End If
    332316
     
    396380
    397381    Sub replaceCore(oldStr As String, newStr As String, start As Long, count As Long)
    398         If Object.ReferenceEquals(oldStr, Nothing) Then
    399             'Throw ArgumentNullException
    400             Debug
     382        If ActiveBasic.IsNothing(oldStr) Then
     383            Throw New ArgumentNullException("StringBuilder.Replace: An argument was null", "oldStr")
    401384        ElseIf oldStr.Length = 0 Then
    402             'Throw ArgumentException
    403             Debug
     385            Throw New ArgumentException("StringBuilder.Replace: The argument 'oldStr' is empty string. ", "oldStr")
    404386        End If
    405387
     
    412394            End If
    413395           
    414             s.appendCore(chars, curPos, nextPos)
     396            s.appendCore(chars, curPos, nextPos As Long)
    415397            s.Append(newStr)
    416             curPos += nextPos + oldStr.Length
     398            curPos += nextPos As Long + oldStr.Length
    417399        Loop
    418400        chars = s.chars
     
    446428    Sub Capacity(c As Long)
    447429        If c < size Or c > MaxCapacity Then 'sizeとの比較でcが負の場合も対応
    448             'Throw ArgumentOutOfRangeException
     430            Throw New ArgumentOutOfRangeException("StringBuilder.Append: An argument have out of range value.", "c")
    449431        End If
    450432        EnsureCapacity(c)
     
    453435    Const Function Chars(i As Long) As StrChar
    454436        If i >= Length Or i < 0 Then
    455             'Throw IndexOutOfRangeException
    456             Debug
     437            Throw New IndexOutOfRangeException("StringBuilder.Chars: The index argument 'i' have out of range value.")
    457438        End If
    458439        Return chars[i]
     
    461442    Sub Chars(i As Long, c As StrChar)
    462443        If i >= Length Or i < 0 Then
    463             'Throw ArgumentOutOfRangeException
    464             Debug
     444            Throw New ArgumentOutOfRangeException("StringBuilder.Chars: An argument have out of range value.", "i")
    465445        End If
    466446        chars[i] = c
     
    494474    Sub initialize(capacity As Long, maxCapacity = LONG_MAX As Long)
    495475        If capacity < 0 Or maxCapacity < 1 Or maxCapacity < capacity Then
    496             'Throw ArgumentOutOfRangeException
     476            Throw New ArgumentOutOfRangeException("StringBuilder constructor: One or more arguments have out of range value.", "capacity or maxCapacity or both")
    497477        End If
    498478
     
    526506    Sub rangeCheck(index As Long)
    527507        If index < 0 Or index > size Then
    528             'Throw ArgumentOutOfRangeException
    529             Debug
     508            Throw New ArgumentOutOfRangeException("StringBuilder: Index argument has out of range value.")
    530509        End If
    531510    End Sub
     
    538517        'length < 0は判定に入っていないことに注意
    539518        If startIndex < 0 Or count < 0 Or startIndex + count > length Then
    540             'Throw ArgumentOutOfRangeException
    541             Debug
     519            Throw New ArgumentOutOfRangeException("StringBuilder: One or more arguments have out of range value.", "startIndex or count or both")
    542520        End If
    543521    End Sub
  • trunk/Include/Classes/index.ab

    r371 r385  
     1#require "./ActiveBasic/misc.ab"
    12#require "./ActiveBasic/Core/InterfaceInfo.ab"
    23#require "./ActiveBasic/Core/TypeInfo.ab"
     
    1112#require "./System/Delegate.ab"
    1213#require "./System/Environment.ab"
     14#require "./System/Exception.ab"
    1315#require "./System/GC.ab"
    1416#require "./System/Math.ab"
  • trunk/Include/basic/function.sbp

    r383 r385  
    427427End Function
    428428
    429 Dim _System_ecvt_buffer[16] As StrChar
    430 Sub _ecvt_support(count As Long)
     429Sub _ecvt_support(buf As *StrChar, count As Long, size As Long)
    431430    Dim i As Long
    432     If _System_ecvt_buffer[count]=9 Then
    433         _System_ecvt_buffer[count]=0
    434         If count=0 Then
    435             For i=16 To 1 Step -1
    436                 _System_ecvt_buffer[i]=_System_ecvt_buffer[i-1]
     431    If buf[count] = 9 Then
     432        buf[count] = 0
     433        If count = 0 Then
     434            For i = size To 1 Step -1
     435                buf[i] = buf[i-1]
    437436            Next
    438             _System_ecvt_buffer[0]=1
     437            buf[0] = 1
    439438        Else
    440             _ecvt_support(count-1)
    441         End If
    442     Else
    443         _System_ecvt_buffer[count]++
     439            _ecvt_support(buf, count-1, size)
     440        End If
     441    Else
     442        buf[count]++
    444443    End If
    445444End Sub
    446 Function _ecvt(value As Double, count As Long, ByRef dec As Long, ByRef sign As Long) As *StrChar
     445
     446Sub _ecvt(buffer As *StrChar, value As Double, count As Long, ByRef dec As Long, ByRef sign As Boolean)
    447447    Dim i As Long, i2 As Long
    448 
    449     _ecvt=_System_ecvt_buffer
    450448
    451449    '値が0の場合
    452450    If value = 0 Then
    453         ActiveBasic.Strings.ChrFill(_System_ecvt_buffer, count As SIZE_T, &H30 As StrChar)
    454         _System_ecvt_buffer[count] = 0
     451        ActiveBasic.Strings.ChrFill(buffer, count As SIZE_T, &h30 As StrChar)
     452        buffer[count] = 0
    455453        dec = 0
    456454        sign = 0
     
    460458    '符号の判断(同時に符号を取り除く)
    461459    If value < 0 Then
    462         sign = 1
     460        sign = True
    463461        value = -value
    464462    Else
    465         sign = 0
     463        sign = False
    466464    End If
    467465
     
    477475    Wend
    478476
    479     For i=0 To count-1
    480         _System_ecvt_buffer[i] = Int(value) As StrChar
    481 
     477    For i = 0 To count - 1
     478        buffer[i] = Int(value) As StrChar
    482479        value = (value-CDbl(Int(value))) * 10
    483480    Next
    484     _System_ecvt_buffer[i] = 0
    485481
    486482    i--
    487483    If value >= 5 Then
    488484        '切り上げ処理
    489         _ecvt_support(i)
    490     End If
    491 
    492     For i=0 To ELM(count)
    493         _System_ecvt_buffer[i] += &H30
     485        _ecvt_support(buffer, i, count)
     486    End If
     487
     488    For i = 0 To count - 1
     489        buffer[i] += &H30
    494490    Next
    495     _System_ecvt_buffer[i] = 0
    496 End Function
     491    buffer[i] = 0
     492End Sub
    497493
    498494Function Str$(dbl As Double) As String
     
    506502        End If
    507503    End If
    508     Dim dec As Long, sign As Long
    509     Dim buffer[32] As StrChar, temp As *StrChar
    510     Dim i As Long, i2 As Long, i3 As Long
     504    Dim dec As Long, sign As Boolean
     505    Dim buffer[32] As StrChar, temp[15] As StrChar
     506    Dim i = 0 As Long
    511507
    512508    '浮動小数点を文字列に変換
    513     temp = _ecvt(dbl, 15, dec, sign)
    514 
    515     i=0
     509    _ecvt(temp, dbl, 15, dec, sign)
    516510
    517511    '符号の取り付け
     
    521515    End If
    522516
    523     If dec>15 Then
    524         '指数表示(桁が大きい場合)
     517    If dec > 15 Or dec < -3 Then
     518        '指数表示
    525519        buffer[i] = temp[0]
    526520        i++
     
    529523        ActiveBasic.Strings.ChrCopy(VarPtr(buffer[i]), VarPtr(temp[1]), 14 As SIZE_T)
    530524        i += 14
    531         buffer[i] = Asc("e")
    532         i++
    533         _stprintf(VarPtr(buffer[i]), "+%03d", dec - 1) 'ToDo: __STRING_UNICODE_WINDOWS_ANSI状態への対応
    534 
    535         Return MakeStr(buffer)
    536     End If
    537 
    538     If dec < -3 Then
    539         '指数表示(桁が小さい場合)
    540         buffer[i] = temp[0]
    541         i++
    542         buffer[i] = Asc(".")
    543         i++
    544         ActiveBasic.Strings.ChrCopy(VarPtr(buffer[i]), VarPtr(temp[1]), 14 As SIZE_T)
    545         i+=14
    546         buffer[i] = Asc("e")
    547         i++
    548         _stprintf(VarPtr(buffer[i]), "+%03d", dec - 1) 'ToDo: __STRING_UNICODE_WINDOWS_ANSI状態への対応
    549 
    550         Return MakeStr(buffer)
     525        buffer[i] = 0
     526        Return MakeStr(buffer) + ActiveBasic.Strings.SPrintf("e%+03d", New System.Int32(dec - 1))
    551527    End If
    552528
    553529    '整数部
    554     i2=dec
    555     i3=0
     530    Dim i2 = dec
     531    Dim i3 = 0
    556532    If i2>0 Then
    557533        While i2>0
     
    593569End Function
    594570
    595 Function Str$(i As Int64) As String
    596     If i < 0 Then
    597         Return "-" & Str$(-i As QWord)
    598     Else
    599         Return Str$(i As QWord)
    600     End If
     571Function Str$(x As Int64) As String
     572    Imports ActiveBasic.Strings.Detail
     573    Return FormatIntegerEx(TraitsIntegerD[1], x As QWord, 1, 0, None)
    601574End Function
    602575
    603576Function Str$(x As QWord) As String
    604     If x = 0 Then
    605         Return "0"
    606     End If
    607 
    608     Dim buf[20] As StrChar
    609     'buf[20] = 0
    610     Dim i = 19 As Long
    611     Do
    612         buf[i] = (x Mod 10 + &h30) As StrChar
    613         x \= 10
    614         If x = 0 Then
    615             Exit Do
    616         End If
    617         i--
    618     Loop
    619     Return New String(VarPtr(buf[i]), 20 - i)
     577    Imports ActiveBasic.Strings.Detail
     578    Return FormatIntegerEx(TraitsIntegerU[1], x, 1, 0, None)
    620579End Function
    621580
    622581Function Str$(x As Long) As String
    623 #ifdef _WIN64
    624     Return Str$(x As Int64)
    625 #else
    626     If x < 0 Then
    627         Return "-" & Str$(-x As DWord)
    628     Else
    629         Return Str$(x As DWord)
    630     End If
    631 #endif
     582    Imports ActiveBasic.Strings.Detail
     583    Return FormatIntegerEx(TraitsIntegerD[0], x, 1, 0, None)
    632584End Function
    633585
    634586Function Str$(x As DWord) As String
    635 #ifdef _WIN64
    636     Return Str$(x As QWord)
    637 #else
    638     If x = 0 Then
    639         Return "0"
    640     End If
    641 
    642     Dim buf[10] As StrChar
    643     buf[10] = 0
    644     Dim i = 9 As Long
    645     Do
    646         buf[i] = (x As Int64 Mod 10 + &h30) As StrChar 'Int64への型変換は#117対策
    647         x \= 10
    648         If x = 0 Then
    649             Return New String(VarPtr(buf[i]), 10 - i)
    650         End If
    651         i--
    652     Loop   
    653 #endif
     587    Imports ActiveBasic.Strings.Detail
     588    Return FormatIntegerEx(TraitsIntegerU[0], x, 1, 0, None)
    654589End Function
    655590
    656591Function Str$(x As Word) As String
    657     Return Str$(x As ULONG_PTR)
     592    Return Str$(x As DWord)
    658593End Function
    659594
    660595Function Str$(x As Integer) As String
    661     Return Str$(x As LONG_PTR)
     596    Return Str$(x As Long)
    662597End Function
    663598
    664599Function Str$(x As Byte) As String
    665     Return Str$(x As ULONG_PTR)
     600    Return Str$(x As DWord)
    666601End Function
    667602
    668603Function Str$(x As SByte) As String
    669     Return Str$(x As LONG_PTR)
     604    Return Str$(x As Long)
    670605End Function
    671606
  • trunk/Include/system/string.sbp

    r383 r385  
    99
    1010Function StrPtr(s As String) As *StrChar
    11     StrPtr = s.StrPtr
     11    If ActiveBasic.IsNothing(s) Then
     12        StrPtr = s.StrPtr
     13    End If
    1214End Function
    1315'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている
     
    3941Function GetStr(psz As PSTR, len As SIZE_T, ByRef wcs As PWSTR) As SIZE_T
    4042    If psz = 0 Then Return 0
    41     Dim lenWCS = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, len As Long, 0, 0)
     43    Dim lenWCS = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, (len As DWord) As Long, 0, 0)
    4244    wcs = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
    43     GetStr = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, len As Long, wcs, lenWCS)
     45    GetStr = MultiByteToWideChar(CP_THREAD_ACP, 0, psz, (len As DWord) As Long, wcs, lenWCS)
    4446    wcs[GetStr] = 0
    4547End Function
     
    7375Function GetStr(psz As PWSTR, len As SIZE_T, ByRef mbs As PSTR) As SIZE_T
    7476    If psz = 0 Then Return 0
    75     Dim lenMBS = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, len As Long, 0, 0, 0, 0)
     77    Dim lenMBS = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, (len As DWord) As Long, 0, 0, 0, 0)
    7678    mbs = _System_AllocForConvertedString(SizeOf (SByte) * (lenMBS + 1)) As PSTR
    77     GetStr = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, len As Long, mbs, lenMBS, 0, 0) As SIZE_T
     79    GetStr = WideCharToMultiByte(CP_THREAD_ACP, 0, psz, (len As DWord) As Long, mbs, lenMBS, 0, 0) As SIZE_T
    7880    mbs[GetStr] = 0
    7981End Function
  • trunk/TestCase/SimpleTestCase/ExceptionTest.ab

    r375 r385  
    22
    33Sub BadProc()
    4     Throw New String("hello! I am bad proc.")
     4    Throw New System.Exception("hello! I am bad proc.")
    55End Sub
    66
     
    1515            resultOfTest1 = True
    1616        End Try
    17     Catch msg As String
     17    Catch msg As System.Exception
    1818        ' msgの受け渡しが未完成
    19         If msg = "hello! I am bad proc." Then
     19        If msg.Message = "hello! I am bad proc." Then
    2020            resultOfTest2 = True
    2121        End If
    2222    Catch
    2323        System.Diagnostics.Trace.WriteLine( "Catchが実行された。ここに来るのは正しくない" )
     24        UnitTest( "到達しないコードへ到達", False)
    2425    Finally
    2526        resultOfTest3 = True
  • trunk/TestCase/SimpleTestCase/SPrintFTest.ab

    r383 r385  
    140140    s = FormatCharacter(Asc("Y"), DWORD_MAX, 0, None)
    141141    UnitTest(Ex"FormatCharacter(\qY\q)", s, "Y")
    142     s = FormatCharacter(Asc("I"), DWORD_MAX, 3, Left)
     142    s = FormatCharacter(Asc("I"), DWORD_MAX, 3, LeftSide)
    143143    UnitTest(Ex"FormatCharacter(\qI\q, Left)", s, "I  ")
    144144End Sub
Note: See TracChangeset for help on using the changeset viewer.