Re: String->HTML
Re: String->HTML
- Subject: Re: String->HTML
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 27 Sep 2005 22:08:15 -0700
On Sep 27, 2005, at 9:53 PM, Jeff LaMarche wrote:
On Sep 27, 2005, at 6:18 PM, Scott Ribe wrote:
Is there any method anywhere to take an arbitrary string and get
it back as
HTML-encoded entities. *Not* URL encoding, but & to & > to
> and so
on. I can write this, but it just seems like something that ought
be there
somewhere. (Strings are 99.99% guaranteed to be plain ASCII;
possibility of
high-ASCII MacRoman encoding is so rare that I can ignore it; no
other
encodings need be supported.)
Here's a method that Clark S. Cox posted to the list a few years
ago and I've kept in my snippets folder... I think it's what you're
looking for:
NSString *EncodeForHTML(NSString *input)
{
unsigned length = [input length];
unichar *buffer = malloc(length * sizeof *buffer);
if(buffer)
{
NSMutableString *result = [NSMutableString string];
for(unsigned i=0; i<length; ++i)
{
if(buffer[i] >= 'a' && buffer[i] <= 'z' ||
buffer[i] >= 'A' && buffer[i] <= 'Z' ||
buffer[i] >= '0' && buffer[i] <= '9')
{
[result appendFormat: @"%c", buffer[i]];
}
else
{
[result appendFormat: @"&#x%x;", buffer[i]];
}
}
free(buffer);
return result;
}
return nil;
}
???
This function looks like a no-op to me. It's scanning newly-
allocated memory. Did you leave out a line or two?
-jcr
John C. Randolph <email@hidden> (408) 914-0013
Roaming Cocoa Engineer,
Available for your projects at great Expense and Inconvenience.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden