Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -106,10 +106,22 @@ * @return A new autoreleased OFString */ + (instancetype)stringWithUTF8String: (const char*)UTF8String length: (size_t)UTF8StringLength; +/*! + * @brief Creates a new OFString from a UTF-8 encoded C string without copying + * the string. + * + * @param UTF8String A UTF-8 encoded C string to initialize the OFString with + * @param freeWhenDone Whether to free the C string when the OFString gets + * deallocated + * @return A new autoreleased OFString + */ ++ (instancetype)stringWithUTF8StringNoCopy: (const char*)UTF8String + freeWhenDone: (BOOL)freeWhenDone; + /*! * @brief Creates a new OFString from a C string with the specified encoding. * * @param cString A C string to initialize the OFString with * @param encoding The encoding of the C string Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -368,10 +368,18 @@ { return [[[self alloc] initWithUTF8String: UTF8String length: UTF8StringLength] autorelease]; } + ++ (instancetype)stringWithUTF8StringNoCopy: (const char*)UTF8String + freeWhenDone: (BOOL)freeWhenDone +{ + return [[[self alloc] + initWithUTF8StringNoCopy: UTF8String + freeWhenDone: freeWhenDone] autorelease]; +} + (instancetype)stringWithCString: (const char*)cString encoding: (of_string_encoding_t)encoding { return [[[self alloc] initWithCString: cString Index: src/OFString_UTF8.m ================================================================== --- src/OFString_UTF8.m +++ src/OFString_UTF8.m @@ -403,19 +403,17 @@ return self; } - initWithUTF8StringNoCopy: (const char*)UTF8String - freeWhenDone: (BOOL)freeWhenDone + freeWhenDone: (BOOL)freeWhenDone_ { self = [super init]; @try { size_t UTF8StringLength = strlen(UTF8String); - - if (freeWhenDone) - s->freeWhenDone = (char*)UTF8String; + char *freeWhenDone = (char*)UTF8String; if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) { UTF8String += 3; UTF8StringLength -= 3; @@ -424,10 +422,13 @@ s = &s_store; s->cString = (char*)UTF8String; s->cStringLength = UTF8StringLength; + if (freeWhenDone_) + s->freeWhenDone = freeWhenDone; + switch (of_string_utf8_check(UTF8String, UTF8StringLength, &s->length)) { case 1: s->isUTF8 = YES; break;