1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #include "upb/lex/unicode.h" 9 10 // Must be last. 11 #include "upb/port/def.inc" 12 upb_Unicode_ToUTF8(uint32_t cp,char * out)13int upb_Unicode_ToUTF8(uint32_t cp, char* out) { 14 if (cp <= 0x7f) { 15 out[0] = cp; 16 return 1; 17 } 18 if (cp <= 0x07ff) { 19 out[0] = (cp >> 6) | 0xc0; 20 out[1] = (cp & 0x3f) | 0x80; 21 return 2; 22 } 23 if (cp <= 0xffff) { 24 out[0] = (cp >> 12) | 0xe0; 25 out[1] = ((cp >> 6) & 0x3f) | 0x80; 26 out[2] = (cp & 0x3f) | 0x80; 27 return 3; 28 } 29 if (cp <= 0x10ffff) { 30 out[0] = (cp >> 18) | 0xf0; 31 out[1] = ((cp >> 12) & 0x3f) | 0x80; 32 out[2] = ((cp >> 6) & 0x3f) | 0x80; 33 out[3] = (cp & 0x3f) | 0x80; 34 return 4; 35 } 36 return 0; 37 } 38