Overview
Comment: | Initial UTF-8 support for OFString. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
b4a4e9579803c9f230b72c6d0f7d5d1c |
User & Date: | js on 2008-12-21 15:42:52 |
Other Links: | manifest | tags |
Context
2008-12-21
| ||
15:54 | Nicer exception catching in tests. check-in: 1c77f45484 user: js tags: trunk | |
15:42 | Initial UTF-8 support for OFString. check-in: b4a4e95798 user: js tags: trunk | |
2008-12-20
| ||
14:41 | Don't put void* in lists, but objects. check-in: b2c9f574cb user: js tags: trunk | |
Changes
Modified src/OFExceptions.h from [e7cf625e34] to [e908641783].
︙ | ︙ | |||
130 131 132 133 134 135 136 | /** * \return An error message for the exception as a C String */ - (const char*)cString; @end /** | | | | 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | /** * \return An error message for the exception as a C String */ - (const char*)cString; @end /** * An OFException indicating that the encoding is invalid for this object. */ @interface OFInvalidEncodingException: OFException {} /** * \return An error message for the exception as a C String */ - (const char*)cString; @end /** |
︙ | ︙ |
Modified src/OFExceptions.m from [1c2da67154] to [7e98f0feae].
︙ | ︙ | |||
137 138 139 140 141 142 143 | asprintf(&string, "Value out of range in object of class %s!", object != nil ? [object name] : "(null)"); return string; } @end | | | | 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | asprintf(&string, "Value out of range in object of class %s!", object != nil ? [object name] : "(null)"); return string; } @end @implementation OFInvalidEncodingException - (const char*)cString { if (string != NULL) return string; asprintf(&string, "The encoding is invalid for object of classs %s!", [object name]); return string; } @end @implementation OFOpenFileFailedException |
︙ | ︙ |
Modified src/OFString.h from [80f285a39b] to [e46bf07ec6].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * A class for storing and modifying strings. */ @interface OFString: OFObject { char *string; size_t length; } /** * Creates a new OFString. * * \return An initialized OFString */ | > | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * A class for storing and modifying strings. */ @interface OFString: OFObject { char *string; size_t length; BOOL is_utf8; } /** * Creates a new OFString. * * \return An initialized OFString */ |
︙ | ︙ |
Modified src/OFString.m from [737d54ed05] to [f391445888].
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #import <stdlib.h> #import <string.h> #import <ctype.h> #import "OFString.h" #import "OFExceptions.h" @implementation OFString + new { return [[self alloc] init]; } + newFromCString: (const char*)str { return [[self alloc] initFromCString: str]; } - init { if ((self = [super init])) { length = 0; string = NULL; } return self; } - initFromCString: (const char*)str { if ((self = [super init])) { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > | > > > > > > > | | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #import <stdlib.h> #import <string.h> #import <ctype.h> #import "OFString.h" #import "OFExceptions.h" #import "OFMacros.h" static OF_INLINE int check_utf8(const char *str, size_t len) { size_t i; BOOL utf8; utf8 = NO; for (i = 0; i < len; i++) { /* No sign of UTF-8 here */ if (OF_LIKELY(~str[i] & 0x80)) continue; utf8 = YES; /* We're missing a start byte here */ if (OF_UNLIKELY(~str[i] & 0x40)) return -1; /* We have at minimum a 2 byte character -> check next byte */ if (OF_UNLIKELY(len < i + 1 || ~str[i + 1] & 0x80 || str[i + 1] & 0x40)) return -1; /* Check if we have at minimum a 3 byte character */ if (OF_LIKELY(~str[i] & 0x20)) { i++; continue; } /* We have at minimum a 3 byte char -> check second next byte */ if (OF_UNLIKELY(len < i + 2 || ~str[i + 2] & 0x80 || str[i + 2] & 0x40)) return -1; /* Check if we have a 4 byte character */ if (OF_LIKELY(~str[i] & 0x10)) { i += 2; continue; } /* We have a 4 byte character -> check third next byte */ if (OF_UNLIKELY(len < i + 3 || ~str[i + 3] & 0x80 || str[i + 3] & 0x40)) return -1; /* * Just in case, check if there's a 5th character, which is * forbidden by UTF-8 */ if (OF_UNLIKELY(str[i] & 0x08)) return -1; i += 3; } return (utf8 ? 1 : 0); } @implementation OFString + new { return [[self alloc] init]; } + newFromCString: (const char*)str { return [[self alloc] initFromCString: str]; } - init { if ((self = [super init])) { length = 0; string = NULL; is_utf8 = NO; } return self; } - initFromCString: (const char*)str { if ((self = [super init])) { if (str != NULL) { length = strlen(str); switch (check_utf8(str, length)) { case 1: is_utf8 = YES; break; case -1: [super free]; @throw [OFInvalidEncodingException newWithObject: self]; } string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } } return self; } |
︙ | ︙ | |||
91 92 93 94 95 96 97 | char *newstr; size_t newlen, strlength; if (string == NULL) return [self setTo: [OFString newFromCString: str]]; strlength = strlen(str); | | > > > > > > | > > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | char *newstr; size_t newlen, strlength; if (string == NULL) return [self setTo: [OFString newFromCString: str]]; strlength = strlen(str); switch (check_utf8(str, strlength)) { case 1: is_utf8 = YES; break; case -1: @throw [OFInvalidEncodingException newWithObject: self]; } newlen = length + strlength; newstr = [self resizeMem: string toSize: newlen + 1]; memcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; return self; } - reverse { size_t i, j, len = length / 2; /* We reverse all bytes and restore UTF-8 later, if necessary */ for (i = 0, j = length - 1; i < len; i++, j--) { string[i] ^= string[j]; string[j] ^= string[i]; string[i] ^= string[j]; } if (!is_utf8) return self; for (i = 0; i < length; i++) { /* ASCII */ if (OF_LIKELY(~string[i] & 0x80)) continue; /* A start byte can't happen first as we reversed everything */ if (OF_UNLIKELY(string[i] & 0x40)) @throw [OFInvalidEncodingException newWithObject: self]; /* Next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 1 || ~string[i + 1] & 0x80)) @throw [OFInvalidEncodingException newWithObject: self]; /* Next byte is the start byte */ if (OF_LIKELY(string[i + 1] & 0x40)) { string[i] ^= string[i + 1]; string[i + 1] ^= string[i]; string[i] ^= string[i + 1]; i++; continue; } /* Second next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 2 || ~string[i + 2] & 0x80)) @throw [OFInvalidEncodingException newWithObject: self]; /* Second next byte is the start byte */ if (OF_LIKELY(string[i + 2] & 0x40)) { string[i] ^= string[i + 2]; string[i + 2] ^= string[i]; string[i] ^= string[i + 2]; i += 2; continue; } /* Third next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 3 || ~string[i + 3] & 0x80)) @throw [OFInvalidEncodingException newWithObject: self]; /* Third next byte is the start byte */ if (OF_LIKELY(string[i + 3] & 0x40)) { string[i] ^= string[i + 3]; string[i + 3] ^= string[i]; string[i] ^= string[i + 3]; string[i + 1] ^= string[i + 2]; string[i + 2] ^= string[i + 1]; string[i + 1] ^= string[i + 2]; i += 3; continue; } /* UTF-8 does not allow more than 4 bytes per character */ @throw [OFInvalidEncodingException newWithObject: self]; } return self; } - upper { size_t i = length; if (is_utf8) @throw [OFInvalidEncodingException newWithObject: self]; while (i--) string[i] = toupper(string[i]); return self; } - lower { size_t i = length; if (is_utf8) @throw [OFInvalidEncodingException newWithObject: self]; while (i--) string[i] = tolower(string[i]); return self; } @end |
Modified tests/OFString/OFString.m from [acd4c64051] to [09c95b9e26].
︙ | ︙ | |||
11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #import "config.h" #import <stdio.h> #import <string.h> #import "OFString.h" int main() { OFString *s1 = [OFString newFromCString: "test"]; OFString *s2 = [OFString newFromCString: ""]; OFString *s3; | > | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #import "config.h" #import <stdio.h> #import <string.h> #import "OFString.h" #import "OFExceptions.h" int main() { OFString *s1 = [OFString newFromCString: "test"]; OFString *s2 = [OFString newFromCString: ""]; OFString *s3; |
︙ | ︙ | |||
79 80 81 82 83 84 85 86 87 88 | } /* Also clears all the memory of the returned C strings */ [s1 free]; [s2 free]; [s3 free]; [s4 free]; return 0; } | > > > > > > > > > > > > > > > > > > > > > > > > > > > | 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | } /* Also clears all the memory of the returned C strings */ [s1 free]; [s2 free]; [s3 free]; [s4 free]; /* UTF-8 tests */ @try { s1 = [OFString newFromCString: "\xE0\x80"]; puts("First invalid UTF-8 not detected!"); return 1; } @catch (OFInvalidEncodingException *e) { puts("First invalid UTF-8 successfully detected!"); } @try { s1 = [OFString newFromCString: "\xF0\x80\x80\xC0"]; puts("Second UTF-8 not detected!"); return 1; } @catch (OFInvalidEncodingException *e) { puts("Second UTF-8 successfully detected!"); } s1 = [OFString newFromCString: "äöü€𝄞"]; if (!strcmp([[s1 reverse] cString], "𝄞€üöä")) puts("Reversed UTF-8 string is expected string! GOOD!"); else { puts("Reversed UTF-8 string is NOT expected string!"); return 1; } return 0; } |