freetype-commit
[Top][All Lists]
Advanced

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

[Git][freetype/freetype-demos][master] * src/common.c (utf8_next): Use m


From: Alexei Podtelezhnikov (@apodtele)
Subject: [Git][freetype/freetype-demos][master] * src/common.c (utf8_next): Use more efficient algorithm.
Date: Tue, 16 Aug 2022 02:55:44 +0000

Alexei Podtelezhnikov pushed to branch master at FreeType / FreeType Demo Programs

Commits:

  • 2e32dc9f
    by Alexei Podtelezhnikov at 2022-08-15T22:51:11-04:00
    * src/common.c (utf8_next): Use more efficient algorithm.
    

1 changed file:

Changes:

  • src/common.c
    ... ... @@ -77,49 +77,31 @@
    77 77
       {
    
    78 78
         const unsigned char*  p = (const unsigned char*)*pcursor;
    
    79 79
         int                   ch;
    
    80
    +    int                   mask = 0x80;  /* the first decision bit */
    
    80 81
     
    
    81 82
     
    
    82
    -    if ( (const char*)p >= end ) /* end of stream */
    
    83
    -      return -1;
    
    83
    +    if ( (const char*)p >= end || ( *p & 0xc0 ) == 0x80 )
    
    84
    +      goto BAD_DATA;
    
    84 85
     
    
    85 86
         ch = *p++;
    
    86
    -    if ( ch >= 0x80 )
    
    87
    -    {
    
    88
    -      int  len;
    
    89
    -
    
    90 87
     
    
    91
    -      if ( ch < 0xc0 )  /* malformed data */
    
    92
    -        goto BAD_DATA;
    
    93
    -      else if ( ch < 0xe0 )
    
    94
    -      {
    
    95
    -        len = 1;
    
    96
    -        ch &= 0x1f;
    
    97
    -      }
    
    98
    -      else if ( ch < 0xf0 )
    
    99
    -      {
    
    100
    -        len = 2;
    
    101
    -        ch &= 0x0f;
    
    102
    -      }
    
    103
    -      else
    
    104
    -      {
    
    105
    -        len = 3;
    
    106
    -        ch &= 0x07;
    
    107
    -      }
    
    88
    +    if ( ch & mask )
    
    89
    +    {
    
    90
    +      mask = 0x40;
    
    108 91
     
    
    109
    -      while ( len > 0 )
    
    92
    +      do
    
    110 93
           {
    
    111
    -        if ( (const char*)p >= end || ( p[0] & 0xc0 ) != 0x80 )
    
    94
    +        if ( (const char*)p >= end || ( *p & 0xc0 ) != 0x80 )
    
    112 95
               goto BAD_DATA;
    
    113 96
     
    
    114
    -        ch   = ( ch << 6 ) | ( p[0] & 0x3f );
    
    115
    -        p   += 1;
    
    116
    -        len -= 1;
    
    117
    -      }
    
    97
    +        ch     = ( ch << 6 ) | ( *p++ & 0x3f );
    
    98
    +        mask <<= 5;  /* the next decision bit after shift */
    
    99
    +      } while ( ch & mask && mask <= 0x200000 );
    
    118 100
         }
    
    119 101
     
    
    120 102
         *pcursor = (const char*)p;
    
    121 103
     
    
    122
    -    return ch;
    
    104
    +    return ch & ( mask - 1 );  /* dropping the decision bits */
    
    123 105
     
    
    124 106
       BAD_DATA:
    
    125 107
         return -1;
    


  • reply via email to

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