1 | // Copyright 2017 The Abseil Authors. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | // UTF8 utilities, implemented to reduce dependencies. |
16 | |
17 | #include "absl/strings/internal/utf8.h" |
18 | |
19 | namespace absl { |
20 | namespace strings_internal { |
21 | |
22 | size_t EncodeUTF8Char(char *buffer, char32_t utf8_char) { |
23 | if (utf8_char <= 0x7F) { |
24 | *buffer = static_cast<char>(utf8_char); |
25 | return 1; |
26 | } else if (utf8_char <= 0x7FF) { |
27 | buffer[1] = 0x80 | (utf8_char & 0x3F); |
28 | utf8_char >>= 6; |
29 | buffer[0] = 0xC0 | utf8_char; |
30 | return 2; |
31 | } else if (utf8_char <= 0xFFFF) { |
32 | buffer[2] = 0x80 | (utf8_char & 0x3F); |
33 | utf8_char >>= 6; |
34 | buffer[1] = 0x80 | (utf8_char & 0x3F); |
35 | utf8_char >>= 6; |
36 | buffer[0] = 0xE0 | utf8_char; |
37 | return 3; |
38 | } else { |
39 | buffer[3] = 0x80 | (utf8_char & 0x3F); |
40 | utf8_char >>= 6; |
41 | buffer[2] = 0x80 | (utf8_char & 0x3F); |
42 | utf8_char >>= 6; |
43 | buffer[1] = 0x80 | (utf8_char & 0x3F); |
44 | utf8_char >>= 6; |
45 | buffer[0] = 0xF0 | utf8_char; |
46 | return 4; |
47 | } |
48 | } |
49 | |
50 | } // namespace strings_internal |
51 | } // namespace absl |
52 | |