UnboundedQueue: Add LgAlign template parameter - Refactor code
[folly.git] / folly / Unicode.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/Unicode.h>
18 #include <folly/Conv.h>
19
20 namespace folly {
21
22 //////////////////////////////////////////////////////////////////////
23
24 std::string codePointToUtf8(char32_t cp) {
25   std::string result;
26
27   // Based on description from http://en.wikipedia.org/wiki/UTF-8.
28
29   if (cp <= 0x7f) {
30     result.resize(1);
31     result[0] = static_cast<char>(cp);
32   } else if (cp <= 0x7FF) {
33     result.resize(2);
34     result[1] = static_cast<char>(0x80 | (0x3f & cp));
35     result[0] = static_cast<char>(0xC0 | (cp >> 6));
36   } else if (cp <= 0xFFFF) {
37     result.resize(3);
38     result[2] = static_cast<char>(0x80 | (0x3f & cp));
39     result[1] = (0x80 | static_cast<char>((0x3f & (cp >> 6))));
40     result[0] = (0xE0 | static_cast<char>(cp >> 12));
41   } else if (cp <= 0x10FFFF) {
42     result.resize(4);
43     result[3] = static_cast<char>(0x80 | (0x3f & cp));
44     result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
45     result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
46     result[0] = static_cast<char>(0xF0 | (cp >> 18));
47   }
48
49   return result;
50 }
51
52
53 char32_t utf8ToCodePoint(
54     const unsigned char*& p,
55     const unsigned char* const e,
56     bool skipOnError) {
57   /* The following encodings are valid, except for the 5 and 6 byte
58    * combinations:
59    * 0xxxxxxx
60    * 110xxxxx 10xxxxxx
61    * 1110xxxx 10xxxxxx 10xxxxxx
62    * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
63    * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
64    * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
65    */
66
67   auto skip = [&] { ++p; return U'\ufffd'; };
68
69   if (p >= e) {
70     if (skipOnError) {
71       return skip();
72     }
73     throw std::runtime_error("folly::utf8ToCodePoint empty/invalid string");
74   }
75
76   unsigned char fst = *p;
77   if (!(fst & 0x80)) {
78     // trivial case
79     return *p++;
80   }
81
82   static const uint32_t bitMask[] = {
83     (1 << 7) - 1,
84     (1 << 11) - 1,
85     (1 << 16) - 1,
86     (1 << 21) - 1
87   };
88
89   // upper control bits are masked out later
90   uint32_t d = fst;
91
92   if ((fst & 0xC0) != 0xC0) {
93     if (skipOnError) {
94       return skip();
95     }
96     throw std::runtime_error(to<std::string>("folly::utf8ToCodePoint i=0 d=", d));
97   }
98
99   fst <<= 1;
100
101   for (unsigned int i = 1; i != 3 && p + i < e; ++i) {
102     unsigned char tmp = p[i];
103
104     if ((tmp & 0xC0) != 0x80) {
105       if (skipOnError) {
106         return skip();
107       }
108       throw std::runtime_error(
109         to<std::string>("folly::utf8ToCodePoint i=", i, " tmp=", (uint32_t)tmp));
110     }
111
112     d = (d << 6) | (tmp & 0x3F);
113     fst <<= 1;
114
115     if (!(fst & 0x80)) {
116       d &= bitMask[i];
117
118       // overlong, could have been encoded with i bytes
119       if ((d & ~bitMask[i - 1]) == 0) {
120         if (skipOnError) {
121           return skip();
122         }
123         throw std::runtime_error(
124           to<std::string>("folly::utf8ToCodePoint i=", i, " d=", d));
125       }
126
127       // check for surrogates only needed for 3 bytes
128       if (i == 2) {
129         if ((d >= 0xD800 && d <= 0xDFFF) || d > 0x10FFFF) {
130           if (skipOnError) {
131             return skip();
132           }
133           throw std::runtime_error(
134             to<std::string>("folly::utf8ToCodePoint i=", i, " d=", d));
135         }
136       }
137
138       p += i + 1;
139       return d;
140     }
141   }
142
143   if (skipOnError) {
144     return skip();
145   }
146   throw std::runtime_error("folly::utf8ToCodePoint encoding length maxed out");
147 }
148
149 //////////////////////////////////////////////////////////////////////
150
151 } // namespace folly