Ignore:
Timestamp:
Nov 25, 2007, 4:31:35 PM (17 years ago)
Author:
イグトランス (egtra)
Message:

Stringなどで例外を投げるようにした。
#147の解決。
CType ASCII文字判定関数群の追加。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/Classes/System/String.ab

    r383 r388  
    6161        Sub String(initStr As PCWSTR, start As Long, length As Long)
    6262            If start < 0 Or length Or start + length < 0 Then
    63                 'Throw New ArgumentOutOfRangeException
     63                Throw New ArgumentOutOfRangeException("String constractor: One or more arguments are out of range value.", "start or length or both")
    6464            End If
    6565            validPointerCheck(initStr + start, length)
     
    7878
    7979        Sub String(initStr As PCSTR, start As Long, length As Long)
    80             If start < 0 Or length Or start + length < 0 Then
    81                 'Throw New ArgumentOutOfRangeException
     80            If start < 0 Or length < 0 Then
     81                Throw New ArgumentOutOfRangeException("String constructor: One or more arguments are out of range value.", "start or length or both")
    8282            End If
    8383            validPointerCheck(initStr + start, length)
     
    9797        End Sub
    9898
    99         Sub String(sb As System.Text.StringBuilder)
     99        Sub String(sb As Text.StringBuilder)
    100100            Chars = StrPtr(sb)
    101101            m_Length = sb.Length
     
    246246
    247247        Function CompareTo(y As Object) As Long
    248             Dim s = y As String
    249     '       If y is not String Then
    250     '           Throw New ArgumentException
    251     '       End If
     248            If Not Object.Equals(This.GetType(), y.GetType()) Then
     249                Throw New ArgumentException("String.CompareTo: An argument is out of range value.", "y")
     250            End If
    252251            Return CompareTo(y As String)
    253252        End Function
     
    348347        Const Function Contains(s As String) As Boolean
    349348            If Object.ReferenceEquals(s, Nothing) Then
    350                 'Throw New ArgumentNullException
    351             End If
    352             Return IndexOf(s, 0, m_Length) >= 0
     349                Throw New ArgumentNullException("String.Contains: An argument is out of range value.", "s")
     350            ElseIf s = "" Then
     351                Return True
     352            Else
     353                Return IndexOf(s, 0, m_Length) >= 0
     354            End If
    353355        End Function
    354356
     
    385387            rangeCheck(startIndex, count)
    386388            If Object.ReferenceEquals(s, Nothing) Then
    387                 'Throw New ArgumentNullException
    388                 Debug
     389                Throw New ArgumentNullException("String.IndexOf: An argument is out of range value.", "s")
    389390            End If
    390391
     
    405406        End Function
    406407
     408        Const Function LastIndexOf(c As StrChar) As Long
     409            Return lastIndexOf(c, m_Length - 1, m_Length)
     410        End Function
     411
     412        Const Function LastIndexOf(c As StrChar, start As Long) As Long
     413            rangeCheck(start)
     414            Return lastIndexOf(c, start, start + 1)
     415        End Function
     416
     417        Const Function LastIndexOf(c As StrChar, start As Long, count As Long) As Long
     418            rangeCheck(start)
     419            Dim lastFindPos = start - (count - 1)
     420            If Not (m_Length > lastFindPos And lastFindPos >= 0) Then
     421                Throw New ArgumentOutOfRangeException("String.LastIndexOf: An argument is out of range value.", "count")
     422            End If
     423            Return lastIndexOf(c, start, count)
     424        End Function
     425    Private
     426        Const Function lastIndexOf(c As StrChar, start As Long, count As Long) As Long
     427            Dim lastFindPos = start - (count - 1)
     428            Dim i As Long
     429            For i = start To lastFindPos Step -1
     430                If Chars[i] = c Then
     431                    Return i
     432                End If
     433            Next
     434            Return -1
     435        End Function
     436
     437    Public
    407438        Const Function LastIndexOf(s As String) As Long
    408439            Return LastIndexOf(s, m_Length - 1, m_Length)
     
    413444        End Function
    414445
    415         Const Function LastIndexOf(s As String, startIndex As Long, count As Long) As Long
     446        Const Function LastIndexOf(s As String, start As Long, count As Long) As Long
    416447            If Object.ReferenceEquals(s, Nothing) Then
    417                 'Throw New ArgumentNullException
    418                 Debug
    419             End If
    420 
    421             If startIndex < 0 Or startIndex > m_Length - 1 Or _
    422                 count < 0 Or count > startIndex + 2 Then
    423                 'Throw New ArgumentOutOfRangeException
    424                 Debug
    425             End If
    426             Dim length = s.Length
     448                Throw New ArgumentNullException("String.LastIndexOf: An argument is out of range value.", "s")
     449            End If
     450
     451            If start < 0 Or start > m_Length - 1 Or _
     452                count < 0 Or count > start + 2 Then
     453                Throw New ArgumentOutOfRangeException("String.LastIndexOf: One or more arguments are out of range value.", "start or count or both")
     454            End If
     455            Dim length = s.m_Length
    427456            If length > m_Length Then Return -1
    428             If length = 0 Then Return startIndex
     457            If length = 0 Then Return start
    429458
    430459            Dim i As Long, j As Long
    431             For i = startIndex To  startIndex - count + 1 Step -1
     460            For i = start To  start - count + 1 Step -1
    432461                For j = length - 1 To 0 Step -1
    433462                    If Chars[i + j] = s[j] Then
     
    450479
    451480        Const Function Insert(startIndex As Long, text As String) As String
    452             Dim sb = New System.Text.StringBuilder(This)
     481            Dim sb = New Text.StringBuilder(This)
    453482            sb.Insert(startIndex, text)
    454483            Return sb.ToString
     
    471500
    472501        Const Function Remove(startIndex As Long, count As Long) As String
    473             Dim sb = New System.Text.StringBuilder(This)
     502            Dim sb = New Text.StringBuilder(This)
    474503            sb.Remove(startIndex, count)
    475504            Remove = sb.ToString
     
    486515
    487516        Const Function Replace(oldChar As StrChar, newChar As StrChar) As String
    488             Dim sb = New System.Text.StringBuilder(This)
     517            Dim sb = New Text.StringBuilder(This)
    489518            sb.Replace(oldChar, newChar)
    490519            Replace = sb.ToString
     
    492521
    493522        Const Function Replace(oldStr As String, newStr As String) As String
    494             Dim sb = New System.Text.StringBuilder(This)
     523            Dim sb = New Text.StringBuilder(This)
    495524            sb.Replace(oldStr, newStr)
    496525            Return sb.ToString
     
    498527
    499528        Const Function ToLower() As String
    500             Dim sb = New System.Text.StringBuilder(m_Length)
     529            Dim sb = New Text.StringBuilder(m_Length)
    501530            sb.Length = m_Length
    502531            Dim i As Long
    503532            For i = 0 To ELM(m_Length)
    504                 sb[i] = _System_ASCII_ToLower(Chars[i])
     533                sb[i] = ActiveBasic.CType.ToLower(Chars[i])
    505534            Next
    506535            Return sb.ToString
     
    508537
    509538        Const Function ToUpper() As String
    510             Dim sb = New System.Text.StringBuilder(m_Length)
     539            Dim sb = New Text.StringBuilder(m_Length)
    511540            sb.Length = m_Length
    512541            Dim i As Long
    513542            For i = 0 To ELM(m_Length)
    514                 sb[i] = _System_ASCII_ToUpper(Chars[i])
     543                sb[i] = ActiveBasic.CType.ToUpper(Chars[i])
    515544            Next
    516545            Return sb.ToString
     
    539568            Dim size = m_Length
    540569#endif
    541             Return _System_GetHashFromWordArray(Chars As *Word, size) Xor size
     570            Return _System_GetHashFromWordArray(Chars As *Word, size) Xor m_Length
    542571        End Function
    543572
     
    548577        Function PadLeft(total As Long, c As StrChar) As String
    549578            If total < 0 Then
    550                 'Throw New ArgumentException
     579                Throw New ArgumentOutOfRangeException("String.PadLeft: An arguments is out of range value.", "total")
    551580            End If
    552581            If total >= m_Length Then
    553582                Return This
    554583            End If
    555             Dim sb = New System.Text.StringBuilder(total)
     584            Dim sb = New Text.StringBuilder(total)
    556585            sb.Append(c, total - m_Length)
    557586            sb.Append(This)
     
    565594        Function PadRight(total As Long, c As StrChar) As String
    566595            If total < 0 Then
    567                 'Throw New ArgumentException
     596                Throw New ArgumentOutOfRangeException("String.PadRight: An arguments is out of range value.", "total")
    568597            End If
    569598            If total >= m_Length Then
    570599                Return This
    571600            End If
    572             Dim sb = New System.Text.StringBuilder(total)
     601            Dim sb = New Text.StringBuilder(total)
    573602            sb.Append(This)
    574603            sb.Append(c, total - m_Length)
     
    596625        Const Sub rangeCheck(index As Long)
    597626            If index < 0 Or index > m_Length Then
    598                 Debug 'ArgumentOutOfRangeException
     627                Throw New ArgumentOutOfRangeException("String: An arguments is out of range value.", "index")
    599628            End If
    600629        End Sub
     
    602631        Const Sub rangeCheck(start As Long, length As Long)
    603632            If start < 0 Or start > This.m_Length Or length < 0 Then
    604                 Debug 'ArgumentOutOfRangeException
     633                Throw New ArgumentOutOfRangeException("String: One or more arguments are out of range value.", "start or length or both")
    605634            End If
    606635        End Sub
Note: See TracChangeset for help on using the changeset viewer.