@@ -26,10 +26,11 @@ #endif #import "OFUTF8String.h" #import "OFUTF8String+Private.h" #import "OFArray.h" +#import "OFData.h" #import "OFMutableUTF8String.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" @@ -178,12 +179,12 @@ self = [super init]; @try { _s = &_storage; - _s->cString = [self allocMemoryWithSize: 1]; - _s->cString[0] = '\0'; + _s->cString = of_calloc(1, 1); + _s->freeWhenDone = true; } @catch (id e) { [self release]; @throw e; } @@ -244,12 +245,13 @@ cStringLength -= 3; } _s = &_storage; - _s->cString = [self allocMemoryWithSize: cStringLength + 1]; + _s->cString = of_malloc(cStringLength + 1, 1); _s->cStringLength = cStringLength; + _s->freeWhenDone = true; if (encoding == OF_STRING_ENCODING_UTF_8 || encoding == OF_STRING_ENCODING_ASCII) { switch (of_string_utf8_check(cString, cStringLength, &_s->length)) { @@ -291,13 +293,12 @@ if (bytes == 0) @throw [OFInvalidEncodingException exception]; _s->cStringLength += bytes - 1; - _s->cString = [self - resizeMemory: _s->cString - size: _s->cStringLength + 1]; + _s->cString = of_realloc(_s->cString, + _s->cStringLength + 1, 1); memcpy(_s->cString + j, buffer, bytes); j += bytes; } @@ -305,15 +306,15 @@ return self; } switch (encoding) { -#define CASE(encoding, var) \ - case encoding: \ - table = var; \ - tableOffset = var##_offset; \ - break; +#define CASE(encoding, var) \ + case encoding: \ + table = var; \ + tableOffset = var##_offset; \ + break; #ifdef HAVE_ISO_8859_2 CASE(OF_STRING_ENCODING_ISO_8859_2, of_iso_8859_2_table) #endif #ifdef HAVE_ISO_8859_3 CASE(OF_STRING_ENCODING_ISO_8859_3, of_iso_8859_3_table) @@ -372,13 +373,12 @@ if (byteLength == 0) @throw [OFInvalidEncodingException exception]; _s->cStringLength += byteLength - 1; - _s->cString = [self - resizeMemory: _s->cString - size: _s->cStringLength + 1]; + _s->cString = of_realloc(_s->cString, + _s->cStringLength + 1, 1); memcpy(_s->cString + j, buffer, byteLength); j += byteLength; } @@ -424,13 +424,11 @@ @throw [OFInvalidEncodingException exception]; } _s->cString = (char *)UTF8String; _s->cStringLength = UTF8StringLength; - - if (freeWhenDone) - _s->freeWhenDone = UTF8String; + _s->freeWhenDone = freeWhenDone; } @catch (id e) { [self release]; @throw e; } @@ -452,12 +450,13 @@ else _s->isUTF8 = true; _s->length = string.length; - _s->cString = [self allocMemoryWithSize: _s->cStringLength + 1]; + _s->cString = of_malloc(_s->cStringLength + 1, 1); memcpy(_s->cString, string.UTF8String, _s->cStringLength + 1); + _s->freeWhenDone = true; } @catch (id e) { [self release]; @throw e; } @@ -472,12 +471,13 @@ @try { size_t j; _s = &_storage; - _s->cString = [self allocMemoryWithSize: (length * 4) + 1]; + _s->cString = of_malloc((length * 4) + 1, 1); _s->length = length; + _s->freeWhenDone = true; j = 0; for (size_t i = 0; i < length; i++) { size_t len = of_string_utf8_encode(characters[i], _s->cString + j); @@ -493,12 +493,11 @@ _s->cString[j] = '\0'; _s->cStringLength = j; @try { - _s->cString = [self resizeMemory: _s->cString - size: j + 1]; + _s->cString = of_realloc(_s->cString, j + 1, 1); } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @@ -528,12 +527,13 @@ } else if (byteOrder != OF_BYTE_ORDER_NATIVE) swap = true; _s = &_storage; - _s->cString = [self allocMemoryWithSize: (length * 4) + 1]; + _s->cString = of_malloc((length * 4) + 1, 1); _s->length = length; + _s->freeWhenDone = true; j = 0; for (size_t i = 0; i < length; i++) { of_unichar_t character = (swap ? OF_BSWAP16(string[i]) : string[i]); @@ -578,12 +578,11 @@ _s->cString[j] = '\0'; _s->cStringLength = j; @try { - _s->cString = [self resizeMemory: _s->cString - size: j + 1]; + _s->cString = of_realloc(_s->cString, j + 1, 1); } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @@ -613,12 +612,13 @@ } else if (byteOrder != OF_BYTE_ORDER_NATIVE) swap = true; _s = &_storage; - _s->cString = [self allocMemoryWithSize: (length * 4) + 1]; + _s->cString = of_malloc((length * 4) + 1, 1); _s->length = length; + _s->freeWhenDone = true; j = 0; for (size_t i = 0; i < length; i++) { char buffer[4]; size_t len = of_string_utf8_encode( @@ -645,12 +645,11 @@ _s->cString[j] = '\0'; _s->cStringLength = j; @try { - _s->cString = [self resizeMemory: _s->cString - size: j + 1]; + _s->cString = of_realloc(_s->cString, j + 1, 1); } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only tried to make it smaller */ } } @catch (id e) { [self release]; @@ -688,13 +687,13 @@ break; case -1: @throw [OFInvalidEncodingException exception]; } - _s->cString = [self - allocMemoryWithSize: cStringLength + 1]; + _s->cString = of_malloc(cStringLength + 1, 1); memcpy(_s->cString, tmp, cStringLength + 1); + _s->freeWhenDone = true; } @finally { free(tmp); } } @catch (id e) { [self release]; @@ -704,12 +703,12 @@ return self; } - (void)dealloc { - if (_s != NULL && _s->freeWhenDone != NULL) - free(_s->freeWhenDone); + if (_s != NULL && _s->freeWhenDone) + free(_s->cString); [super dealloc]; } - (size_t)getCString: (char *)cString @@ -1154,67 +1153,65 @@ return array; } - (const of_unichar_t *)characters { - OFObject *object = [[[OFObject alloc] init] autorelease]; - of_unichar_t *ret; - size_t i, j; - - ret = [object allocMemoryWithSize: sizeof(of_unichar_t) - count: _s->length]; - - i = j = 0; + of_unichar_t *buffer = of_malloc(_s->length, sizeof(of_unichar_t)); + size_t i = 0, j = 0; while (i < _s->cStringLength) { of_unichar_t c; ssize_t cLen; cLen = of_string_utf8_decode(_s->cString + i, _s->cStringLength - i, &c); - if (cLen <= 0 || c > 0x10FFFF) + if (cLen <= 0 || c > 0x10FFFF) { + free(buffer); @throw [OFInvalidEncodingException exception]; + } - ret[j++] = c; + buffer[j++] = c; i += cLen; } - return ret; + return [[OFData dataWithItemsNoCopy: buffer + count: _s->length + itemSize: sizeof(of_unichar_t) + freeWhenDone: true] items]; } - (const of_char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder { - OFObject *object = [[[OFObject alloc] init] autorelease]; - of_char32_t *ret; - size_t i, j; - - ret = [object allocMemoryWithSize: sizeof(of_unichar_t) - count: _s->length + 1]; - - i = j = 0; + of_char32_t *buffer = of_malloc(_s->length + 1, sizeof(of_char32_t)); + size_t i = 0, j = 0; while (i < _s->cStringLength) { - of_unichar_t c; + of_char32_t c; ssize_t cLen; cLen = of_string_utf8_decode(_s->cString + i, _s->cStringLength - i, &c); - if (cLen <= 0 || c > 0x10FFFF) + if (cLen <= 0 || c > 0x10FFFF) { + free(buffer); @throw [OFInvalidEncodingException exception]; + } if (byteOrder != OF_BYTE_ORDER_NATIVE) - ret[j++] = OF_BSWAP32(c); + buffer[j++] = OF_BSWAP32(c); else - ret[j++] = c; + buffer[j++] = c; i += cLen; } - ret[j] = 0; + buffer[j] = 0; - return ret; + return [[OFData dataWithItemsNoCopy: buffer + count: _s->length + 1 + itemSize: sizeof(of_char32_t) + freeWhenDone: true] items]; } #ifdef OF_HAVE_BLOCKS - (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block {