@@ -19,20 +19,20 @@ #include #include #include #import "OFMutableUTF8String.h" +#import "OFASPrintF.h" #import "OFString.h" #import "OFUTF8String.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" -#import "of_asprintf.h" #import "unicode.h" @implementation OFMutableUTF8String + (void)initialize { @@ -44,72 +44,72 @@ freeWhenDone: (bool)freeWhenDone { self = [self initWithUTF8String: UTF8String]; if (freeWhenDone) - free(UTF8String); + OFFreeMemory(UTF8String); return self; } - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String length: (size_t)UTF8StringLength freeWhenDone: (bool)freeWhenDone { - self = [self initWithUTF8String: UTF8String - length: UTF8StringLength]; + self = [self initWithUTF8String: UTF8String length: UTF8StringLength]; if (freeWhenDone) - free(UTF8String); + OFFreeMemory(UTF8String); return self; } -- (void)of_convertWithWordStartTable: (const of_unichar_t *const[])startTable - wordMiddleTable: (const of_unichar_t *const[])middleTable +#ifdef OF_HAVE_UNICODE_TABLES +- (void)of_convertWithWordStartTable: (const OFUnichar *const [])startTable + wordMiddleTable: (const OFUnichar *const [])middleTable wordStartTableSize: (size_t)startTableSize - wordMiddleTableSize: (size_t)middleTableSize OF_DIRECT + wordMiddleTableSize: (size_t)middleTableSize { - of_unichar_t *unicodeString; + OFUnichar *unicodeString; size_t unicodeLen, newCStringLength; size_t i, j; char *newCString; bool isStart = true; if (!_s->isUTF8) { uint8_t t; - const of_unichar_t *const *table; + const OFUnichar *const *table; assert(startTableSize >= 1 && middleTableSize >= 1); - _s->hashed = false; + _s->hasHash = false; for (i = 0; i < _s->cStringLength; i++) { if (isStart) table = startTable; else table = middleTable; - isStart = of_ascii_isspace(_s->cString[i]); + isStart = OFASCIIIsSpace(_s->cString[i]); if ((t = table[0][(uint8_t)_s->cString[i]]) != 0) _s->cString[i] = t; } return; } unicodeLen = self.length; - unicodeString = of_alloc(unicodeLen, sizeof(of_unichar_t)); + unicodeString = OFAllocMemory(unicodeLen, sizeof(OFUnichar)); i = j = 0; newCStringLength = 0; while (i < _s->cStringLength) { - const of_unichar_t *const *table; + const OFUnichar *const *table; size_t tableSize; - of_unichar_t c; + OFUnichar c; ssize_t cLen; if (isStart) { table = startTable; tableSize = middleTableSize; @@ -116,22 +116,22 @@ } else { table = middleTable; tableSize = middleTableSize; } - cLen = of_string_utf8_decode(_s->cString + i, + cLen = OFUTF8StringDecode(_s->cString + i, _s->cStringLength - i, &c); if (cLen <= 0 || c > 0x10FFFF) { - free(unicodeString); + OFFreeMemory(unicodeString); @throw [OFInvalidEncodingException exception]; } - isStart = of_ascii_isspace(c); + isStart = OFASCIIIsSpace(c); if (c >> 8 < tableSize) { - of_unichar_t tc = table[c >> 8][c & 0xFF]; + OFUnichar tc = table[c >> 8][c & 0xFF]; if (tc) c = tc; } unicodeString[j++] = c; @@ -143,88 +143,88 @@ else if (c < 0x10000) newCStringLength += 3; else if (c < 0x110000) newCStringLength += 4; else { - free(unicodeString); + OFFreeMemory(unicodeString); @throw [OFInvalidEncodingException exception]; } i += cLen; } @try { - newCString = of_alloc(newCStringLength + 1, 1); + newCString = OFAllocMemory(newCStringLength + 1, 1); } @catch (id e) { - free(unicodeString); + OFFreeMemory(unicodeString); @throw e; } j = 0; for (i = 0; i < unicodeLen; i++) { size_t d; - if ((d = of_string_utf8_encode(unicodeString[i], + if ((d = OFUTF8StringEncode(unicodeString[i], newCString + j)) == 0) { - free(unicodeString); - free(newCString); + OFFreeMemory(unicodeString); + OFFreeMemory(newCString); @throw [OFInvalidEncodingException exception]; } j += d; } assert(j == newCStringLength); newCString[j] = 0; - free(unicodeString); + OFFreeMemory(unicodeString); - free(_s->cString); - _s->hashed = false; + OFFreeMemory(_s->cString); + _s->hasHash = false; _s->cString = newCString; _s->cStringLength = newCStringLength; /* * Even though cStringLength can change, length cannot, therefore no * need to change it. */ } +#endif -- (void)setCharacter: (of_unichar_t)character - atIndex: (size_t)idx +- (void)setCharacter: (OFUnichar)character atIndex: (size_t)idx { char buffer[4]; - of_unichar_t c; + OFUnichar c; size_t lenNew; ssize_t lenOld; if (_s->isUTF8) - idx = of_string_utf8_get_position(_s->cString, idx, + idx = OFUTF8StringIndexToPosition(_s->cString, idx, _s->cStringLength); if (idx >= _s->cStringLength) @throw [OFOutOfRangeException exception]; /* Shortcut if old and new character both are ASCII */ if (character < 0x80 && !(_s->cString[idx] & 0x80)) { - _s->hashed = false; + _s->hasHash = false; _s->cString[idx] = character; return; } - if ((lenNew = of_string_utf8_encode(character, buffer)) == 0) + if ((lenNew = OFUTF8StringEncode(character, buffer)) == 0) @throw [OFInvalidEncodingException exception]; - if ((lenOld = of_string_utf8_decode(_s->cString + idx, + if ((lenOld = OFUTF8StringDecode(_s->cString + idx, _s->cStringLength - idx, &c)) <= 0) @throw [OFInvalidEncodingException exception]; - _s->hashed = false; + _s->hasHash = false; if (lenNew == (size_t)lenOld) memcpy(_s->cString + idx, buffer, lenNew); else if (lenNew > (size_t)lenOld) { - _s->cString = of_realloc(_s->cString, + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength - lenOld + lenNew + 1, 1); memmove(_s->cString + idx + lenNew, _s->cString + idx + lenOld, _s->cStringLength - idx - lenOld); memcpy(_s->cString + idx, buffer, lenNew); @@ -246,11 +246,11 @@ if (character >= 0x80) _s->isUTF8 = true; @try { - _s->cString = of_realloc(_s->cString, + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1, 1); } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -265,20 +265,20 @@ memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) { UTF8String += 3; UTF8StringLength -= 3; } - switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) { + switch (OFUTF8StringCheck(UTF8String, UTF8StringLength, &length)) { case 1: _s->isUTF8 = true; break; case -1: @throw [OFInvalidEncodingException exception]; } - _s->hashed = false; - _s->cString = of_realloc(_s->cString, + _s->hasHash = false; + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + UTF8StringLength + 1, 1); memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength + 1); _s->cStringLength += UTF8StringLength; @@ -294,20 +294,20 @@ memcmp(UTF8String, "\xEF\xBB\xBF", 3) == 0) { UTF8String += 3; UTF8StringLength -= 3; } - switch (of_string_utf8_check(UTF8String, UTF8StringLength, &length)) { + switch (OFUTF8StringCheck(UTF8String, UTF8StringLength, &length)) { case 1: _s->isUTF8 = true; break; case -1: @throw [OFInvalidEncodingException exception]; } - _s->hashed = false; - _s->cString = of_realloc(_s->cString, + _s->hasHash = false; + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + UTF8StringLength + 1, 1); memcpy(_s->cString + _s->cStringLength, UTF8String, UTF8StringLength); _s->cStringLength += UTF8StringLength; _s->length += length; @@ -314,24 +314,23 @@ _s->cString[_s->cStringLength] = 0; } - (void)appendCString: (const char *)cString - encoding: (of_string_encoding_t)encoding + encoding: (OFStringEncoding)encoding { [self appendCString: cString encoding: encoding length: strlen(cString)]; } - (void)appendCString: (const char *)cString - encoding: (of_string_encoding_t)encoding + encoding: (OFStringEncoding)encoding length: (size_t)cStringLength { - if (encoding == OF_STRING_ENCODING_UTF_8) - [self appendUTF8String: cString - length: cStringLength]; + if (encoding == OFStringEncodingUTF8) + [self appendUTF8String: cString length: cStringLength]; else { void *pool = objc_autoreleasePoolPush(); [self appendString: [OFString stringWithCString: cString @@ -349,12 +348,12 @@ if (string == nil) @throw [OFInvalidArgumentException exception]; UTF8StringLength = string.UTF8StringLength; - _s->hashed = false; - _s->cString = of_realloc(_s->cString, + _s->hasHash = false; + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + UTF8StringLength + 1, 1); memcpy(_s->cString + _s->cStringLength, string.UTF8String, UTF8StringLength); _s->cStringLength += UTF8StringLength; @@ -368,22 +367,20 @@ _s->isUTF8 = true; } else _s->isUTF8 = true; } -- (void)appendCharacters: (const of_unichar_t *)characters - length: (size_t)length +- (void)appendCharacters: (const OFUnichar *)characters length: (size_t)length { - char *tmp = of_alloc((length * 4) + 1, 1); + char *tmp = OFAllocMemory((length * 4) + 1, 1); @try { size_t j = 0; bool isUTF8 = false; for (size_t i = 0; i < length; i++) { - size_t len = of_string_utf8_encode(characters[i], - tmp + j); + size_t len = OFUTF8StringEncode(characters[i], tmp + j); if (len == 0) @throw [OFInvalidEncodingException exception]; if (len > 1) @@ -392,51 +389,49 @@ j += len; } tmp[j] = '\0'; - _s->hashed = false; - _s->cString = of_realloc(_s->cString, + _s->hasHash = false; + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + j + 1, 1); memcpy(_s->cString + _s->cStringLength, tmp, j + 1); _s->cStringLength += j; _s->length += length; if (isUTF8) _s->isUTF8 = true; } @finally { - free(tmp); + OFFreeMemory(tmp); } } -- (void)appendFormat: (OFConstantString *)format - arguments: (va_list)arguments +- (void)appendFormat: (OFConstantString *)format arguments: (va_list)arguments { char *UTF8String; int UTF8StringLength; if (format == nil) @throw [OFInvalidArgumentException exception]; - if ((UTF8StringLength = of_vasprintf(&UTF8String, format.UTF8String, + if ((UTF8StringLength = OFVASPrintF(&UTF8String, format.UTF8String, arguments)) == -1) @throw [OFInvalidFormatException exception]; @try { - [self appendUTF8String: UTF8String - length: UTF8StringLength]; + [self appendUTF8String: UTF8String length: UTF8StringLength]; } @finally { free(UTF8String); } } - (void)reverse { size_t i, j; - _s->hashed = false; + _s->hasHash = false; /* We reverse all bytes and restore UTF-8 later, if necessary */ for (i = 0, j = _s->cStringLength - 1; i < _s->cStringLength / 2; i++, j--) { _s->cString[i] ^= _s->cString[j]; @@ -508,25 +503,24 @@ /* UTF-8 does not allow more than 4 bytes per character */ @throw [OFInvalidEncodingException exception]; } } -- (void)insertString: (OFString *)string - atIndex: (size_t)idx +- (void)insertString: (OFString *)string atIndex: (size_t)idx { size_t newCStringLength; if (idx > _s->length) @throw [OFOutOfRangeException exception]; if (_s->isUTF8) - idx = of_string_utf8_get_position(_s->cString, idx, + idx = OFUTF8StringIndexToPosition(_s->cString, idx, _s->cStringLength); newCStringLength = _s->cStringLength + string.UTF8StringLength; - _s->hashed = false; - _s->cString = of_realloc(_s->cString, newCStringLength + 1, 1); + _s->hasHash = false; + _s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, 1); memmove(_s->cString + idx + string.UTF8StringLength, _s->cString + idx, _s->cStringLength - idx); memcpy(_s->cString + idx, string.UTF8String, string.UTF8StringLength); @@ -541,40 +535,41 @@ _s->isUTF8 = true; } else _s->isUTF8 = true; } -- (void)deleteCharactersInRange: (of_range_t)range +- (void)deleteCharactersInRange: (OFRange)range { size_t start = range.location; size_t end = range.location + range.length; if (range.length > SIZE_MAX - range.location || end > _s->length) @throw [OFOutOfRangeException exception]; if (_s->isUTF8) { - start = of_string_utf8_get_position(_s->cString, start, + start = OFUTF8StringIndexToPosition(_s->cString, start, _s->cStringLength); - end = of_string_utf8_get_position(_s->cString, end, + end = OFUTF8StringIndexToPosition(_s->cString, end, _s->cStringLength); } memmove(_s->cString + start, _s->cString + end, _s->cStringLength - end); - _s->hashed = false; + _s->hasHash = false; _s->length -= range.length; _s->cStringLength -= end - start; _s->cString[_s->cStringLength] = 0; @try { - _s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1, + 1); } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } -- (void)replaceCharactersInRange: (of_range_t)range +- (void)replaceCharactersInRange: (OFRange)range withString: (OFString *)replacement { size_t start = range.location; size_t end = range.location + range.length; size_t newCStringLength, newLength; @@ -586,19 +581,19 @@ @throw [OFOutOfRangeException exception]; newLength = _s->length - range.length + replacement.length; if (_s->isUTF8) { - start = of_string_utf8_get_position(_s->cString, start, + start = OFUTF8StringIndexToPosition(_s->cString, start, _s->cStringLength); - end = of_string_utf8_get_position(_s->cString, end, + end = OFUTF8StringIndexToPosition(_s->cString, end, _s->cStringLength); } newCStringLength = _s->cStringLength - (end - start) + replacement.UTF8StringLength; - _s->hashed = false; + _s->hasHash = false; /* * If the new string is bigger, we need to resize it first so we can * memmove() the rest of the string to the end. * @@ -605,11 +600,12 @@ * We must not resize the string if the new string is smaller, because * then we can't memmove() the rest of the string forward as the rest is * lost due to the resize! */ if (newCStringLength > _s->cStringLength) - _s->cString = of_realloc(_s->cString, newCStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, + 1); memmove(_s->cString + start + replacement.UTF8StringLength, _s->cString + end, _s->cStringLength - end); memcpy(_s->cString + start, replacement.UTF8String, replacement.UTF8StringLength); @@ -618,11 +614,12 @@ /* * If the new string is smaller, we can safely resize it now as we're * done with memmove(). */ if (newCStringLength < _s->cStringLength) - _s->cString = of_realloc(_s->cString, newCStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, newCStringLength + 1, + 1); _s->cStringLength = newCStringLength; _s->length = newLength; if ([replacement isKindOfClass: [OFUTF8String class]] || @@ -634,11 +631,11 @@ } - (void)replaceOccurrencesOfString: (OFString *)string withString: (OFString *)replacement options: (int)options - range: (of_range_t)range + range: (OFRange)range { const char *searchString = string.UTF8String; const char *replacementString = replacement.UTF8String; size_t searchLength = string.UTF8StringLength; size_t replacementLength = replacement.UTF8StringLength; @@ -651,13 +648,13 @@ if (range.length > SIZE_MAX - range.location || range.location + range.length > self.length) @throw [OFOutOfRangeException exception]; if (_s->isUTF8) { - range.location = of_string_utf8_get_position(_s->cString, + range.location = OFUTF8StringIndexToPosition(_s->cString, range.location, _s->cStringLength); - range.length = of_string_utf8_get_position( + range.length = OFUTF8StringIndexToPosition( _s->cString + range.location, range.length, _s->cStringLength - range.location); } if (string.UTF8StringLength > range.length) @@ -671,15 +668,15 @@ for (size_t i = range.location; i <= range.length - searchLength; i++) { if (memcmp(_s->cString + i, searchString, searchLength) != 0) continue; @try { - newCString = of_realloc(newCString, + newCString = OFResizeMemory(newCString, newCStringLength + i - last + replacementLength + 1, 1); } @catch (id e) { - free(newCString); + OFFreeMemory(newCString); @throw e; } memcpy(newCString + newCStringLength, _s->cString + last, i - last); memcpy(newCString + newCStringLength + i - last, @@ -691,23 +688,23 @@ i += searchLength - 1; last = i + 1; } @try { - newCString = of_realloc(newCString, + newCString = OFResizeMemory(newCString, newCStringLength + _s->cStringLength - last + 1, 1); } @catch (id e) { - free(newCString); + OFFreeMemory(newCString); @throw e; } memcpy(newCString + newCStringLength, _s->cString + last, _s->cStringLength - last); newCStringLength += _s->cStringLength - last; newCString[newCStringLength] = 0; - free(_s->cString); - _s->hashed = false; + OFFreeMemory(_s->cString); + _s->hasHash = false; _s->cString = newCString; _s->cStringLength = newCStringLength; _s->length = newLength; if ([replacement isKindOfClass: [OFUTF8String class]] || @@ -721,22 +718,23 @@ - (void)deleteLeadingWhitespaces { size_t i; for (i = 0; i < _s->cStringLength; i++) - if (!of_ascii_isspace(_s->cString[i])) + if (!OFASCIIIsSpace(_s->cString[i])) break; - _s->hashed = false; + _s->hasHash = false; _s->cStringLength -= i; _s->length -= i; memmove(_s->cString, _s->cString + i, _s->cStringLength); _s->cString[_s->cStringLength] = '\0'; @try { - _s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1, + 1); } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -743,15 +741,15 @@ - (void)deleteTrailingWhitespaces { size_t d; char *p; - _s->hashed = false; + _s->hasHash = false; d = 0; for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) { - if (!of_ascii_isspace(*p)) + if (!OFASCIIIsSpace(*p)) break; *p = '\0'; d++; } @@ -758,11 +756,12 @@ _s->cStringLength -= d; _s->length -= d; @try { - _s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1, + 1); } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -769,15 +768,15 @@ - (void)deleteEnclosingWhitespaces { size_t d, i; char *p; - _s->hashed = false; + _s->hasHash = false; d = 0; for (p = _s->cString + _s->cStringLength - 1; p >= _s->cString; p--) { - if (!of_ascii_isspace(*p)) + if (!OFASCIIIsSpace(*p)) break; *p = '\0'; d++; } @@ -784,21 +783,22 @@ _s->cStringLength -= d; _s->length -= d; for (i = 0; i < _s->cStringLength; i++) - if (!of_ascii_isspace(_s->cString[i])) + if (!OFASCIIIsSpace(_s->cString[i])) break; _s->cStringLength -= i; _s->length -= i; memmove(_s->cString, _s->cString + i, _s->cStringLength); _s->cString[_s->cStringLength] = '\0'; @try { - _s->cString = of_realloc(_s->cString, _s->cStringLength + 1, 1); + _s->cString = OFResizeMemory(_s->cString, _s->cStringLength + 1, + 1); } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } }