diff options
| author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-06-20 00:00:00 +0000 |
|---|---|---|
| committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-12-17 13:35:20 +0500 |
| commit | a3e1d227377188734b82f023f96f8e25dc40f3e6 (patch) | |
| tree | 23cad8d47eb23d26ea727b4f7f4a65124f724065 /CPP/Common/MyString.cpp | |
| parent | f19f813537c7aea1c20749c914e756b54a9c3cf5 (diff) | |
| download | 7zip-a3e1d227377188734b82f023f96f8e25dc40f3e6.tar.gz 7zip-a3e1d227377188734b82f023f96f8e25dc40f3e6.tar.bz2 7zip-a3e1d227377188734b82f023f96f8e25dc40f3e6.zip | |
22.0022.00
Diffstat (limited to 'CPP/Common/MyString.cpp')
| -rw-r--r-- | CPP/Common/MyString.cpp | 187 |
1 files changed, 113 insertions, 74 deletions
diff --git a/CPP/Common/MyString.cpp b/CPP/Common/MyString.cpp index db202f4..bf1638e 100644 --- a/CPP/Common/MyString.cpp +++ b/CPP/Common/MyString.cpp | |||
| @@ -30,8 +30,8 @@ inline const char* MyStringGetNextCharPointer(const char *p) throw() | |||
| 30 | } | 30 | } |
| 31 | */ | 31 | */ |
| 32 | 32 | ||
| 33 | #define MY_STRING_NEW_char(_size_) MY_STRING_NEW(char, _size_) | 33 | #define MY_STRING_NEW_char(_size_) MY_STRING_NEW(char, (_size_)) |
| 34 | #define MY_STRING_NEW_wchar_t(_size_) MY_STRING_NEW(wchar_t, _size_) | 34 | #define MY_STRING_NEW_wchar_t(_size_) MY_STRING_NEW(wchar_t, (_size_)) |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | int FindCharPosInString(const char *s, char c) throw() | 37 | int FindCharPosInString(const char *s, char c) throw() |
| @@ -190,8 +190,8 @@ bool IsString1PrefixedByString2(const char *s1, const char *s2) throw() | |||
| 190 | { | 190 | { |
| 191 | for (;;) | 191 | for (;;) |
| 192 | { | 192 | { |
| 193 | unsigned char c2 = (unsigned char)*s2++; if (c2 == 0) return true; | 193 | const unsigned char c2 = (unsigned char)*s2++; if (c2 == 0) return true; |
| 194 | unsigned char c1 = (unsigned char)*s1++; if (c1 != c2) return false; | 194 | const unsigned char c1 = (unsigned char)*s1++; if (c1 != c2) return false; |
| 195 | } | 195 | } |
| 196 | } | 196 | } |
| 197 | 197 | ||
| @@ -199,8 +199,8 @@ bool StringsAreEqualNoCase(const wchar_t *s1, const wchar_t *s2) throw() | |||
| 199 | { | 199 | { |
| 200 | for (;;) | 200 | for (;;) |
| 201 | { | 201 | { |
| 202 | wchar_t c1 = *s1++; | 202 | const wchar_t c1 = *s1++; |
| 203 | wchar_t c2 = *s2++; | 203 | const wchar_t c2 = *s2++; |
| 204 | if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2)) return false; | 204 | if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2)) return false; |
| 205 | if (c1 == 0) return true; | 205 | if (c1 == 0) return true; |
| 206 | } | 206 | } |
| @@ -213,10 +213,10 @@ bool AString::IsPrefixedBy_Ascii_NoCase(const char *s) const throw() | |||
| 213 | const char *s1 = _chars; | 213 | const char *s1 = _chars; |
| 214 | for (;;) | 214 | for (;;) |
| 215 | { | 215 | { |
| 216 | char c2 = *s++; | 216 | const char c2 = *s++; |
| 217 | if (c2 == 0) | 217 | if (c2 == 0) |
| 218 | return true; | 218 | return true; |
| 219 | char c1 = *s1++; | 219 | const char c1 = *s1++; |
| 220 | if (MyCharLower_Ascii(c1) != | 220 | if (MyCharLower_Ascii(c1) != |
| 221 | MyCharLower_Ascii(c2)) | 221 | MyCharLower_Ascii(c2)) |
| 222 | return false; | 222 | return false; |
| @@ -228,10 +228,10 @@ bool UString::IsPrefixedBy_Ascii_NoCase(const char *s) const throw() | |||
| 228 | const wchar_t *s1 = _chars; | 228 | const wchar_t *s1 = _chars; |
| 229 | for (;;) | 229 | for (;;) |
| 230 | { | 230 | { |
| 231 | char c2 = *s++; | 231 | const char c2 = *s++; |
| 232 | if (c2 == 0) | 232 | if (c2 == 0) |
| 233 | return true; | 233 | return true; |
| 234 | wchar_t c1 = *s1++; | 234 | const wchar_t c1 = *s1++; |
| 235 | if (MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2)) | 235 | if (MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2)) |
| 236 | return false; | 236 | return false; |
| 237 | } | 237 | } |
| @@ -241,7 +241,7 @@ bool StringsAreEqual_Ascii(const char *u, const char *a) throw() | |||
| 241 | { | 241 | { |
| 242 | for (;;) | 242 | for (;;) |
| 243 | { | 243 | { |
| 244 | char c = *a; | 244 | const char c = *a; |
| 245 | if (c != *u) | 245 | if (c != *u) |
| 246 | return false; | 246 | return false; |
| 247 | if (c == 0) | 247 | if (c == 0) |
| @@ -255,7 +255,7 @@ bool StringsAreEqual_Ascii(const wchar_t *u, const char *a) throw() | |||
| 255 | { | 255 | { |
| 256 | for (;;) | 256 | for (;;) |
| 257 | { | 257 | { |
| 258 | unsigned char c = (unsigned char)*a; | 258 | const unsigned char c = (unsigned char)*a; |
| 259 | if (c != *u) | 259 | if (c != *u) |
| 260 | return false; | 260 | return false; |
| 261 | if (c == 0) | 261 | if (c == 0) |
| @@ -269,8 +269,8 @@ bool StringsAreEqualNoCase_Ascii(const char *s1, const char *s2) throw() | |||
| 269 | { | 269 | { |
| 270 | for (;;) | 270 | for (;;) |
| 271 | { | 271 | { |
| 272 | char c1 = *s1++; | 272 | const char c1 = *s1++; |
| 273 | char c2 = *s2++; | 273 | const char c2 = *s2++; |
| 274 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) | 274 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) |
| 275 | return false; | 275 | return false; |
| 276 | if (c1 == 0) | 276 | if (c1 == 0) |
| @@ -282,8 +282,8 @@ bool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const wchar_t *s2) throw() | |||
| 282 | { | 282 | { |
| 283 | for (;;) | 283 | for (;;) |
| 284 | { | 284 | { |
| 285 | wchar_t c1 = *s1++; | 285 | const wchar_t c1 = *s1++; |
| 286 | wchar_t c2 = *s2++; | 286 | const wchar_t c2 = *s2++; |
| 287 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) | 287 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) |
| 288 | return false; | 288 | return false; |
| 289 | if (c1 == 0) | 289 | if (c1 == 0) |
| @@ -295,8 +295,8 @@ bool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const char *s2) throw() | |||
| 295 | { | 295 | { |
| 296 | for (;;) | 296 | for (;;) |
| 297 | { | 297 | { |
| 298 | wchar_t c1 = *s1++; | 298 | const wchar_t c1 = *s1++; |
| 299 | char c2 = *s2++; | 299 | const char c2 = *s2++; |
| 300 | if (c1 != (unsigned char)c2 && (c1 > 0x7F || MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2))) | 300 | if (c1 != (unsigned char)c2 && (c1 > 0x7F || MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2))) |
| 301 | return false; | 301 | return false; |
| 302 | if (c1 == 0) | 302 | if (c1 == 0) |
| @@ -308,8 +308,8 @@ bool IsString1PrefixedByString2(const wchar_t *s1, const wchar_t *s2) throw() | |||
| 308 | { | 308 | { |
| 309 | for (;;) | 309 | for (;;) |
| 310 | { | 310 | { |
| 311 | wchar_t c2 = *s2++; if (c2 == 0) return true; | 311 | const wchar_t c2 = *s2++; if (c2 == 0) return true; |
| 312 | wchar_t c1 = *s1++; if (c1 != c2) return false; | 312 | const wchar_t c1 = *s1++; if (c1 != c2) return false; |
| 313 | } | 313 | } |
| 314 | } | 314 | } |
| 315 | 315 | ||
| @@ -317,8 +317,8 @@ bool IsString1PrefixedByString2(const wchar_t *s1, const char *s2) throw() | |||
| 317 | { | 317 | { |
| 318 | for (;;) | 318 | for (;;) |
| 319 | { | 319 | { |
| 320 | unsigned char c2 = (unsigned char)(*s2++); if (c2 == 0) return true; | 320 | const unsigned char c2 = (unsigned char)(*s2++); if (c2 == 0) return true; |
| 321 | wchar_t c1 = *s1++; if (c1 != c2) return false; | 321 | const wchar_t c1 = *s1++; if (c1 != c2) return false; |
| 322 | } | 322 | } |
| 323 | } | 323 | } |
| 324 | 324 | ||
| @@ -326,8 +326,8 @@ bool IsString1PrefixedByString2_NoCase_Ascii(const char *s1, const char *s2) thr | |||
| 326 | { | 326 | { |
| 327 | for (;;) | 327 | for (;;) |
| 328 | { | 328 | { |
| 329 | char c2 = *s2++; if (c2 == 0) return true; | 329 | const char c2 = *s2++; if (c2 == 0) return true; |
| 330 | char c1 = *s1++; | 330 | const char c1 = *s1++; |
| 331 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) | 331 | if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2)) |
| 332 | return false; | 332 | return false; |
| 333 | } | 333 | } |
| @@ -337,8 +337,8 @@ bool IsString1PrefixedByString2_NoCase_Ascii(const wchar_t *s1, const char *s2) | |||
| 337 | { | 337 | { |
| 338 | for (;;) | 338 | for (;;) |
| 339 | { | 339 | { |
| 340 | char c2 = *s2++; if (c2 == 0) return true; | 340 | const char c2 = *s2++; if (c2 == 0) return true; |
| 341 | wchar_t c1 = *s1++; | 341 | const wchar_t c1 = *s1++; |
| 342 | if (c1 != (unsigned char)c2 && MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2)) | 342 | if (c1 != (unsigned char)c2 && MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2)) |
| 343 | return false; | 343 | return false; |
| 344 | } | 344 | } |
| @@ -348,8 +348,8 @@ bool IsString1PrefixedByString2_NoCase(const wchar_t *s1, const wchar_t *s2) thr | |||
| 348 | { | 348 | { |
| 349 | for (;;) | 349 | for (;;) |
| 350 | { | 350 | { |
| 351 | wchar_t c2 = *s2++; if (c2 == 0) return true; | 351 | const wchar_t c2 = *s2++; if (c2 == 0) return true; |
| 352 | wchar_t c1 = *s1++; | 352 | const wchar_t c1 = *s1++; |
| 353 | if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2)) | 353 | if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2)) |
| 354 | return false; | 354 | return false; |
| 355 | } | 355 | } |
| @@ -360,12 +360,12 @@ int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) throw() | |||
| 360 | { | 360 | { |
| 361 | for (;;) | 361 | for (;;) |
| 362 | { | 362 | { |
| 363 | wchar_t c1 = *s1++; | 363 | const wchar_t c1 = *s1++; |
| 364 | wchar_t c2 = *s2++; | 364 | const wchar_t c2 = *s2++; |
| 365 | if (c1 != c2) | 365 | if (c1 != c2) |
| 366 | { | 366 | { |
| 367 | wchar_t u1 = MyCharUpper(c1); | 367 | const wchar_t u1 = MyCharUpper(c1); |
| 368 | wchar_t u2 = MyCharUpper(c2); | 368 | const wchar_t u2 = MyCharUpper(c2); |
| 369 | if (u1 < u2) return -1; | 369 | if (u1 < u2) return -1; |
| 370 | if (u1 > u2) return 1; | 370 | if (u1 > u2) return 1; |
| 371 | } | 371 | } |
| @@ -401,14 +401,13 @@ void AString::InsertSpace(unsigned &index, unsigned size) | |||
| 401 | MoveItems(index + size, index); | 401 | MoveItems(index + size, index); |
| 402 | } | 402 | } |
| 403 | 403 | ||
| 404 | #define k_Alloc_Len_Limit 0x40000000 | 404 | #define k_Alloc_Len_Limit (0x40000000 - 2) |
| 405 | 405 | ||
| 406 | void AString::ReAlloc(unsigned newLimit) | 406 | void AString::ReAlloc(unsigned newLimit) |
| 407 | { | 407 | { |
| 408 | if (newLimit < _len || newLimit >= k_Alloc_Len_Limit) throw 20130220; | 408 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, (size_t)_len + 1); |
| 409 | // MY_STRING_REALLOC(_chars, char, newLimit + 1, _len + 1); | 409 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); |
| 410 | char *newBuf = MY_STRING_NEW_char(newLimit + 1); | 410 | memcpy(newBuf, _chars, (size_t)_len + 1); |
| 411 | memcpy(newBuf, _chars, (size_t)(_len + 1)); | ||
| 412 | MY_STRING_DELETE(_chars); | 411 | MY_STRING_DELETE(_chars); |
| 413 | _chars = newBuf; | 412 | _chars = newBuf; |
| 414 | _limit = newLimit; | 413 | _limit = newLimit; |
| @@ -416,9 +415,9 @@ void AString::ReAlloc(unsigned newLimit) | |||
| 416 | 415 | ||
| 417 | void AString::ReAlloc2(unsigned newLimit) | 416 | void AString::ReAlloc2(unsigned newLimit) |
| 418 | { | 417 | { |
| 419 | if (newLimit >= k_Alloc_Len_Limit) throw 20130220; | 418 | if (newLimit > k_Alloc_Len_Limit) throw 20130220; |
| 420 | // MY_STRING_REALLOC(_chars, char, newLimit + 1, 0); | 419 | // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0); |
| 421 | char *newBuf = MY_STRING_NEW_char(newLimit + 1); | 420 | char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1); |
| 422 | newBuf[0] = 0; | 421 | newBuf[0] = 0; |
| 423 | MY_STRING_DELETE(_chars); | 422 | MY_STRING_DELETE(_chars); |
| 424 | _chars = newBuf; | 423 | _chars = newBuf; |
| @@ -427,8 +426,8 @@ void AString::ReAlloc2(unsigned newLimit) | |||
| 427 | 426 | ||
| 428 | void AString::SetStartLen(unsigned len) | 427 | void AString::SetStartLen(unsigned len) |
| 429 | { | 428 | { |
| 430 | _chars = 0; | 429 | _chars = NULL; |
| 431 | _chars = MY_STRING_NEW_char(len + 1); | 430 | _chars = MY_STRING_NEW_char((size_t)len + 1); |
| 432 | _len = len; | 431 | _len = len; |
| 433 | _limit = len; | 432 | _limit = len; |
| 434 | } | 433 | } |
| @@ -439,20 +438,30 @@ void AString::Grow_1() | |||
| 439 | next += next / 2; | 438 | next += next / 2; |
| 440 | next += 16; | 439 | next += 16; |
| 441 | next &= ~(unsigned)15; | 440 | next &= ~(unsigned)15; |
| 442 | ReAlloc(next - 1); | 441 | next--; |
| 442 | if (next < _len || next > k_Alloc_Len_Limit) | ||
| 443 | next = k_Alloc_Len_Limit; | ||
| 444 | if (next <= _len) | ||
| 445 | throw 20130220; | ||
| 446 | ReAlloc(next); | ||
| 447 | // Grow(1); | ||
| 443 | } | 448 | } |
| 444 | 449 | ||
| 445 | void AString::Grow(unsigned n) | 450 | void AString::Grow(unsigned n) |
| 446 | { | 451 | { |
| 447 | unsigned freeSize = _limit - _len; | 452 | const unsigned freeSize = _limit - _len; |
| 448 | if (n <= freeSize) | 453 | if (n <= freeSize) |
| 449 | return; | 454 | return; |
| 450 | |||
| 451 | unsigned next = _len + n; | 455 | unsigned next = _len + n; |
| 452 | next += next / 2; | 456 | next += next / 2; |
| 453 | next += 16; | 457 | next += 16; |
| 454 | next &= ~(unsigned)15; | 458 | next &= ~(unsigned)15; |
| 455 | ReAlloc(next - 1); | 459 | next--; |
| 460 | if (next < _len || next > k_Alloc_Len_Limit) | ||
| 461 | next = k_Alloc_Len_Limit; | ||
| 462 | if (next <= _len || next - _len < n) | ||
| 463 | throw 20130220; | ||
| 464 | ReAlloc(next); | ||
| 456 | } | 465 | } |
| 457 | 466 | ||
| 458 | AString::AString(unsigned num, const char *s) | 467 | AString::AString(unsigned num, const char *s) |
| @@ -500,7 +509,7 @@ static const unsigned kStartStringCapacity = 4; | |||
| 500 | 509 | ||
| 501 | AString::AString() | 510 | AString::AString() |
| 502 | { | 511 | { |
| 503 | _chars = 0; | 512 | _chars = NULL; |
| 504 | _chars = MY_STRING_NEW_char(kStartStringCapacity); | 513 | _chars = MY_STRING_NEW_char(kStartStringCapacity); |
| 505 | _len = 0; | 514 | _len = 0; |
| 506 | _limit = kStartStringCapacity - 1; | 515 | _limit = kStartStringCapacity - 1; |
| @@ -548,7 +557,7 @@ AString &AString::operator=(const char *s) | |||
| 548 | unsigned len = MyStringLen(s); | 557 | unsigned len = MyStringLen(s); |
| 549 | if (len > _limit) | 558 | if (len > _limit) |
| 550 | { | 559 | { |
| 551 | char *newBuf = MY_STRING_NEW_char(len + 1); | 560 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 552 | MY_STRING_DELETE(_chars); | 561 | MY_STRING_DELETE(_chars); |
| 553 | _chars = newBuf; | 562 | _chars = newBuf; |
| 554 | _limit = len; | 563 | _limit = len; |
| @@ -565,7 +574,7 @@ AString &AString::operator=(const AString &s) | |||
| 565 | unsigned len = s._len; | 574 | unsigned len = s._len; |
| 566 | if (len > _limit) | 575 | if (len > _limit) |
| 567 | { | 576 | { |
| 568 | char *newBuf = MY_STRING_NEW_char(len + 1); | 577 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 569 | MY_STRING_DELETE(_chars); | 578 | MY_STRING_DELETE(_chars); |
| 570 | _chars = newBuf; | 579 | _chars = newBuf; |
| 571 | _limit = len; | 580 | _limit = len; |
| @@ -590,7 +599,7 @@ void AString::SetFromWStr_if_Ascii(const wchar_t *s) | |||
| 590 | } | 599 | } |
| 591 | if (len > _limit) | 600 | if (len > _limit) |
| 592 | { | 601 | { |
| 593 | char *newBuf = MY_STRING_NEW_char(len + 1); | 602 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 594 | MY_STRING_DELETE(_chars); | 603 | MY_STRING_DELETE(_chars); |
| 595 | _chars = newBuf; | 604 | _chars = newBuf; |
| 596 | _limit = len; | 605 | _limit = len; |
| @@ -614,7 +623,7 @@ void AString::SetFromBstr_if_Ascii(BSTR s) | |||
| 614 | } | 623 | } |
| 615 | if (len > _limit) | 624 | if (len > _limit) |
| 616 | { | 625 | { |
| 617 | char *newBuf = MY_STRING_NEW_char(len + 1); | 626 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 618 | MY_STRING_DELETE(_chars); | 627 | MY_STRING_DELETE(_chars); |
| 619 | _chars = newBuf; | 628 | _chars = newBuf; |
| 620 | _limit = len; | 629 | _limit = len; |
| @@ -631,6 +640,7 @@ void AString::SetFromBstr_if_Ascii(BSTR s) | |||
| 631 | void AString::Add_Space() { operator+=(' '); } | 640 | void AString::Add_Space() { operator+=(' '); } |
| 632 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } | 641 | void AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); } |
| 633 | void AString::Add_LF() { operator+=('\n'); } | 642 | void AString::Add_LF() { operator+=('\n'); } |
| 643 | void AString::Add_Slash() { operator+=('/'); } | ||
| 634 | 644 | ||
| 635 | AString &AString::operator+=(const char *s) | 645 | AString &AString::operator+=(const char *s) |
| 636 | { | 646 | { |
| @@ -667,11 +677,23 @@ void UString::Add_UInt64(UInt64 v) | |||
| 667 | _len = (unsigned)(ConvertUInt64ToString(v, _chars + _len) - _chars); | 677 | _len = (unsigned)(ConvertUInt64ToString(v, _chars + _len) - _chars); |
| 668 | } | 678 | } |
| 669 | 679 | ||
| 680 | void AString::AddFrom(const char *s, unsigned len) // no check | ||
| 681 | { | ||
| 682 | if (len != 0) | ||
| 683 | { | ||
| 684 | Grow(len); | ||
| 685 | memcpy(_chars + _len, s, len); | ||
| 686 | len += _len; | ||
| 687 | _chars[len] = 0; | ||
| 688 | _len = len; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 670 | void AString::SetFrom(const char *s, unsigned len) // no check | 692 | void AString::SetFrom(const char *s, unsigned len) // no check |
| 671 | { | 693 | { |
| 672 | if (len > _limit) | 694 | if (len > _limit) |
| 673 | { | 695 | { |
| 674 | char *newBuf = MY_STRING_NEW_char(len + 1); | 696 | char *newBuf = MY_STRING_NEW_char((size_t)len + 1); |
| 675 | MY_STRING_DELETE(_chars); | 697 | MY_STRING_DELETE(_chars); |
| 676 | _chars = newBuf; | 698 | _chars = newBuf; |
| 677 | _limit = len; | 699 | _limit = len; |
| @@ -976,9 +998,8 @@ void UString::InsertSpace(unsigned index, unsigned size) | |||
| 976 | 998 | ||
| 977 | void UString::ReAlloc(unsigned newLimit) | 999 | void UString::ReAlloc(unsigned newLimit) |
| 978 | { | 1000 | { |
| 979 | if (newLimit < _len || newLimit >= k_Alloc_Len_Limit) throw 20130221; | 1001 | // MY_STRING_REALLOC(_chars, wchar_t, (size_t)newLimit + 1, (size_t)_len + 1); |
| 980 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, _len + 1); | 1002 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); |
| 981 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(newLimit + 1); | ||
| 982 | wmemcpy(newBuf, _chars, _len + 1); | 1003 | wmemcpy(newBuf, _chars, _len + 1); |
| 983 | MY_STRING_DELETE(_chars); | 1004 | MY_STRING_DELETE(_chars); |
| 984 | _chars = newBuf; | 1005 | _chars = newBuf; |
| @@ -987,9 +1008,9 @@ void UString::ReAlloc(unsigned newLimit) | |||
| 987 | 1008 | ||
| 988 | void UString::ReAlloc2(unsigned newLimit) | 1009 | void UString::ReAlloc2(unsigned newLimit) |
| 989 | { | 1010 | { |
| 990 | if (newLimit >= k_Alloc_Len_Limit) throw 20130221; | 1011 | if (newLimit > k_Alloc_Len_Limit) throw 20130221; |
| 991 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1012 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
| 992 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(newLimit + 1); | 1013 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); |
| 993 | newBuf[0] = 0; | 1014 | newBuf[0] = 0; |
| 994 | MY_STRING_DELETE(_chars); | 1015 | MY_STRING_DELETE(_chars); |
| 995 | _chars = newBuf; | 1016 | _chars = newBuf; |
| @@ -999,7 +1020,7 @@ void UString::ReAlloc2(unsigned newLimit) | |||
| 999 | void UString::SetStartLen(unsigned len) | 1020 | void UString::SetStartLen(unsigned len) |
| 1000 | { | 1021 | { |
| 1001 | _chars = 0; | 1022 | _chars = 0; |
| 1002 | _chars = MY_STRING_NEW_wchar_t(len + 1); | 1023 | _chars = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1003 | _len = len; | 1024 | _len = len; |
| 1004 | _limit = len; | 1025 | _limit = len; |
| 1005 | } | 1026 | } |
| @@ -1010,19 +1031,28 @@ void UString::Grow_1() | |||
| 1010 | next += next / 2; | 1031 | next += next / 2; |
| 1011 | next += 16; | 1032 | next += 16; |
| 1012 | next &= ~(unsigned)15; | 1033 | next &= ~(unsigned)15; |
| 1013 | ReAlloc(next - 1); | 1034 | next--; |
| 1035 | if (next < _len || next > k_Alloc_Len_Limit) | ||
| 1036 | next = k_Alloc_Len_Limit; | ||
| 1037 | if (next <= _len) | ||
| 1038 | throw 20130220; | ||
| 1039 | ReAlloc(next); | ||
| 1014 | } | 1040 | } |
| 1015 | 1041 | ||
| 1016 | void UString::Grow(unsigned n) | 1042 | void UString::Grow(unsigned n) |
| 1017 | { | 1043 | { |
| 1018 | unsigned freeSize = _limit - _len; | 1044 | const unsigned freeSize = _limit - _len; |
| 1019 | if (n <= freeSize) | 1045 | if (n <= freeSize) |
| 1020 | return; | 1046 | return; |
| 1021 | |||
| 1022 | unsigned next = _len + n; | 1047 | unsigned next = _len + n; |
| 1023 | next += next / 2; | 1048 | next += next / 2; |
| 1024 | next += 16; | 1049 | next += 16; |
| 1025 | next &= ~(unsigned)15; | 1050 | next &= ~(unsigned)15; |
| 1051 | next--; | ||
| 1052 | if (next < _len || next > k_Alloc_Len_Limit) | ||
| 1053 | next = k_Alloc_Len_Limit; | ||
| 1054 | if (next <= _len || next - _len < n) | ||
| 1055 | throw 20130220; | ||
| 1026 | ReAlloc(next - 1); | 1056 | ReAlloc(next - 1); |
| 1027 | } | 1057 | } |
| 1028 | 1058 | ||
| @@ -1149,7 +1179,7 @@ UString &UString::operator=(const wchar_t *s) | |||
| 1149 | unsigned len = MyStringLen(s); | 1179 | unsigned len = MyStringLen(s); |
| 1150 | if (len > _limit) | 1180 | if (len > _limit) |
| 1151 | { | 1181 | { |
| 1152 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1182 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1153 | MY_STRING_DELETE(_chars); | 1183 | MY_STRING_DELETE(_chars); |
| 1154 | _chars = newBuf; | 1184 | _chars = newBuf; |
| 1155 | _limit = len; | 1185 | _limit = len; |
| @@ -1166,7 +1196,7 @@ UString &UString::operator=(const UString &s) | |||
| 1166 | unsigned len = s._len; | 1196 | unsigned len = s._len; |
| 1167 | if (len > _limit) | 1197 | if (len > _limit) |
| 1168 | { | 1198 | { |
| 1169 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1199 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1170 | MY_STRING_DELETE(_chars); | 1200 | MY_STRING_DELETE(_chars); |
| 1171 | _chars = newBuf; | 1201 | _chars = newBuf; |
| 1172 | _limit = len; | 1202 | _limit = len; |
| @@ -1180,7 +1210,7 @@ void UString::SetFrom(const wchar_t *s, unsigned len) // no check | |||
| 1180 | { | 1210 | { |
| 1181 | if (len > _limit) | 1211 | if (len > _limit) |
| 1182 | { | 1212 | { |
| 1183 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1213 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1184 | MY_STRING_DELETE(_chars); | 1214 | MY_STRING_DELETE(_chars); |
| 1185 | _chars = newBuf; | 1215 | _chars = newBuf; |
| 1186 | _limit = len; | 1216 | _limit = len; |
| @@ -1218,7 +1248,7 @@ void UString::SetFromBstr(LPCOLESTR s) | |||
| 1218 | 1248 | ||
| 1219 | if (len > _limit) | 1249 | if (len > _limit) |
| 1220 | { | 1250 | { |
| 1221 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1251 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1222 | MY_STRING_DELETE(_chars); | 1252 | MY_STRING_DELETE(_chars); |
| 1223 | _chars = newBuf; | 1253 | _chars = newBuf; |
| 1224 | _limit = len; | 1254 | _limit = len; |
| @@ -1258,7 +1288,7 @@ UString &UString::operator=(const char *s) | |||
| 1258 | unsigned len = MyStringLen(s); | 1288 | unsigned len = MyStringLen(s); |
| 1259 | if (len > _limit) | 1289 | if (len > _limit) |
| 1260 | { | 1290 | { |
| 1261 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1291 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1262 | MY_STRING_DELETE(_chars); | 1292 | MY_STRING_DELETE(_chars); |
| 1263 | _chars = newBuf; | 1293 | _chars = newBuf; |
| 1264 | _limit = len; | 1294 | _limit = len; |
| @@ -1566,15 +1596,24 @@ void UString::DeleteFrontal(unsigned num) throw() | |||
| 1566 | 1596 | ||
| 1567 | void UString2::ReAlloc2(unsigned newLimit) | 1597 | void UString2::ReAlloc2(unsigned newLimit) |
| 1568 | { | 1598 | { |
| 1569 | if (newLimit >= k_Alloc_Len_Limit) throw 20130221; | 1599 | // wrong (_len) is allowed after this function |
| 1600 | if (newLimit > k_Alloc_Len_Limit) throw 20130221; | ||
| 1570 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); | 1601 | // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0); |
| 1571 | _chars = MY_STRING_NEW_wchar_t(newLimit + 1); | 1602 | if (_chars) |
| 1603 | { | ||
| 1604 | MY_STRING_DELETE(_chars); | ||
| 1605 | _chars = NULL; | ||
| 1606 | // _len = 0; | ||
| 1607 | } | ||
| 1608 | _chars = MY_STRING_NEW_wchar_t((size_t)newLimit + 1); | ||
| 1609 | _chars[0] = 0; | ||
| 1610 | // _len = newLimit; | ||
| 1572 | } | 1611 | } |
| 1573 | 1612 | ||
| 1574 | void UString2::SetStartLen(unsigned len) | 1613 | void UString2::SetStartLen(unsigned len) |
| 1575 | { | 1614 | { |
| 1576 | _chars = 0; | 1615 | _chars = NULL; |
| 1577 | _chars = MY_STRING_NEW_wchar_t(len + 1); | 1616 | _chars = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1578 | _len = len; | 1617 | _len = len; |
| 1579 | } | 1618 | } |
| 1580 | 1619 | ||
| @@ -1591,7 +1630,7 @@ UString2::UString2(wchar_t c) | |||
| 1591 | 1630 | ||
| 1592 | UString2::UString2(const wchar_t *s) | 1631 | UString2::UString2(const wchar_t *s) |
| 1593 | { | 1632 | { |
| 1594 | unsigned len = MyStringLen(s); | 1633 | const unsigned len = MyStringLen(s); |
| 1595 | SetStartLen(len); | 1634 | SetStartLen(len); |
| 1596 | wmemcpy(_chars, s, len + 1); | 1635 | wmemcpy(_chars, s, len + 1); |
| 1597 | } | 1636 | } |
| @@ -1628,7 +1667,7 @@ UString2 &UString2::operator=(const wchar_t *s) | |||
| 1628 | unsigned len = MyStringLen(s); | 1667 | unsigned len = MyStringLen(s); |
| 1629 | if (len > _len) | 1668 | if (len > _len) |
| 1630 | { | 1669 | { |
| 1631 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1670 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1632 | if (_chars) | 1671 | if (_chars) |
| 1633 | MY_STRING_DELETE(_chars); | 1672 | MY_STRING_DELETE(_chars); |
| 1634 | _chars = newBuf; | 1673 | _chars = newBuf; |
| @@ -1643,7 +1682,7 @@ void UString2::SetFromAscii(const char *s) | |||
| 1643 | unsigned len = MyStringLen(s); | 1682 | unsigned len = MyStringLen(s); |
| 1644 | if (len > _len) | 1683 | if (len > _len) |
| 1645 | { | 1684 | { |
| 1646 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1685 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1647 | if (_chars) | 1686 | if (_chars) |
| 1648 | MY_STRING_DELETE(_chars); | 1687 | MY_STRING_DELETE(_chars); |
| 1649 | _chars = newBuf; | 1688 | _chars = newBuf; |
| @@ -1662,7 +1701,7 @@ UString2 &UString2::operator=(const UString2 &s) | |||
| 1662 | unsigned len = s._len; | 1701 | unsigned len = s._len; |
| 1663 | if (len > _len) | 1702 | if (len > _len) |
| 1664 | { | 1703 | { |
| 1665 | wchar_t *newBuf = MY_STRING_NEW_wchar_t(len + 1); | 1704 | wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1); |
| 1666 | if (_chars) | 1705 | if (_chars) |
| 1667 | MY_STRING_DELETE(_chars); | 1706 | MY_STRING_DELETE(_chars); |
| 1668 | _chars = newBuf; | 1707 | _chars = newBuf; |
