Differences From Artifact [e446d14903]:
- File
src/OFMutableString.m
— part of check-in
[f173477bef]
at
2011-09-19 16:34:04
on branch trunk
— Rename -[allocMemoryForNItems:withSize:] and friends.
It is now -[allocMemoryForNItems:ofSize:]. (user: js, size: 15696) [annotate] [blame] [check-ins using]
To Artifact [471b2f9466]:
- File
src/OFMutableString.m
— part of check-in
[e1e7ffa903]
at
2011-09-22 23:25:42
on branch trunk
— Exceptions are now autoreleased.
This is safe as an "exception loop" can't happen, since if allocating
an exception fails, it throws an OFAllocFailedException which is
preallocated and can always be thrown.So, the worst case would be that an autorelease of an exception fails,
triggering an OFOutOfMemoryException for which there is no memory,
resulting in an OFAllocFailedException to be thrown. (user: js, size: 15789) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
74 75 76 77 78 79 80 | while (i < s->cStringLength) { cLen = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c); if (cLen == 0 || c > 0x10FFFF) { [self freeMemory: unicodeString]; | | > | > | > | 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | while (i < s->cStringLength) { cLen = of_string_utf8_to_unicode(s->cString + i, s->cStringLength - i, &c); if (cLen == 0 || c > 0x10FFFF) { [self freeMemory: unicodeString]; @throw [OFInvalidEncodingException exceptionWithClass: isa]; } if (c >> 8 < tableSize) { of_unichar_t tc = table[c >> 8][c & 0xFF]; if (tc) c = tc; } unicodeString[j++] = c; if (c < 0x80) newCStringLength++; else if (c < 0x800) newCStringLength += 2; else if (c < 0x10000) newCStringLength += 3; else if (c < 0x110000) newCStringLength += 4; else { [self freeMemory: unicodeString]; @throw [OFInvalidEncodingException exceptionWithClass: isa]; } i += cLen; } @try { newCString = [self allocMemoryWithSize: newCStringLength + 1]; } @catch (id e) { [self freeMemory: unicodeString]; @throw e; } j = 0; for (i = 0; i < unicodeLen; i++) { if ((d = of_string_unicode_to_utf8(unicodeString[i], newCString + j)) == 0) { [self freeMemory: unicodeString]; [self freeMemory: newCString]; @throw [OFInvalidEncodingException exceptionWithClass: isa]; } j += d; } assert(j == newCStringLength); newCString[j] = 0; [self freeMemory: unicodeString]; |
︙ | ︙ | |||
149 150 151 152 153 154 155 | } switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) { case 1: s->UTF8 = YES; break; case -1: | | | 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | } switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) { case 1: s->UTF8 = YES; break; case -1: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1); |
︙ | ︙ | |||
176 177 178 179 180 181 182 | } switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) { case 1: s->UTF8 = YES; break; case -1: | | | 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | } switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) { case 1: s->UTF8 = YES; break; case -1: @throw [OFInvalidEncodingException exceptionWithClass: isa]; } s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength); |
︙ | ︙ | |||
220 221 222 223 224 225 226 | } - (void)appendString: (OFString*)string { size_t UTF8StringLength; if (string == nil) | | | | 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | } - (void)appendString: (OFString*)string { size_t UTF8StringLength; if (string == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; UTF8StringLength = [string UTF8StringLength]; s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + UTF8StringLength + 1]; memcpy(s->cString + s->cStringLength, string->s->cString, |
︙ | ︙ | |||
257 258 259 260 261 262 263 | - (void)appendFormat: (OFConstantString*)format withArguments: (va_list)arguments { char *UTF8String; int UTF8StringLength; if (format == nil) | | | | | 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | - (void)appendFormat: (OFConstantString*)format withArguments: (va_list)arguments { char *UTF8String; int UTF8StringLength; if (format == nil) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; if ((UTF8StringLength = of_vasprintf(&UTF8String, [format UTF8String], arguments)) == -1) @throw [OFInvalidFormatException exceptionWithClass: isa]; @try { [self appendUTF8String: UTF8String withLength: UTF8StringLength]; } @finally { free(UTF8String); } |
︙ | ︙ | |||
305 306 307 308 309 310 311 | /* ASCII */ if (OF_LIKELY(!(s->cString[i] & 0x80))) continue; /* A start byte can't happen first as we reversed everything */ if (OF_UNLIKELY(s->cString[i] & 0x40)) { madvise(s->cString, s->cStringLength, MADV_NORMAL); | | > | > | > | > | | 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | /* ASCII */ if (OF_LIKELY(!(s->cString[i] & 0x80))) continue; /* A start byte can't happen first as we reversed everything */ if (OF_UNLIKELY(s->cString[i] & 0x40)) { madvise(s->cString, s->cStringLength, MADV_NORMAL); @throw [OFInvalidEncodingException exceptionWithClass: isa]; } /* Next byte must not be ASCII */ if (OF_UNLIKELY(s->cStringLength < i + 1 || !(s->cString[i + 1] & 0x80))) { madvise(s->cString, s->cStringLength, MADV_NORMAL); @throw [OFInvalidEncodingException exceptionWithClass: isa]; } /* Next byte is the start byte */ if (OF_LIKELY(s->cString[i + 1] & 0x40)) { s->cString[i] ^= s->cString[i + 1]; s->cString[i + 1] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 1]; i++; continue; } /* Second next byte must not be ASCII */ if (OF_UNLIKELY(s->cStringLength < i + 2 || !(s->cString[i + 2] & 0x80))) { madvise(s->cString, s->cStringLength, MADV_NORMAL); @throw [OFInvalidEncodingException exceptionWithClass: isa]; } /* Second next byte is the start byte */ if (OF_LIKELY(s->cString[i + 2] & 0x40)) { s->cString[i] ^= s->cString[i + 2]; s->cString[i + 2] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 2]; i += 2; continue; } /* Third next byte must not be ASCII */ if (OF_UNLIKELY(s->cStringLength < i + 3 || !(s->cString[i + 3] & 0x80))) { madvise(s->cString, s->cStringLength, MADV_NORMAL); @throw [OFInvalidEncodingException exceptionWithClass: isa]; } /* Third next byte is the start byte */ if (OF_LIKELY(s->cString[i + 3] & 0x40)) { s->cString[i] ^= s->cString[i + 3]; s->cString[i + 3] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 3]; s->cString[i + 1] ^= s->cString[i + 2]; s->cString[i + 2] ^= s->cString[i + 1]; s->cString[i + 1] ^= s->cString[i + 2]; i += 3; continue; } /* UTF-8 does not allow more than 4 bytes per character */ madvise(s->cString, s->cStringLength, MADV_NORMAL); @throw [OFInvalidEncodingException exceptionWithClass: isa]; } madvise(s->cString, s->cStringLength, MADV_NORMAL); } - (void)upper { |
︙ | ︙ | |||
389 390 391 392 393 394 395 | - (void)insertString: (OFString*)string atIndex: (size_t)index { size_t newCStringLength; if (index > s->length) | | | 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | - (void)insertString: (OFString*)string atIndex: (size_t)index { size_t newCStringLength; if (index > s->length) @throw [OFOutOfRangeException exceptionWithClass: isa]; if (s->UTF8) index = of_string_index_to_position(s->cString, index, s->cStringLength); newCStringLength = s->cStringLength + [string UTF8StringLength]; s->cString = [self resizeMemory: s->cString |
︙ | ︙ | |||
415 416 417 418 419 420 421 | - (void)deleteCharactersInRange: (of_range_t)range { size_t start = range.start; size_t end = range.start + range.length; if (start > end) | | | | < | | | | 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | - (void)deleteCharactersInRange: (of_range_t)range { size_t start = range.start; size_t end = range.start + range.length; if (start > end) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; if (end > s->length) @throw [OFOutOfRangeException exceptionWithClass: isa]; s->length -= end - start; if (s->UTF8) { start = of_string_index_to_position(s->cString, start, s->cStringLength); end = of_string_index_to_position(s->cString, end, s->cStringLength); } memmove(s->cString + start, s->cString + end, s->cStringLength - end); s->cStringLength -= end - start; s->cString[s->cStringLength] = 0; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } - (void)replaceCharactersInRange: (of_range_t)range withString: (OFString*)replacement { size_t start = range.start; size_t end = range.start + range.length; size_t newCStringLength, newLength; if (start > end) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; if (end > s->length) @throw [OFOutOfRangeException exceptionWithClass: isa]; newLength = s->length - (end - start) + [replacement length]; if (s->UTF8) { start = of_string_index_to_position(s->cString, start, s->cStringLength); end = of_string_index_to_position(s->cString, end, |
︙ | ︙ | |||
565 566 567 568 569 570 571 | s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ | < | 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } - (void)deleteTrailingWhitespaces { size_t d; char *p; |
︙ | ︙ | |||
592 593 594 595 596 597 598 | s->length -= d; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ | < | 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | s->length -= d; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } - (void)deleteEnclosingWhitespaces { size_t d, i; char *p; |
︙ | ︙ | |||
631 632 633 634 635 636 637 | s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ | < | 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | s->cString[s->cStringLength] = '\0'; @try { s->cString = [self resizeMemory: s->cString toSize: s->cStringLength + 1]; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } - copy { return [[OFString alloc] initWithString: self]; } - (void)makeImmutable { isa = [OFString class]; } @end |