aboutsummaryrefslogtreecommitdiff
path: root/lout/unicode.cc
blob: 7d2502dcb14b0c5c66f91a05e473fffc5c12b6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "unicode.hh"
#include "misc.hh"

using namespace lout::misc;

namespace lout {

namespace unicode {

static unsigned char alpha[0x500] = {
   // 0000-007F: C0 Controls and Basic Latin
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
   // 0080-00FF: C1 Controls and Latin-1 Supplement
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff,
   // 0100-017F: Latin Extended-A
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   // 0180-024F: Latin Extended-B
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff,
   // 0250–02AF: IPA Extensions
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff,
   // 02B0–02FF: Spacing Modifier Letters
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00,
   // 0300–036F: Combining Diacritical Marks
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   // 0370–03FF: Greek and Coptic
   0xcf, 0x00, 0x40, 0x7d, 0xff, 0xff, 0xfb, 0xff,
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff,
   // 0400–04FF: Cyrillic
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0x03, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

/**
 * Returns whether a given unicode character is an alphabetic character.
 */
bool isAlpha (int ch)
{
   return ch < 0x500 && (alpha[ch / 8] & (1 << (ch & 7)));
}

int decodeUtf8 (const char *s)
{
   if((s[0] & 0x80) == 0)
      return s[0];
   else {
      int mask = 0xe0, bits = 0xc0, done = 0, ch = 0;
      for(int j = 1; !done && j < 7;
          j++, mask = 0x80 | (mask >> 1), bits = 0x80 | (bits >> 1)) {
         if(((unsigned char)s[0] & mask) == bits) {
            done = 1;
            ch = (unsigned char)s[0] & ~mask & 0xff;
            for(int k = 0; k < j; k++)
               ch = (ch << 6) | ((unsigned char)s[k + 1] & 0x3f);
         }
      }

      return ch;
   }
}

static const char *_nextUtf8Char (const char *s)
{
   if (s == NULL)
      return NULL;

   const char *r;
   if((s[0] & 0x80) == 0)
      r = s + 1;
   else {
      int mask = 0xe0, bits = 0xc0, done = 0;
      for(int j = 1; !done && j < 7;
          j++, mask = 0x80 | (mask >> 1), bits = 0x80 | (bits >> 1)) {
         if(((unsigned char)s[0] & mask) == bits) {
            done = 1;
            r = s + j + 1;
         }
      }

      if(!done) {
         assertNotReached();
         return NULL;
      }
   }

   return r;
}

const char *nextUtf8Char (const char *s)
{
   const char *r = _nextUtf8Char (s);
   if (r != NULL && r[0] == 0)
      return NULL;
   else
      return r;
}

const char *nextUtf8Char (const char *s, int len)
{
   if (len <= 0)
      return NULL;
   
   const char *r = _nextUtf8Char (s);
   if (r != NULL && r - s >= len)
      return NULL;
   else
      return r;
}

} // namespace lout

} // namespace unicode