Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -38,15 +38,14 @@ SRCS += ${OBJC_SYNC_M} \ ${ASPRINTF_C} \ iso_8859_15.c \ windows_1252.c \ - unicode_lower.m \ unicode_upper.m \ - unicode_titlecase.m + unicode_lower.m include ../buildsys.mk CPPFLAGS += -I.. CFLAGS += ${LIB_CFLAGS} OBJCFLAGS += ${LIB_CFLAGS} LD = ${OBJC} Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -30,10 +30,105 @@ #import "asprintf.h" extern const of_unichar_t* const of_unicode_upper_table[0x1100]; extern const of_unichar_t* const of_unicode_lower_table[0x1100]; + +static void +apply_table(id self, Class isa, char **string, unsigned int *length, + BOOL is_utf8, const of_unichar_t* const table[]) +{ + of_unichar_t c, tc; + of_unichar_t *ustr; + size_t ulen, nlen; + size_t i, j, d; + char *nstr; + + if (!is_utf8) { + uint8_t *p = (uint8_t*)*string + *length; + uint8_t t; + + while (--p >= (uint8_t*)*string) { + t = table[0][*p]; + if (t != 0) + *p = t; + } + + return; + } + + ulen = [self length]; + ustr = [self allocMemoryForNItems: [self length] + withSize: ulen]; + + j = 0; + nlen = 0; + + for (i = 0; i < *length; i++) { + c = of_string_utf8_to_unicode(*string + i, *length - i); + + if (c == OF_INVALID_UNICHAR || c > 0x10FFFF) { + [self freeMemory: ustr]; + @throw [OFInvalidEncodingException newWithClass: isa]; + } + + if ((tc = table[c >> 8][c & 0xFF]) == 0) + tc = c; + ustr[j++] = tc; + + if (tc < 0x80) + nlen++; + else if (tc < 0x800) + nlen += 2; + else if (tc < 0x10000) + nlen += 3; + else if (tc < 0x110000) + nlen += 4; + else { + [self freeMemory: ustr]; + @throw [OFInvalidEncodingException newWithClass: isa]; + } + + if (c < 0x80); + else if (c < 0x800) + i++; + else if (c < 0x10000) + i += 2; + else if (c < 0x110000) + i += 3; + else { + [self freeMemory: ustr]; + @throw [OFInvalidEncodingException newWithClass: isa]; + } + } + + @try { + nstr = [self allocMemoryWithSize: nlen + 1]; + } @catch (OFException *e) { + [self freeMemory: ustr]; + @throw e; + } + + j = 0; + + for (i = 0; i < ulen; i++) { + if ((d = of_string_unicode_to_utf8(ustr[i], nstr + j)) == 0) { + [self freeMemory: ustr]; + [self freeMemory: nstr]; + @throw [OFInvalidEncodingException newWithClass: isa]; + } + j += d; + } + + assert(j == nlen); + nstr[j] = 0; + [self freeMemory: ustr]; + + [self freeMemory: *string]; + *string = nstr; + *length = nlen; +} @implementation OFMutableString - setToCString: (const char*)str { size_t len; @@ -277,196 +372,20 @@ return self; } - upper { - of_unichar_t c, uc; - of_unichar_t *ustr; - size_t ulen, nlen; - size_t i, j, d; - char *nstr; - - if (!is_utf8) { - uint8_t *p = (uint8_t*)string + length; - uint8_t t; - - while (--p >= (uint8_t*)string) { - t = of_unicode_upper_table[0][*p]; - if (t != 0) - *p = t; - } - - return self; - } - - ulen = [self length]; - ustr = [self allocMemoryForNItems: [self length] - withSize: ulen]; - - j = 0; - nlen = 0; - - for (i = 0; i < length; i++) { - c = of_string_utf8_to_unicode(string + i, length - i); - - if (c == OF_INVALID_UNICHAR || c > 0x10FFFF) { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - - uc = of_unicode_upper_table[c >> 8][c & 0xFF]; - if (uc == 0) - uc = c; - ustr[j++] = uc; - - if (uc < 0x80) - nlen++; - else if (uc < 0x800) - nlen += 2; - else if (uc < 0x10000) - nlen += 3; - else if (uc < 0x110000) - nlen += 4; - else { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - - if (c < 0x80); - else if (c < 0x800) - i++; - else if (c < 0x10000) - i += 2; - else if (c < 0x110000) - i += 3; - else { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - } - - @try { - nstr = [self allocMemoryWithSize: nlen + 1]; - } @catch (OFException *e) { - [self freeMemory: ustr]; - @throw e; - } - - j = 0; - - for (i = 0; i < ulen; i++) { - if ((d = of_string_unicode_to_utf8(ustr[i], nstr + j)) == 0) { - [self freeMemory: ustr]; - [self freeMemory: nstr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - j += d; - } - - assert(j == nlen); - nstr[j] = 0; - [self freeMemory: ustr]; - - [self freeMemory: string]; - string = nstr; - length = nlen; + apply_table(self, isa, &string, &length, is_utf8, + of_unicode_upper_table); return self; } - lower { - of_unichar_t c, lc; - of_unichar_t *ustr; - size_t ulen, nlen; - size_t i, j, d; - char *nstr; - - if (!is_utf8) { - uint8_t *p = (uint8_t*)string + length; - uint8_t t; - - while (--p >= (uint8_t*)string) { - t = of_unicode_lower_table[0][*p]; - if (t != 0) - *p = t; - } - - return self; - } - - ulen = [self length]; - ustr = [self allocMemoryForNItems: [self length] - withSize: ulen]; - - j = 0; - nlen = 0; - - for (i = 0; i < length; i++) { - c = of_string_utf8_to_unicode(string + i, length - i); - - if (c == OF_INVALID_UNICHAR || c > 0x10FFFF) { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - - lc = of_unicode_lower_table[c >> 8][c & 0xFF]; - if (lc == 0) - lc = c; - ustr[j++] = lc; - - if (lc < 0x80) - nlen++; - else if (lc < 0x800) - nlen += 2; - else if (lc < 0x10000) - nlen += 3; - else if (lc < 0x110000) - nlen += 4; - else { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - - if (c < 0x80); - else if (c < 0x800) - i++; - else if (c < 0x10000) - i += 2; - else if (c < 0x110000) - i += 3; - else { - [self freeMemory: ustr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - } - - @try { - nstr = [self allocMemoryWithSize: nlen + 1]; - } @catch (OFException *e) { - [self freeMemory: ustr]; - @throw e; - } - - j = 0; - - for (i = 0; i < ulen; i++) { - if ((d = of_string_unicode_to_utf8(ustr[i], nstr + j)) == 0) { - [self freeMemory: ustr]; - [self freeMemory: nstr]; - @throw [OFInvalidEncodingException newWithClass: isa]; - } - j += d; - } - - assert(j == nlen); - nstr[j] = 0; - [self freeMemory: ustr]; - - [self freeMemory: string]; - string = nstr; - length = nlen; + apply_table(self, isa, &string, &length, is_utf8, + of_unicode_lower_table); return self; } - removeCharactersFromIndex: (size_t)start Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -36,17 +36,13 @@ * A class for managing strings. */ @interface OFString: OFObject { char *string; -#ifdef __objc_INCLUDE_GNU unsigned int length; -#else - int length; -#if __LP64__ +#if !defined(__objc_INCLUDE_GNU) && __LP64__ int _unused; -#endif #endif BOOL is_utf8; } /**