| 1 |
/* |
| 2 |
** NSString+Extensions.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2014-2022 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/NSString+Extensions.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWCharset.h> |
| 27 |
#import <Pantomime/CWConstants.h> |
| 28 |
#import <Pantomime/CWInternetAddress.h> |
| 29 |
#import <Pantomime/CWPart.h> |
| 30 |
#import <Pantomime/NSData+Extensions.h> |
| 31 |
|
| 32 |
#import <Foundation/NSBundle.h> |
| 33 |
|
| 34 |
// |
| 35 |
// We include the CoreFoundation headers under Mac OS X so we can support |
| 36 |
// more string encodings. |
| 37 |
// |
| 38 |
#ifdef MACOSX |
| 39 |
#include <CoreFoundation/CFString.h> |
| 40 |
#include <CoreFoundation/CFStringEncodingExt.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <ctype.h> |
| 44 |
|
| 45 |
#ifdef HAVE_ICONV |
| 46 |
#include <iconv.h> |
| 47 |
#if defined (MACOSX) || defined (__NetBSD__) || defined (__FreeBSD__) |
| 48 |
#define iconv_const_qualifier const |
| 49 |
#else |
| 50 |
#define iconv_const_qualifier |
| 51 |
#endif |
| 52 |
#endif |
| 53 |
|
| 54 |
#define IS_PRINTABLE(c) (isascii(c) && isprint(c)) |
| 55 |
|
| 56 |
// |
| 57 |
// |
| 58 |
// |
| 59 |
@implementation NSString (PantomimeStringExtensions) |
| 60 |
|
| 61 |
#ifdef MACOSX |
| 62 |
- (NSString *) stringByTrimmingWhiteSpaces |
| 63 |
{ |
| 64 |
NSMutableString *aMutableString; |
| 65 |
|
| 66 |
aMutableString = [[NSMutableString alloc] initWithString: self]; |
| 67 |
CFStringTrimWhitespace((CFMutableStringRef)aMutableString); |
| 68 |
|
| 69 |
return AUTORELEASE(aMutableString); |
| 70 |
} |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
// |
| 75 |
// |
| 76 |
// |
| 77 |
- (NSUInteger) indexOfCharacter: (unichar) theCharacter |
| 78 |
{ |
| 79 |
return [self indexOfCharacter: theCharacter fromIndex: 0u]; |
| 80 |
} |
| 81 |
|
| 82 |
// |
| 83 |
// |
| 84 |
// |
| 85 |
- (NSUInteger) indexOfLastCharacter: (unichar) theCharacter |
| 86 |
{ |
| 87 |
NSUInteger len; |
| 88 |
NSUInteger i; |
| 89 |
|
| 90 |
len = [self length]; |
| 91 |
|
| 92 |
for (i = 1; i <= len; i++) |
| 93 |
{ |
| 94 |
if ([self characterAtIndex: len-i] == theCharacter) |
| 95 |
{ |
| 96 |
return len-i; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return NSNotFound; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
// |
| 105 |
// |
| 106 |
// |
| 107 |
- (NSUInteger) indexOfCharacter: (unichar) theCharacter |
| 108 |
fromIndex: (NSUInteger) theIndex |
| 109 |
{ |
| 110 |
NSUInteger len; |
| 111 |
NSUInteger i; |
| 112 |
|
| 113 |
len = [self length]; |
| 114 |
|
| 115 |
for (i = theIndex; i < len; i++) |
| 116 |
{ |
| 117 |
if ([self characterAtIndex: i] == theCharacter) |
| 118 |
{ |
| 119 |
return i; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return NSNotFound; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
// |
| 128 |
// |
| 129 |
// |
| 130 |
- (BOOL) hasCaseInsensitivePrefix: (NSString *) thePrefix |
| 131 |
{ |
| 132 |
if (thePrefix) |
| 133 |
{ |
| 134 |
return [[self uppercaseString] hasPrefix: [thePrefix uppercaseString]]; |
| 135 |
} |
| 136 |
|
| 137 |
return NO; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
// |
| 142 |
// |
| 143 |
// |
| 144 |
- (BOOL) hasCaseInsensitiveSuffix: (NSString *) theSuffix |
| 145 |
{ |
| 146 |
if (theSuffix) |
| 147 |
{ |
| 148 |
return [[self uppercaseString] hasSuffix: [theSuffix uppercaseString]]; |
| 149 |
} |
| 150 |
|
| 151 |
return NO; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
// |
| 156 |
// |
| 157 |
// |
| 158 |
- (NSString *) stringFromQuotedString |
| 159 |
{ |
| 160 |
NSUInteger len; |
| 161 |
|
| 162 |
len = [self length]; |
| 163 |
|
| 164 |
if (len > 1 && |
| 165 |
[self characterAtIndex: 0] == '"' && |
| 166 |
[self characterAtIndex: (len-1)] == '"') |
| 167 |
{ |
| 168 |
return [self substringWithRange: NSMakeRange(1, len-2)]; |
| 169 |
} |
| 170 |
|
| 171 |
return self; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
// |
| 176 |
// |
| 177 |
// |
| 178 |
+ (NSString *) stringValueOfTransferEncoding: (PantomimeEncoding) theEncoding |
| 179 |
{ |
| 180 |
switch (theEncoding) |
| 181 |
{ |
| 182 |
case PantomimeEncodingNone: |
| 183 |
break; |
| 184 |
case PantomimeEncodingQuotedPrintable: |
| 185 |
return @"quoted-printable"; |
| 186 |
case PantomimeEncodingBase64: |
| 187 |
return @"base64"; |
| 188 |
case PantomimeEncoding8bit: |
| 189 |
return @"8bit"; |
| 190 |
case PantomimeEncodingBinary: |
| 191 |
return @"binary"; |
| 192 |
default: |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
// PantomimeEncoding7bit will also fall back here. |
| 197 |
return @"7bit"; |
| 198 |
} |
| 199 |
|
| 200 |
// |
| 201 |
// |
| 202 |
// |
| 203 |
+ (unsigned long) encodingForCharset: (NSData *) theCharset |
| 204 |
{ |
| 205 |
return [self encodingForCharset: theCharset convertToNSStringEncoding: YES]; |
| 206 |
} |
| 207 |
|
| 208 |
// |
| 209 |
// Convenience to be able to use CoreFoundation conversion instead of NSString |
| 210 |
// |
| 211 |
+ (unsigned long) encodingForCharset: (NSData *) theCharset |
| 212 |
convertToNSStringEncoding: (BOOL) shouldConvert |
| 213 |
{ |
| 214 |
// We define some aliases for the string encoding. |
| 215 |
static struct { NSString *name; unsigned long encoding; BOOL fromCoreFoundation; } encodings[] = { |
| 216 |
{@"ascii" ,NSASCIIStringEncoding ,NO}, |
| 217 |
{@"us-ascii" ,NSASCIIStringEncoding ,NO}, |
| 218 |
{@"default" ,NSASCIIStringEncoding ,NO}, // Ah... spammers. |
| 219 |
{@"utf-8" ,NSUTF8StringEncoding ,NO}, |
| 220 |
{@"iso-8859-1" ,NSISOLatin1StringEncoding ,NO}, |
| 221 |
{@"x-user-defined",NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Outlook. |
| 222 |
{@"unknown" ,NSISOLatin1StringEncoding ,NO}, // Once more, blame Outlook. |
| 223 |
{@"x-unknown" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Pine 4.21. |
| 224 |
{@"unknown-8bit" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Mutt/1.3.28i |
| 225 |
{@"0" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in QUALCOMM Windows Eudora Version 6.0.1.1 |
| 226 |
{@"" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Ximian Evolution |
| 227 |
{@"iso8859_1" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Openwave WebEngine |
| 228 |
{@"iso-8859-2" ,NSISOLatin2StringEncoding ,NO}, |
| 229 |
#ifndef MACOSX |
| 230 |
{@"iso-8859-3" ,NSISOLatin3StringEncoding ,NO}, |
| 231 |
{@"iso-8859-4" ,NSISOLatin4StringEncoding ,NO}, |
| 232 |
{@"iso-8859-5" ,NSISOCyrillicStringEncoding ,NO}, |
| 233 |
{@"iso-8859-6" ,NSISOArabicStringEncoding ,NO}, |
| 234 |
{@"iso-8859-7" ,NSISOGreekStringEncoding ,NO}, |
| 235 |
{@"iso-8859-8" ,NSISOHebrewStringEncoding ,NO}, |
| 236 |
{@"iso-8859-9" ,NSISOLatin5StringEncoding ,NO}, |
| 237 |
{@"iso-8859-10" ,NSISOLatin6StringEncoding ,NO}, |
| 238 |
{@"iso-8859-11" ,NSISOThaiStringEncoding ,NO}, |
| 239 |
{@"iso-8859-13" ,NSISOLatin7StringEncoding ,NO}, |
| 240 |
{@"iso-8859-14" ,NSISOLatin8StringEncoding ,NO}, |
| 241 |
{@"iso-8859-15" ,NSISOLatin9StringEncoding ,NO}, |
| 242 |
{@"koi8-r" ,NSKOI8RStringEncoding ,NO}, |
| 243 |
{@"big5" ,NSBig5StringEncoding ,NO}, |
| 244 |
{@"gb2312" ,NSHZ_GB_2312StringEncoding ,NO}, |
| 245 |
{@"utf-7" ,NSUTF7StringEncoding ,NO}, |
| 246 |
{@"unicode-1-1-utf-7", NSUTF7StringEncoding ,NO}, // To prever a bug (sort of) in MS Hotmail |
| 247 |
{@"euc-kr" ,NSKoreanEUCStringEncoding ,NO}, |
| 248 |
#else // Core foundation support |
| 249 |
{@"iso-8859-3" ,kCFStringEncodingISOLatin3 ,YES}, |
| 250 |
{@"koi8-r" ,kCFStringEncodingKOI8_R ,YES}, |
| 251 |
{@"gb2312" ,kCFStringEncodingEUC_CN ,YES}, // tested instead of proper GB2312-80 |
| 252 |
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4 |
| 253 |
{@"koi8-u" ,kCFStringEncodingKOI8_U ,YES}, |
| 254 |
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 |
| 255 |
{@"utf-7" ,kCFStringEncodingUTF7 ,YES}, |
| 256 |
#endif |
| 257 |
#endif |
| 258 |
{@"euc-kr" ,kCFStringEncodingEUC_KR ,YES}, |
| 259 |
#endif |
| 260 |
{@"windows-1250" ,NSWindowsCP1250StringEncoding ,NO}, |
| 261 |
{@"windows-1251" ,NSWindowsCP1251StringEncoding ,NO}, |
| 262 |
{@"cyrillic (windows-1251)", NSWindowsCP1251StringEncoding ,NO}, // To prevent a bug in MS Hotmail |
| 263 |
{@"windows-1252" ,NSWindowsCP1252StringEncoding ,NO}, |
| 264 |
{@"windows-1253" ,NSWindowsCP1253StringEncoding ,NO}, |
| 265 |
{@"windows-1254" ,NSWindowsCP1254StringEncoding ,NO}, |
| 266 |
{@"iso-2022-jp" ,NSISO2022JPStringEncoding ,NO}, |
| 267 |
{@"euc-jp" ,NSJapaneseEUCStringEncoding ,NO}, |
| 268 |
}; |
| 269 |
|
| 270 |
NSString *name; |
| 271 |
unsigned i; |
| 272 |
|
| 273 |
name = [[NSString stringWithCString: [theCharset bytes] length: [theCharset length]] lowercaseString]; |
| 274 |
for (i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) |
| 275 |
{ |
| 276 |
if ([name isEqualToString: encodings[i].name]) |
| 277 |
{ |
| 278 |
unsigned long enc = encodings[i].encoding; |
| 279 |
// Under OS X, we use CoreFoundation if necessary to convert the encoding |
| 280 |
// to a NSString encoding. |
| 281 |
#ifdef MACOSX |
| 282 |
if (encodings[i].fromCoreFoundation) |
| 283 |
{ |
| 284 |
if (shouldConvert) |
| 285 |
{ |
| 286 |
NSStringEncoding nsEnc; |
| 287 |
|
| 288 |
nsEnc = CFStringConvertEncodingToNSStringEncoding ((CFStringEncoding)enc); |
| 289 |
return (unsigned long)nsEnc; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
return enc; |
| 294 |
} |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
if (shouldConvert) |
| 299 |
{ |
| 300 |
return enc; |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
unsigned long enc2; |
| 305 |
|
| 306 |
enc2 = (unsigned long)CFStringConvertNSStringEncodingToEncoding(enc); |
| 307 |
return enc2; |
| 308 |
} |
| 309 |
} |
| 310 |
#else |
| 311 |
return enc; |
| 312 |
#endif |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
#ifdef MACOSX |
| 317 |
// Last resort: try using CoreFoundation... |
| 318 |
CFStringEncoding enc; |
| 319 |
|
| 320 |
enc = CFStringConvertIANACharSetNameToEncoding((CFStringRef)name); |
| 321 |
if (kCFStringEncodingInvalidId != enc) |
| 322 |
{ |
| 323 |
if (shouldConvert) |
| 324 |
{ |
| 325 |
return CFStringConvertEncodingToNSStringEncoding(enc); |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
return enc; |
| 330 |
} |
| 331 |
} |
| 332 |
#endif |
| 333 |
|
| 334 |
return NSNotFound; |
| 335 |
} |
| 336 |
|
| 337 |
// |
| 338 |
// |
| 339 |
// |
| 340 |
+ (unsigned long) encodingForPart: (CWPart *) thePart |
| 341 |
{ |
| 342 |
return [self encodingForPart: thePart convertToNSStringEncoding: YES]; |
| 343 |
} |
| 344 |
|
| 345 |
// |
| 346 |
// Convenience to be able to use CoreFoundation conversion instead of NSString |
| 347 |
// |
| 348 |
+ (unsigned long) encodingForPart: (CWPart *) thePart |
| 349 |
convertToNSStringEncoding: (BOOL) shouldConvert |
| 350 |
{ |
| 351 |
unsigned long encoding; |
| 352 |
|
| 353 |
// We get the encoding we are gonna use. We always favor the default encoding. |
| 354 |
if ([thePart defaultCharset]) |
| 355 |
{ |
| 356 |
encoding = [self encodingForCharset: [[thePart defaultCharset] dataUsingEncoding: NSASCIIStringEncoding] |
| 357 |
convertToNSStringEncoding: shouldConvert]; |
| 358 |
} |
| 359 |
else if ([thePart charset]) |
| 360 |
{ |
| 361 |
encoding = [self encodingForCharset: [[thePart charset] dataUsingEncoding: NSASCIIStringEncoding] |
| 362 |
convertToNSStringEncoding: shouldConvert]; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
encoding = [NSString defaultCStringEncoding]; |
| 367 |
} |
| 368 |
|
| 369 |
if (encoding == NSNotFound || encoding == NSASCIIStringEncoding) |
| 370 |
{ |
| 371 |
encoding = NSISOLatin1StringEncoding; |
| 372 |
} |
| 373 |
|
| 374 |
return encoding; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
// |
| 379 |
// |
| 380 |
// |
| 381 |
+ (NSString *) stringWithData: (NSData *) theData |
| 382 |
charset: (NSData *) theCharset |
| 383 |
{ |
| 384 |
unsigned long encoding; |
| 385 |
|
| 386 |
if (theData == nil) |
| 387 |
{ |
| 388 |
return nil; |
| 389 |
} |
| 390 |
|
| 391 |
#ifdef MACOSX |
| 392 |
encoding = [NSString encodingForCharset: theCharset |
| 393 |
convertToNSStringEncoding: NO]; |
| 394 |
#else |
| 395 |
encoding = [NSString encodingForCharset: theCharset]; |
| 396 |
#endif |
| 397 |
|
| 398 |
if (encoding == NSNotFound) |
| 399 |
{ |
| 400 |
#ifdef HAVE_ICONV |
| 401 |
NSString *aString; |
| 402 |
|
| 403 |
const char *i_bytes, *from_code; |
| 404 |
char *o_bytes; |
| 405 |
char *o_buff; |
| 406 |
|
| 407 |
size_t i_length, o_length, ret; |
| 408 |
size_t total_length; |
| 409 |
iconv_t conv; |
| 410 |
|
| 411 |
// Instead of calling cString directly on theCharset, we first try |
| 412 |
// to obtain the ASCII string of the data object. |
| 413 |
from_code = [[theCharset asciiString] cString]; |
| 414 |
NSLog(@"stringWithData - iconv fallback, charset: %s", from_code); |
| 415 |
if (!from_code) |
| 416 |
{ |
| 417 |
return nil; |
| 418 |
} |
| 419 |
|
| 420 |
conv = iconv_open("UTF-8", from_code); |
| 421 |
|
| 422 |
if (conv == (iconv_t)-1) |
| 423 |
{ |
| 424 |
// Let's assume we got US-ASCII here. |
| 425 |
return AUTORELEASE([[NSString alloc] initWithData: theData encoding: NSASCIIStringEncoding]); |
| 426 |
} |
| 427 |
|
| 428 |
i_bytes = [theData bytes]; |
| 429 |
i_length = [theData length]; |
| 430 |
|
| 431 |
total_length = sizeof(unichar)*i_length; |
| 432 |
o_length = total_length; |
| 433 |
o_buff = (char *)malloc(o_length); // original pointer to the buffer |
| 434 |
o_bytes = o_buff; // o_bytes is the working iconv pointer |
| 435 |
|
| 436 |
if (o_bytes == NULL) return nil; |
| 437 |
|
| 438 |
while (i_length > 0) |
| 439 |
{ |
| 440 |
ret = iconv(conv, (iconv_const_qualifier char **)&i_bytes, &i_length, &o_bytes, &o_length); |
| 441 |
|
| 442 |
// *i_bytes Pointer to the byte just after the last character fetched. |
| 443 |
// *i_bytes Number of remaining bytes in the source buffer. |
| 444 |
// *o_bytes Pointer to the byte just after the last character stored. |
| 445 |
// *o_length Number of remainder bytes in the destination buffer. |
| 446 |
if (ret == (size_t)-1) |
| 447 |
{ |
| 448 |
// was our buffer too small? |
| 449 |
if (errno == E2BIG) |
| 450 |
{ |
| 451 |
// realloc and try again |
| 452 |
total_length += sizeof(unichar); |
| 453 |
o_buff = realloc(o_buff, total_length); |
| 454 |
if (o_buff == NULL) |
| 455 |
{ |
| 456 |
NSLog(@"stringWithData, realloc() failed while enlarging, returning nil"); |
| 457 |
iconv_close(conv); |
| 458 |
return nil; |
| 459 |
} |
| 460 |
o_bytes = o_buff; |
| 461 |
o_length = total_length; |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
// we could have had an invalid sequence EILSEQ or incomplete sequence EINVAL |
| 466 |
NSLog(@"stringWithData, invalid sequence, returning nil"); |
| 467 |
iconv_close(conv); |
| 468 |
free(o_buff); |
| 469 |
return nil; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
total_length = total_length - o_length; |
| 475 |
|
| 476 |
// If we haven't used all our allocated buffer, we do not need shrink it. |
| 477 |
// We just pass the reduced length to NSData and tell it not to free it |
| 478 |
aString = [[NSString alloc] initWithData: [NSData dataWithBytesNoCopy: o_buff |
| 479 |
length: total_length |
| 480 |
freeWhenDone:NO] |
| 481 |
encoding: NSUTF8StringEncoding]; |
| 482 |
|
| 483 |
iconv_close(conv); |
| 484 |
|
| 485 |
// free the whole original buffer, NSData will be invalid, but aString remains fine |
| 486 |
free(o_buff); |
| 487 |
return AUTORELEASE(aString); |
| 488 |
#else |
| 489 |
return nil; |
| 490 |
#endif |
| 491 |
} |
| 492 |
|
| 493 |
#ifdef MACOSX |
| 494 |
return AUTORELEASE((NSString *)CFStringCreateFromExternalRepresentation(NULL, (CFDataRef)theData, (CFStringEncoding)encoding)); |
| 495 |
#else |
| 496 |
return AUTORELEASE([[NSString alloc] initWithData: theData encoding: encoding]); |
| 497 |
#endif |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
// |
| 502 |
// |
| 503 |
// Return only charset which we can also later encoode (encodingForCharset:) |
| 504 |
#warning return Charset instead? |
| 505 |
- (NSString *) charset |
| 506 |
{ |
| 507 |
NSMutableArray *aMutableArray; |
| 508 |
NSString *aString; |
| 509 |
CWCharset *aCharset; |
| 510 |
NSUInteger i, j; |
| 511 |
|
| 512 |
aMutableArray = [[NSMutableArray alloc] initWithCapacity: 21]; |
| 513 |
|
| 514 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-1"]]; |
| 515 |
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1252"]]; |
| 516 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-2"]]; |
| 517 |
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1251"]]; |
| 518 |
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1253"]]; |
| 519 |
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1250"]]; |
| 520 |
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1254"]]; |
| 521 |
#ifndef MACOSX |
| 522 |
[aMutableArray addObject: [CWCharset charsetForName: @"koi8-r"]]; |
| 523 |
[aMutableArray addObject: [CWCharset charsetForName: @"koi8-u"]]; |
| 524 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-3"]]; |
| 525 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-4"]]; |
| 526 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-5"]]; |
| 527 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-6"]]; |
| 528 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-7"]]; |
| 529 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-8"]]; |
| 530 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-9"]]; |
| 531 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-10"]]; |
| 532 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-11"]]; |
| 533 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-13"]]; |
| 534 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-14"]]; |
| 535 |
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-15"]]; |
| 536 |
#endif |
| 537 |
|
| 538 |
// we look for possible charsets, either we find no possible ones |
| 539 |
// or finish the string with a one or more charsets |
| 540 |
i = 0; |
| 541 |
while (i < [self length] && [aMutableArray count] > 0) |
| 542 |
{ |
| 543 |
j = 0; |
| 544 |
// For character we remove all charset it cannot be represented with |
| 545 |
while (j < [aMutableArray count]) |
| 546 |
{ |
| 547 |
if ([[aMutableArray objectAtIndex: j] characterIsInCharset: [self characterAtIndex: i]]) |
| 548 |
{ |
| 549 |
j++; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
// Character is not in the charset |
| 554 |
[aMutableArray removeObjectAtIndex: j]; |
| 555 |
} |
| 556 |
} |
| 557 |
i++; |
| 558 |
} |
| 559 |
|
| 560 |
NSDebugLog(@"We have %lu charsets: %@", (unsigned long) [aMutableArray count], aMutableArray); |
| 561 |
if ([aMutableArray count]) |
| 562 |
{ |
| 563 |
aCharset = [aMutableArray objectAtIndex: 0]; |
| 564 |
[aMutableArray removeAllObjects]; |
| 565 |
aString = [aCharset name]; |
| 566 |
} |
| 567 |
else |
| 568 |
{ |
| 569 |
// We have no charset, we try to "guess" a default charset |
| 570 |
if ([self canBeConvertedToEncoding: NSISO2022JPStringEncoding]) |
| 571 |
{ |
| 572 |
// ISO-2022-JP is the standard of Japanese character encoding |
| 573 |
aString = @"iso-2022-jp"; |
| 574 |
} |
| 575 |
else |
| 576 |
{ |
| 577 |
// We have no charset, we return a default charset |
| 578 |
aString = @"utf-8"; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
RELEASE(aMutableArray); |
| 583 |
return aString; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
// |
| 588 |
// |
| 589 |
// |
| 590 |
- (NSString *) modifiedUTF7String |
| 591 |
{ |
| 592 |
#ifndef MACOSX |
| 593 |
NSMutableData *aMutableData, *modifiedData; |
| 594 |
NSString *aString; |
| 595 |
|
| 596 |
const char *b; |
| 597 |
BOOL escaped; |
| 598 |
unichar ch; |
| 599 |
NSUInteger i, len; |
| 600 |
|
| 601 |
// |
| 602 |
// We UTF-7 encode _only_ the non-ASCII parts. |
| 603 |
// |
| 604 |
aMutableData = [[NSMutableData alloc] init]; |
| 605 |
AUTORELEASE(aMutableData); |
| 606 |
len = [self length]; |
| 607 |
|
| 608 |
for (i = 0; i < len; i++) |
| 609 |
{ |
| 610 |
ch = [self characterAtIndex: i]; |
| 611 |
|
| 612 |
if (IS_PRINTABLE(ch)) |
| 613 |
{ |
| 614 |
[aMutableData appendCFormat: @"%c", ch]; |
| 615 |
} |
| 616 |
else |
| 617 |
{ |
| 618 |
NSUInteger j; |
| 619 |
|
| 620 |
j = i+1; |
| 621 |
// We got a non-ASCII character, let's get the substring and encode it using UTF-7. |
| 622 |
while (j < len && !IS_PRINTABLE([self characterAtIndex: j])) |
| 623 |
{ |
| 624 |
j++; |
| 625 |
} |
| 626 |
|
| 627 |
// Get the substring. |
| 628 |
[aMutableData appendData: [[self substringWithRange: NSMakeRange(i,j-i)] dataUsingEncoding: NSUTF7StringEncoding]]; |
| 629 |
i = j-1; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
b = [aMutableData bytes]; |
| 634 |
len = [aMutableData length]; |
| 635 |
escaped = NO; |
| 636 |
|
| 637 |
// |
| 638 |
// We replace: |
| 639 |
// |
| 640 |
// & -> &- |
| 641 |
// + -> & |
| 642 |
// +- -> + |
| 643 |
// / -> , |
| 644 |
// |
| 645 |
// in order to produce our modified UTF-7 string. |
| 646 |
// |
| 647 |
modifiedData = [[NSMutableData alloc] init]; |
| 648 |
AUTORELEASE(modifiedData); |
| 649 |
|
| 650 |
for (i = 0; i < len; i++, b++) |
| 651 |
{ |
| 652 |
if (!escaped && *b == '&') |
| 653 |
{ |
| 654 |
[modifiedData appendCString: "&-"]; |
| 655 |
} |
| 656 |
else if (!escaped && *b == '+') |
| 657 |
{ |
| 658 |
if (*(b+1) == '-') |
| 659 |
{ |
| 660 |
[modifiedData appendCString: "+"]; |
| 661 |
} |
| 662 |
else |
| 663 |
{ |
| 664 |
[modifiedData appendCString: "&"]; |
| 665 |
|
| 666 |
// We enter the escaped mode. |
| 667 |
escaped = YES; |
| 668 |
} |
| 669 |
} |
| 670 |
else if (escaped && *b == '/') |
| 671 |
{ |
| 672 |
[modifiedData appendCString: ","]; |
| 673 |
} |
| 674 |
else if (escaped && *b == '-') |
| 675 |
{ |
| 676 |
[modifiedData appendCString: "-"]; |
| 677 |
|
| 678 |
// We leave the escaped mode. |
| 679 |
escaped = NO; |
| 680 |
} |
| 681 |
else |
| 682 |
{ |
| 683 |
[modifiedData appendCFormat: @"%c", *b]; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
// If we're still in the escaped mode we haven't added our trailing -, |
| 688 |
// let's add it right now. |
| 689 |
if (escaped) |
| 690 |
{ |
| 691 |
[modifiedData appendCString: "-"]; |
| 692 |
} |
| 693 |
|
| 694 |
aString = AUTORELEASE([[NSString alloc] initWithData: modifiedData encoding: NSASCIIStringEncoding]); |
| 695 |
|
| 696 |
return (aString != nil ? aString : self); |
| 697 |
#else |
| 698 |
return self; |
| 699 |
#endif |
| 700 |
} |
| 701 |
|
| 702 |
// |
| 703 |
// |
| 704 |
// |
| 705 |
- (NSString *) stringFromModifiedUTF7 |
| 706 |
{ |
| 707 |
#ifndef MACOSX |
| 708 |
NSMutableData *aMutableData; |
| 709 |
|
| 710 |
BOOL escaped; |
| 711 |
unichar ch; |
| 712 |
NSUInteger i, len; |
| 713 |
|
| 714 |
aMutableData = [[NSMutableData alloc] init]; |
| 715 |
AUTORELEASE(aMutableData); |
| 716 |
|
| 717 |
len = [self length]; |
| 718 |
escaped = NO; |
| 719 |
|
| 720 |
// |
| 721 |
// We replace: |
| 722 |
// |
| 723 |
// & -> + |
| 724 |
// &- -> & |
| 725 |
// , -> / |
| 726 |
// |
| 727 |
// If we are in escaped mode. That is, between a &....- |
| 728 |
// |
| 729 |
for (i = 0; i < len; i++) |
| 730 |
{ |
| 731 |
ch = [self characterAtIndex: i]; |
| 732 |
|
| 733 |
if (!escaped && ch == '&') |
| 734 |
{ |
| 735 |
if ( (i+1) < len && [self characterAtIndex: (i+1)] != '-' ) |
| 736 |
{ |
| 737 |
[aMutableData appendCString: "+"]; |
| 738 |
|
| 739 |
// We enter the escaped mode. |
| 740 |
escaped = YES; |
| 741 |
} |
| 742 |
else |
| 743 |
{ |
| 744 |
// We replace &- by & |
| 745 |
[aMutableData appendCString: "&"]; |
| 746 |
i++; |
| 747 |
} |
| 748 |
} |
| 749 |
else if (escaped && ch == ',') |
| 750 |
{ |
| 751 |
[aMutableData appendCString: "/"]; |
| 752 |
} |
| 753 |
else if (escaped && ch == '-') |
| 754 |
{ |
| 755 |
[aMutableData appendCString: "-"]; |
| 756 |
|
| 757 |
// We leave the escaped mode. |
| 758 |
escaped = NO; |
| 759 |
} |
| 760 |
else |
| 761 |
{ |
| 762 |
[aMutableData appendCFormat: @"%c", ch]; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
return AUTORELEASE([[NSString alloc] initWithData: aMutableData encoding: NSUTF7StringEncoding]); |
| 767 |
#else |
| 768 |
return nil; |
| 769 |
#endif |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
// |
| 774 |
// |
| 775 |
// |
| 776 |
- (BOOL) hasREPrefix |
| 777 |
{ |
| 778 |
if ([self hasCaseInsensitivePrefix: @"re:"] || |
| 779 |
[self hasCaseInsensitivePrefix: @"re :"] || |
| 780 |
[self hasCaseInsensitivePrefix: _(@"PantomimeReferencePrefix")] || |
| 781 |
[self hasCaseInsensitivePrefix: _(@"PantomimeResponsePrefix")]) |
| 782 |
{ |
| 783 |
return YES; |
| 784 |
} |
| 785 |
|
| 786 |
return NO; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
// |
| 792 |
// |
| 793 |
// |
| 794 |
- (NSString *) stringByReplacingOccurrencesOfCharacter: (unichar) theTarget |
| 795 |
withCharacter: (unichar) theReplacement |
| 796 |
{ |
| 797 |
NSMutableString *aMutableString; |
| 798 |
NSUInteger len, i; |
| 799 |
unichar c; |
| 800 |
|
| 801 |
if (!theTarget || !theReplacement || theTarget == theReplacement) |
| 802 |
{ |
| 803 |
return self; |
| 804 |
} |
| 805 |
|
| 806 |
len = [self length]; |
| 807 |
|
| 808 |
aMutableString = [NSMutableString stringWithCapacity: len]; |
| 809 |
|
| 810 |
for (i = 0; i < len; i++) |
| 811 |
{ |
| 812 |
c = [self characterAtIndex: i]; |
| 813 |
|
| 814 |
if (c == theTarget) |
| 815 |
{ |
| 816 |
[aMutableString appendFormat: @"%C", theReplacement]; |
| 817 |
} |
| 818 |
else |
| 819 |
{ |
| 820 |
[aMutableString appendFormat: @"%C", c]; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
return aMutableString; |
| 825 |
} |
| 826 |
|
| 827 |
// |
| 828 |
// |
| 829 |
// |
| 830 |
- (NSString *) stringByDeletingLastPathComponentWithSeparator: (unichar) theSeparator |
| 831 |
{ |
| 832 |
NSUInteger i; |
| 833 |
NSUInteger c; |
| 834 |
|
| 835 |
c = [self length]; |
| 836 |
|
| 837 |
for (i = c; i > 0; i--) |
| 838 |
{ |
| 839 |
if ([self characterAtIndex: i-1] == theSeparator) |
| 840 |
{ |
| 841 |
return [self substringToIndex: i-1]; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
return @""; |
| 846 |
} |
| 847 |
|
| 848 |
// |
| 849 |
// |
| 850 |
// |
| 851 |
- (NSString *) stringByDeletingFirstPathSeparator: (unichar) theSeparator |
| 852 |
{ |
| 853 |
if ([self length] && [self characterAtIndex: 0] == theSeparator) |
| 854 |
{ |
| 855 |
return [self substringFromIndex: 1]; |
| 856 |
} |
| 857 |
|
| 858 |
return self; |
| 859 |
} |
| 860 |
|
| 861 |
// |
| 862 |
// |
| 863 |
// |
| 864 |
- (BOOL) is7bitSafe |
| 865 |
{ |
| 866 |
NSUInteger i, len; |
| 867 |
|
| 868 |
// We search for a non-ASCII character. |
| 869 |
len = [self length]; |
| 870 |
|
| 871 |
for (i = 0; i < len; i++) |
| 872 |
{ |
| 873 |
if ([self characterAtIndex: i] > 0x007E) |
| 874 |
{ |
| 875 |
return NO; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
return YES; |
| 880 |
} |
| 881 |
|
| 882 |
// |
| 883 |
// |
| 884 |
// |
| 885 |
- (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart |
| 886 |
{ |
| 887 |
return [self dataUsingEncodingFromPart: thePart allowLossyConversion: NO]; |
| 888 |
} |
| 889 |
|
| 890 |
// |
| 891 |
// |
| 892 |
// |
| 893 |
- (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart |
| 894 |
allowLossyConversion: (BOOL) lossy |
| 895 |
{ |
| 896 |
#ifdef MACOSX |
| 897 |
// Use the CF decoding to get the data, bypassing Foundation... |
| 898 |
unsigned long enc; |
| 899 |
NSData *data; |
| 900 |
|
| 901 |
enc = [NSString encodingForPart: thePart convertToNSStringEncoding: NO]; |
| 902 |
data = (NSData *)CFStringCreateExternalRepresentation(NULL, (CFStringRef)self, |
| 903 |
(CFStringEncoding)enc, (lossy) ? '?' : 0); |
| 904 |
return [data autorelease]; |
| 905 |
#else |
| 906 |
return [self dataUsingEncoding: [object_getClass(self) |
| 907 |
encodingForPart: thePart] |
| 908 |
allowLossyConversion: lossy]; |
| 909 |
#endif |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
// |
| 914 |
// |
| 915 |
// |
| 916 |
- (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset |
| 917 |
{ |
| 918 |
return [self dataUsingEncodingWithCharset: theCharset allowLossyConversion: NO]; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
// |
| 923 |
// |
| 924 |
// |
| 925 |
- (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset |
| 926 |
allowLossyConversion: (BOOL)lossy |
| 927 |
{ |
| 928 |
#ifdef MACOSX |
| 929 |
// Use the CF decoding to get the data, bypassing Foundation... |
| 930 |
unsigned long enc; |
| 931 |
NSData *data; |
| 932 |
|
| 933 |
enc = [NSString encodingForCharset: [theCharset dataUsingEncoding: NSASCIIStringEncoding] |
| 934 |
convertToNSStringEncoding: NO]; |
| 935 |
data = (NSData *)CFStringCreateExternalRepresentation(NULL, (CFStringRef)self, |
| 936 |
(CFStringEncoding)enc, (lossy) ? '?' : 0); |
| 937 |
return [data autorelease]; |
| 938 |
#else |
| 939 |
return [self dataUsingEncoding: |
| 940 |
[object_getClass(self) |
| 941 |
encodingForCharset: |
| 942 |
[theCharset dataUsingEncoding: NSASCIIStringEncoding]] |
| 943 |
allowLossyConversion: lossy]; |
| 944 |
#endif |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
// |
| 949 |
// |
| 950 |
// |
| 951 |
+ (NSString *) stringFromRecipients: (NSArray *) theRecipients |
| 952 |
type: (PantomimeRecipientType) theRecipientType |
| 953 |
{ |
| 954 |
CWInternetAddress *anInternetAddress; |
| 955 |
NSMutableString *aMutableString; |
| 956 |
NSUInteger i, count; |
| 957 |
|
| 958 |
aMutableString = [[NSMutableString alloc] init]; |
| 959 |
count = [theRecipients count]; |
| 960 |
|
| 961 |
for (i = 0; i < count; i++) |
| 962 |
{ |
| 963 |
anInternetAddress = [theRecipients objectAtIndex: i]; |
| 964 |
|
| 965 |
if ([anInternetAddress type] == theRecipientType) |
| 966 |
{ |
| 967 |
[aMutableString appendFormat: @"%@, ", [anInternetAddress stringValue]]; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
return AUTORELEASE(aMutableString); |
| 972 |
} |
| 973 |
|
| 974 |
@end |