Author: rfm
Date: Mon May 25 13:11:24 2015
New Revision: 38539

URL: http://svn.gna.org/viewcvs/gnustep?rev=38539&view=rev
Log:
Small optimisation for creating NSString from UTF8 C string

Modified:
    libs/base/trunk/Source/Additions/GSXML.m
    libs/base/trunk/Source/GSString.m
    libs/base/trunk/Source/NSString.m

Modified: libs/base/trunk/Source/Additions/GSXML.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/Additions/GSXML.m?rev=38539&r1=38538&r2=38539&view=diff
==============================================================================
--- libs/base/trunk/Source/Additions/GSXML.m    (original)
+++ libs/base/trunk/Source/Additions/GSXML.m    Mon May 25 13:11:24 2015
@@ -1411,15 +1411,20 @@
 - (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;
 

Modified: libs/base/trunk/Source/GSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/GSString.m?rev=38539&r1=38538&r2=38539&view=diff
==============================================================================
--- libs/base/trunk/Source/GSString.m   (original)
+++ libs/base/trunk/Source/GSString.m   Mon May 25 13:11:24 2015
@@ -1667,6 +1667,61 @@
        */
       me = (GSStr)newUInline(length, [self zone]);
       [string getCharacters: me->_contents.u];
+    }
+  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;
 }

Modified: libs/base/trunk/Source/NSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSString.m?rev=38539&r1=38538&r2=38539&view=diff
==============================================================================
--- libs/base/trunk/Source/NSString.m   (original)
+++ libs/base/trunk/Source/NSString.m   Mon May 25 13:11:24 2015
@@ -981,7 +981,14 @@
   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);
 }


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to