Re: stringWithQuotedPrintableString almost perfect
Re: stringWithQuotedPrintableString almost perfect
- Subject: Re: stringWithQuotedPrintableString almost perfect
- From: "Stephen J. Butler" <email@hidden>
- Date: Fri, 20 Aug 2010 00:07:10 -0500
On Thu, Aug 19, 2010 at 9:27 PM, Brad Stone <email@hidden> wrote:
> Can someone help me figure out why (brackets not included) [• m] (which is, on the Mac, an option-8 character, a tab character and a lowercase m) converts to (brackets not included [‚Ä¢ m] ?
>
> The source text is quoted-printable UTF-8 text I created and saved in an XML file in a different application. All the other characters and line returns translate perfectly but this option-8 tab combination does not.
>
> Here is my source code.
I refactored your code (it's how I debug), and it works for me. One
big thing I noticed is that you never zero the memory you get from
malloc(). Bad, bad, bad, especially considering that you later call
strlen() on the buffer. You also don't allocate a buffer that is large
enough. If your string contains no quoted printable characters,
there's no room for the null on the end. I changed it to a calloc()
call and adjusted the size.
char *cStringPtr, *cString = "=E2=80=A2=09m";
char *cBufferPtr, *cBuffer = calloc( strlen( cString ) + 1, 1 );
cStringPtr = cString;
cBufferPtr = cBuffer;
while (*cStringPtr) {
switch (*cStringPtr) {
case '=':
NSAssert1( cStringPtr[ 1 ] != 0, @"Premature end in
quoted printable string: %s", cString );
if (cStringPtr[ 1 ] == '\r') {
*(cBuffer++) = '\r';
cStringPtr += 2;
} else {
char byte[ 2 ] = { 0 };
NSAssert1( cStringPtr[ 2 ] != 0, @"Premature end
in quoted printable string: %s", cString );
for (int i = 0; i < 2; ++i) {
NSAssert2(
((cStringPtr[ i + 1 ] >= '0') &&
(cStringPtr[ i + 1 ] <= '9')) ||
((cStringPtr[ i + 1 ] >= 'A') &&
(cStringPtr[ i + 1 ] <= 'F')),
@"Invalid character '%c' in quoted
printable string: %s",
cStringPtr[ i + 1 ],
cString
);
byte[ i ] = cStringPtr[ i + 1 ];
byte[ i ] -= isdigit( byte[ i ] ) ? '0' : ('A' - 10);
}
*(cBufferPtr++) = (byte[ 0 ] << 4) | byte[ 1 ];
cStringPtr += 3;
}
break;
default:
*(cBufferPtr++) = *(cStringPtr++);
break;
}
}
NSLog( @"buffer = %s", cBuffer );
NSString *result = [[[NSString alloc] initWithBytesNoCopy:cBuffer
length:strlen( cBuffer ) encoding:NSUTF8StringEncoding
freeWhenDone:YES] autorelease];
NSLog( @"result = %@", result );
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden