Small optimisation for creating NSString from UTF8 C string

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38539 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2015-05-25 11:11:24 +00:00
parent 5f0cf74552
commit 1fbfe0cdf6
3 changed files with 70 additions and 3 deletions

View file

@ -1411,15 +1411,20 @@ static NSMapTable *nodeNames = 0;
- (NSString*) objectForKey: (NSString*)key
{
NSString *value = nil;
const char *str = 0;
xmlAttrPtr prop;
prop = ((xmlNodePtr)(lib))->properties;
while (prop != NULL)
{
const void *name = prop->name;
NSString *n = UTF8Str(name);
if ([key isEqualToString: n] == YES)
if (0 == str)
{
str = [key UTF8String];
}
if (strcmp(str, name) == 0)
{
xmlNodePtr child = prop->children;

View file

@ -1671,6 +1671,61 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
return (id)me;
}
- (id) initWithUTF8String: (const char*)bytes
{
BOOL ascii = YES;
NSUInteger length;
GSStr me;
if (0 == bytes)
{
return (id)@"";
}
/* Skip leading BOM
*/
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
{
bytes = &bytes[3];
}
if (0 == bytes[0])
{
return (id)@"";
}
for (length = 0; bytes[length]; length++)
{
if (bytes[length] > 127)
{
ascii = NO;
}
}
if (YES == ascii)
{
me = (GSStr)newCInline(length, [self zone]);
memcpy(me->_contents.c, bytes, length);
return (id)me;
}
else
{
const unsigned char *b = (const unsigned char*)bytes;
NSZone *z = [self zone];
unichar *u = 0;
unsigned l = 0;
if (GSToUnicode(&u, &l, b, length, NSUTF8StringEncoding, z, 0) == NO)
{
return nil; // Invalid data
}
me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, z);
me->_contents.u = u;
me->_count = l;
me->_flags.wide = 1;
me->_flags.owned = YES;
}
return (id)me;
}
- (NSUInteger) length
{
[NSException raise: NSInternalInconsistencyException

View file

@ -981,7 +981,14 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale)
if (NULL == bytes)
[NSException raise: NSInvalidArgumentException
format: @"[NSString+stringWithUTF8String:]: NULL cString"];
obj = [self allocWithZone: NSDefaultMallocZone()];
if (self == NSStringClass)
{
obj = defaultPlaceholderString;
}
else
{
obj = [self allocWithZone: NSDefaultMallocZone()];
}
obj = [obj initWithUTF8String: bytes];
return AUTORELEASE(obj);
}