Overview
Comment: | More style improvements. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
6f4bf74c7d363a9b63a0db0866542916 |
User & Date: | js on 2011-04-23 13:23:51 |
Other Links: | manifest | tags |
Context
2011-04-23
| ||
14:46 | Add -[uppercaseString] and -[lowercaseString] to OFString. check-in: becb4c9b86 user: js tags: trunk | |
13:23 | More style improvements. check-in: 6f4bf74c7d user: js tags: trunk | |
02:15 | OFMutableString: Call madvise() on the correct length. check-in: a4df72758c user: js tags: trunk | |
Changes
Modified src/OFMutableString.h from [870877ba5c] to [960bff6aa8].
︙ | ︙ | |||
22 23 24 25 26 27 28 | /** * \brief A class for storing and modifying strings. */ @interface OFMutableString: OFString /** * Sets the OFString to the specified UTF-8 encoded C string. * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 133 134 | /** * \brief A class for storing and modifying strings. */ @interface OFMutableString: OFString /** * Sets the OFString to the specified UTF-8 encoded C string. * * \param string A UTF-8 encoded C string to set the OFString to. */ - (void)setToCString: (const char*)string; /** * Appends a UTF-8 encoded C string to the OFString. * * \param string A UTF-8 encoded C string to append */ - (void)appendCString: (const char*)string; /** * Appends a UTF-8 encoded C string with the specified length to the OFString. * * \param string A UTF-8 encoded C string to append * \param length The length of the UTF-8 encoded C string */ - (void)appendCString: (const char*)string withLength: (size_t)length; /** * Appends a UTF-8 encoded C string to the OFString without checking whether it * is valid UTF-8. * * Only use this if you are 100% sure the string you append is either ASCII or * UTF-8! * * \param string A UTF-8 encoded C string to append */ - (void)appendCStringWithoutUTF8Checking: (const char*)string; /** * Appends a UTF-8 encoded C string with the specified length to the OFString * without checking whether it is valid UTF-8. * * Only use this if you are 100% sure the string you append is either ASCII or * UTF-8! * * \param string A UTF-8 encoded C string to append * \param length The length of the UTF-8 encoded C string */ - (void)appendCStringWithoutUTF8Checking: (const char*)string length: (size_t)length; /** * Appends another OFString to the OFString. * * \param string An OFString to append */ - (void)appendString: (OFString*)string; /** * Appends a formatted UTF-8 encoded C string to the OFString. * See printf for the format syntax. * * \param format A format string which generates the string to append */ - (void)appendFormat: (OFString*)format, ...; /** * Appends a formatted UTF-8 encoded C string to the OFString. * See printf for the format syntax. * * \param format A format string which generates the string to append * \param arguments The arguments used in the format string */ - (void)appendFormat: (OFString*)format withArguments: (va_list)arguments; /** * Prepends another OFString to the OFString. * * \param string An OFString to prepend */ - (void)prependString: (OFString*)string; /** * Reverse the OFString. */ - (void)reverse; /** * Upper the OFString. */ - (void)upper; /** * Lower the OFString. */ - (void)lower; /** * Inserts a string at the specified index. * * \param string The string to insert * \param index The index */ - (void)insertString: (OFString*)string atIndex: (size_t)index; /** * Deletes the characters at the specified range. * * \param start The index where the deletion should be started * \param end The index until which the characters should be deleted. * This points BEHIND the last character! |
︙ | ︙ | |||
145 146 147 148 149 150 151 | /** * Replaces the characters at the specified range. * * \param start The index where the replacement should be started * \param end The index until which the characters should be replaced. * This points BEHIND the last character! | | | | | | | | | | 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | /** * Replaces the characters at the specified range. * * \param start The index where the replacement should be started * \param end The index until which the characters should be replaced. * This points BEHIND the last character! * \param replacement The string to the replace the characters with */ - (void)replaceCharactersFromIndex: (size_t)start toIndex: (size_t)end withString: (OFString*)replacement; /** * Deletes the characters at the specified range. * * \param range The range of the characters which should be replaced * \param replacement The string to the replace the characters with */ - (void)replaceCharactersInRange: (of_range_t)range withString: (OFString*)replacement; /** * Deletes all occurrences of a string with another string. * * \param string The string to replace * \param replacement The string with which it should be replaced */ - (void)replaceOccurrencesOfString: (OFString*)string withString: (OFString*)replacement; /** * Deletes all whitespaces at the beginning of a string. */ - (void)deleteLeadingWhitespaces; /** |
︙ | ︙ |
Modified src/OFMutableString.m from [4ddd782651] to [fa9bcee55c].
︙ | ︙ | |||
39 40 41 42 43 44 45 | #import "macros.h" #import "of_asprintf.h" #import "unicode.h" @implementation OFMutableString - (void)_applyTable: (const of_unichar_t* const[])table | | | | | | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #import "macros.h" #import "of_asprintf.h" #import "unicode.h" @implementation OFMutableString - (void)_applyTable: (const of_unichar_t* const[])table withSize: (size_t)tableSize { of_unichar_t c; of_unichar_t *unicodeString; size_t unicodeLen, newLength, cLen; size_t i, j, d; char *newString; if (!isUTF8) { assert(tableSize >= 1); uint8_t *p = (uint8_t*)string + length; uint8_t t; while (--p >= (uint8_t*)string) if ((t = table[0][*p]) != 0) *p = t; return; } unicodeLen = [self length]; unicodeString = [self allocMemoryForNItems: unicodeLen withSize: sizeof(of_unichar_t)]; i = j = 0; newLength = 0; while (i < length) { cLen = of_string_utf8_to_unicode(string + i, length - i, &c); if (cLen == 0 || c > 0x10FFFF) { [self freeMemory: unicodeString]; @throw [OFInvalidEncodingException newWithClass: isa]; } if (c >> 8 < tableSize) { of_unichar_t tc = table[c >> 8][c & 0xFF]; if (tc) c = tc; } unicodeString[j++] = c; if (c < 0x80) newLength++; else if (c < 0x800) newLength += 2; else if (c < 0x10000) newLength += 3; else if (c < 0x110000) newLength += 4; else { [self freeMemory: unicodeString]; @throw [OFInvalidEncodingException newWithClass: isa]; } i += cLen; } @try { newString = [self allocMemoryWithSize: newLength + 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], newString + j)) == 0) { [self freeMemory: unicodeString]; [self freeMemory: newString]; @throw [OFInvalidEncodingException newWithClass: isa]; } j += d; } assert(j == newLength); newString[j] = 0; [self freeMemory: unicodeString]; [self freeMemory: string]; string = newString; length = newLength; } - (void)setToCString: (const char*)string_ { size_t len; [self freeMemory: string]; len = strlen(string_); switch (of_string_check_utf8(string_, len)) { case 0: isUTF8 = NO; break; case 1: isUTF8 = YES; break; case -1: string = NULL; length = 0; isUTF8 = NO; @throw [OFInvalidEncodingException newWithClass: isa]; } length = len; string = [self allocMemoryWithSize: length + 1]; memcpy(string, string_, length + 1); } - (void)appendCString: (const char*)string_ { size_t len; len = strlen(string_); switch (of_string_check_utf8(string_, len)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } string = [self resizeMemory: string toSize: length + len + 1]; memcpy(string + length, string_, len + 1); length += len; } - (void)appendCString: (const char*)string_ withLength: (size_t)length_ { switch (of_string_check_utf8(string_, length_)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } string = [self resizeMemory: string toSize: length + length_ + 1]; memcpy(string + length, string_, length_); length += length_; string[length] = 0; } - (void)appendCStringWithoutUTF8Checking: (const char*)string_ { size_t len; len = strlen(string_); string = [self resizeMemory: string toSize: length + len + 1]; memcpy(string + length, string_, len + 1); length += len; } - (void)appendCStringWithoutUTF8Checking: (const char*)string_ length: (size_t)length_ { string = [self resizeMemory: string toSize: length + length_ + 1]; memcpy(string + length, string_, length_); length += length_; string[length] = 0; } - (void)appendString: (OFString*)string_ { if (string_ == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; [self appendCString: [string_ cString]]; } - (void)appendFormat: (OFString*)format, ... { va_list arguments; va_start(arguments, format); [self appendFormat: format withArguments: arguments]; va_end(arguments); } - (void)appendFormat: (OFString*)format withArguments: (va_list)arguments { char *t; int len; if (format == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if ((len = of_vasprintf(&t, [format cString], arguments)) == -1) @throw [OFInvalidFormatException newWithClass: isa]; @try { [self appendCString: t withLength: len]; } @finally { free(t); } } - (void)prependString: (OFString*)string_ { return [self insertString: string_ atIndex: 0]; } - (void)reverse { size_t i, j, len = length / 2; |
︙ | ︙ | |||
360 361 362 363 364 365 366 | - (void)lower { [self _applyTable: of_unicode_lower_table withSize: OF_UNICODE_LOWER_TABLE_SIZE]; } | | | | | | | | | > | | | | 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 387 388 389 390 391 392 393 394 395 | - (void)lower { [self _applyTable: of_unicode_lower_table withSize: OF_UNICODE_LOWER_TABLE_SIZE]; } - (void)insertString: (OFString*)string_ atIndex: (size_t)index { size_t newLength; if (isUTF8) index = of_string_index_to_position(string, index, length); if (index > length) @throw [OFOutOfRangeException newWithClass: isa]; newLength = length + [string_ cStringLength]; string = [self resizeMemory: string toSize: newLength + 1]; memmove(string + index + [string_ cStringLength], string + index, length - index); memcpy(string + index, [string_ cString], [string_ cStringLength]); string[newLength] = '\0'; length = newLength; } - (void)deleteCharactersFromIndex: (size_t)start toIndex: (size_t)end { if (isUTF8) { start = of_string_index_to_position(string, start, length); |
︙ | ︙ | |||
418 419 420 421 422 423 424 | { [self deleteCharactersFromIndex: range.start toIndex: range.start + range.length]; } - (void)replaceCharactersFromIndex: (size_t)start toIndex: (size_t)end | | | | | | | > | | | | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | | | 420 421 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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | { [self deleteCharactersFromIndex: range.start toIndex: range.start + range.length]; } - (void)replaceCharactersFromIndex: (size_t)start toIndex: (size_t)end withString: (OFString*)replacement { size_t newLength; if (isUTF8) { start = of_string_index_to_position(string, start, length); end = of_string_index_to_position(string, end, length); } if (start > end) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if (end > length) @throw [OFOutOfRangeException newWithClass: isa]; newLength = length - (end - start) + [replacement cStringLength]; string = [self resizeMemory: string toSize: newLength + 1]; memmove(string + end, string + start + [replacement cStringLength], length - end); memcpy(string + start, [replacement cString], [replacement cStringLength]); string[newLength] = '\0'; length = newLength; } - (void)replaceCharactersInRange: (of_range_t)range withString: (OFString*)replacement { [self replaceCharactersFromIndex: range.start toIndex: range.start + range.length withString: replacement]; } - (void)replaceOccurrencesOfString: (OFString*)string_ withString: (OFString*)replacement { const char *cString = [string_ cString]; const char *replacementCString = [replacement cString]; size_t stringLen = [string_ cStringLength]; size_t replacementLen = [replacement cStringLength]; size_t i, last, newLength; char *newString; if (stringLen > length) return; newString = NULL; newLength = 0; for (i = 0, last = 0; i <= length - stringLen; i++) { if (memcmp(string + i, cString, stringLen)) continue; @try { newString = [self resizeMemory: newString toSize: newLength + i - last + replacementLen + 1]; } @catch (id e) { [self freeMemory: newString]; @throw e; } memcpy(newString + newLength, string + last, i - last); memcpy(newString + newLength + i - last, replacementCString, replacementLen); newLength += i - last + replacementLen; i += stringLen - 1; last = i + 1; } @try { newString = [self resizeMemory: newString toSize: newLength + length - last + 1]; } @catch (id e) { [self freeMemory: newString]; @throw e; } memcpy(newString + newLength, string + last, length - last); newLength += length - last; newString[newLength] = 0; [self freeMemory: string]; string = newString; length = newLength; } - (void)deleteLeadingWhitespaces { size_t i; for (i = 0; i < length; i++) |
︙ | ︙ |
Modified src/OFString+Hashing.m from [4e1492dfab] to [afd3c22e1b].
︙ | ︙ | |||
25 26 27 28 29 30 31 | @implementation OFString (Hashing) - (OFString*)MD5Hash { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *hash = [OFMD5Hash MD5Hash]; uint8_t *digest; | | | | | | | | | | 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | @implementation OFString (Hashing) - (OFString*)MD5Hash { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *hash = [OFMD5Hash MD5Hash]; uint8_t *digest; char ret[OF_MD5_DIGEST_SIZE * 2]; size_t i; [hash updateWithBuffer: string length: length]; digest = [hash digest]; for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) { uint8_t high, low; high = digest[i] >> 4; low = digest[i] & 0x0F; ret[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); } [pool release]; return [OFString stringWithCString: ret length: 32]; } - (OFString*)SHA1Hash { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *hash = [OFSHA1Hash SHA1Hash]; uint8_t *digest; char ret[OF_SHA1_DIGEST_SIZE * 2]; size_t i; [hash updateWithBuffer: string length: length]; digest = [hash digest]; for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) { uint8_t high, low; high = digest[i] >> 4; low = digest[i] & 0x0F; ret[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0'); ret[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0'); } [pool release]; return [OFString stringWithCString: ret length: 40]; } @end |
Modified src/OFString.h from [3da081c65e] to [8c8a66350c].
︙ | ︙ | |||
61 62 63 64 65 66 67 | * \return A new autoreleased OFString */ + string; /** * Creates a new OFString from a UTF-8 encoded C string. * | | | | | | | | | | | | | | | | | | | | 61 62 63 64 65 66 67 68 69 70 71 72 73 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 133 134 135 136 137 | * \return A new autoreleased OFString */ + string; /** * Creates a new OFString from a UTF-8 encoded C string. * * \param string A UTF-8 encoded C string to initialize the OFString with * \return A new autoreleased OFString */ + stringWithCString: (const char*)string; /** * Creates a new OFString from a C string with the specified encoding. * * \param string A C string to initialize the OFString with * \param encoding The encoding of the C string * \return A new autoreleased OFString */ + stringWithCString: (const char*)string encoding: (of_string_encoding_t)encoding; /** * Creates a new OFString from a C string with the specified encoding and * length. * * \param string A C string to initialize the OFString with * \param encoding The encoding of the C string * \param length The length of the C string * \return A new autoreleased OFString */ + stringWithCString: (const char*)string encoding: (of_string_encoding_t)encoding length: (size_t)length; /** * Creates a new OFString from a UTF-8 encoded C string with the specified * length. * * \param string A UTF-8 encoded C string to initialize the OFString with * \param length The length of the UTF-8 encoded C string * \return A new autoreleased OFString */ + stringWithCString: (const char*)string length: (size_t)length; /** * Creates a new OFString from a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString * \return A new autoreleased OFString */ + stringWithFormat: (OFString*)format, ...; /** * Creates a new OFString containing the constructed specified path. * * \param firstComponent The first component of the path * \return A new autoreleased OFString */ + stringWithPath: (OFString*)firstComponent, ...; /** * Creates a new OFString from another string. * * \param string A string to initialize the OFString with * \return A new autoreleased OFString */ + stringWithString: (OFString*)string; /** * Creates a new OFString with the contents of the specified UTF-8 encoded file. * * \param path The path to the file * \return A new autoreleased OFString */ |
︙ | ︙ | |||
148 149 150 151 152 153 154 | + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Creates a new OFString with the contents of the specified URL, assuming * UTF-8 encoding. * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Creates a new OFString with the contents of the specified URL, assuming * UTF-8 encoding. * * \param URL The URL to the contents for the string * \return A new autoreleased OFString */ + stringWithContentsOfURL: (OFURL*)URL; /** * Creates a new OFString with the contents of the specified URL in the * specified encoding. * * \param URL The URL to the contents for the string * \param encoding The encoding to assume * \return A new autoreleased OFString */ + stringWithContentsOfURL: (OFURL*)URL encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString from a UTF-8 encoded C string. * * \param string A UTF-8 encoded C string to initialize the OFString with * \return An initialized OFString */ - initWithCString: (const char*)string; /** * Initializes an already allocated OFString from a C string with the specified * encoding. * * \param string A C string to initialize the OFString with * \param encoding The encoding of the C string * \return An initialized OFString */ - initWithCString: (const char*)string encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString from a C string with the specified * encoding and length. * * \param string A C string to initialize the OFString with * \param encoding The encoding of the C string * \param length The length of the C string * \return An initialized OFString */ - initWithCString: (const char*)string encoding: (of_string_encoding_t)encoding length: (size_t)length; /** * Initializes an already allocated OFString from a UTF-8 encoded C string with * the specified length. * * \param string A UTF-8 encoded C string to initialize the OFString with * \param length The length of the UTF-8 encoded C string * \return An initialized OFString */ - initWithCString: (const char*)string length: (size_t)length; /** * Initializes an already allocated OFString with a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString * \return An initialized OFString */ - initWithFormat: (OFString*)format, ...; /** * Initializes an already allocated OFString with a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString * \param arguments The arguments used in the format string * \return An initialized OFString */ - initWithFormat: (OFString*)format arguments: (va_list)arguments; /** * Initializes an already allocated OFString with the constructed specified * path. * * \param firstComponent The first component of the path * \return A new autoreleased OFString */ - initWithPath: (OFString*)firstComponent, ...; /** * Initializes an already allocated OFString with the constructed specified * path. * * \param firstComponent The first component of the path * \param arguments A va_list with the other components of the path * \return A new autoreleased OFString */ - initWithPath: (OFString*)firstComponent arguments: (va_list)arguments; /** * Initializes an already allocated OFString with another string. * * \param string A string to initialize the OFString with * \return An initialized OFString */ - initWithString: (OFString*)string; /** * Initializes an already allocated OFString with the contents of the specified * file in the specified encoding. * * \param path The path to the file * \return An initialized OFString |
︙ | ︙ | |||
279 280 281 282 283 284 285 | - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString with the contents of the specified * URL, assuming UTF-8 encoding. * | | | | | | 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString with the contents of the specified * URL, assuming UTF-8 encoding. * * \param URL The URL to the contents for the string * \return An initialized OFString */ - initWithContentsOfURL: (OFURL*)URL; /** * Initializes an already allocated OFString with the contents of the specified * URL in the specified encoding. * * \param URL The URL to the contents for the string * \param encoding The encoding to assume * \return An initialized OFString */ - initWithContentsOfURL: (OFURL*)URL encoding: (of_string_encoding_t)encoding; /** * \return The OFString as a UTF-8 encoded C string */ - (const char*)cString; |
︙ | ︙ | |||
317 318 319 320 321 322 323 | /// \cond internal - (BOOL)isUTF8; /// \endcond /** * Compares the OFString to another OFString without caring about the case. * | | | | | | | | | | | | 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 | /// \cond internal - (BOOL)isUTF8; /// \endcond /** * Compares the OFString to another OFString without caring about the case. * * \param otherString A string to compare with * \return An of_comparison_result_t */ - (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString; /** * \param index The index of the Unicode character to return * \return The Unicode character at the specified index */ - (of_unichar_t)characterAtIndex: (size_t)index; /** * \param string The string to search * \return The index of the first occurrence of the string or OF_INVALID_INDEX * if it was not found */ - (size_t)indexOfFirstOccurrenceOfString: (OFString*)string; /** * \param string The string to search * \return The index of the last occurrence of the string or OF_INVALID_INDEX if * it was not found */ - (size_t)indexOfLastOccurrenceOfString: (OFString*)string; /** * \param string The string to search * \return Whether the string contains the specified string */ - (BOOL)containsString: (OFString*)string; /** * \param start The index where the substring starts * \param end The index where the substring ends. * This points BEHIND the last character! * \return The substring as a new autoreleased OFString */ - (OFString*)substringFromIndex: (size_t)start toIndex: (size_t)end; /** * \param range The range of the substring * \return The substring as a new autoreleased OFString */ - (OFString*)substringWithRange: (of_range_t)range; /** * Creates a new string by appending another string. * * \param string The string to append * \return A new autoreleased OFString with the specified string appended */ - (OFString*)stringByAppendingString: (OFString*)string; /** * Creates a new string by deleting leading whitespaces. * * \return A new autoreleased OFString with leading whitespaces deleted */ - (OFString*)stringByDeletingLeadingWhitespaces; |
︙ | ︙ |
Modified src/OFString.m from [ead9a0ea73] to [7863930129].
︙ | ︙ | |||
59 60 61 62 63 64 65 | _OFString_Hashing_reference = 1; _OFString_URLEncoding_reference = 1; _OFString_XMLEscaping_reference = 1; _OFString_XMLUnescaping_reference = 1; }; static inline int | | | | | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | _OFString_Hashing_reference = 1; _OFString_URLEncoding_reference = 1; _OFString_XMLEscaping_reference = 1; _OFString_XMLUnescaping_reference = 1; }; static inline int memcasecmp(const char *first, const char *second, size_t len) { size_t i; for (i = 0; i < len; i++) { if (tolower((int)first[i]) > tolower((int)second[i])) return OF_ORDERED_DESCENDING; if (tolower((int)first[i]) < tolower((int)second[i])) return OF_ORDERED_ASCENDING; } return OF_ORDERED_SAME; } int |
︙ | ︙ | |||
242 243 244 245 246 247 248 | @implementation OFString + string { return [[[self alloc] init] autorelease]; } | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | @implementation OFString + string { return [[[self alloc] init] autorelease]; } + stringWithCString: (const char*)string { return [[[self alloc] initWithCString: string] autorelease]; } + stringWithCString: (const char*)string encoding: (of_string_encoding_t)encoding { return [[[self alloc] initWithCString: string encoding: encoding] autorelease]; } + stringWithCString: (const char*)string encoding: (of_string_encoding_t)encoding length: (size_t)length { return [[[self alloc] initWithCString: string encoding: encoding length: length] autorelease]; } + stringWithCString: (const char*)string length: (size_t)length { return [[[self alloc] initWithCString: string length: length] autorelease]; } + stringWithFormat: (OFString*)format, ... { id ret; va_list arguments; va_start(arguments, format); ret = [[[self alloc] initWithFormat: format arguments: arguments] autorelease]; va_end(arguments); return ret; } + stringWithPath: (OFString*)firstComponent, ... { id ret; va_list arguments; va_start(arguments, firstComponent); ret = [[[self alloc] initWithPath: firstComponent arguments: arguments] autorelease]; va_end(arguments); return ret; } + stringWithString: (OFString*)string { return [[[self alloc] initWithString: string] autorelease]; } + stringWithContentsOfFile: (OFString*)path { return [[[self alloc] initWithContentsOfFile: path] autorelease]; } + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding { return [[[self alloc] initWithContentsOfFile: path encoding: encoding] autorelease]; } + stringWithContentsOfURL: (OFURL*)URL { return [[[self alloc] initWithContentsOfURL: URL] autorelease]; } + stringWithContentsOfURL: (OFURL*)URL encoding: (of_string_encoding_t)encoding { return [[[self alloc] initWithContentsOfURL: URL encoding: encoding] autorelease]; } - init { self = [super init]; @try { string = [self allocMemoryWithSize: 1]; string[0] = '\0'; } @catch (id e) { [self release]; @throw e; } return self; } - initWithCString: (const char*)string_ { return [self initWithCString: string_ encoding: OF_STRING_ENCODING_UTF_8 length: strlen(string_)]; } - initWithCString: (const char*)string_ encoding: (of_string_encoding_t)encoding { return [self initWithCString: string_ encoding: encoding length: strlen(string_)]; } - initWithCString: (const char*)string_ encoding: (of_string_encoding_t)encoding length: (size_t)length_ { self = [super init]; @try { size_t i, j; const uint16_t *table; if (encoding == OF_STRING_ENCODING_UTF_8 && length_ >= 3 && !memcmp(string_, "\xEF\xBB\xBF", 3)) { string_ += 3; length_ -= 3; } string = [self allocMemoryWithSize: length_ + 1]; length = length_; if (encoding == OF_STRING_ENCODING_UTF_8) { switch (of_string_check_utf8(string_, length)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } memcpy(string, string_, length); string[length] = 0; return self; } if (encoding == OF_STRING_ENCODING_ISO_8859_1) { for (i = j = 0; i < length_; i++) { char buf[4]; size_t bytes; if (!(string_[i] & 0x80)) { string[j++] = string_[i]; continue; } isUTF8 = YES; bytes = of_string_unicode_to_utf8( (uint8_t)string_[i], buf); if (bytes == 0) @throw [OFInvalidEncodingException newWithClass: isa]; length += bytes - 1; string = [self resizeMemory: string |
︙ | ︙ | |||
432 433 434 435 436 437 438 | case OF_STRING_ENCODING_WINDOWS_1252: table = of_windows_1252; break; default: @throw [OFInvalidEncodingException newWithClass: isa]; } | | | | | | | | | > | | | | | | | | | | | | | | | | | | > | 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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | case OF_STRING_ENCODING_WINDOWS_1252: table = of_windows_1252; break; default: @throw [OFInvalidEncodingException newWithClass: isa]; } for (i = j = 0; i < length_; i++) { char buf[4]; of_unichar_t character; size_t characterBytes; if (!(string_[i] & 0x80)) { string[j++] = string_[i]; continue; } character = table[(uint8_t)string_[i]]; if (character == 0xFFFD) @throw [OFInvalidEncodingException newWithClass: isa]; isUTF8 = YES; characterBytes = of_string_unicode_to_utf8(character, buf); if (characterBytes == 0) @throw [OFInvalidEncodingException newWithClass: isa]; length += characterBytes - 1; string = [self resizeMemory: string toSize: length + 1]; memcpy(string + j, buf, characterBytes); j += characterBytes; } string[length] = 0; } @catch (id e) { [self release]; @throw e; } return self; } - initWithCString: (const char*)string_ length: (size_t)length_ { return [self initWithCString: string_ encoding: OF_STRING_ENCODING_UTF_8 length: length_]; } - initWithFormat: (OFString*)format, ... { id ret; va_list arguments; va_start(arguments, format); ret = [self initWithFormat: format arguments: arguments]; va_end(arguments); return ret; } - initWithFormat: (OFString*)format arguments: (va_list)arguments { self = [super init]; @try { int len; if (format == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; if ((len = of_vasprintf(&string, [format cString], arguments)) == -1) @throw [OFInvalidFormatException newWithClass: isa]; @try { length = len; switch (of_string_check_utf8(string, length)) { case 1: |
︙ | ︙ | |||
532 533 534 535 536 537 538 | [self release]; @throw e; } return self; } | | | | | | | | | | | | > | | | | | | | | | 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | [self release]; @throw e; } return self; } - initWithPath: (OFString*)firstComponent, ... { id ret; va_list arguments; va_start(arguments, firstComponent); ret = [self initWithPath: firstComponent arguments: arguments]; va_end(arguments); return ret; } - initWithPath: (OFString*)firstComponent arguments: (va_list)arguments { self = [super init]; @try { OFString *component; size_t len, i; va_list argumentsCopy; length = [firstComponent cStringLength]; switch (of_string_check_utf8([firstComponent cString], length)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } /* Calculate length */ va_copy(argumentsCopy, arguments); while ((component = va_arg(argumentsCopy, OFString*)) != nil) { len = [component cStringLength]; length += 1 + len; switch (of_string_check_utf8([component cString], len)) { case 1: isUTF8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithClass: isa]; } } string = [self allocMemoryWithSize: length + 1]; len = [firstComponent cStringLength]; memcpy(string, [firstComponent cString], len); i = len; while ((component = va_arg(arguments, OFString*)) != nil) { len = [component cStringLength]; string[i] = OF_PATH_DELIM; memcpy(string + i + 1, [component cString], len); i += len + 1; } string[i] = '\0'; } @catch (id e) { [self release]; @throw e; } return self; } - initWithString: (OFString*)string_ { self = [super init]; @try { /* We have no -[dealloc], so this is ok */ string = (char*)[string_ cString]; length = [string_ cStringLength]; switch (of_string_check_utf8(string, length)) { case 1: isUTF8 = YES; break; case -1:; @throw [OFInvalidEncodingException newWithClass: isa]; |
︙ | ︙ | |||
686 687 688 689 690 691 692 | encoding: encoding length: s.st_size]; [self freeMemory: tmp]; return self; } | | | | | | | | | | | | | | | | | 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | encoding: encoding length: s.st_size]; [self freeMemory: tmp]; return self; } - initWithContentsOfURL: (OFURL*)URL { return [self initWithContentsOfURL: URL encoding: OF_STRING_ENCODING_UTF_8]; } - initWithContentsOfURL: (OFURL*)URL encoding: (of_string_encoding_t)encoding { OFAutoreleasePool *pool; OFHTTPRequest *request; OFHTTPRequestResult *result; Class c; c = isa; [self release]; self = nil; pool = [[OFAutoreleasePool alloc] init]; if ([[URL scheme] isEqual: @"file"]) { self = [[c alloc] initWithContentsOfFile: [URL path] encoding: encoding]; [pool release]; return self; } request = [OFHTTPRequest requestWithURL: URL]; result = [request perform]; if ([result statusCode] != 200) @throw [OFHTTPRequestFailedException newWithClass: [request class] HTTPRequest: request statusCode: [result statusCode]]; self = [[c alloc] initWithCString: (char*)[[result data] cArray] encoding: encoding length: [[result data] count]]; [pool release]; return self; } - (const char*)cString { return string; |
︙ | ︙ | |||
751 752 753 754 755 756 757 | } - (BOOL)isUTF8 { return isUTF8; } | | > > | > > > | | > | | | | > > | | | | | | | | | | | | | | | > | 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 | } - (BOOL)isUTF8 { return isUTF8; } - (BOOL)isEqual: (id)object { OFString *otherString; if (![object isKindOfClass: [OFString class]]) return NO; otherString = (OFString*)object; if (strcmp(string, [otherString cString])) return NO; return YES; } - copy { return [self retain]; } - mutableCopy { return [[OFMutableString alloc] initWithString: self]; } - (of_comparison_result_t)compare: (id)object { OFString *otherString; size_t otherLen, minLen; int cmp; if (![object isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; otherString = (OFString*)object; otherLen = [otherString cStringLength]; minLen = (length > otherLen ? otherLen : length); if ((cmp = memcmp(string, [otherString cString], minLen)) == 0) { if (length > otherLen) return OF_ORDERED_DESCENDING; if (length < otherLen) return OF_ORDERED_ASCENDING; return OF_ORDERED_SAME; } if (cmp > 0) return OF_ORDERED_DESCENDING; else return OF_ORDERED_ASCENDING; } - (of_comparison_result_t)caseInsensitiveCompare: (OFString*)otherString; { const char *otherCString; size_t i, j, otherLen, minLen; int cmp; if (![otherString isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; otherCString = [otherString cString]; otherLen = [otherString cStringLength]; if (![self isUTF8]) { minLen = (length > otherLen ? otherLen : length); if ((cmp = memcasecmp(string, otherCString, minLen)) == 0) { if (length > otherLen) return OF_ORDERED_DESCENDING; if (length < otherLen) return OF_ORDERED_ASCENDING; return OF_ORDERED_SAME; } if (cmp > 0) return OF_ORDERED_DESCENDING; else return OF_ORDERED_ASCENDING; } i = j = 0; while (i < length && j < otherLen) { of_unichar_t c1, c2; size_t l1, l2; l1 = of_string_utf8_to_unicode(string + i, length - i, &c1); l2 = of_string_utf8_to_unicode(otherCString + j, otherLen - j, &c2); if (l1 == 0 || l2 == 0 || c1 > 0x10FFFF || c2 > 0x10FFFF) @throw [OFInvalidEncodingException newWithClass: isa]; if (c1 >> 8 < OF_UNICODE_CASEFOLDING_TABLE_SIZE) { of_unichar_t tc = of_unicode_casefolding_table[c1 >> 8][c1 & 0xFF]; |
︙ | ︙ | |||
864 865 866 867 868 869 870 | if (c1 < c2) return OF_ORDERED_ASCENDING; i += l1; j += l2; } | | | | 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | if (c1 < c2) return OF_ORDERED_ASCENDING; i += l1; j += l2; } if (length - i > otherLen - j) return OF_ORDERED_DESCENDING; else if (length - i < otherLen - j) return OF_ORDERED_ASCENDING; return OF_ORDERED_SAME; } - (uint32_t)hash { |
︙ | ︙ | |||
912 913 914 915 916 917 918 | if (!of_string_utf8_to_unicode(string + index, length - index, &c)) @throw [OFInvalidEncodingException newWithClass: isa]; return c; } | | | | | | | | | | | | | | | | | | | | | | | 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | if (!of_string_utf8_to_unicode(string + index, length - index, &c)) @throw [OFInvalidEncodingException newWithClass: isa]; return c; } - (size_t)indexOfFirstOccurrenceOfString: (OFString*)string_ { const char *cString = [string_ cString]; size_t stringLen = [string_ cStringLength]; size_t i; if (stringLen == 0) return 0; if (stringLen > length) return OF_INVALID_INDEX; for (i = 0; i <= length - stringLen; i++) if (!memcmp(string + i, cString, stringLen)) return of_string_position_to_index(string, i); return OF_INVALID_INDEX; } - (size_t)indexOfLastOccurrenceOfString: (OFString*)string_ { const char *cString = [string_ cString]; size_t stringLen = [string_ cStringLength]; size_t i; if (stringLen == 0) return of_string_position_to_index(string, length); if (stringLen > length) return OF_INVALID_INDEX; for (i = length - stringLen;; i--) { if (!memcmp(string + i, cString, stringLen)) return of_string_position_to_index(string, i); /* Did not match and we're at the last char */ if (i == 0) return OF_INVALID_INDEX; } } - (BOOL)containsString: (OFString*)string_ { const char *cString = [string_ cString]; size_t stringLen = [string_ cStringLength]; size_t i; if (stringLen == 0) return YES; if (stringLen > length) return NO; for (i = 0; i <= length - stringLen; i++) if (!memcmp(string + i, cString, stringLen)) return YES; return NO; } - (OFString*)substringFromIndex: (size_t)start toIndex: (size_t)end |
︙ | ︙ | |||
997 998 999 1000 1001 1002 1003 | - (OFString*)substringWithRange: (of_range_t)range { return [self substringFromIndex: range.start toIndex: range.start + range.length]; } | | | | 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 | - (OFString*)substringWithRange: (of_range_t)range { return [self substringFromIndex: range.start toIndex: range.start + range.length]; } - (OFString*)stringByAppendingString: (OFString*)string_ { OFMutableString *new; new = [OFMutableString stringWithString: self]; [new appendString: string_]; /* * Class swizzle the string to be immutable. We declared the return type * to be OFString*, so it can't be modified anyway. But not swizzling it * would create a real copy each time -[copy] is called. */ new->isa = [OFString class]; |
︙ | ︙ | |||
1086 1087 1088 1089 1090 1091 1092 | } - (OFArray*)componentsSeparatedByString: (OFString*)delimiter { OFAutoreleasePool *pool; OFMutableArray *array; const char *delim = [delimiter cString]; | | | | | | | | | | | | | 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | } - (OFArray*)componentsSeparatedByString: (OFString*)delimiter { OFAutoreleasePool *pool; OFMutableArray *array; const char *delim = [delimiter cString]; size_t delimLen = [delimiter cStringLength]; size_t i, last; array = [OFMutableArray array]; pool = [[OFAutoreleasePool alloc] init]; if (delimLen > length) { [array addObject: [[self copy] autorelease]]; [pool release]; return array; } for (i = 0, last = 0; i <= length - delimLen; i++) { if (memcmp(string + i, delim, delimLen)) continue; [array addObject: [OFString stringWithCString: string + last length: i - last]]; i += delimLen - 1; last = i + 1; } [array addObject: [OFString stringWithCString: string + last]]; [pool release]; /* * Class swizzle the array to be immutable. We declared the return type * to be OFArray*, so it can't be modified anyway. But not swizzling it * would create a real copy each time -[copy] is called. */ array->isa = [OFArray class]; return array; } - (OFArray*)pathComponents { OFMutableArray *ret; OFAutoreleasePool *pool; size_t i, last = 0, pathLen = length; ret = [OFMutableArray array]; if (pathLen == 0) return ret; pool = [[OFAutoreleasePool alloc] init]; #ifndef _WIN32 if (string[pathLen - 1] == OF_PATH_DELIM) #else if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\') #endif pathLen--; for (i = 0; i < pathLen; i++) { #ifndef _WIN32 if (string[i] == OF_PATH_DELIM) { #else if (string[i] == '/' || string[i] == '\\') { #endif [ret addObject: [OFString stringWithCString: string + last |
︙ | ︙ | |||
1170 1171 1172 1173 1174 1175 1176 | */ ret->isa = [OFArray class]; return ret; } - (OFString*)lastPathComponent { | | | | | | | | | | | | | | | | 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 | */ ret->isa = [OFArray class]; return ret; } - (OFString*)lastPathComponent { size_t pathLen = length; ssize_t i; if (pathLen == 0) return @""; #ifndef _WIN32 if (string[pathLen - 1] == OF_PATH_DELIM) #else if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\') #endif pathLen--; for (i = pathLen - 1; i >= 0; i--) { #ifndef _WIN32 if (string[i] == OF_PATH_DELIM) { #else if (string[i] == '/' || string[i] == '\\') { #endif i++; break; } } /* * Only one component, but the trailing delimiter might have been * removed, so return a new string anyway. */ if (i < 0) i = 0; return [OFString stringWithCString: string + i length: pathLen - i]; } - (OFString*)stringByDeletingLastPathComponent; { size_t i, pathLen = length; if (pathLen == 0) return @""; #ifndef _WIN32 if (string[pathLen - 1] == OF_PATH_DELIM) #else if (string[pathLen - 1] == '/' || string[pathLen - 1] == '\\') #endif pathLen--; if (pathLen == 0) return [OFString stringWithCString: string length: 1]; for (i = pathLen - 1; i >= 1; i--) #ifndef _WIN32 if (string[i] == OF_PATH_DELIM) #else if (string[i] == '/' || string[i] == '\\') #endif return [OFString stringWithCString: string length: i]; |
︙ | ︙ | |||
1351 1352 1353 1354 1355 1356 1357 | return num; } - (float)floatValue { const char *str = string; | | | | | | | | | | | | | | 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 | return num; } - (float)floatValue { const char *str = string; char *endPtr; float value; /* Don't depend on isspace and thus the used locale */ while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++; value = strtof(str, &endPtr); /* Check if there are any invalid chars left */ if (endPtr != NULL) { for (; *endPtr != '\0'; endPtr++) if (*endPtr != ' ' && *endPtr != '\t' && *endPtr != '\n' && *endPtr != '\r') @throw [OFInvalidFormatException newWithClass: isa]; } return value; } - (double)doubleValue { const char *str = string; char *endPtr; double value; /* Don't depend on isspace and thus the used locale */ while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++; value = strtod(str, &endPtr); /* Check if there are any invalid chars left */ if (endPtr != NULL) { for (; *endPtr != '\0'; endPtr++) if (*endPtr != ' ' && *endPtr != '\t' && *endPtr != '\n' && *endPtr != '\r') @throw [OFInvalidFormatException newWithClass: isa]; } return value; } |
︙ | ︙ | |||
1410 1411 1412 1413 1414 1415 1416 | if ((ret = malloc((len + 1) * sizeof(of_unichar_t))) == NULL) @throw [OFOutOfMemoryException newWithClass: isa]; i = j = 0; while (i < length) { of_unichar_t c; | | | | | | 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 | if ((ret = malloc((len + 1) * sizeof(of_unichar_t))) == NULL) @throw [OFOutOfMemoryException newWithClass: isa]; i = j = 0; while (i < length) { of_unichar_t c; size_t cLen; cLen = of_string_utf8_to_unicode(string + i, length - 1, &c); if (cLen == 0 || c > 0x10FFFF) { free(ret); @throw [OFInvalidEncodingException newWithClass: isa]; } ret[j++] = c; i += cLen; } ret[j] = 0; return ret; } |
︙ | ︙ |
Modified src/base64.m from [5331624491] to [763d209bf9].
︙ | ︙ | |||
36 37 38 39 40 41 42 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 }; OFString* | | | | | | | | | | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 }; OFString* of_base64_encode(const char *data, size_t len) { OFMutableString *ret = [OFMutableString string]; uint8_t *buf = (uint8_t*)data; size_t i; uint8_t rest; char tb[4]; uint32_t sb; rest = len % 3; for (i = 0; i < len - rest; i += 3) { sb = (buf[i] << 16) | (buf[i + 1] << 8) | buf[i + 2]; tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18]; tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12]; tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6]; tb[3] = of_base64_encode_table[sb & 0x00003F]; [ret appendCStringWithoutUTF8Checking: tb length: 4]; } switch (rest) { case 1: tb[0] = of_base64_encode_table[buf[i] >> 2]; tb[1] = of_base64_encode_table[(buf[i] & 3) << 4]; tb[2] = tb[3] = '='; [ret appendCStringWithoutUTF8Checking: tb length: 4]; break; case 2: sb = (buf[i] << 16) | (buf[i + 1] << 8); tb[0] = of_base64_encode_table[(sb & 0xFC0000) >> 18]; tb[1] = of_base64_encode_table[(sb & 0x03F000) >> 12]; tb[2] = of_base64_encode_table[(sb & 0x000FC0) >> 6]; tb[3] = '='; [ret appendCStringWithoutUTF8Checking: tb |
︙ | ︙ | |||
93 94 95 96 97 98 99 | * would create a real copy each time -[copy] is called. */ ret->isa = [OFString class]; return ret; } BOOL | | | | | | | | | | | | | | 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | * would create a real copy each time -[copy] is called. */ ret->isa = [OFString class]; return ret; } BOOL of_base64_decode(OFDataArray *data, const char *str, size_t len) { const uint8_t *buf = (const uint8_t*)str; size_t i; if ((len & 3) != 0) return NO; for (i = 0; i < len; i += 4) { uint32_t sb = 0; uint8_t count = 3; char db[3]; char tmp; if (buf[i] > 0x7F || buf[i + 1] > 0x7F || buf[i + 2] > 0x7F || buf[i + 3] > 0x7F) return NO; if (buf[i] == '=' || buf[i + 1] == '=' || (buf[i + 2] == '=' && buf[i + 3] != '=')) return NO; if (buf[i + 2] == '=') count--; if (buf[i + 3] == '=') count--; if ((tmp = of_base64_decode_table[buf[i]]) == -1) return NO; sb |= tmp << 18; if ((tmp = of_base64_decode_table[buf[i + 1]]) == -1) return NO; sb |= tmp << 12; if ((tmp = of_base64_decode_table[buf[i + 2]]) == -1) return NO; sb |= tmp << 6; if ((tmp = of_base64_decode_table[buf[i + 3]]) == -1) return NO; sb |= tmp; db[0] = (sb & 0xFF0000) >> 16; db[1] = (sb & 0x00FF00) >> 8; db[2] = sb & 0x0000FF; |
︙ | ︙ |