1  Class String


2  m_Length As Long


3  Public


4  Chars As LPSTR


5 


6  Sub String()


7  Chars = _System_calloc(1)


8  m_Length = 0


9  End Sub


10 


11  Sub String(initStr As LPSTR)


12  String()


13  Assign(initStr)


14  End Sub


15 


16  Sub String(ByRef initStr As String)


17  String()


18  Assign(initStr)


19  End Sub


20 


21  /*


22  Sub String(length As Long)


23  ReSize(length)


24  End Sub


25  */


26  Sub String(initChar As Byte, length As Long)


27  ReSize(length, initChar)


28  End Sub


29 


30  Sub ~String()


31  _System_free(Chars)


32  Chars = 0


33  #ifdef _DEBUG


34  m_Length = 0


35  #endif


36  End Sub


37 


38  Function Length() As Long


39  Return m_Length


40  End Function


41 


42  Function Operator() As LPSTR


43  Return Chars


44  End Function


45 


46  Sub Operator = (ByRef objString As String)


47  Assign(objString.Chars, objString.m_Length)


48  End Sub


49 


50  Sub Operator = (text As LPSTR)


51  Assign(text)


52  End Sub


53 


54  Function Operator[] (n As Long) As Byte


55  Return Chars[n]


56  End Function


57 


58  Sub Operator[]= (n As Long, c As Byte)


59  Chars[n] = c


60  End Sub


61 


62  Function Operator+ (lpszText As LPSTR) As String


63  Return Concat(lpszText, lstrlen(lpszText))


64  End Function


65 


66  Function Operator+ (ByRef objString As String) As String


67  Return Concat(objString, objString.m_Length)


68  End Function


69 


70  Function Operator& (lpszText As LPSTR) As String


71  Dim tempString As String


72  tempString=This+lpszText


73  Return tempString


74  End Function


75 


76  Function Operator& (ByRef objString As String) As String


77  Dim tempString As String


78  tempString=This+objString


79  Return tempString


80  End Function


81 


82  Function Operator== (ByRef objString As String) As Long


83  If lstrcmp(This, objString) = 0 Then


84  Return _System_TRUE


85  Else


86  Return _System_FALSE


87  End If


88  End Function


89 


90  Function Operator== (lpszText As LPSTR) As Long


91  If lstrcmp(This, lpszText) = 0 Then


92  Return _System_TRUE


93  Else


94  Return _System_FALSE


95  End If


96  End Function


97 


98  Function Operator<> (ByRef objString As String) As Long


99  Return lstrcmp(This, objString)


100  End Function


101 


102  Function Operator<> (lpszText As LPSTR) As Long


103  Return lstrcmp(This, lpszText)


104  End Function


105 


106  Function Operator< (ByRef objString As String) As Long


107  If lstrcmp(This, objString) < 0 Then


108  Return _System_TRUE


109  Else


110  Return _System_FALSE


111  End If


112  End Function


113 


114  Function Operator< (lpszText As LPSTR) As Long


115  If lstrcmp(This, lpszText) < 0 Then


116  Return _System_TRUE


117  Else


118  Return _System_FALSE


119  End If


120  End Function


121 


122  Function Operator> (ByRef objString As String) As Long


123  If lstrcmp(This, objString) > 0 Then


124  Return _System_TRUE


125  Else


126  Return _System_FALSE


127  End If


128  End Function


129 


130  Function Operator> (lpszText As LPSTR) As Long


131  If lstrcmp(This, lpszText) > 0 Then


132  Return _System_TRUE


133  Else


134  Return _System_FALSE


135  End If


136  End Function


137 


138  Function Operator<= (ByRef objString As String) As Long


139  If lstrcmp(This, objString) <= 0 Then


140  Return _System_TRUE


141  Else


142  Return _System_FALSE


143  End If


144  End Function


145 


146  Function Operator<= (lpszText As LPSTR) As Long


147  If lstrcmp(This, lpszText) <= 0 Then


148  Return _System_TRUE


149  Else


150  Return _System_FALSE


151  End If


152  End Function


153 


154  Function Operator>= (ByRef objString As String) As Long


155  If lstrcmp(This, objString) => 0 Then


156  Return _System_TRUE


157  Else


158  Return _System_FALSE


159  End If


160  End Function


161 


162  Function Operator>= (lpszText As LPSTR) As Long


163  If lstrcmp(This, lpszText) => 0 Then


164  Return _System_TRUE


165  Else


166  Return _System_FALSE


167  End If


168  End Function


169 


170  Function StrPtr() As LPSTR


171  Return Chars


172  End Function


173 


174  Sub ReSize(allocLength As Long)


175  If allocLength < 0 Then Exit Sub


176  If allocLength > m_Length Then


177  Dim oldLength As Long


178  oldLength = m_Length


179  If AllocStringBuffer(allocLength) <> 0 Then


180  ZeroMemory(Chars + oldLength, m_Length  oldLength + 1)


181  End If


182  Else


183  m_Length = allocLength


184  Chars[m_Length] = 0


185  End If


186  End Sub


187 


188  Sub ReSize(allocLength As Long, c As Byte)


189  If allocLength < 0 Then


190  Exit Sub


191  ElseIf allocLength > m_Length Then


192  Dim oldLength As Long


193  oldLength = m_Length


194  If AllocStringBuffer(allocLength) <> 0 Then


195  FillMemory(Chars + oldLength, m_Length  oldLength, c)


196  End If


197  Else


198  m_Length = allocLength


199  End If


200  Chars[m_Length] = 0


201  End Sub


202 


203  Sub Assign(lpszText As LPSTR, textLength As Long)


204  If lpszText = Chars Then Exit Sub


205  If AllocStringBuffer(textLength) <> 0 Then


206  memcpy(Chars, lpszText, textLength)


207  Chars[m_Length] = 0


208  End If


209  End Sub


210 


211  Sub Assign(ByRef objString As String)


212  Assign(objString.Chars, objString.m_Length)


213  End Sub


214 


215  Sub Assign(lpszText As LPSTR)


216  If lpszText Then


217  Assign(lpszText, lstrlen(lpszText))


218  Else


219  'Chars=_System_realloc(Chars,1)


220  Chars[0] = 0


221  m_Length = 0


222  End If


223  End Sub


224 


225  Sub Append(lpszText As LPSTR, textLength As Long)


226  Dim prevLen As Long


227  prevLen = m_Length


228  If AllocStringBuffer(m_Length + textLength) <> 0 Then


229  memcpy(Chars + prevLen, lpszText, textLength)


230  Chars[m_Length] = 0


231  End If


232  End Sub


233 


234  Sub Append(text As LPSTR)


235  Append(text, lstrlen(text))


236  End Sub


237 


238  Sub Append(ByRef str As String)


239  Append(str.Chars, str.m_Length)


240  End Sub


241 


242  Function Concat(lpszText As LPSTR, textLength As Long) As String


243  Dim tempString As String


244  With tempString


245  .AllocStringBuffer(This.m_Length + textLength)


246  memcpy(.Chars, This.Chars, This.m_Length)


247  memcpy(.Chars + This.m_Length, lpszText, textLength)


248  .Chars[.m_Length] = 0


249  End With


250  Return tempString


251  End Function


252 


253  Function Contains(ByRef objString As String) As BOOL


254  If IndexOf(objString, 0, m_Length) >= 0 Then


255  Return _System_TRUE


256  Else


257  Return _System_FALSE


258  End If


259  End Function


260 


261  Function Contains(lpszText As LPSTR) As BOOL


262  If IndexOf(lpszText, 0, m_Length) >= 0 Then


263  Return _System_TRUE


264  Else


265  Return _System_FALSE


266  End If


267  End Function


268 


269  Function IndexOf(lpszText As LPSTR) As Long


270  Return IndexOf(lpszText, 0, m_Length)


271  End Function


272 


273  Function IndexOf(lpszText As LPSTR, startIndex As Long) As Long


274  Return IndexOf(lpszText, startIndex, m_Length  startIndex)


275  End Function


276 


277  Function IndexOf(lpszText As LPSTR, startIndex As Long, count As Long) As Long


278  Dim length As Long


279  length = lstrlen(lpszText)


280 


281  If startIndex < 0 Then Return 1


282  If count < 1 Or count + startIndex > m_Length Then Return 1


283  If length > m_Length Then Return 1


284 


285  If length = 0 Then Return startIndex


286 


287  Dim i As Long, j As Long


288  For i = startIndex To startIndex + count  1


289  For j = 0 To length  1


290  If Chars[i + j] = lpszText[j] Then


291  If j = length  1 Then Return i


292  Else


293  Exit For


294  End If


295  Next


296  Next


297  Return 1


298  End Function


299 


300  Function LastIndexOf(lpszText As LPSTR) As Long


301  Return LastIndexOf(lpszText, m_Length  1, m_Length)


302  End Function


303 


304  Function LastIndexOf(lpszText As LPSTR, startIndex As Long) As Long


305  Return LastIndexOf(lpszText As LPSTR, startIndex, startIndex + 1)


306  End Function


307 


308  Function LastIndexOf(lpszText As LPSTR, startIndex As Long, count As Long) As Long


309  Dim length As Long


310  length = lstrlen(lpszText)


311 


312  If startIndex < 0 Or startIndex > m_Length  1 Then Return 1


313  If count < 1 Or count > startIndex + 2 Then Return 1


314  If length > m_Length Then Return 1


315 


316  If length = 0 Then Return startIndex


317 


318  Dim i As Long, j As Long


319  For i = startIndex To startIndex  count + 1 Step 1


320  For j = length  1 To 0 Step 1


321  If Chars[i + j] = lpszText[j] Then


322  If j = 0 Then Return i


323  Else


324  Exit For


325  End If


326  Next


327  Next


328  Return 1


329  End Function


330 


331  Function StartsWith(lpszText As LPSTR) As BOOL


332  If IndexOf(lpszText) = 0 Then


333  Return _System_TRUE


334  Else


335  Return _System_FALSE


336  End If


337  End Function


338 


339  Function EndsWith(lpszText As LPSTR) As BOOL


340  If LastIndexOf(lpszText) = m_Length  lstrlen(lpszText) Then


341  Return _System_TRUE


342  Else


343  Return _System_FALSE


344  End If


345  End Function


346 


347  Function Insert(startIndex As Long, lpszText As LPSTR) As Long


348  Dim length As Long


349  length = lstrlen(lpszText)


350 


351  If startIndex < 0 Or startIndex > m_Length Then Return 1


352 


353  Dim newChars As LPSTR


354  newChars = _System_malloc(length + m_Length + 1)


355  If newChars = 0 Then Return 1


356 


357  memcpy(newChars, Chars, startIndex)


358  memcpy(newChars + startIndex, lpszText, length)


359  memcpy(newChars + startIndex + length, Chars + startIndex, m_Length  startIndex + 1)


360 


361  _System_free(Chars)


362  Chars = newChars


363  m_Length = length + m_Length


364  Return m_Length


365  End Function


366 


367  Function SubString(startIndex As Long) As String


368  Return SubString(startIndex, m_Length  startIndex)


369  End Function


370 


371  Function SubString(startIndex As Long, length As Long) As String


372  If startIndex < 0 Or length <= 0 Then Return ""


373  If startIndex + length > m_Length Then Return ""


374 


375  Dim temp As String


376  temp.AllocStringBuffer(length)


377  memcpy(temp.Chars, VarPtr(Chars[startIndex]), length)


378  Chars[m_Length] = 0


379  Return temp


380  End Function


381 


382  Function Remove(startIndex As Long) As Long


383  If startIndex < 0 Or startIndex > m_Length Then Return 1


384  Chars[startIndex] = 0


385  m_Length = startIndex


386  Return m_Length


387  End Function


388 


389  Function Remove(startIndex As Long, count As Long) As Long


390  If startIndex < 0 Or count < 0 Then Return 1


391  If startIndex + count > m_Length Then Return 1


392 


393  Dim newChars As LPSTR


394  newChars = _System_malloc(m_Length  count + 1)


395  If newChars = 0 Then Return 1


396 


397  memcpy(newChars, Chars, startIndex)


398  memcpy(newChars + startIndex, Chars + startIndex + count, m_Length  startIndex  count)


399  newChars[m_Length  count] = 0


400 


401  _System_free(Chars)


402  Chars = newChars


403  m_Length = m_Length  count


404  Return m_Length


405  End Function


406 


407  Function IsNullOrEmpty() As BOOL


408  If m_Length = 0 Then


409  Return _System_TRUE


410  Else


411  Return _System_FALSE


412  End If


413  End Function


414 


415  Sub Replace(oldChar As Byte, newChar As Byte)


416  Dim i As Long


417  For i = 0 To ELM(m_Length)


418  If Chars[i] = oldChar Then


419  Chars[i] = newChar


420  End If


421  Next


422  End Sub


423 


424  Sub Replace(ByRef oldStr As String, ByRef newStr As String)


425  Replace(oldStr, oldStr.m_Length, newStr, newStr.m_Length)


426  End Sub


427 


428  Sub Replace(oldStr As PCSTR, newStr As PCSTR)


429  Replace(oldStr, lstrlen(oldStr), newStr, lstrlen(newStr))


430  End Sub


431 


432  Sub Replace(oldStr As PCSTR, oldLen As Long, newStr As PCSTR, newLen As Long)


433  Dim tempString As String


434  With tempString


435  Dim current = 0 As Long


436  Do


437  Dim pos As Long


438  pos = IndexOf(oldStr, current)


439  If pos = 1 Then


440  Exit Do


441  End If


442  .Append(Chars + current, pos  current)


443  .Append(newStr, newLen)


444  current = pos + oldLen


445  Loop


446  .Append(Chars + current, m_Length  current)


447  End With


448  Swap(tempString)


449  End Sub


450 


451  Sub ToLower()


452  CharLower(Chars)


453  End Sub


454 


455  Sub ToUpper()


456  CharUpper(Chars)


457  End Sub


458 


459  Sub Swap(ByRef x As String)


460  Dim tempLen As Long


461  Dim tempChars As PSTR


462  tempLen = x.m_Length


463  tempChars = x.Chars


464  x.m_Length = This.m_Length


465  x.Chars = This.Chars


466  This.m_Length = tempLen


467  This.Chars = tempChars


468  End Sub


469 


470  Private


471  ' メモリ確保に失敗すると元の文字列は失われない。（例外安全でいう強い保障）


472  Function AllocStringBuffer(textLength As Long) As LPSTR


473  If textLength < 0 Then


474  Return 0


475  ElseIf textLength > m_Length Then


476  AllocStringBuffer = _System_realloc(Chars, textLength + 1)


477  If AllocStringBuffer <> 0 Then


478  m_Length = textLength


479  Chars = AllocStringBuffer


480  End If


481  Else


482  m_Length = textLength


483  AllocStringBuffer = Chars


484  End If


485  End Function


486 


487  End Class


488 

