Ignore:
Timestamp:
Mar 7, 2007, 8:11:35 PM (18 years ago)
Author:
イグトランス (egtra)
Message:

コミット漏れ

File:
1 edited

Legend:

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

    r135 r139  
    22
    33#require <basic/function.sbp>
     4
     5#ifdef __STRING_IS_NOT_ALWAYS_UNICODE
     6TypeDef StrChar = Char
     7#ifndef UNICODE
     8#define __STRING_IS_NOT_UNICODE
     9#endif
     10#else
     11TypeDef StrChar = WCHAR
     12#endif
    413
    514Class String
    615    m_Length As Long
    716Public
    8     Chars As *Char
     17    Chars As *StrChar
    918
    1019    Sub String()
    11         Chars = _System_calloc(SizeOf (Char) * 1)
     20        Chars = 0
    1221        m_Length = 0
    1322    End Sub
    1423
    15     Sub String(initStr As *Char)
     24    Sub String(initStr As *Byte)
     25        String()
     26        Assign(initStr As PCTSTR)
     27    End Sub
     28
     29    Sub String(initStr As PCSTR)
    1630        String()
    1731        Assign(initStr)
    1832    End Sub
    1933
    20     Sub String(initStr As *Char, length As Long)
     34    Sub String(initStr As PCSTR, length As Long)
     35        String()
     36        Assign(initStr, length)
     37    End Sub
     38
     39    Sub String(initStr As PCWSTR)
     40        String()
     41        Assign(initStr)
     42    End Sub
     43
     44    Sub String(initStr As PCWSTR, length As Long)
    2145        String()
    2246        Assign(initStr, length)
     
    3357    End Sub
    3458
    35     Sub String(initChar As Char, length As Long)
     59    Sub String(initChar As StrChar, length As Long)
    3660        ReSize(length, initChar)
    3761    End Sub
     
    4973    End Function
    5074
    51     Function Operator() As *Char
     75    Function Operator() As *StrChar
    5276        Return Chars
    5377    End Function
     
    5781    End Sub
    5882
    59     Sub Operator = (text As *Char)
     83    Sub Operator = (text As *Byte)
     84        Assign(text As PCTSTR)
     85    End Sub
     86
     87    Sub Operator = (text As PCSTR)
    6088        Assign(text)
    6189    End Sub
    6290
    63     Const Function Operator [] (n As Long) As Char
     91    Sub Operator = (text As PCWSTR)
     92        Assign(text)
     93    End Sub
     94
     95    Const Function Operator [] (n As Long) As StrChar
     96#ifdef _DEBUG
     97        If n > Length Then
     98            'Throw ArgumentOutOfRangeException
     99            Debug
     100        End If
     101#endif
    64102        Return Chars[n]
    65103    End Function
    66104
    67     Sub Operator []= (n As Long, c As Char)
     105    Sub Operator []= (n As Long, c As StrChar)
     106#ifdef _DEBUG
     107        If n >= Length Then
     108            'Throw ArgumentOutOfRangeException
     109            Debug
     110        End If
     111#endif
    68112        Chars[n] = c
    69113    End Sub
    70114
    71     Const Function Operator + (pszText As *Char) As String
    72         Return Concat(pszText, lstrlen(pszText))
     115    Const Function Operator + (text As *Byte) As String
     116        Return Concat(text As PCTSTR, lstrlen(text))
     117    End Function
     118
     119    Const Function Operator + (text As PCSTR) As String
     120        Return Concat(text, lstrlenA(text))
     121    End Function
     122
     123    Const Function Operator + (text As PCWSTR) As String
     124        Return Concat(text, lstrlenW(text))
    73125    End Function
    74126
    75127    Const Function Operator + (ByRef objString As String) As String
    76         Return Concat(objString, objString.m_Length)
    77     End Function
    78 
    79     Const Function Operator & (pszText As *Char) As String
    80         Dim tempString = This + pszText
     128        Return Concat(objString.Chars, objString.m_Length)
     129    End Function
     130
     131    Const Function Operator & (text As PCSTR) As String
     132        Dim tempString = This + text
     133        Return tempString
     134    End Function
     135
     136    Const Function Operator & (text As PCWSTR) As String
     137        Dim tempString = This + text
    81138        Return tempString
    82139    End Function
     
    91148    End Function
    92149
    93     Const Function Operator == (text As *Char) As Long
     150    Const Function Operator == (text As *StrChar) As Long
    94151        Return _System_StrCmp(This, text) = 0
    95152    End Function
    96153
    97154    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)
     155        Return _System_StrCmp(This, objString) <> 0
     156    End Function
     157
     158    Const Function Operator <> (text As *StrChar) As Boolean
     159        Return _System_StrCmp(This, text) <> 0
    103160    End Function
    104161
     
    107164    End Function
    108165
    109     Const Function Operator < (text As *Char) As Boolean
     166    Const Function Operator < (text As *StrChar) As Boolean
    110167        Return _System_StrCmp(This, text) < 0
    111168    End Function
     
    115172    End Function
    116173
    117     Const Function Operator > (text As *Char) As Boolean
     174    Const Function Operator > (text As *StrChar) As Boolean
    118175        Return _System_StrCmp(This, text) > 0
    119176    End Function
     
    123180    End Function
    124181
    125     Const Function Operator <= (text As *Char) As Boolean
     182    Const Function Operator <= (text As *StrChar) As Boolean
    126183        Return _System_StrCmp(This, text) <= 0
    127184    End Function
    128185
    129186    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
     187        Return _System_StrCmp(This, objString) >= 0
     188    End Function
     189
     190    Const Function Operator >= (text As *StrChar) As Boolean
     191        Return _System_StrCmp(This, text) >= 0
     192    End Function
     193
     194    Const Function StrPtr() As *StrChar
    138195        Return Chars
    139196    End Function
     
    145202            oldLength = m_Length
    146203            If AllocStringBuffer(allocLength) <> 0 Then
    147                 ZeroMemory(VarPtr(Chars[oldLength]), SizeOf (Char) * (m_Length - oldLength + 1))
     204                ZeroMemory(VarPtr(Chars[oldLength]), SizeOf (StrChar) * (m_Length - oldLength + 1))
    148205            End If
    149206        Else
     
    153210    End Sub
    154211
    155     Sub ReSize(allocLength As Long, c As Char)
     212    Sub ReSize(allocLength As Long, c As StrChar)
    156213        If allocLength < 0 Then
    157214            Exit Sub
     
    160217            oldLength = m_Length
    161218            If AllocStringBuffer(allocLength) <> 0 Then
    162                 Dim p = VarPtr(Chars[oldLength]) As *Char
     219                Dim p = VarPtr(Chars[oldLength]) As *StrChar
    163220                Dim fillLen = m_Length - oldLength
    164221                Dim i As Long
     
    173230    End Sub
    174231
    175     Sub Assign(text As *Char, textLength As Long)
     232    Sub AssignFromStrChar(text As *StrChar, textLength As Long)
    176233        If text = Chars Then Exit Sub
    177234        If AllocStringBuffer(textLength) <> 0 Then
    178             memcpy(Chars, text, SizeOf (Char) * textLength)
     235            memcpy(Chars, text, SizeOf (StrChar) * textLength)
    179236            Chars[m_Length] = 0
    180         End If     
     237        End If
     238    End Sub
     239#ifdef __STRCHAR_IS_NOT_UNICODE
     240    Sub AssignFromWideChar(text As PCWSTR, textLengthW As Long)
     241        Dim textLengthA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, 0, 0, 0, 0)
     242        If AllocStringBuffer(textLengthA) <> 0 Then
     243            WideCharToMultiByte(CP_THREAD_ACP, 0, text, textLengthW, Chars, textLengthA, 0, 0)
     244        End If
     245    End Sub
     246#else
     247    Sub AssignFromMultiByte(text As PCSTR, textLengthA As Long)
     248        Dim textLengthW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, 0, 0)
     249        If AllocStringBuffer(textLengthW) <> 0 Then
     250            MultiByteToWideChar(CP_THREAD_ACP, 0, text, textLengthA, Chars, textLengthW)
     251        End If
     252    End Sub
     253#endif
     254    Sub Assign(text As PCSTR, textLength As Long)
     255#ifdef __STRING_IS_NOT_UNICODE
     256        AssignFromStrChar(text, textLength)
     257#else
     258        AssignFromMultiByte(text, textLength)
     259#endif
     260    End Sub
     261
     262    Sub Assign(text As PCWSTR, textLength As Long)
     263#ifdef __STRING_IS_NOT_UNICODE
     264        AssignFromWideChar(text, textLength)
     265#else
     266        AssignFromStrChar(text, textLength)
     267#endif
    181268    End Sub
    182269
     
    185272    End Sub
    186273
    187     Sub Assign(text As *Char)
     274    Sub Assign(text As PCSTR)
    188275        If text Then
    189             Assign(text, lstrlen(text))
     276            Assign(text, lstrlenA(text))
    190277        Else
    191             'Chars=_System_realloc(Chars,1)
    192             Chars[0] = 0
     278            If Chars <> 0 Then
     279                Chars[0] = 0
     280            End If
    193281            m_Length = 0
    194282        End If
    195283    End Sub
    196284
    197     Sub Append(text As *Char, textLength As Long)
     285    Sub Assign(text As PCWSTR)
     286        If text Then
     287            Assign(text, lstrlenW(text))
     288        Else
     289            If Chars <> 0 Then
     290                Chars[0] = 0
     291            End If
     292            m_Length = 0
     293        End If
     294    End Sub
     295
     296    Sub Append(text As *StrChar, textLength As Long)
    198297        Dim prevLen As Long
    199298        prevLen = m_Length
    200299        If AllocStringBuffer(m_Length + textLength) <> 0 Then
    201             memcpy(VarPtr(Chars[prevLen]), text, SizeOf (Char) * textLength)
     300            memcpy(VarPtr(Chars[prevLen]), text, SizeOf (StrChar) * textLength)
    202301            Chars[m_Length] = 0
    203302        End If
    204303    End Sub
    205304
    206     Sub Append(text As *Char)
     305    Sub Append(text As *StrChar)
    207306        Append(text, lstrlen(text))
    208307    End Sub
     
    215314        Return This
    216315    End Function
    217 
    218     Const Function Concat(lpszText As *Char, textLength As Long) As String
    219         Dim tempString As String
    220         With tempString
    221             .AllocStringBuffer(This.m_Length + textLength)
    222             memcpy(.Chars, This.Chars, SizeOf (Char) * This.m_Length)
    223             memcpy(VarPtr(.Chars[This.m_Length]), lpszText, SizeOf (Char) * textLength)
    224             .Chars[.m_Length] = 0
     316Private
     317    Static Const Function ConcatStrChar(text1 As *StrChar, text1Length As Long, text2 As *StrChar, text2Length As Long) As String
     318        With ConcatStrChar
     319            .AllocStringBuffer(text1Length + text2Length)
     320            memcpy(.Chars, text1, SizeOf (StrChar) * text1Length)
     321            memcpy(VarPtr(.Chars[text1Length]), text2, SizeOf (StrChar) * text2Length)
     322            .Chars[text1Length + text2Length] = 0
    225323        End With
    226         Return tempString
    227     End Function
    228 
     324    End Function
     325Public
     326    Const Function Concat(text As PCSTR, len As Long) As String
     327#ifdef __STRING_IS_NOT_UNICODE
     328        Return ConcatStrChar(This.Chars, m_Length, text, len)
     329#else
     330        With Concat
     331            Dim lenW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0)
     332            .AllocStringBuffer(m_Length + lenW)
     333            memcpy(.Chars, This.Chars, m_Length)
     334            MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenW)
     335            .Chars[m_Length + lenW] = 0
     336        End With
     337#endif
     338    End Function
     339
     340    Const Function Concat(text As PCWSTR, len As Long) As String
     341#ifdef __STRING_IS_NOT_UNICODE
     342        With Concat
     343            Dim lenA = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0, 0, 0)
     344            .AllocStringBuffer(m_Length + lenA)
     345            memcpy(.Chars, This.Chars, m_Length)
     346            MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenA, 0, 0)
     347            .Chars[m_Length + lenA] = 0
     348        End With
     349#else
     350        Return ConcatStrChar(This.Chars, m_Length, text, len)
     351#endif
     352    End Function
    229353    Const Function Contains(ByRef objString As String) As Boolean
    230354        Return IndexOf(objString, 0, m_Length) >= 0
    231355    End Function
    232356
    233     Const Function Contains(lpszText As *Char) As Boolean
     357    Const Function Contains(lpszText As *StrChar) As Boolean
    234358        Return IndexOf(lpszText, 0, m_Length) >= 0
    235359    End Function
    236360
    237     Const Function IndexOf(lpszText As *Char) As Long
     361    Const Function IndexOf(lpszText As *StrChar) As Long
    238362        Return IndexOf(lpszText, 0, m_Length)
    239363    End Function
    240364
    241     Const Function IndexOf(lpszText As *Char, startIndex As Long) As Long
     365    Const Function IndexOf(lpszText As *StrChar, startIndex As Long) As Long
    242366        Return IndexOf(lpszText, startIndex, m_Length - startIndex)
    243367    End Function
    244368
    245     Const Function IndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
     369    Const Function IndexOf(lpszText As *StrChar, startIndex As Long, count As Long) As Long
    246370        Dim length = lstrlen(lpszText)
    247371
     
    265389    End Function
    266390
    267     Const Function LastIndexOf(lpszText As *Char) As Long
     391    Const Function LastIndexOf(lpszText As *StrChar) As Long
    268392        Return LastIndexOf(lpszText, m_Length - 1, m_Length)
    269393    End Function
    270394
    271     Const Function LastIndexOf(lpszText As *Char, startIndex As Long) As Long
    272         Return LastIndexOf(lpszText As *Char, startIndex, startIndex + 1)
    273     End Function
    274 
    275     Const Function LastIndexOf(lpszText As *Char, startIndex As Long, count As Long) As Long
     395    Const Function LastIndexOf(lpszText As *StrChar, startIndex As Long) As Long
     396        Return LastIndexOf(lpszText As *StrChar, startIndex, startIndex + 1)
     397    End Function
     398
     399    Const Function LastIndexOf(lpszText As *StrChar, startIndex As Long, count As Long) As Long
    276400        Dim length = lstrlen(lpszText)
    277401
     
    295419    End Function
    296420
    297     Const Function StartsWith(lpszText As *Char) As Boolean
     421    Const Function StartsWith(lpszText As *StrChar) As Boolean
    298422        Return IndexOf(lpszText) = 0
    299423    End Function
    300424
    301     Const Function EndsWith(lpszText As *Char) As Boolean
     425    Const Function EndsWith(lpszText As *StrChar) As Boolean
    302426        Return LastIndexOf(lpszText) = m_Length - lstrlen(lpszText)
    303427    End Function
     
    307431    End Function
    308432
    309     Const Function Insert(startIndex As Long, text As *Char) As String
     433    Const Function Insert(startIndex As Long, text As *StrChar) As String
    310434        Return Insert(startIndex, text, lstrlen(text))
    311435    End Function
    312436
    313     Const Function Insert(startIndex As Long, text As *Char, length As Long) As String
     437    Const Function Insert(startIndex As Long, text As *StrChar, length As Long) As String
    314438        If startIndex < 0 Or startIndex > m_Length Or length < 0 Then
    315439            Debug 'ArgumentOutOfRangeException
     
    317441        End If
    318442        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))
     443        memcpy(Insert.Chars, Chars, SizeOf (StrChar) * startIndex)
     444        memcpy(VarPtr(Insert.Chars[startIndex]), text, SizeOf (StrChar) * length)
     445        memcpy(VarPtr(Insert.Chars[startIndex + length]), VarPtr(Chars[startIndex]), SizeOf (StrChar) * (m_Length - startIndex + 1))
    322446    End Function
    323447
     
    332456        Dim temp As String
    333457        temp.AllocStringBuffer(length)
    334         memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf (Char) * length)
     458        memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf (StrChar) * length)
    335459        Chars[m_Length] = 0
    336460        Return temp
     
    343467
    344468        Remove.ReSize(startIndex)
    345         memcpy(Remove.Chars, This.Chars, SizeOf (Char) * startIndex)
     469        memcpy(Remove.Chars, This.Chars, SizeOf (StrChar) * startIndex)
    346470    End Function
    347471
     
    352476
    353477        Remove.ReSize(m_Length - count)
    354         memcpy(Remove.Chars, This.Chars, SizeOf (Char) * startIndex)
    355         memcpy(VarPtr(Remove.Chars[startIndex]), VarPtr(This.Chars[startIndex + count]), SizeOf (Char) * (m_Length - startIndex - count))
     478        memcpy(Remove.Chars, This.Chars, SizeOf (StrChar) * startIndex)
     479        memcpy(VarPtr(Remove.Chars[startIndex]), VarPtr(This.Chars[startIndex + count]), SizeOf (StrChar) * (m_Length - startIndex - count))
    356480    End Function
    357481/*
     
    365489    End Function
    366490*/
    367     Const Function Replace(oldChar As Char, newChar As Char) As String
     491    Const Function Replace(oldChar As StrChar, newChar As StrChar) As String
    368492        Replace = Copy(This)
    369493        With Replace
     
    387511    End Function
    388512
    389     Const Function Replace(oldStr As *Char, newStr As *Char)
     513    Const Function Replace(oldStr As *StrChar, newStr As *StrChar)
    390514        If oldStr = 0 Then Debug 'Throw ArgumentNullException
    391515        If newStr = 0 Then newStr = ""
     
    393517    End Function
    394518
    395     Const Function Replace(oldStr As *Char, oldLen As Long, newStr As *Char, newLen As Long) As String
     519    Const Function Replace(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String
    396520        If oldStr = 0 Then Debug 'Throw ArgumentNullException
    397521        If newStr = 0 Then
     
    420544    Sub Swap(ByRef x As String)
    421545        Dim tempLen As Long
    422         Dim tempChars As *Char
     546        Dim tempChars As *StrChar
    423547        tempLen = x.m_Length
    424548        tempChars = x.Chars
     
    435559    Static Function Copy(s As String) As String
    436560        Copy.Resize(s.m_Length)
    437         memcpy(Copy.Chars, This.Chars, SizeOf (Char) * m_Length)
     561        memcpy(Copy.Chars, This.Chars, SizeOf (StrChar) * m_Length)
    438562    End Function
    439563
    440564Private
    441565    ' メモリ確保に失敗すると元の文字列は失われない。(例外安全でいう強い保障)
    442     Function AllocStringBuffer(textLength As Long) As *Char
     566    Function AllocStringBuffer(textLength As Long) As *StrChar
    443567        If textLength < 0 Then
    444568            Return 0
    445569        ElseIf textLength > m_Length Then
    446             AllocStringBuffer = _System_realloc(Chars, SizeOf(Char) * (textLength + 1))
     570            AllocStringBuffer = _System_realloc(Chars, SizeOf(StrChar) * (textLength + 1))
    447571            If AllocStringBuffer <> 0 Then
    448572                m_Length = textLength
     
    455579    End Function
    456580
    457     Function ReplaceCore(oldStr As *Char, oldLen As Long, newStr As *Char, newLen As Long) As String
     581    Function ReplaceCore(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String
    458582        If oldLen = 0 Then
    459583            Debug 'Throw ArgumentException
     
    478602End Class
    479603
     604Function GetMultiByteStr(ByRef s As String) As PCSTR
     605#ifdef __STRING_IS_NOT_UNICODE
     606    GetMultiByteStr = s.Chars
     607#else
     608    Dim sizeA = WideCharToMultiByte(CP_THREAD_ACP, 0, s.Chars, s.Length + 1, 0, 0, 0, 0)
     609    GetMultiByteStr = GC_malloc_atomic(SizeOf (SByte) * sizeA)
     610    WideCharToMultiByte(CP_THREAD_ACP, 0, s.Chars, s.Length + 1, GetMultiByteStr, sizeA, 0, 0)
     611#endif
     612End Function
     613
     614Function GetWideCharStr(ByRef s As String) As PCSTR
     615#ifdef __STRING_IS_NOT_UNICODE
     616    Dim sizeW = MultiByteToWideChar(CP_THREAD_ACP, 0, s.Chars, s.Length + 1, 0, 0)
     617    GetWideCharStr = GC_malloc_atomic(SizeOf (WCHAR) * sizeW)
     618    MultiByteToWideChar(CP_THREAD_ACP, 0, s.Chars, s.Length + 1, GetWideCharStr, sizeW)
     619#else
     620    GetMultiByteStr = s.Chars
     621#endif
     622End Function
     623
Note: See TracChangeset for help on using the changeset viewer.