Index: src/OFMutableString.h ================================================================== --- src/OFMutableString.h +++ src/OFMutableString.h @@ -81,26 +81,10 @@ * \param length The length of the UTF-8 encoded C string */ - (void)appendCStringWithoutUTF8Checking: (const char*)string length: (size_t)length; -/** - * Appends a C string with the specified encoding and length length to the - * OFString without checking whether it is valid UTF-8 if the specified encoding - * is UTF-8. - * - * Only use this if you are 100% sure the string you append is either ASCII or - * UTF-8 if you specified UTF-8 as encoding! - * - * \param string A C string to append - * \param encoding The encoding of the C string - * \param length The length of the UTF-8 encoded C string - */ -- (void)appendCStringWithoutUTF8Checking: (const char*)string - encoding: (of_string_encoding_t)encoding - length: (size_t)length; - /** * Appends another OFString to the OFMutableString. * * \param string An OFString to append */ Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -246,26 +246,10 @@ memcpy(string + length, string_, length_); length += length_; string[length] = 0; } -- (void)appendCStringWithoutUTF8Checking: (const char*)string_ - encoding: (of_string_encoding_t)encoding - length: (size_t)length_ -{ - if (encoding == OF_STRING_ENCODING_UTF_8) - [self appendCStringWithoutUTF8Checking: string_ - length: length_]; - else { - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - [self appendString: [OFString stringWithCString: string_ - encoding: encoding - length: length_]]; - [pool release]; - } -} - - (void)appendString: (OFString*)string_ { if (string_ == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -37,10 +37,26 @@ #import "macros.h" typedef void (*state_function)(id, SEL, const char*, size_t*, size_t*); static SEL selectors[OF_XMLPARSER_NUM_STATES]; static state_function lookupTable[OF_XMLPARSER_NUM_STATES]; + +static void +cache_append(OFMutableString *cache, const char *string, + of_string_encoding_t encoding, size_t length) +{ + if (OF_LIKELY(encoding == OF_STRING_ENCODING_UTF_8)) + [cache appendCStringWithoutUTF8Checking: string + length: length]; + else { + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + [cache appendString: [OFString stringWithCString: string + encoding: encoding + length: length]]; + [pool release]; + } +} static OFString* transform_string(OFMutableString *cache, size_t cut, BOOL unescape, OFObject *delegate) { @@ -214,35 +230,34 @@ - (void)setDelegate: (id )delegate_ { OF_SETTER(delegate, delegate_, YES, NO) } -- (void)parseBuffer: (const char*)buf +- (void)parseBuffer: (const char*)buffer withLength: (size_t)length { size_t i, last = 0; for (i = 0; i < length; i++) { size_t j = i; - lookupTable[state](self, selectors[state], buf, &i, &last); + lookupTable[state](self, selectors[state], buffer, &i, &last); /* Ensure we don't count this character twice */ if (i != j) continue; - if (buf[i] == '\r' || (buf[i] == '\n' && !lastCarriageReturn)) + if (buffer[i] == '\r' || (buffer[i] == '\n' && + !lastCarriageReturn)) lineNumber++; - lastCarriageReturn = (buf[i] == '\r'); + lastCarriageReturn = (buffer[i] == '\r'); } /* In OF_XMLPARSER_IN_TAG, there can be only spaces */ if (length - last > 0 && state != OF_XMLPARSER_IN_TAG) - [cache appendCStringWithoutUTF8Checking: buf + last - encoding: encoding - length: length - last]; + cache_append(cache, buffer + last, encoding, length - last); } - (void)parseString: (OFString*)string { [self parseBuffer: [string cString] @@ -299,13 +314,11 @@ if (buffer[*i] != '<') return; if ((length = *i - *last) > 0) - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: length]; + cache_append(cache, buffer + *last, encoding, length); if ([cache cStringLength] > 0) { OFString *characters; OFAutoreleasePool *pool; @@ -467,13 +480,11 @@ level = 1; else if (level == 1 && buffer[*i] == '>') { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFString *pi; - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: *i - *last]; + cache_append(cache, buffer + *last, encoding, *i - *last); pi = transform_string(cache, 1, NO, nil); if ([pi isEqual: @"xml"] || [pi hasPrefix: @"xml "] || [pi hasPrefix: @"xml\t"] || [pi hasPrefix: @"xml\r"] || [pi hasPrefix: @"xml\n"]) @@ -506,13 +517,11 @@ if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' && buffer[*i] != '>' && buffer[*i] != '/') return; if ((length = *i - *last) > 0) - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: length]; + cache_append(cache, buffer + *last, encoding, length); cacheCString = [cache cString]; cacheLength = [cache cStringLength]; if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) { @@ -601,13 +610,11 @@ if (buffer[*i] != ' ' && buffer[*i] != '\t' && buffer[*i] != '\n' && buffer[*i] != '\r' && buffer[*i] != '>') return; if ((length = *i - *last) > 0) - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: length]; + cache_append(cache, buffer + *last, encoding, length); cacheCString = [cache cString]; cacheLength = [cache cStringLength]; if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) { @@ -753,13 +760,11 @@ if (buffer[*i] != '=') return; if ((length = *i - *last) > 0) - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: length]; + cache_append(cache, buffer + *last, encoding, length); [cache deleteLeadingAndTrailingWhitespaces]; cacheCString = [cache cString]; cacheLength = [cache cStringLength]; @@ -812,13 +817,11 @@ if (buffer[*i] != delimiter) return; if ((length = *i - *last) > 0) - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: length]; + cache_append(cache, buffer + *last, encoding, length); pool = [[OFAutoreleasePool alloc] init]; attributeValue = transform_string(cache, 0, YES, self); if (attributePrefix == nil && [attributeName isEqual: @"xmlns"]) @@ -942,13 +945,11 @@ return; } pool = [[OFAutoreleasePool alloc] init]; - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: *i - *last]; + cache_append(cache, buffer + *last, encoding, *i - *last); CDATA = transform_string(cache, 2, NO, nil); #if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) if (CDATAHandler != NULL) CDATAHandler(self, CDATA); @@ -1003,13 +1004,11 @@ @throw [OFMalformedXMLException newWithClass: isa parser: self]; pool = [[OFAutoreleasePool alloc] init]; - [cache appendCStringWithoutUTF8Checking: buffer + *last - encoding: encoding - length: *i - *last]; + cache_append(cache, buffer + *last, encoding, *i - *last); comment = transform_string(cache, 2, NO, nil); #if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) if (commentHandler != NULL) commentHandler(self, comment);