Re: Translating accented characters to HTML entity equivalents
Re: Translating accented characters to HTML entity equivalents
- Subject: Re: Translating accented characters to HTML entity equivalents
- From: Aki Inoue <email@hidden>
- Date: Thu, 13 Mar 2003 15:38:27 -0800
Phil,
As others already mentioned, @"" format only supports 7bit ASCII.
The sample attached is not terribly efficient but should give you the
basic idea.
Aki
static NSString *ISO8859_1_Table[] = {
@" ", // " "
@"¡", // "¡"
@"¢", // "¢"
@"£", // "£"
@"¤", // "¤"
@"¥", // "¥"
@"¦", // "¦"
@"§", // "§"
@"¨", // "¨"
@"©", // "©"
@"ª", // "ª"
@"«", // "«"
@"¬", // "¬"
@"­", // "­"
@"®", // "®"
@"¯", // "¯"
@"°", // "°"
@"±", // "±"
@"²", // "²"
@"³", // "³"
@"´", // "´"
@"µ", // "µ"
@"¶", // "¶"
@"·", // "·"
@"¸", // "¸"
@"¹", // "¹"
@"º", // "º"
@"»", // "»"
@"¼", // "¼"
@"½", // "½"
@"¾", // "¾"
@"¿", // "¿"
@"À", // "À"
@"Á", // "Á"
@"Â", // "Â"
@"Ã", // "Ã"
@"Ä", // "Ä"
@"Å", // "Å"
@"Æ", // "Æ"
@"Ç", // "Ç"
@"È", // "È"
@"É", // "É"
@"Ê", // "Ê"
@"Ë", // "Ë"
@"Ì", // "Ì"
@"Í", // "Í"
@"Î", // "Î"
@"Ï", // "Ï"
@"Ð", // "Ð"
@"Ñ", // "Ñ"
@"Ò", // "Ò"
@"Ó", // "Ó"
@"Ô", // "Ô"
@"Õ", // "Õ"
@"Ö", // "Ö"
@"×", // "×"
@"Ø", // "Ø"
@"Ù", // "Ù"
@"Ú", // "Ú"
@"Û", // "Û"
@"Ü", // "Ü"
@"Ý", // "Ý"
@"Þ", // "Þ"
@"ß", // "ß"
@"à", // "à"
@"á", // "á"
@"â", // "â"
@"ã", // "ã"
@"ä", // "ä"
@"å", // "å"
@"æ", // "æ"
@"ç", // "ç"
@"è", // "è"
@"é", // "é"
@"ê", // "ê"
@"ë", // "ë"
@"ì", // "ì"
@"í", // "í"
@"î", // "î"
@"ï", // "ï"
@"ð", // "ð"
@"ñ", // "ñ"
@"ò", // "ò"
@"ó", // "ó"
@"ô", // "ô"
@"õ", // "õ"
@"ö", // "ö"
@"÷", // "÷"
@"ø", // "ø"
@"ù", // "ù"
@"ú", // "ú"
@"û", // "û"
@"ü", // "ü"
@"ý", // "ý"
@"þ", // "þ"
@"ÿ", // "ÿ"
};
static NSString *createStringWithCharacterReference(NSString *string) {
NSMutableString *mString = nil;
NSString *result = string;
NSString *entityString;
NSRange currentRange = NSMakeRange(0, 0);
unsigned int stringLength = [string length];
unsigned int currentIndex = 0;
UniChar character;
while (currentIndex < stringLength) {
character = [string characterAtIndex:currentIndex];
if (character < 0xA0) { // ASCII & C1
switch (character) {
case '"': entityString = @"""; break;
case '&': entityString = @"&"; break;
case '<': entityString = @"<"; break;
case '>': entityString = @">"; break;
default: entityString = nil;
}
} else if (character < 0x100) { // ISO 8859-1
entityString = ISO8859_1_Table[character - 0xA0];
} else if (character < 0xFFFE) { // Rest of Unicode
entityString = [NSString stringWithFormat:@"#x%X",
character];
} else {
entityString = @""; // UFFFE & UFFFF shouldn't be in HTML
}
if (entityString) {
if (!mString) {
mString = [NSMutableString string];
result = mString;
}
if (currentRange.location < currentIndex) {
currentRange.length = currentIndex -
currentRange.location;
[mString appendString:[string
substringWithRange:currentRange]];
}
[mString appendString:entityString];
currentRange.location = currentIndex + 1;
}
++currentIndex;
}
// The remaining run
if (mString && (currentRange.location < currentIndex)) {
currentRange.length = currentIndex - currentRange.location;
[mString appendString:[string substringWithRange:currentRange]];
}
return result;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.