- Timestamp:
- Mar 7, 2007, 8:11:35 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Include/Classes/System/String.ab
r135 r139 2 2 3 3 #require <basic/function.sbp> 4 5 #ifdef __STRING_IS_NOT_ALWAYS_UNICODE 6 TypeDef StrChar = Char 7 #ifndef UNICODE 8 #define __STRING_IS_NOT_UNICODE 9 #endif 10 #else 11 TypeDef StrChar = WCHAR 12 #endif 4 13 5 14 Class String 6 15 m_Length As Long 7 16 Public 8 Chars As * Char17 Chars As *StrChar 9 18 10 19 Sub String() 11 Chars = _System_calloc(SizeOf (Char) * 1)20 Chars = 0 12 21 m_Length = 0 13 22 End Sub 14 23 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) 16 30 String() 17 31 Assign(initStr) 18 32 End Sub 19 33 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) 21 45 String() 22 46 Assign(initStr, length) … … 33 57 End Sub 34 58 35 Sub String(initChar As Char, length As Long)59 Sub String(initChar As StrChar, length As Long) 36 60 ReSize(length, initChar) 37 61 End Sub … … 49 73 End Function 50 74 51 Function Operator() As * Char75 Function Operator() As *StrChar 52 76 Return Chars 53 77 End Function … … 57 81 End Sub 58 82 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) 60 88 Assign(text) 61 89 End Sub 62 90 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 64 102 Return Chars[n] 65 103 End Function 66 104 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 68 112 Chars[n] = c 69 113 End Sub 70 114 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)) 73 125 End Function 74 126 75 127 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 81 138 Return tempString 82 139 End Function … … 91 148 End Function 92 149 93 Const Function Operator == (text As * Char) As Long150 Const Function Operator == (text As *StrChar) As Long 94 151 Return _System_StrCmp(This, text) = 0 95 152 End Function 96 153 97 154 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 Boolean102 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 103 160 End Function 104 161 … … 107 164 End Function 108 165 109 Const Function Operator < (text As * Char) As Boolean166 Const Function Operator < (text As *StrChar) As Boolean 110 167 Return _System_StrCmp(This, text) < 0 111 168 End Function … … 115 172 End Function 116 173 117 Const Function Operator > (text As * Char) As Boolean174 Const Function Operator > (text As *StrChar) As Boolean 118 175 Return _System_StrCmp(This, text) > 0 119 176 End Function … … 123 180 End Function 124 181 125 Const Function Operator <= (text As * Char) As Boolean182 Const Function Operator <= (text As *StrChar) As Boolean 126 183 Return _System_StrCmp(This, text) <= 0 127 184 End Function 128 185 129 186 Const Function Operator >= (ByRef objString As String) As Boolean 130 Return _System_StrCmp(This, objString) =>0131 End Function 132 133 Const Function Operator >= (text As * Char) As Boolean134 Return _System_StrCmp(This, text) =>0135 End Function 136 137 Const Function StrPtr() As * Char187 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 138 195 Return Chars 139 196 End Function … … 145 202 oldLength = m_Length 146 203 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)) 148 205 End If 149 206 Else … … 153 210 End Sub 154 211 155 Sub ReSize(allocLength As Long, c As Char)212 Sub ReSize(allocLength As Long, c As StrChar) 156 213 If allocLength < 0 Then 157 214 Exit Sub … … 160 217 oldLength = m_Length 161 218 If AllocStringBuffer(allocLength) <> 0 Then 162 Dim p = VarPtr(Chars[oldLength]) As * Char219 Dim p = VarPtr(Chars[oldLength]) As *StrChar 163 220 Dim fillLen = m_Length - oldLength 164 221 Dim i As Long … … 173 230 End Sub 174 231 175 Sub Assign (text As *Char, textLength As Long)232 Sub AssignFromStrChar(text As *StrChar, textLength As Long) 176 233 If text = Chars Then Exit Sub 177 234 If AllocStringBuffer(textLength) <> 0 Then 178 memcpy(Chars, text, SizeOf ( Char) * textLength)235 memcpy(Chars, text, SizeOf (StrChar) * textLength) 179 236 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 181 268 End Sub 182 269 … … 185 272 End Sub 186 273 187 Sub Assign(text As *Char)274 Sub Assign(text As PCSTR) 188 275 If text Then 189 Assign(text, lstrlen (text))276 Assign(text, lstrlenA(text)) 190 277 Else 191 'Chars=_System_realloc(Chars,1) 192 Chars[0] = 0 278 If Chars <> 0 Then 279 Chars[0] = 0 280 End If 193 281 m_Length = 0 194 282 End If 195 283 End Sub 196 284 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) 198 297 Dim prevLen As Long 199 298 prevLen = m_Length 200 299 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) 202 301 Chars[m_Length] = 0 203 302 End If 204 303 End Sub 205 304 206 Sub Append(text As * Char)305 Sub Append(text As *StrChar) 207 306 Append(text, lstrlen(text)) 208 307 End Sub … … 215 314 Return This 216 315 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 316 Private 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 225 323 End With 226 Return tempString 227 End Function 228 324 End Function 325 Public 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 229 353 Const Function Contains(ByRef objString As String) As Boolean 230 354 Return IndexOf(objString, 0, m_Length) >= 0 231 355 End Function 232 356 233 Const Function Contains(lpszText As * Char) As Boolean357 Const Function Contains(lpszText As *StrChar) As Boolean 234 358 Return IndexOf(lpszText, 0, m_Length) >= 0 235 359 End Function 236 360 237 Const Function IndexOf(lpszText As * Char) As Long361 Const Function IndexOf(lpszText As *StrChar) As Long 238 362 Return IndexOf(lpszText, 0, m_Length) 239 363 End Function 240 364 241 Const Function IndexOf(lpszText As * Char, startIndex As Long) As Long365 Const Function IndexOf(lpszText As *StrChar, startIndex As Long) As Long 242 366 Return IndexOf(lpszText, startIndex, m_Length - startIndex) 243 367 End Function 244 368 245 Const Function IndexOf(lpszText As * Char, startIndex As Long, count As Long) As Long369 Const Function IndexOf(lpszText As *StrChar, startIndex As Long, count As Long) As Long 246 370 Dim length = lstrlen(lpszText) 247 371 … … 265 389 End Function 266 390 267 Const Function LastIndexOf(lpszText As * Char) As Long391 Const Function LastIndexOf(lpszText As *StrChar) As Long 268 392 Return LastIndexOf(lpszText, m_Length - 1, m_Length) 269 393 End Function 270 394 271 Const Function LastIndexOf(lpszText As * Char, startIndex As Long) As Long272 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 Long395 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 276 400 Dim length = lstrlen(lpszText) 277 401 … … 295 419 End Function 296 420 297 Const Function StartsWith(lpszText As * Char) As Boolean421 Const Function StartsWith(lpszText As *StrChar) As Boolean 298 422 Return IndexOf(lpszText) = 0 299 423 End Function 300 424 301 Const Function EndsWith(lpszText As * Char) As Boolean425 Const Function EndsWith(lpszText As *StrChar) As Boolean 302 426 Return LastIndexOf(lpszText) = m_Length - lstrlen(lpszText) 303 427 End Function … … 307 431 End Function 308 432 309 Const Function Insert(startIndex As Long, text As * Char) As String433 Const Function Insert(startIndex As Long, text As *StrChar) As String 310 434 Return Insert(startIndex, text, lstrlen(text)) 311 435 End Function 312 436 313 Const Function Insert(startIndex As Long, text As * Char, length As Long) As String437 Const Function Insert(startIndex As Long, text As *StrChar, length As Long) As String 314 438 If startIndex < 0 Or startIndex > m_Length Or length < 0 Then 315 439 Debug 'ArgumentOutOfRangeException … … 317 441 End If 318 442 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)) 322 446 End Function 323 447 … … 332 456 Dim temp As String 333 457 temp.AllocStringBuffer(length) 334 memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf ( Char) * length)458 memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf (StrChar) * length) 335 459 Chars[m_Length] = 0 336 460 Return temp … … 343 467 344 468 Remove.ReSize(startIndex) 345 memcpy(Remove.Chars, This.Chars, SizeOf ( Char) * startIndex)469 memcpy(Remove.Chars, This.Chars, SizeOf (StrChar) * startIndex) 346 470 End Function 347 471 … … 352 476 353 477 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)) 356 480 End Function 357 481 /* … … 365 489 End Function 366 490 */ 367 Const Function Replace(oldChar As Char, newChar AsChar) As String491 Const Function Replace(oldChar As StrChar, newChar As StrChar) As String 368 492 Replace = Copy(This) 369 493 With Replace … … 387 511 End Function 388 512 389 Const Function Replace(oldStr As * Char, newStr As *Char)513 Const Function Replace(oldStr As *StrChar, newStr As *StrChar) 390 514 If oldStr = 0 Then Debug 'Throw ArgumentNullException 391 515 If newStr = 0 Then newStr = "" … … 393 517 End Function 394 518 395 Const Function Replace(oldStr As * Char, oldLen As Long, newStr As *Char, newLen As Long) As String519 Const Function Replace(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String 396 520 If oldStr = 0 Then Debug 'Throw ArgumentNullException 397 521 If newStr = 0 Then … … 420 544 Sub Swap(ByRef x As String) 421 545 Dim tempLen As Long 422 Dim tempChars As * Char546 Dim tempChars As *StrChar 423 547 tempLen = x.m_Length 424 548 tempChars = x.Chars … … 435 559 Static Function Copy(s As String) As String 436 560 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) 438 562 End Function 439 563 440 564 Private 441 565 ' メモリ確保に失敗すると元の文字列は失われない。(例外安全でいう強い保障) 442 Function AllocStringBuffer(textLength As Long) As * Char566 Function AllocStringBuffer(textLength As Long) As *StrChar 443 567 If textLength < 0 Then 444 568 Return 0 445 569 ElseIf textLength > m_Length Then 446 AllocStringBuffer = _System_realloc(Chars, SizeOf( Char) * (textLength + 1))570 AllocStringBuffer = _System_realloc(Chars, SizeOf(StrChar) * (textLength + 1)) 447 571 If AllocStringBuffer <> 0 Then 448 572 m_Length = textLength … … 455 579 End Function 456 580 457 Function ReplaceCore(oldStr As * Char, oldLen As Long, newStr As *Char, newLen As Long) As String581 Function ReplaceCore(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String 458 582 If oldLen = 0 Then 459 583 Debug 'Throw ArgumentException … … 478 602 End Class 479 603 604 Function 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 612 End Function 613 614 Function 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 622 End Function 623
Note:
See TracChangeset
for help on using the changeset viewer.