Author: rfm
Date: Thu Jun 23 20:27:22 2016
New Revision: 39915

URL: http://svn.gna.org/viewcvs/gnustep?rev=39915&view=rev
Log:
Optimise fetching ascii or utf8 from literal string

Modified:
    libs/base/trunk/ChangeLog
    libs/base/trunk/Source/GSString.m
    libs/base/trunk/Source/NSString.m

Modified: libs/base/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/ChangeLog?rev=39915&r1=39914&r2=39915&view=diff
==============================================================================
--- libs/base/trunk/ChangeLog   (original)
+++ libs/base/trunk/ChangeLog   Thu Jun 23 20:27:22 2016
@@ -1,3 +1,8 @@
+2016-06-23  Richard Frith-Macdonald <r...@gnu.org>
+
+       * Source/GSString.m: Optimise getting ascii or utf8 characters
+       from a literal string.
+
 2016-06-22  Richard Frith-Macdonald <r...@gnu.org>
 
        * Source/NSObject.m: Make sure we treat the reference count as a

Modified: libs/base/trunk/Source/GSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/GSString.m?rev=39915&r1=39914&r2=39915&view=diff
==============================================================================
--- libs/base/trunk/Source/GSString.m   (original)
+++ libs/base/trunk/Source/GSString.m   Thu Jun 23 20:27:22 2016
@@ -5916,6 +5916,67 @@
     }
 }
 
+- (BOOL) getCString: (char*)buffer
+         maxLength: (NSUInteger)maxLength
+          encoding: (NSStringEncoding)encoding
+{
+  const uint8_t *ptr = (const uint8_t*)nxcsptr;
+  int           length = nxcslen;
+  int           index;
+
+  if (0 == maxLength || 0 == buffer)
+    {
+      return NO;       // Can't fit in here
+    }
+  if (NSUTF8StringEncoding == encoding)
+    {
+      /* We are already using UTF-8 so we can just copy directly.
+       */
+      if (maxLength <= length)
+        {
+          length = maxLength - 1;
+        }
+      for (index = 0; index < length; index++)
+        {
+          buffer[index] = (char)ptr[index];
+        }
+      /* Step back before any multibyte sequence
+       */
+      while (index > 0 && (ptr[index - 1] & 0x80))
+       {
+          index--;
+        }
+      buffer[index] = '\0';
+      return YES;
+    }
+  else if (isByteEncoding(encoding))
+    {
+      /* We want a single-byte encoding (ie ascii is a subset),
+       * so as long as this constant string is ascii, we can just
+       * copy directly.
+       */
+      if (maxLength <= length)
+        {
+          length = maxLength - 1;
+        }
+      for (index = 0; index < length; index++)
+        {
+          buffer[index] = (char)ptr[index];
+          if (ptr[index] & 0x80)
+            {
+              break;    // Not ascii 
+            }
+        }
+      if (index == length)
+        {
+          buffer[index] = '\0';
+          return YES;                   // All copied.
+        }
+      // Fall through to use superclass method.
+    }
+  return [super getCString: buffer maxLength: maxLength encoding: encoding];
+}
+
 /* Must match the implementation in NSString
  * To avoid allocating memory, we build the hash incrementally.
  */

Modified: libs/base/trunk/Source/NSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSString.m?rev=39915&r1=39914&r2=39915&view=diff
==============================================================================
--- libs/base/trunk/Source/NSString.m   (original)
+++ libs/base/trunk/Source/NSString.m   Thu Jun 23 20:27:22 2016
@@ -3665,6 +3665,7 @@
          maxLength: (NSUInteger)maxLength
           encoding: (NSStringEncoding)encoding
 {
+  if (0 == maxLength || 0 == buffer) return NO;
   if (encoding == NSUnicodeStringEncoding)
     {
       unsigned length = [self length];


_______________________________________________
Gnustep-cvs mailing list
Gnustep-cvs@gna.org
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to