Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Utf16.hpp Source File

Utf16.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 #include <stdint.h>  // uint16_t, uint32_t
00010 
00011 // The high surrogate may be uninitialized if the pair is invalid,
00012 // we choose to ignore the problem to reduce the size of the code
00013 // Garbage in => Garbage out
00014 #if defined(__GNUC__)
00015 #if __GNUC__ >= 7
00016 #pragma GCC diagnostic push
00017 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
00018 #endif
00019 #endif
00020 
00021 namespace ARDUINOJSON_NAMESPACE {
00022 
00023 namespace Utf16 {
00024 inline bool isHighSurrogate(uint16_t codeunit) {
00025   return codeunit >= 0xD800 && codeunit < 0xDC00;
00026 }
00027 
00028 inline bool isLowSurrogate(uint16_t codeunit) {
00029   return codeunit >= 0xDC00 && codeunit < 0xE000;
00030 }
00031 
00032 class Codepoint {
00033  public:
00034   Codepoint() : _highSurrogate(0) {}
00035 
00036   bool append(uint16_t codeunit) {
00037     if (isHighSurrogate(codeunit)) {
00038       _highSurrogate = codeunit & 0x3FF;
00039       return false;
00040     }
00041 
00042     if (isLowSurrogate(codeunit)) {
00043       _codepoint =
00044           uint32_t(0x10000 + ((_highSurrogate << 10) | (codeunit & 0x3FF)));
00045       return true;
00046     }
00047 
00048     _codepoint = codeunit;
00049     return true;
00050   }
00051 
00052   uint32_t value() const {
00053     return _codepoint;
00054   }
00055 
00056  private:
00057   uint16_t _highSurrogate;
00058   uint32_t _codepoint;
00059 };
00060 }  // namespace Utf16
00061 }  // namespace ARDUINOJSON_NAMESPACE
00062 
00063 #if defined(__GNUC__)
00064 #if __GNUC__ >= 8
00065 #pragma GCC diagnostic pop
00066 #endif
00067 #endif