aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

rune.c (5001B)


      1 /* MIT/X consortium copyright (c) 2012 connor lane smith <cls@lubutu.com>
      2  *
      3  * permission is hereby granted, free of charge, to any person obtaining a
      4  * copy of this software and associated documentation files (the "software"),
      5  * to deal in the software without restriction, including without limitation
      6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      7  * and/or sell copies of the software, and to permit persons to whom the
      8  * software is furnished to do so, subject to the following conditions:
      9  *
     10  * the above copyright notice and this permission notice shall be included in
     11  * all copies or substantial portions of the software
     12  *
     13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15  * FITNESS FOR a PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     16  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     19  * DEALINGS IN THE SOFTWARE
     20  */
     21 #include "../utf.h"
     22 
     23 #define MIN(x, y) ((x) < (y) ? (x) : (y))
     24 
     25 #define UTFSEQ(x)                                                                                  \
     26   ((((x) & 0x80) == 0x00)   ? 1 /* 0xxxxxxx */                                                     \
     27    : (((x) & 0xC0) == 0x80) ? 0 /* 10xxxxxx */                                                     \
     28    : (((x) & 0xE0) == 0xC0) ? 2 /* 110xxxxx */                                                     \
     29    : (((x) & 0xF0) == 0xE0) ? 3 /* 1110xxxx */                                                     \
     30    : (((x) & 0xF8) == 0xF0) ? 4 /* 11110xxx */                                                     \
     31    : (((x) & 0xFC) == 0xF8) ? 5 /* 111110xx */                                                     \
     32    : (((x) & 0xFE) == 0xFC) ? 6 /* 1111110x */                                                     \
     33                             : 0)
     34 
     35 #define BADRUNE(x)                                                                                 \
     36   ((x) < 0 || (x) > Runemax || ((x) & 0xFFFE) == 0xFFFE || ((x) >= 0xD800 && (x) <= 0xDFFF)        \
     37    || ((x) >= 0xFDD0 && (x) <= 0xFDEF))
     38 
     39 int
     40 runetochar(char *s, const Rune *p)
     41 {
     42   Rune r = *p;
     43 
     44   switch (runelen(r)) {
     45     case 1: /* 0aaaaaaa */
     46       s[0] = r;
     47       return 1;
     48     case 2:                                /* 00000aaa aabbbbbb */
     49       s[0] = 0xC0 | ((r & 0x0007C0) >> 6); /* 110aaaaa */
     50       s[1] = 0x80 | (r & 0x00003F);        /* 10bbbbbb */
     51       return 2;
     52     case 3:                                 /* aaaabbbb bbcccccc */
     53       s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
     54       s[1] = 0x80 | ((r & 0x000FC0) >> 6);  /* 10bbbbbb */
     55       s[2] = 0x80 | (r & 0x00003F);         /* 10cccccc */
     56       return 3;
     57     case 4:                                 /* 000aaabb bbbbcccc ccdddddd */
     58       s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
     59       s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
     60       s[2] = 0x80 | ((r & 0x000FC0) >> 6);  /* 10cccccc */
     61       s[3] = 0x80 | (r & 0x00003F);         /* 10dddddd */
     62       return 4;
     63     default:
     64       return 0; /* error */
     65   }
     66 }
     67 
     68 int
     69 chartorune(Rune *p, const char *s)
     70 {
     71   return charntorune(p, s, UTFmax);
     72 }
     73 
     74 int
     75 charntorune(Rune *p, const char *s, size_t len)
     76 {
     77   unsigned int i, n;
     78   Rune         r;
     79 
     80   if (len == 0) /* cant even look at s[0] */
     81     return 0;
     82 
     83   switch ((n = UTFSEQ(s[0]))) {
     84     case 1:
     85       r = s[0];
     86       break; /* 0xxxxxxx */
     87     case 2:
     88       r = s[0] & 0x1F;
     89       break; /* 110xxxxx */
     90     case 3:
     91       r = s[0] & 0x0F;
     92       break; /* 1110xxxx */
     93     case 4:
     94       r = s[0] & 0x07;
     95       break; /* 11110xxx */
     96     case 5:
     97       r = s[0] & 0x03;
     98       break; /* 111110xx */
     99     case 6:
    100       r = s[0] & 0x01;
    101       break; /* 1111110x */
    102     default: /* invalid sequence */
    103       *p = Runeerror;
    104       return 1;
    105   }
    106   /* add values from continuation bytes */
    107   for (i = 1; i < MIN(n, len); i++)
    108     if ((s[i] & 0xC0) == 0x80) {
    109       /* add bits from continuation byte to rune value
    110        * cannot overflow: 6 byte sequences contain 31 bits */
    111       r = (r << 6) | (s[i] & 0x3F); /* 10xxxxxx */
    112     } else {                        /* expected continuation */
    113       *p = Runeerror;
    114       return i;
    115     }
    116 
    117   if (i < n) /* must have reached len limit */
    118     return 0;
    119 
    120   /* reject invalid or overlong sequences */
    121   if (BADRUNE(r) || runelen(r) < (int)n)
    122     r = Runeerror;
    123 
    124   *p = r;
    125   return n;
    126 }
    127 
    128 int
    129 runelen(Rune r)
    130 {
    131   if (BADRUNE(r))
    132     return 0; /* error */
    133   else if (r <= 0x7F)
    134     return 1;
    135   else if (r <= 0x07FF)
    136     return 2;
    137   else if (r <= 0xFFFF)
    138     return 3;
    139   else
    140     return 4;
    141 }
    142 
    143 size_t
    144 runenlen(const Rune *p, size_t len)
    145 {
    146   size_t i, n = 0;
    147 
    148   for (i = 0; i < len; i++)
    149     n += runelen(p[i]);
    150   return n;
    151 }
    152 
    153 int
    154 fullrune(const char *s, size_t len)
    155 {
    156   Rune r;
    157 
    158   return charntorune(&r, s, len) > 0;
    159 }