mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-01 09:02:01 +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
|
- (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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue