mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 00:11:26 +00:00
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:
parent
0ceff0e920
commit
6a347fee68
1 changed files with 17 additions and 12 deletions
|
@ -2857,27 +2857,32 @@ handle_printf_atsign (FILE *stream,
|
|||
*/
|
||||
- (const char*) cStringUsingEncoding: (NSStringEncoding)encoding
|
||||
{
|
||||
NSData *d;
|
||||
NSMutableData *m;
|
||||
|
||||
d = [self dataUsingEncoding: encoding allowLossyConversion: NO];
|
||||
if (d == nil)
|
||||
if (NSUnicodeStringEncoding == encoding)
|
||||
{
|
||||
[NSException raise: NSCharacterConversionException
|
||||
format: @"unable to convert to cString"];
|
||||
}
|
||||
m = [d mutableCopy];
|
||||
if (encoding == NSUnicodeStringEncoding)
|
||||
{
|
||||
unichar c = 0;
|
||||
unichar *u;
|
||||
unsigned l;
|
||||
|
||||
[m appendBytes: &c length: 2];
|
||||
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];
|
||||
if (d == nil)
|
||||
{
|
||||
[NSException raise: NSCharacterConversionException
|
||||
format: @"unable to convert to cString"];
|
||||
}
|
||||
m = [[d mutableCopy] autorelease];
|
||||
[m appendBytes: "" length: 1];
|
||||
}
|
||||
IF_NO_GC([m autorelease];)
|
||||
return (const char*)[m bytes];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue