freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] master 2e32dc9: * src/common.c (utf8_next): Use more e


From: Werner Lemberg
Subject: [freetype2-demos] master 2e32dc9: * src/common.c (utf8_next): Use more efficient algorithm.
Date: Mon, 15 Aug 2022 22:55:53 -0400 (EDT)

branch: master
commit 2e32dc9fe86f26e59f37f37a13ad0f7d1105ad9f
Author: Alexei Podtelezhnikov <apodtele@gmail.com>
Commit: Alexei Podtelezhnikov <apodtele@gmail.com>

    * src/common.c (utf8_next): Use more efficient algorithm.
---
 src/common.c | 42 ++++++++++++------------------------------
 1 file changed, 12 insertions(+), 30 deletions(-)

diff --git a/src/common.c b/src/common.c
index 509c5fa..e830def 100644
--- a/src/common.c
+++ b/src/common.c
@@ -77,49 +77,31 @@
   {
     const unsigned char*  p = (const unsigned char*)*pcursor;
     int                   ch;
+    int                   mask = 0x80;  /* the first decision bit */
 
 
-    if ( (const char*)p >= end ) /* end of stream */
-      return -1;
+    if ( (const char*)p >= end || ( *p & 0xc0 ) == 0x80 )
+      goto BAD_DATA;
 
     ch = *p++;
-    if ( ch >= 0x80 )
-    {
-      int  len;
-
 
-      if ( ch < 0xc0 )  /* malformed data */
-        goto BAD_DATA;
-      else if ( ch < 0xe0 )
-      {
-        len = 1;
-        ch &= 0x1f;
-      }
-      else if ( ch < 0xf0 )
-      {
-        len = 2;
-        ch &= 0x0f;
-      }
-      else
-      {
-        len = 3;
-        ch &= 0x07;
-      }
+    if ( ch & mask )
+    {
+      mask = 0x40;
 
-      while ( len > 0 )
+      do
       {
-        if ( (const char*)p >= end || ( p[0] & 0xc0 ) != 0x80 )
+        if ( (const char*)p >= end || ( *p & 0xc0 ) != 0x80 )
           goto BAD_DATA;
 
-        ch   = ( ch << 6 ) | ( p[0] & 0x3f );
-        p   += 1;
-        len -= 1;
-      }
+        ch     = ( ch << 6 ) | ( *p++ & 0x3f );
+        mask <<= 5;  /* the next decision bit after shift */
+      } while ( ch & mask && mask <= 0x200000 );
     }
 
     *pcursor = (const char*)p;
 
-    return ch;
+    return ch & ( mask - 1 );  /* dropping the decision bits */
 
   BAD_DATA:
     return -1;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]