@@ -40,25 +40,25 @@ # import "OFFileManager.h" #endif #import "OFLocale.h" #import "OFStream.h" #import "OFSystemInfo.h" -#import "OFURL.h" -#import "OFURLHandler.h" +#import "OFURI.h" +#import "OFURIHandler.h" #import "OFUTF8String.h" #import "OFUTF8String+Private.h" #import "OFXMLElement.h" +#import "OFGetItemAttributesFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" #import "OFOpenItemFailedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" -#import "OFRetrieveItemAttributesFailedException.h" #import "OFTruncatedDataException.h" #import "OFUnsupportedProtocolException.h" #import "unicode.h" @@ -131,13 +131,13 @@ _OFString_CryptographicHashing_reference = 1; _OFString_JSONParsing_reference = 1; #ifdef OF_HAVE_FILES _OFString_PathAdditions_reference = 1; #endif + _OFString_PercentEncoding_reference = 1; _OFString_PropertyListParsing_reference = 1; _OFString_Serialization_reference = 1; - _OFString_URLEncoding_reference = 1; _OFString_XMLEscaping_reference = 1; _OFString_XMLUnescaping_reference = 1; } void @@ -580,19 +580,19 @@ return (id)[[OFUTF8String alloc] initWithContentsOfFile: path encoding: encoding]; } #endif -- (instancetype)initWithContentsOfURL: (OFURL *)URL +- (instancetype)initWithContentsOfURI: (OFURI *)URI { - return (id)[[OFUTF8String alloc] initWithContentsOfURL: URL]; + return (id)[[OFUTF8String alloc] initWithContentsOfURI: URI]; } -- (instancetype)initWithContentsOfURL: (OFURL *)URL +- (instancetype)initWithContentsOfURI: (OFURI *)URI encoding: (OFStringEncoding)encoding { - return (id)[[OFUTF8String alloc] initWithContentsOfURL: URL + return (id)[[OFUTF8String alloc] initWithContentsOfURI: URI encoding: encoding]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { @@ -794,19 +794,19 @@ return [[[self alloc] initWithContentsOfFile: path encoding: encoding] autorelease]; } #endif -+ (instancetype)stringWithContentsOfURL: (OFURL *)URL ++ (instancetype)stringWithContentsOfURI: (OFURI *)URI { - return [[[self alloc] initWithContentsOfURL: URL] autorelease]; + return [[[self alloc] initWithContentsOfURI: URI] autorelease]; } -+ (instancetype)stringWithContentsOfURL: (OFURL *)URL ++ (instancetype)stringWithContentsOfURI: (OFURI *)URI encoding: (OFStringEncoding)encoding { - return [[[self alloc] initWithContentsOfURL: URL + return [[[self alloc] initWithContentsOfURI: URI encoding: encoding] autorelease]; } - (instancetype)init { @@ -992,96 +992,79 @@ } - (instancetype)initWithContentsOfFile: (OFString *)path encoding: (OFStringEncoding)encoding { - char *tmp; - unsigned long long fileSize; + char *buffer = NULL; + OFStreamOffset fileSize; @try { void *pool = objc_autoreleasePoolPush(); - OFFile *file = nil; - - @try { - fileSize = [[OFFileManager defaultManager] - attributesOfItemAtPath: path].fileSize; - } @catch (OFRetrieveItemAttributesFailedException *e) { - @throw [OFOpenItemFailedException - exceptionWithPath: path - mode: @"r" - errNo: e.errNo]; - } - - objc_autoreleasePoolPop(pool); - -# if ULLONG_MAX > SIZE_MAX - if (fileSize > SIZE_MAX) - @throw [OFOutOfRangeException exception]; -#endif + OFFile *file = [OFFile fileWithPath: path mode: @"r"]; + fileSize = [file seekToOffset: 0 whence: OFSeekEnd]; + + if (fileSize < 0 || (unsigned long long)fileSize > SIZE_MAX) + @throw [OFOutOfRangeException exception]; /* * We need one extra byte for the terminating zero if we want * to use -[initWithUTF8StringNoCopy:length:freeWhenDone:]. */ if (SIZE_MAX - (size_t)fileSize < 1) @throw [OFOutOfRangeException exception]; - tmp = OFAllocMemory((size_t)fileSize + 1, 1); - @try { - file = [[OFFile alloc] initWithPath: path mode: @"r"]; - [file readIntoBuffer: tmp - exactLength: (size_t)fileSize]; - } @catch (id e) { - OFFreeMemory(tmp); - @throw e; - } @finally { - [file release]; - } - - tmp[(size_t)fileSize] = '\0'; - } @catch (id e) { + [file seekToOffset: 0 whence: OFSeekSet]; + + buffer = OFAllocMemory((size_t)fileSize + 1, 1); + [file readIntoBuffer: buffer exactLength: (size_t)fileSize]; + buffer[(size_t)fileSize] = '\0'; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + OFFreeMemory(buffer); [self release]; + @throw e; } if (encoding == OFStringEncodingUTF8) { @try { - self = [self initWithUTF8StringNoCopy: tmp + self = [self initWithUTF8StringNoCopy: buffer length: (size_t)fileSize freeWhenDone: true]; } @catch (id e) { - OFFreeMemory(tmp); + OFFreeMemory(buffer); @throw e; } } else { @try { - self = [self initWithCString: tmp + self = [self initWithCString: buffer encoding: encoding length: (size_t)fileSize]; } @finally { - OFFreeMemory(tmp); + OFFreeMemory(buffer); } } return self; } #endif -- (instancetype)initWithContentsOfURL: (OFURL *)URL +- (instancetype)initWithContentsOfURI: (OFURI *)URI { - return [self initWithContentsOfURL: URL + return [self initWithContentsOfURI: URI encoding: OFStringEncodingAutodetect]; } -- (instancetype)initWithContentsOfURL: (OFURL *)URL +- (instancetype)initWithContentsOfURI: (OFURI *)URI encoding: (OFStringEncoding)encoding { void *pool = objc_autoreleasePoolPush(); OFData *data; @try { - data = [OFData dataWithContentsOfURL: URL]; + data = [OFData dataWithContentsOfURI: URI]; } @catch (id e) { [self release]; @throw e; } @@ -1351,12 +1334,11 @@ cString[length] = '\0'; return length; #endif default: - @throw [OFNotImplementedException exceptionWithSelector: _cmd - object: self]; + @throw [OFInvalidArgumentException exception]; } } - (size_t)getCString: (char *)cString maxLength: (size_t)maxLength @@ -1433,11 +1415,11 @@ @throw e; } break; default: - @throw [OFInvalidEncodingException exception]; + @throw [OFInvalidArgumentException exception]; } @try { ret = [[OFData dataWithItemsNoCopy: cString count: cStringLength + 1 @@ -1504,11 +1486,11 @@ case OFStringEncodingMacRoman: case OFStringEncodingKOI8R: case OFStringEncodingKOI8U: return self.length; default: - @throw [OFInvalidEncodingException exception]; + @throw [OFInvalidArgumentException exception]; } } - (size_t)UTF8StringLength { @@ -1683,13 +1665,13 @@ OFHashInit(&hash); for (size_t i = 0; i < length; i++) { const OFUnichar c = characters[i]; - OFHashAdd(&hash, (c & 0xFF0000) >> 16); - OFHashAdd(&hash, (c & 0x00FF00) >> 8); - OFHashAdd(&hash, c & 0x0000FF); + OFHashAddByte(&hash, (c & 0xFF0000) >> 16); + OFHashAddByte(&hash, (c & 0x00FF00) >> 8); + OFHashAddByte(&hash, c & 0x0000FF); } OFHashFinalize(&hash); return hash; @@ -1754,21 +1736,21 @@ const char *cString = self.UTF8String; if ((!OFASCIIIsAlpha(cString[0]) && cString[0] != '_' && cString[0] != '$') || strpbrk(cString, " \n\r\t\b\f\\\"'") != NULL) { - [JSON prependString: @"\""]; + [JSON insertString: @"\"" atIndex: 0]; [JSON appendString: @"\""]; } } else { - [JSON prependString: @"\""]; + [JSON insertString: @"\"" atIndex: 0]; [JSON appendString: @"\""]; } } else { [JSON replaceOccurrencesOfString: @"\n" withString: @"\\n"]; - [JSON prependString: @"\""]; + [JSON insertString: @"\"" atIndex: 0]; [JSON appendString: @"\""]; } [JSON makeImmutable]; @@ -1818,19 +1800,19 @@ - (OFRange)rangeOfString: (OFString *)string { return [self rangeOfString: string options: 0 - range: OFRangeMake(0, self.length)]; + range: OFMakeRange(0, self.length)]; } - (OFRange)rangeOfString: (OFString *)string options: (OFStringSearchOptions)options { return [self rangeOfString: string options: options - range: OFRangeMake(0, self.length)]; + range: OFMakeRange(0, self.length)]; } - (OFRange)rangeOfString: (OFString *)string options: (OFStringSearchOptions)options range: (OFRange)range @@ -1839,14 +1821,14 @@ const OFUnichar *searchCharacters; OFUnichar *characters; size_t searchLength; if ((searchLength = string.length) == 0) - return OFRangeMake(0, 0); + return OFMakeRange(0, 0); if (searchLength > range.length) - return OFRangeMake(OFNotFound, 0); + return OFMakeRange(OFNotFound, 0); if (range.length > SIZE_MAX / sizeof(OFUnichar)) @throw [OFOutOfRangeException exception]; pool = objc_autoreleasePoolPush(); @@ -1860,11 +1842,11 @@ if (options & OFStringSearchBackwards) { for (size_t i = range.length - searchLength;; i--) { if (memcmp(characters + i, searchCharacters, searchLength * sizeof(OFUnichar)) == 0) { objc_autoreleasePoolPop(pool); - return OFRangeMake(range.location + i, + return OFMakeRange(range.location + i, searchLength); } /* No match and we're at the last character */ if (i == 0) @@ -1874,11 +1856,11 @@ for (size_t i = 0; i <= range.length - searchLength; i++) { if (memcmp(characters + i, searchCharacters, searchLength * sizeof(OFUnichar)) == 0) { objc_autoreleasePoolPop(pool); - return OFRangeMake(range.location + i, + return OFMakeRange(range.location + i, searchLength); } } } } @finally { @@ -1885,26 +1867,26 @@ OFFreeMemory(characters); } objc_autoreleasePoolPop(pool); - return OFRangeMake(OFNotFound, 0); + return OFMakeRange(OFNotFound, 0); } - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet { return [self indexOfCharacterFromSet: characterSet options: 0 - range: OFRangeMake(0, self.length)]; + range: OFMakeRange(0, self.length)]; } - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet options: (OFStringSearchOptions)options { return [self indexOfCharacterFromSet: characterSet options: options - range: OFRangeMake(0, self.length)]; + range: OFMakeRange(0, self.length)]; } - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet options: (OFStringSearchOptions)options range: (OFRange)range @@ -1979,16 +1961,16 @@ return false; } - (OFString *)substringFromIndex: (size_t)idx { - return [self substringWithRange: OFRangeMake(idx, self.length - idx)]; + return [self substringWithRange: OFMakeRange(idx, self.length - idx)]; } - (OFString *)substringToIndex: (size_t)idx { - return [self substringWithRange: OFRangeMake(0, idx)]; + return [self substringWithRange: OFMakeRange(0, idx)]; } - (OFString *)substringWithRange: (OFRange)range { void *pool; @@ -2038,18 +2020,10 @@ [new appendFormat: format arguments: arguments]; [new makeImmutable]; return new; } -- (OFString *)stringByPrependingString: (OFString *)string -{ - OFMutableString *new = [[string mutableCopy] autorelease]; - [new appendString: self]; - [new makeImmutable]; - return new; -} - - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string withString: (OFString *)replacement { OFMutableString *new = [[self mutableCopy] autorelease]; [new replaceOccurrencesOfString: string withString: replacement]; @@ -2130,11 +2104,11 @@ tmp = OFAllocMemory(prefixLength, sizeof(OFUnichar)); @try { void *pool = objc_autoreleasePoolPush(); - [self getCharacters: tmp inRange: OFRangeMake(0, prefixLength)]; + [self getCharacters: tmp inRange: OFMakeRange(0, prefixLength)]; hasPrefix = (memcmp(tmp, prefix.characters, prefixLength * sizeof(OFUnichar)) == 0); objc_autoreleasePoolPop(pool); @@ -2160,11 +2134,11 @@ tmp = OFAllocMemory(suffixLength, sizeof(OFUnichar)); @try { void *pool = objc_autoreleasePoolPush(); [self getCharacters: tmp - inRange: OFRangeMake(length - suffixLength, + inRange: OFMakeRange(length - suffixLength, suffixLength)]; suffixCharacters = suffix.characters; hasSuffix = (memcmp(tmp, suffixCharacters, suffixLength * sizeof(OFUnichar)) == 0); @@ -2220,18 +2194,18 @@ if (memcmp(characters + i, delimiterCharacters, delimiterLength * sizeof(OFUnichar)) != 0) continue; component = [self substringWithRange: - OFRangeMake(last, i - last)]; + OFMakeRange(last, i - last)]; if (!skipEmpty || component.length > 0) [array addObject: component]; i += delimiterLength - 1; last = i + 1; } - component = [self substringWithRange: OFRangeMake(last, length - last)]; + component = [self substringWithRange: OFMakeRange(last, length - last)]; if (!skipEmpty || component.length > 0) [array addObject: component]; [array makeImmutable]; @@ -2265,20 +2239,20 @@ for (size_t i = 0; i < length; i++) { if (characterIsMember(characterSet, @selector(characterIsMember:), characters[i])) { if (!skipEmpty || i != last) { OFString *component = [self substringWithRange: - OFRangeMake(last, i - last)]; + OFMakeRange(last, i - last)]; [array addObject: component]; } last = i + 1; } } if (!skipEmpty || length != last) { OFString *component = [self substringWithRange: - OFRangeMake(last, length - last)]; + OFMakeRange(last, length - last)]; [array addObject: component]; } [array makeImmutable]; @@ -2290,11 +2264,11 @@ - (long long)longLongValue { return [self longLongValueWithBase: 10]; } -- (long long)longLongValueWithBase: (int)base +- (long long)longLongValueWithBase: (unsigned char)base { void *pool = objc_autoreleasePoolPush(); const char *UTF8String = self.UTF8String; bool negative = false; long long value = 0; @@ -2366,11 +2340,11 @@ - (unsigned long long)unsignedLongLongValue { return [self unsignedLongLongValueWithBase: 10]; } -- (unsigned long long)unsignedLongLongValueWithBase: (int)base +- (unsigned long long)unsignedLongLongValueWithBase: (unsigned char)base { void *pool = objc_autoreleasePoolPush(); const char *UTF8String = self.UTF8String; unsigned long long value = 0; @@ -2457,14 +2431,14 @@ #else /* * If we have no strtof_l, we have no other choice but to replace "." * with the locale's decimal point. */ - OFString *decimalPoint = [OFLocale decimalPoint]; + OFString *decimalSeparator = [OFLocale decimalSeparator]; const char *UTF8String = [self stringByReplacingOccurrencesOfString: @"." - withString: decimalPoint].UTF8String; + withString: decimalSeparator].UTF8String; #endif char *endPtr = NULL; float value; errno = 0; @@ -2510,14 +2484,14 @@ #else /* * If we have no strtod_l, we have no other choice but to replace "." * with the locale's decimal point. */ - OFString *decimalPoint = [OFLocale decimalPoint]; + OFString *decimalSeparator = [OFLocale decimalSeparator]; const char *UTF8String = [self stringByReplacingOccurrencesOfString: @"." - withString: decimalPoint].UTF8String; + withString: decimalSeparator].UTF8String; #endif char *endPtr = NULL; double value; errno = 0; @@ -2548,11 +2522,11 @@ OFUnichar *buffer; const OFUnichar *ret; buffer = OFAllocMemory(length, sizeof(OFUnichar)); @try { - [self getCharacters: buffer inRange: OFRangeMake(0, length)]; + [self getCharacters: buffer inRange: OFMakeRange(0, length)]; ret = [[OFData dataWithItemsNoCopy: buffer count: length itemSize: sizeof(OFUnichar) freeWhenDone: true] items]; @@ -2656,11 +2630,11 @@ OFChar32 *buffer; const OFChar32 *ret; buffer = OFAllocMemory(length + 1, sizeof(OFChar32)); @try { - [self getCharacters: buffer inRange: OFRangeMake(0, length)]; + [self getCharacters: buffer inRange: OFMakeRange(0, length)]; buffer[length] = 0; if (byteOrder != OFByteOrderNative) for (size_t i = 0; i < length; i++) buffer[i] = OFByteSwap32(buffer[i]); @@ -2748,25 +2722,21 @@ [file writeString: self encoding: encoding]; objc_autoreleasePoolPop(pool); } #endif -- (void)writeToURL: (OFURL *)URL +- (void)writeToURI: (OFURI *)URI { - [self writeToURL: URL encoding: OFStringEncodingUTF8]; + [self writeToURI: URI encoding: OFStringEncodingUTF8]; } -- (void)writeToURL: (OFURL *)URL encoding: (OFStringEncoding)encoding +- (void)writeToURI: (OFURI *)URI encoding: (OFStringEncoding)encoding { void *pool = objc_autoreleasePoolPush(); - OFURLHandler *URLHandler; OFStream *stream; - if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) - @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - - stream = [URLHandler openItemAtURL: URL mode: @"w"]; + stream = [OFURIHandler openItemAtURI: URI mode: @"w"]; [stream writeString: self encoding: encoding]; objc_autoreleasePoolPop(pool); } @@ -2788,22 +2758,22 @@ if (characters[i] == '\n' || characters[i] == '\r') { void *pool2 = objc_autoreleasePoolPush(); block([self substringWithRange: - OFRangeMake(last, i - last)], &stop); + OFMakeRange(last, i - last)], &stop); last = i + 1; objc_autoreleasePoolPop(pool2); } lastCarriageReturn = (characters[i] == '\r'); } if (!stop) - block([self substringWithRange: OFRangeMake(last, i - last)], + block([self substringWithRange: OFMakeRange(last, i - last)], &stop); objc_autoreleasePoolPop(pool); } #endif @end