Ignore:
Timestamp:
Mar 4, 2007, 11:28:44 PM (18 years ago)
Author:
イグトランス (egtra)
Message:

String型の自身を変更するメソッドを、戻り値で返すように変更。
併せて文字列比較を自前の関数で行うように変更。
プロンプトのキャレットの位置計算が正しくなかったバグを修正。

File:
1 edited

Legend:

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

    r125 r132  
     1' Classes/System/String.ab
     2
     3#require <basic/function.sbp>
     4
    15Class String
    26    m_Length As Long
     
    5761    End Sub
    5862
    59     Const Function Operator[] (n As Long) As Char
     63    Const Function Operator [] (n As Long) As Char
    6064        Return Chars[n]
    6165    End Function
    6266
    63     Sub Operator[]= (n As Long, c As Char)
     67    Sub Operator []= (n As Long, c As Char)
    6468        Chars[n] = c
    6569    End Sub
    6670
    67     Const Function Operator + (lpszText As *Char) As String
    68         Return Concat(lpszText, lstrlen(lpszText))
     71    Const Function Operator + (pszText As *Char) As String
     72        Return Concat(pszText, lstrlen(pszText))
    6973    End Function
    7074
     
    7377    End Function
    7478
    75     Const Function Operator & (lpszText As *Char) As String
    76         Dim tempString = This + lpszText
     79    Const Function Operator & (pszText As *Char) As String
     80        Dim tempString = This + pszText
    7781        Return tempString
    7882    End Function
     
    8387    End Function
    8488
    85     Function Operator== (ByRef objString As String) As Long
    86         If lstrcmp(This, objString) = 0 Then
    87             Return _System_TRUE
    88         Else
    89             Return _System_FALSE
    90         End If
    91     End Function
    92 
    93     Function Operator== (lpszText As *Char) As Long
    94         If lstrcmp(This, lpszText) = 0 Then
    95             Return _System_TRUE
    96         Else
    97             Return _System_FALSE
    98         End If
    99     End Function
    100 
    101     Function Operator<> (ByRef objString As String) As Long
    102         Return lstrcmp(This, objString)
    103     End Function
    104 
    105     Function Operator<> (lpszText As *Char) As Long
    106         Return lstrcmp(This, lpszText)
    107     End Function
    108 
    109     Function Operator< (ByRef objString As String) As Long
    110         If lstrcmp(This, objString) < 0 Then
    111             Return _System_TRUE
    112         Else
    113             Return _System_FALSE
    114         End If
    115     End Function
    116 
    117     Function Operator< (lpszText As *Char) As Long
    118         If lstrcmp(This, lpszText) < 0 Then
    119             Return _System_TRUE
    120         Else
    121             Return _System_FALSE
    122         End If
    123     End Function
    124 
    125     Function Operator> (ByRef objString As String) As Long
    126         If lstrcmp(This, objString) > 0 Then
    127             Return _System_TRUE
    128         Else
    129             Return _System_FALSE
    130         End If
    131     End Function
    132 
    133     Function Operator> (lpszText As *Char) As Long
    134         If lstrcmp(This, lpszText) > 0 Then
    135             Return _System_TRUE
    136         Else
    137             Return _System_FALSE
    138         End If
    139     End Function
    140 
    141     Function Operator<= (ByRef objString As String) As Long
    142         If lstrcmp(This, objString) <= 0 Then
    143             Return _System_TRUE
    144         Else
    145             Return _System_FALSE
    146         End If
    147     End Function
    148 
    149     Function Operator<= (lpszText As *Char) As Long
    150         If lstrcmp(This, lpszText) <= 0 Then
    151             Return _System_TRUE
    152         Else
    153             Return _System_FALSE
    154         End If
    155     End Function
    156 
    157     Function Operator>= (ByRef objString As String) As Long
    158         If lstrcmp(This, objString) => 0 Then
    159             Return _System_TRUE
    160         Else
    161             Return _System_FALSE
    162         End If
    163     End Function
    164 
    165     Function Operator>= (lpszText As *Char) As Long
    166         If lstrcmp(This, lpszText) => 0 Then
    167             Return _System_TRUE
    168         Else
    169             Return _System_FALSE
    170         End If
    171     End Function
    172 
    173     Function StrPtr() As *Char
     89    Const Function Operator == (ByRef objString As String) As Boolean
     90        Return _System_StrCmp(This, objString) = 0
     91    End Function
     92
     93    Const Function Operator == (text As *Char) As Long
     94        Return _System_StrCmp(This, text) = 0
     95    End Function
     96
     97    Const Function Operator <> (ByRef objString As String) As Boolean
     98        Return _System_StrCmp(This, objString)
     99    End Function
     100
     101    Const Function Operator <> (text As *Char) As Boolean
     102        Return _System_StrCmp(This, text)
     103    End Function
     104
     105    Const Function Operator < (ByRef objString As String) As Boolean
     106        Return _System_StrCmp(This, objString) < 0
     107    End Function
     108
     109    Const Function Operator < (text As *Char) As Boolean
     110        Return _System_StrCmp(This, text) < 0
     111    End Function
     112
     113    Const Function Operator > (ByRef objString As String) As Boolean
     114        Return _System_StrCmp(This, objString) > 0
     115    End Function
     116
     117    Const Function Operator > (text As *Char) As Boolean
     118        Return _System_StrCmp(This, text) > 0
     119    End Function
     120
     121    Const Function Operator <= (ByRef objString As String) As Boolean
     122        Return _System_StrCmp(This, objString) <= 0
     123    End Function
     124
     125    Const Function Operator <= (text As *Char) As Boolean
     126        Return _System_StrCmp(This, text) <= 0
     127    End Function
     128
     129    Const Function Operator >= (ByRef objString As String) As Boolean
     130        Return _System_StrCmp(This, objString) => 0
     131    End Function
     132
     133    Const Function Operator >= (text As *Char) As Boolean
     134        Return _System_StrCmp(This, text) => 0
     135    End Function
     136
     137    Const Function StrPtr() As *Char
    174138        Return Chars
    175139    End Function
     
    248212    End Sub
    249213
    250     Function Concat(lpszText As *Char, textLength As Long) As String
     214    Const Function Clone() As String
     215        Return This
     216    End Function
     217
     218    Const Function Concat(lpszText As *Char, textLength As Long) As String
    251219        Dim tempString As String
    252220        With tempString
     
    259227    End Function
    260228
    261     Function Contains(ByRef objString As String) As BOOL
    262         If IndexOf(objString, 0, m_Length) >= 0 Then
    263             Return _System_TRUE
    264         Else
    265             Return _System_FALSE
    266         End If
    267     End Function
    268 
    269     Function Contains(lpszText As *Char) As BOOL
    270         If IndexOf(lpszText, 0, m_Length) >= 0 Then
    271             Return _System_TRUE
    272         Else
    273             Return _System_FALSE
    274         End If
    275     End Function
    276 
    277     Function IndexOf(lpszText As *Char) As Long
     229    Const Function Contains(ByRef objString As String) As Boolean
     230        Return IndexOf(objString, 0, m_Length) >= 0
     231    End Function
     232
     233    Const Function Contains(lpszText As *Char) As Boolean
     234        Return IndexOf(lpszText, 0, m_Length) >= 0
     235    End Function
     236
     237    Const Function IndexOf(lpszText As *Char) As Long
    278238        Return IndexOf(lpszText, 0, m_Length)
    279239    End Function
    280240
    281     Function IndexOf(lpszText As *Char, startIndex As Long) As Long
     241    Const Function IndexOf(lpszText As *Char, startIndex As Long) As Long
    282242        Return IndexOf(lpszText, startIndex, m_Length - startIndex)
    283243    End Function
    284244
    285     Function IndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
    286         Dim length As Long
    287         length = lstrlen(lpszText)
     245    Const Function IndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
     246        Dim length = lstrlen(lpszText)
    288247
    289248        If startIndex < 0 Then Return -1
     
    306265    End Function
    307266
    308     Function LastIndexOf(lpszText As *Char) As Long
     267    Const Function LastIndexOf(lpszText As *Char) As Long
    309268        Return LastIndexOf(lpszText, m_Length - 1, m_Length)
    310269    End Function
    311270
    312     Function LastIndexOf(lpszText As *Char, startIndex As Long) As Long
     271    Const Function LastIndexOf(lpszText As *Char, startIndex As Long) As Long
    313272        Return LastIndexOf(lpszText As *Char, startIndex, startIndex + 1)
    314273    End Function
    315274
    316     Function LastIndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
    317         Dim length As Long
    318         length = lstrlen(lpszText)
     275    Const Function LastIndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
     276        Dim length = lstrlen(lpszText)
    319277
    320278        If startIndex < 0 Or startIndex > m_Length - 1 Then Return -1
     
    337295    End Function
    338296
    339     Function StartsWith(lpszText As *Char) As BOOL
    340         If IndexOf(lpszText) = 0 Then
    341             Return _System_TRUE
    342         Else
    343             Return _System_FALSE
    344         End If
    345     End Function
    346 
    347     Function EndsWith(lpszText As *Char) As BOOL
    348         If LastIndexOf(lpszText) = m_Length - lstrlen(lpszText) Then
    349             Return _System_TRUE
    350         Else
    351             Return _System_FALSE
    352         End If
    353     End Function
    354 
    355     Function Insert(startIndex As Long, text As *Char) As Long
    356         Dim length As Long
    357         length = lstrlen(text)
    358 
    359         If startIndex < 0 Or startIndex > m_Length Then Return -1
    360 
    361         Dim newChars As *Char
    362         newChars = _System_malloc(SizeOf (Char) * (length + m_Length + 1))
    363         If newChars = 0 Then Return -1
    364 
    365         memcpy(newChars, Chars, SizeOf (Char) * startIndex)
    366         memcpy(VarPtr(newChars[startIndex]), text, SizeOf (Char) * length)
    367         memcpy(VarPtr(newChars[startIndex + length]), VarPtr(Chars[startIndex]), SizeOf (Char) * (m_Length - startIndex + 1))
    368 
    369         _System_free(Chars)
    370         Chars = newChars
    371         m_Length = length + m_Length
    372         Return m_Length
    373     End Function
    374 
    375     Function SubString(startIndex As Long) As String
     297    Const Function StartsWith(lpszText As *Char) As Boolean
     298        Return IndexOf(lpszText) = 0
     299    End Function
     300
     301    Const Function EndsWith(lpszText As *Char) As Boolean
     302        Return LastIndexOf(lpszText) = m_Length - lstrlen(lpszText)
     303    End Function
     304
     305    Const Function Insert(startIndex As Long, text As String) As String
     306        Return Insert(startIndex, text.Chars, text.Length)
     307    End Function
     308
     309    Const Function Insert(startIndex As Long, text As *Char) As String
     310        Return Insert(startIndex, text, lstrlen(text))
     311    End Function
     312
     313    Const Function Insert(startIndex As Long, text As *Char, length As Long) As String
     314        If startIndex < 0 Or startIndex > m_Length Or length < 0 Then
     315            Debug 'ArgumentOutOfRangeException
     316
     317        End If
     318        Insert.ReSize(m_Length + length)
     319        memcpy(Insert.Chars, Chars, SizeOf (Char) * startIndex)
     320        memcpy(VarPtr(Insert.Chars[startIndex]), text, SizeOf (Char) * length)
     321        memcpy(VarPtr(Insert.Chars[startIndex + length]), VarPtr(Chars[startIndex]), SizeOf (Char) * (m_Length - startIndex + 1))
     322    End Function
     323
     324    Const Function SubString(startIndex As Long) As String
    376325        Return SubString(startIndex, m_Length - startIndex)
    377326    End Function
    378327
    379     Function SubString(startIndex As Long, length As Long) As String
     328    Const Function SubString(startIndex As Long, length As Long) As String
    380329        If startIndex < 0 Or length <= 0 Then Return ""
    381330        If startIndex + length > m_Length Then Return ""
     
    388337    End Function
    389338
    390     Function Remove(startIndex As Long) As Long
    391         If startIndex < 0 Or startIndex > m_Length Then Return -1
    392         Chars[startIndex] = 0
    393         m_Length = startIndex
    394         Return m_Length
    395     End Function
    396 
    397     Function Remove(startIndex As Long, count As Long) As Long
    398         If startIndex < 0 Or count < 0 Then Return -1
    399         If startIndex + count > m_Length Then Return -1
    400 
    401         Dim newChars As *Char
    402         newChars = _System_malloc(SizeOf (Char) * (m_Length - count + 1))
    403         If newChars = 0 Then Return -1
    404 
    405         memcpy(newChars, Chars, SizeOf (Char) * startIndex)
    406         memcpy(VarPtr(newChars[startIndex]), VarPtr(Chars[startIndex + count]), SizeOf (Char) * (m_Length - startIndex - count))
    407         newChars[m_Length - count] = 0
    408 
    409         _System_free(Chars)
    410         Chars = newChars
    411         m_Length = m_Length - count
    412         Return m_Length
    413     End Function
    414 
    415     Function IsNullOrEmpty() As BOOL
    416         If m_Length = 0 Then
    417             Return _System_TRUE
    418         Else
    419             Return _System_FALSE
    420         End If
    421     End Function
    422 
    423 
    424     Sub Replace(oldChar As Char, newChar As Char)
    425         Dim i As Long
    426         For i = 0 To ELM(m_Length)
    427             If Chars[i] = oldChar Then
    428                 Chars[i] = newChar
     339    Const Function Remove(startIndex As Long) As String
     340        Remove.ReSize(startIndex)
     341        memcpy(Remove.Chars, This.Chars, SizeOf (Char) * startIndex)
     342    End Function
     343
     344    Const Function Remove(startIndex As Long, count As Long) As String
     345        Remove.ReSize(m_Length - count)
     346        memcpy(Remove.Chars, This.Chars, SizeOf (Char) * startIndex)
     347        memcpy(VarPtr(Remove.Chars[startIndex]), VarPtr(This.Chars[startIndex + count]), SizeOf (Char) * startIndex)
     348    End Function
     349/*
     350    Static Function IsNullOrEmpty(s As String) As Boolean
     351        If s <> Nothing Then
     352            If s.m_Length > 0 Then
     353                Return True
    429354            End If
    430         Next
    431     End Sub
    432 
    433     Sub Replace(ByRef oldStr As String, ByRef newStr As String)
    434         Replace(oldStr, oldStr.m_Length, newStr, newStr.m_Length)
    435     End Sub
    436 
    437     Sub Replace(oldStr As PCSTR, newStr As PCSTR)
    438         Replace(oldStr, lstrlen(oldStr), newStr, lstrlen(newStr))
    439     End Sub
    440 
    441     Sub Replace(oldStr As PCSTR, oldLen As Long, newStr As PCSTR, newLen As Long)
    442         Dim tempString As String
    443         With tempString
    444             Dim current = 0 As Long
    445             Do
    446                 Dim pos As Long
    447                 pos = IndexOf(oldStr, current)
    448                 If pos = -1 Then
    449                     Exit Do
     355        End If
     356        Return False
     357    End Function
     358*/
     359    Const Function Replace(oldChar As Char, newChar As Char) As String
     360        Replace = Copy(This)
     361        With Replace
     362            Dim i As Long
     363            For i = 0 To ELM(.m_Length)
     364                If .Chars[i] = .oldChar Then
     365                    .Chars[i] = .newChar
    450366                End If
    451                 .Append(Chars + current, pos - current)
    452                 .Append(newStr, newLen)
    453                 current = pos + oldLen
    454             Loop
    455             .Append(Chars + current, m_Length - current)
     367            Next
    456368        End With
    457         Swap(tempString)
    458     End Sub
     369    End Function
     370
     371    Const Function Replace(ByRef oldStr As String, ByRef newStr As String) As String
     372'       If oldStr = Nothing Then Throw ArgumentNullException
     373'
     374'       If newStr = Nothing Then
     375'           Return ReplaceCore(oldStr, oldStr.m_Length, "", 0)
     376'       Else
     377            Return ReplaceCore(oldStr, oldStr.m_Length, newStr, newStr.m_Length)
     378'       End If
     379    End Function
     380
     381    Const Function Replace(oldStr As *Char, newStr As *Char)
     382        If oldStr = 0 Then Debug 'Throw ArgumentNullException
     383        If newStr = 0 Then newStr = ""
     384        Return ReplaceCore(oldStr, lstrlen(oldStr), newStr, lstrlen(newStr)) As String
     385    End Function
     386
     387    Const Function Replace(oldStr As *Char, oldLen As Long, newStr As *Char, newLen As Long) As String
     388        If oldStr = 0 Then Debug 'Throw ArgumentNullException
     389        If newStr = 0 Then
     390            newStr = ""
     391            newLen = 0
     392        End If
     393        Return ReplaceCore(oldStr, oldLen, newStr, newLen)
     394    End Function
    459395
    460396    Sub ToLower()
     
    474410    Sub Swap(ByRef x As String)
    475411        Dim tempLen As Long
    476         Dim tempChars As PSTR
     412        Dim tempChars As *Char
    477413        tempLen = x.m_Length
    478414        tempChars = x.Chars
     
    485421    Override Function ToString() As String
    486422        Return This
     423    End Function
     424
     425    Static Function Copy(s As String) As String
     426        Copy.Resize(s.m_Length)
     427        memcpy(Copy.Chars, This.Chars, SizeOf (Char) * m_Length)
    487428    End Function
    488429
     
    504445    End Function
    505446
     447    Function ReplaceCore(oldStr As *Char, oldLen As Long, newStr As *Char, newLen As Long) As String
     448        If oldLen = 0 Then
     449            Debug 'Throw ArgumentException
     450        End If
     451        Dim tmp As String
     452        With tmp
     453            Dim current = 0 As Long
     454            Do
     455                Dim pos = IndexOf(oldStr, current)
     456                If pos = -1 Then
     457                    Exit Do
     458                End If
     459                .Append(VarPtr(Chars[current]), pos - current)
     460                .Append(newStr, newLen)
     461                current = pos + oldLen
     462            Loop
     463            .Append(VarPtr(Chars[current]), m_Length - current)
     464        End With
     465        Return tmp
     466    End Function
     467
    506468End Class
    507469
Note: See TracChangeset for help on using the changeset viewer.