@@ -139,22 +139,25 @@ return self; } - initFromCString: (const char*)str { + Class c; + if ((self = [super init])) { if (str != NULL) { length = strlen(str); switch (check_utf8(str, length)) { case 1: is_utf8 = YES; break; case -1: + c = [self class]; [super free]; @throw [OFInvalidEncodingException - newWithObject: self]; + newWithClass: c]; } string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } @@ -178,33 +181,36 @@ - initFromFormatCString: (const char*)fmt withArguments: (va_list)args { int t; + Class c; if ((self = [super init])) { - if (fmt == NULL) - @throw [OFInvalidFormatException newWithObject: self]; - - if ((t = vasprintf(&string, fmt, args)) == -1) - /* - * This is only the most likely error to happen. - * Unfortunately, as errno isn't always thread-safe, - * there's no good way for us to find out what really - * happened. - */ - @throw [OFNoMemException newWithObject: self]; + if (fmt == NULL) { + c = [self class]; + [super free]; + @throw [OFInvalidFormatException newWithClass: c]; + } + + if ((t = vasprintf(&string, fmt, args)) == -1) { + c = [self class]; + [super free]; + @throw [OFInitializationFailedException + newWithClass: c]; + } length = t; switch (check_utf8(string, length)) { case 1: is_utf8 = YES; break; case -1: free(string); + c = [self class]; [super free]; - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: c]; } @try { [self addToMemoryPool: string]; } @catch (OFException *e) { @@ -260,11 +266,11 @@ switch (check_utf8(str, strlength)) { case 1: is_utf8 = YES; break; case -1: - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; } newlen = length + strlength; newstr = [self resizeMem: string toSize: newlen + 1]; @@ -294,22 +300,25 @@ andArguments: (va_list)args { char *t; if (fmt == NULL) - @throw [OFInvalidFormatException newWithObject: self]; + @throw [OFInvalidFormatException newWithClass: [self class]]; if ((vasprintf(&t, fmt, args)) == -1) /* * This is only the most likely error to happen. * Unfortunately, as errno isn't always thread-safe, there's * no good way for us to find out what really happened. */ - @throw [OFNoMemException newWithObject: self]; + @throw [OFNoMemException newWithClass: [self class]]; - [self appendCString: t]; - free(t); + @try { + [self appendCString: t]; + } @finally { + free(t); + } return self; } - reverse @@ -336,17 +345,19 @@ continue; /* A start byte can't happen first as we reversed everything */ if (OF_UNLIKELY(string[i] & 0x40)) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 1 || !(string[i + 1] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Next byte is the start byte */ if (OF_LIKELY(string[i + 1] & 0x40)) { string[i] ^= string[i + 1]; @@ -358,11 +369,12 @@ } /* Second next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Second next byte is the start byte */ if (OF_LIKELY(string[i + 2] & 0x40)) { string[i] ^= string[i + 2]; @@ -374,11 +386,12 @@ } /* Third next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Third next byte is the start byte */ if (OF_LIKELY(string[i + 3] & 0x40)) { string[i] ^= string[i + 3]; @@ -393,11 +406,11 @@ continue; } /* UTF-8 does not allow more than 4 bytes per character */ madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; } madvise(string, len, MADV_NORMAL); return self; @@ -406,11 +419,11 @@ - upper { size_t i = length; if (is_utf8) - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = toupper(string[i]); return self; @@ -419,13 +432,13 @@ - lower { size_t i = length; if (is_utf8) - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = tolower(string[i]); return self; } @end