tweak getting cString with specific encoding for unicode

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33987 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2011-10-14 13:19:43 +00:00
parent 0ceff0e920
commit 6a347fee68

View file

@ -2857,27 +2857,32 @@ handle_printf_atsign (FILE *stream,
*/ */
- (const char*) cStringUsingEncoding: (NSStringEncoding)encoding - (const char*) cStringUsingEncoding: (NSStringEncoding)encoding
{ {
NSData *d;
NSMutableData *m; NSMutableData *m;
if (NSUnicodeStringEncoding == encoding)
{
unichar *u;
unsigned l;
l = [self length];
m = [NSMutableData dataWithLength: l + 1];
u = (unichar*)[m mutableBytes];
[self getCharacters: u];
u[l] = 0;
}
else
{
NSData *d;
d = [self dataUsingEncoding: encoding allowLossyConversion: NO]; d = [self dataUsingEncoding: encoding allowLossyConversion: NO];
if (d == nil) if (d == nil)
{ {
[NSException raise: NSCharacterConversionException [NSException raise: NSCharacterConversionException
format: @"unable to convert to cString"]; format: @"unable to convert to cString"];
} }
m = [d mutableCopy]; m = [[d mutableCopy] autorelease];
if (encoding == NSUnicodeStringEncoding)
{
unichar c = 0;
[m appendBytes: &c length: 2];
}
else
{
[m appendBytes: "" length: 1]; [m appendBytes: "" length: 1];
} }
IF_NO_GC([m autorelease];)
return (const char*)[m bytes]; return (const char*)[m bytes];
} }