1 2 3 4 5 6 7 8 9 10 11 12 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
|
/*
** NSString+Extensions.m
**
** Copyright (c) 2001-2007
**
** Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Pantomime/NSString+Extensions.h>
#include <Pantomime/CWCharset.h>
#include <Pantomime/CWConstants.h>
#include <Pantomime/CWInternetAddress.h>
#include <Pantomime/CWPart.h>
#include <Pantomime/NSData+Extensions.h>
#include <Foundation/NSBundle.h>
//
// We include the CoreFoundation headers under Mac OS X so we can support
// more string encodings.
//
#ifdef MACOSX
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFStringEncodingExt.h>
#else
#include <GNUstepBase/Additions.h>
#endif
#include <ctype.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#define IS_PRINTABLE(c) (isascii(c) && isprint(c))
//
//
//
@implementation NSString (PantomimeStringExtensions)
#ifdef MACOSX
- (NSString *) stringByTrimmingWhiteSpaces
{
NSMutableString *aMutableString;
aMutableString = [[NSMutableString alloc] initWithString: self];
CFStringTrimWhitespace((CFMutableStringRef)aMutableString);
return AUTORELEASE(aMutableString);
}
#endif
//
//
//
- (int) indexOfCharacter: (unichar) theCharacter
{
return [self indexOfCharacter: theCharacter fromIndex: 0];
}
//
//
//
- (int) indexOfCharacter: (unichar) theCharacter
fromIndex: (unsigned int) theIndex
{
int i, len;
len = [self length];
for (i = theIndex; i < len; i++)
{
if ([self characterAtIndex: i] == theCharacter)
{
return i;
}
}
return -1;
}
//
//
//
- (BOOL) hasCaseInsensitivePrefix: (NSString *) thePrefix
{
if (thePrefix)
{
return [[self uppercaseString] hasPrefix: [thePrefix uppercaseString]];
}
return NO;
}
//
//
//
- (BOOL) hasCaseInsensitiveSuffix: (NSString *) theSuffix
{
if (theSuffix)
{
return [[self uppercaseString] hasSuffix: [theSuffix uppercaseString]];
}
return NO;
}
//
//
//
- (NSString *) stringFromQuotedString
{
int len;
len = [self length];
if (len > 1 &&
[self characterAtIndex: 0] == '"' &&
[self characterAtIndex: (len-1)] == '"')
{
return [self substringWithRange: NSMakeRange(1, len-2)];
}
return self;
}
//
//
//
+ (NSString *) stringValueOfTransferEncoding: (int) theEncoding
{
switch (theEncoding)
{
case PantomimeEncodingNone:
break;
case PantomimeEncodingQuotedPrintable:
return @"quoted-printable";
case PantomimeEncodingBase64:
return @"base64";
case PantomimeEncoding8bit:
return @"8bit";
case PantomimeEncodingBinary:
return @"binary";
default:
break;
}
// PantomimeEncoding7bit will also fall back here.
return @"7bit";
}
//
//
//
+ (int) encodingForCharset: (NSData *) theCharset
{
return [self encodingForCharset: theCharset convertToNSStringEncoding: YES];
}
//
// Convenience to be able to use CoreFoundation conversion instead of NSString
//
+ (int) encodingForCharset: (NSData *) theCharset
convertToNSStringEncoding: (BOOL) shouldConvert
{
// We define some aliases for the string encoding.
static struct { NSString *name; int encoding; BOOL fromCoreFoundation; } encodings[] = {
{@"ascii" ,NSASCIIStringEncoding ,NO},
{@"us-ascii" ,NSASCIIStringEncoding ,NO},
{@"default" ,NSASCIIStringEncoding ,NO}, // Ah... spammers.
{@"utf-8" ,NSUTF8StringEncoding ,NO},
{@"iso-8859-1" ,NSISOLatin1StringEncoding ,NO},
{@"x-user-defined",NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Outlook.
{@"unknown" ,NSISOLatin1StringEncoding ,NO}, // Once more, blame Outlook.
{@"x-unknown" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Pine 4.21.
{@"unknown-8bit" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Mutt/1.3.28i
{@"0" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in QUALCOMM Windows Eudora Version 6.0.1.1
{@"" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Ximian Evolution
{@"iso8859_1" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Openwave WebEngine
{@"iso-8859-2" ,NSISOLatin2StringEncoding ,NO},
#ifndef MACOSX
{@"iso-8859-3" ,NSISOLatin3StringEncoding ,NO},
{@"iso-8859-4" ,NSISOLatin4StringEncoding ,NO},
{@"iso-8859-5" ,NSISOCyrillicStringEncoding ,NO},
{@"iso-8859-6" ,NSISOArabicStringEncoding ,NO},
{@"iso-8859-7" ,NSISOGreekStringEncoding ,NO},
{@"iso-8859-8" ,NSISOHebrewStringEncoding ,NO},
{@"iso-8859-9" ,NSISOLatin5StringEncoding ,NO},
{@"iso-8859-10" ,NSISOLatin6StringEncoding ,NO},
{@"iso-8859-11" ,NSISOThaiStringEncoding ,NO},
{@"iso-8859-13" ,NSISOLatin7StringEncoding ,NO},
{@"iso-8859-14" ,NSISOLatin8StringEncoding ,NO},
{@"iso-8859-15" ,NSISOLatin9StringEncoding ,NO},
{@"koi8-r" ,NSKOI8RStringEncoding ,NO},
{@"big5" ,NSBIG5StringEncoding ,NO},
{@"gb2312" ,NSGB2312StringEncoding ,NO},
{@"utf-7" ,NSUTF7StringEncoding ,NO},
{@"unicode-1-1-utf-7", NSUTF7StringEncoding ,NO}, // To prever a bug (sort of) in MS Hotmail
#endif
{@"windows-1250" ,NSWindowsCP1250StringEncoding ,NO},
{@"windows-1251" ,NSWindowsCP1251StringEncoding ,NO},
{@"cyrillic (windows-1251)", NSWindowsCP1251StringEncoding ,NO}, // To prevent a bug in MS Hotmail
{@"windows-1252" ,NSWindowsCP1252StringEncoding ,NO},
{@"windows-1253" ,NSWindowsCP1253StringEncoding ,NO},
{@"windows-1254" ,NSWindowsCP1254StringEncoding ,NO},
{@"iso-2022-jp" ,NSISO2022JPStringEncoding ,NO},
{@"euc-jp" ,NSJapaneseEUCStringEncoding ,NO},
};
NSString *name;
int i;
name = [[NSString stringWithCString: [theCharset bytes] length: [theCharset length]] lowercaseString];
for (i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++)
{
if ([name isEqualToString: encodings[i].name])
{
int enc = encodings[i].encoding;
// Under OS X, we use CoreFoundation if necessary to convert the encoding
// to a NSString encoding.
#ifdef MACOSX
if (encodings[i].fromCoreFoundation)
{
if (shouldConvert)
{
return CFStringConvertEncodingToNSStringEncoding(enc);
}
else
{
return enc;
}
}
else
{
if (!shouldConvert)
{
return CFStringConvertNSStringEncodingToEncoding(enc);
}
else
{
return enc;
}
}
#else
return enc;
#endif
}
}
#ifdef MACOSX
// Last resort: try using CoreFoundation...
CFStringEncoding enc;
enc = CFStringConvertIANACharSetNameToEncoding((CFStringRef)name);
if (kCFStringEncodingInvalidId != enc)
{
if (shouldConvert)
{
return CFStringConvertEncodingToNSStringEncoding(enc);
}
else
{
return enc;
}
}
#endif
return -1;
}
//
//
//
+ (int) encodingForPart: (CWPart *) thePart
{
return [self encodingForPart: thePart convertToNSStringEncoding: YES];
}
//
// Convenience to be able to use CoreFoundation conversion instead of NSString
//
+ (int) encodingForPart: (CWPart *) thePart
convertToNSStringEncoding: (BOOL) shouldConvert
{
int encoding;
// We get the encoding we are gonna use. We always favor the default encoding.
if ([thePart defaultCharset])
{
encoding = [self encodingForCharset: [[thePart defaultCharset] dataUsingEncoding: NSASCIIStringEncoding]
convertToNSStringEncoding: shouldConvert];
}
else if ([thePart charset])
{
encoding = [self encodingForCharset: [[thePart charset] dataUsingEncoding: NSASCIIStringEncoding]
convertToNSStringEncoding: shouldConvert];
}
else
{
encoding = [NSString defaultCStringEncoding];
}
if (encoding == -1 || encoding == NSASCIIStringEncoding)
{
encoding = NSISOLatin1StringEncoding;
}
return encoding;
}
//
//
//
+ (NSString *) stringWithData: (NSData *) theData
charset: (NSData *) theCharset
{
int encoding;
if (theData == nil)
{
return nil;
}
#ifdef MACOSX
encoding = [NSString encodingForCharset: theCharset
convertToNSStringEncoding: NO];
#else
encoding = [NSString encodingForCharset: theCharset];
#endif
if (encoding == -1)
{
#ifdef HAVE_ICONV
NSString *aString;
const char *i_bytes, *from_code;
char *o_bytes;
size_t i_length, o_length, ret;
int total_length;
iconv_t conv;
// Instead of calling cString directly on theCharset, we first try
// to obtain the ASCII string of the data object.
from_code = [[theCharset asciiString] cString];
if (!from_code)
{
return nil;
}
conv = iconv_open("UTF-8", from_code);
if (conv == (iconv_t) -1)
{
// Let's assume we got US-ASCII here.
return AUTORELEASE([[NSString alloc] initWithData: theData encoding: NSASCIIStringEncoding]);
}
i_bytes = [theData bytes];
i_length = [theData length];
total_length = o_length = sizeof(unichar)*i_length;
o_bytes = (char *)malloc(o_length);
if (o_bytes == NULL) return nil;
while (i_length > 0)
{
ret = iconv(conv, (char **)&i_bytes, &i_length, &o_bytes, &o_length);
if (ret == (size_t)-1)
{
iconv_close(conv);
total_length = total_length - o_length;
o_bytes -= total_length;
free(o_bytes);
return nil;
}
}
total_length = total_length - o_length;
o_bytes -= total_length;
// If we haven't used all our allocated buffer, we shrink it.
if (o_length > 0 && total_length > 0)
{
if (realloc(o_bytes, total_length) == NULL)
{
NSLog(@"stringWithData, realloc() failed, returning nil");
iconv_close(conv);
return nil;
}
}
aString = [[NSString alloc] initWithData: [NSData dataWithBytesNoCopy: o_bytes
length: total_length]
encoding: NSUTF8StringEncoding];
iconv_close(conv);
return AUTORELEASE(aString);
#else
return nil;
#endif
}
#ifdef MACOSX
return AUTORELEASE((NSString *)CFStringCreateFromExternalRepresentation(NULL, (CFDataRef)theData, encoding));
#else
return AUTORELEASE([[NSString alloc] initWithData: theData encoding: encoding]);
#endif
}
//
//
//
#warning return Charset instead?
- (NSString *) charset
{
NSMutableArray *aMutableArray;
NSString *aString;
CWCharset *aCharset;
unsigned int i, j;
aMutableArray = [[NSMutableArray alloc] initWithCapacity: 21];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-1"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-2"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-3"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-4"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-5"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-6"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-7"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-8"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-9"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-10"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-11"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-13"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-14"]];
[aMutableArray addObject: [CWCharset charsetForName: @"iso-8859-15"]];
[aMutableArray addObject: [CWCharset charsetForName: @"koi8-r"]];
[aMutableArray addObject: [CWCharset charsetForName: @"koi8-u"]];
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1250"]];
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1251"]];
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1252"]];
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1253"]];
[aMutableArray addObject: [CWCharset charsetForName: @"windows-1254"]];
for (i = 0; i < [self length]; i++)
{
for (j = 0; j < [aMutableArray count]; j++)
{
if (![[aMutableArray objectAtIndex: j] characterIsInCharset: [self characterAtIndex: i]])
{
// Character is not in the charset
[aMutableArray removeObjectAtIndex: j];
j--;
}
}
// FIXME: can't break even if there is only one left. First we have to check
// whether that encoding will actually work for the entire string. If it
// doesn't we'll need to fall back to utf-8 (or something else that can encode
// _everything_).
//
// Intelligent string splitting would help, of course
//
if ([aMutableArray count] < 1)
{
// We have zero or one charset
break;
}
}
if ([aMutableArray count])
{
aCharset = [aMutableArray objectAtIndex: 0];
[aMutableArray removeAllObjects];
aString = [aCharset name];
}
else
{
// We have no charset, we try to "guess" a default charset
if ([self canBeConvertedToEncoding: NSISO2022JPStringEncoding])
{
// ISO-2022-JP is the standard of Japanese character encoding
aString = @"iso-2022-jp";
}
else
{
// We have no charset, we return a default charset
aString = @"utf-8";
}
}
RELEASE(aMutableArray);
return aString;
}
//
//
//
- (NSString *) modifiedUTF7String
{
#ifndef MACOSX
NSMutableData *aMutableData, *modifiedData;
NSString *aString;
const char *b;
BOOL escaped;
unichar ch;
int i, len;
//
// We UTF-7 encode _only_ the non-ASCII parts.
//
aMutableData = [[NSMutableData alloc] init];
AUTORELEASE(aMutableData);
len = [self length];
for (i = 0; i < len; i++)
{
ch = [self characterAtIndex: i];
if (IS_PRINTABLE(ch))
{
[aMutableData appendCFormat: @"%c", ch];
}
else
{
int j;
j = i+1;
// We got a non-ASCII character, let's get the substring and encode it using UTF-7.
while (j < len && !IS_PRINTABLE([self characterAtIndex: j]))
{
j++;
}
// Get the substring.
[aMutableData appendData: [[self substringWithRange: NSMakeRange(i,j-i)] dataUsingEncoding: NSUTF7StringEncoding]];
i = j-1;
}
}
b = [aMutableData bytes];
len = [aMutableData length];
escaped = NO;
//
// We replace:
//
// & -> &-
// + -> &
// +- -> +
// / -> ,
//
// in order to produce our modified UTF-7 string.
//
modifiedData = [[NSMutableData alloc] init];
AUTORELEASE(modifiedData);
for (i = 0; i < len; i++, b++)
{
if (!escaped && *b == '&')
{
[modifiedData appendCString: "&-"];
}
else if (!escaped && *b == '+')
{
if (*(b+1) == '-')
{
[modifiedData appendCString: "+"];
}
else
{
[modifiedData appendCString: "&"];
// We enter the escaped mode.
escaped = YES;
}
}
else if (escaped && *b == '/')
{
[modifiedData appendCString: ","];
}
else if (escaped && *b == '-')
{
[modifiedData appendCString: "-"];
// We leave the escaped mode.
escaped = NO;
}
else
{
[modifiedData appendCFormat: @"%c", *b];
}
}
// If we're still in the escaped mode we haven't added our trailing -,
// let's add it right now.
if (escaped)
{
[modifiedData appendCString: "-"];
}
aString = AUTORELEASE([[NSString alloc] initWithData: modifiedData encoding: NSASCIIStringEncoding]);
return (aString != nil ? aString : self);
#else
return self;
#endif
}
//
//
//
- (NSString *) stringFromModifiedUTF7
{
#ifndef MACOSX
NSMutableData *aMutableData;
BOOL escaped;
unichar ch;
int i, len;
aMutableData = [[NSMutableData alloc] init];
AUTORELEASE(aMutableData);
len = [self length];
escaped = NO;
//
// We replace:
//
// & -> +
// &- -> &
// , -> /
//
// If we are in escaped mode. That is, between a &....-
//
for (i = 0; i < len; i++)
{
ch = [self characterAtIndex: i];
if (!escaped && ch == '&')
{
if ( (i+1) < len && [self characterAtIndex: (i+1)] != '-' )
{
[aMutableData appendCString: "+"];
// We enter the escaped mode.
escaped = YES;
}
else
{
// We replace &- by &
[aMutableData appendCString: "&"];
i++;
}
}
else if (escaped && ch == ',')
{
[aMutableData appendCString: "/"];
}
else if (escaped && ch == '-')
{
[aMutableData appendCString: "-"];
// We leave the escaped mode.
escaped = NO;
}
else
{
[aMutableData appendCFormat: @"%c", ch];
}
}
return AUTORELEASE([[NSString alloc] initWithData: aMutableData encoding: NSUTF7StringEncoding]);
#else
return nil;
#endif
}
//
//
//
- (BOOL) hasREPrefix
{
if ([self hasCaseInsensitivePrefix: @"re:"] ||
[self hasCaseInsensitivePrefix: @"re :"] ||
[self hasCaseInsensitivePrefix: _(@"PantomimeReferencePrefix")] ||
[self hasCaseInsensitivePrefix: _(@"PantomimeResponsePrefix")])
{
return YES;
}
return NO;
}
//
//
//
- (NSString *) stringByReplacingOccurrencesOfCharacter: (unichar) theTarget
withCharacter: (unichar) theReplacement
{
NSMutableString *aMutableString;
int len, i;
unichar c;
if (!theTarget || !theReplacement || theTarget == theReplacement)
{
return self;
}
len = [self length];
aMutableString = [NSMutableString stringWithCapacity: len];
for (i = 0; i < len; i++)
{
c = [self characterAtIndex: i];
if (c == theTarget)
{
[aMutableString appendFormat: @"%c", theReplacement];
}
else
{
[aMutableString appendFormat: @"%c", c];
}
}
return aMutableString;
}
//
//
//
- (NSString *) stringByDeletingLastPathComponentWithSeparator: (unsigned char) theSeparator
{
int i, c;
c = [self length];
for (i = c-1; i >= 0; i--)
{
if ([self characterAtIndex: i] == theSeparator)
{
return [self substringToIndex: i];
}
}
return @"";
}
//
//
//
- (NSString *) stringByDeletingFirstPathSeparator: (unsigned char) theSeparator
{
if ([self length] && [self characterAtIndex: 0] == theSeparator)
{
return [self substringFromIndex: 1];
}
return self;
}
//
//
//
- (BOOL) is7bitSafe
{
int i, len;
// We search for a non-ASCII character.
len = [self length];
for (i = 0; i < len; i++)
{
if ([self characterAtIndex: i] > 0x007E)
{
return NO;
}
}
return YES;
}
//
//
//
- (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart
{
return [self dataUsingEncodingFromPart: thePart allowLossyConversion: NO];
}
//
//
//
- (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart
allowLossyConversion: (BOOL) lossy
{
#ifdef MACOSX
// Use the CF decoding to get the data, bypassing Foundation...
CFStringEncoding enc;
NSData *data;
enc = [isa encodingForPart: thePart convertToNSStringEncoding: NO];
data = (NSData *)CFStringCreateExternalRepresentation(NULL, (CFStringRef)self,
enc, (lossy) ? '?' : 0);
return [data autorelease];
#else
return [self dataUsingEncoding: [object_getClass(self)
encodingForPart: thePart]
allowLossyConversion: lossy];
#endif
}
//
//
//
- (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset
{
return [self dataUsingEncodingWithCharset: theCharset allowLossyConversion: NO];
}
//
//
//
- (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset
allowLossyConversion: (BOOL)lossy
{
#ifdef MACOSX
// Use the CF decoding to get the data, bypassing Foundation...
CFStringEncoding enc;
NSData *data;
enc = [isa encodingForCharset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]
convertToNSStringEncoding: NO];
data = (NSData *)CFStringCreateExternalRepresentation(NULL, (CFStringRef)self,
enc, (lossy) ? '?' : 0);
return [data autorelease];
#else
return [self dataUsingEncoding:
[object_getClass(self)
encodingForCharset:
[theCharset dataUsingEncoding: NSASCIIStringEncoding]]
allowLossyConversion: lossy];
#endif
}
//
//
//
+ (NSString *) stringFromRecipients: (NSArray *) theRecipients
type: (PantomimeRecipientType) theRecipientType
{
CWInternetAddress *anInternetAddress;
NSMutableString *aMutableString;
int i, count;
aMutableString = [[NSMutableString alloc] init];
count = [theRecipients count];
for (i = 0; i < count; i++)
{
anInternetAddress = [theRecipients objectAtIndex: i];
if ([anInternetAddress type] == theRecipientType)
{
[aMutableString appendFormat: @"%@, ", [anInternetAddress stringValue]];
}
}
return AUTORELEASE(aMutableString);
}
@end
|