2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/Unicode.h"
21 //////////////////////////////////////////////////////////////////////
23 fbstring codePointToUtf8(char32_t cp) {
26 // Based on description from http://en.wikipedia.org/wiki/UTF-8.
30 result[0] = static_cast<char>(cp);
31 } else if (cp <= 0x7FF) {
33 result[1] = static_cast<char>(0x80 | (0x3f & cp));
34 result[0] = static_cast<char>(0xC0 | (cp >> 6));
35 } else if (cp <= 0xFFFF) {
37 result[2] = static_cast<char>(0x80 | (0x3f & cp));
38 result[1] = (0x80 | static_cast<char>((0x3f & (cp >> 6))));
39 result[0] = (0xE0 | static_cast<char>(cp >> 12));
40 } else if (cp <= 0x10FFFF) {
42 result[3] = static_cast<char>(0x80 | (0x3f & cp));
43 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
44 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
45 result[0] = static_cast<char>(0xF0 | (cp >> 18));
51 //////////////////////////////////////////////////////////////////////