Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Utf8.hpp Source File

Utf8.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Namespace.hpp>
00008 
00009 namespace ARDUINOJSON_NAMESPACE {
00010 
00011 namespace Utf8 {
00012 template <typename TStringBuilder>
00013 inline void encodeCodepoint(uint32_t codepoint32, TStringBuilder& str) {
00014   // this function was optimize for code size on AVR
00015 
00016   // a buffer to store the string in reverse
00017   char buf[5];
00018   char* p = buf;
00019 
00020   *(p++) = 0;
00021   if (codepoint32 < 0x80) {
00022     *(p++) = char((codepoint32));
00023   } else {
00024     *(p++) = char((codepoint32 | 0x80) & 0xBF);
00025     uint16_t codepoint16 = uint16_t(codepoint32 >> 6);
00026     if (codepoint16 < 0x20) {  // 0x800
00027       *(p++) = char(codepoint16 | 0xC0);
00028     } else {
00029       *(p++) = char((codepoint16 | 0x80) & 0xBF);
00030       codepoint16 = uint16_t(codepoint16 >> 6);
00031       if (codepoint16 < 0x10) {  // 0x10000
00032         *(p++) = char(codepoint16 | 0xE0);
00033       } else {
00034         *(p++) = char((codepoint16 | 0x80) & 0xBF);
00035         codepoint16 = uint16_t(codepoint16 >> 6);
00036         *(p++) = char(codepoint16 | 0xF0);
00037       }
00038     }
00039   }
00040 
00041   while (*(--p)) {
00042     str.append(*p);
00043   }
00044 }
00045 }  // namespace Utf8
00046 }  // namespace ARDUINOJSON_NAMESPACE