Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

univalue_utffilter.h
Go to the documentation of this file.
1 // Copyright 2016 Wladimir J. van der Laan
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef UNIVALUE_UTFFILTER_H
5 #define UNIVALUE_UTFFILTER_H
6 
7 #include <string>
8 
10 {
11 public:
12  explicit JSONUTF8StringFilter(std::string &s):
13  str(s), is_valid(true), codepoint(0), state(0), surpair(0)
14  {
15  }
16  // Write single 8-bit char (may be part of UTF-8 sequence)
17  void push_back(unsigned char ch)
18  {
19  if (state == 0) {
20  if (ch < 0x80) // 7-bit ASCII, fast direct pass-through
21  str.push_back(ch);
22  else if (ch < 0xc0) // Mid-sequence character, invalid in this state
23  is_valid = false;
24  else if (ch < 0xe0) { // Start of 2-byte sequence
25  codepoint = (ch & 0x1f) << 6;
26  state = 6;
27  } else if (ch < 0xf0) { // Start of 3-byte sequence
28  codepoint = (ch & 0x0f) << 12;
29  state = 12;
30  } else if (ch < 0xf8) { // Start of 4-byte sequence
31  codepoint = (ch & 0x07) << 18;
32  state = 18;
33  } else // Reserved, invalid
34  is_valid = false;
35  } else {
36  if ((ch & 0xc0) != 0x80) // Not a continuation, invalid
37  is_valid = false;
38  state -= 6;
39  codepoint |= (ch & 0x3f) << state;
40  if (state == 0)
42  }
43  }
44  // Write codepoint directly, possibly collating surrogate pairs
45  void push_back_u(unsigned int codepoint_)
46  {
47  // Only accept full codepoints in open state
48  if (state)
49  is_valid = false;
50  if (codepoint_ >= 0xD800 && codepoint_ < 0xDC00) { // First half of surrogate pair
51  if (surpair) // Two subsequent surrogate pair openers - fail
52  is_valid = false;
53  else
54  surpair = codepoint_;
55  } else if (codepoint_ >= 0xDC00 && codepoint_ < 0xE000) { // Second half of surrogate pair
56  if (surpair) { // Open surrogate pair, expect second half
57  // Compute code point from UTF-16 surrogate pair
58  append_codepoint(0x10000 | ((surpair - 0xD800)<<10) | (codepoint_ - 0xDC00));
59  surpair = 0;
60  } else // First half of surrogate pair not followed by second
61  is_valid = false;
62  } else {
63  if (surpair) // First half of surrogate pair not followed by second
64  is_valid = false;
65  else
66  append_codepoint(codepoint_);
67  }
68  }
69  // Check that we're in a state where the string can be ended
70  // No open sequences, no open surrogate pairs, etc
71  bool finalize()
72  {
73  if (state || surpair)
74  is_valid = false;
75  return is_valid;
76  }
77 private:
78  std::string &str;
79  bool is_valid;
80  // Current UTF-8 decoding state
81  unsigned int codepoint;
82  int state; // Top bit to be filled in for next UTF-8 byte, or 0
83  // Keep track of this state to handle the following section of RFC4627:
84  //
85  // To escape an extended character that is not in the Basic Multilingual
86  // Plane, the character is represented as a twelve-character sequence,
87  // encoding the UTF-16 surrogate pair. So, for example, a string
88  // containing only the G clef character (U+1D11E) may be represented as
89  // "\uD834\uDD1E".
90  //
91  // Two subsequent \u.... may have to be replaced with one actual codepoint.
92  unsigned int surpair; // First of UTF-16 surrogate pair
93 
94  void append_codepoint(unsigned int codepoint_)
95  {
96  if (codepoint_ <= 0x7f)
97  str.push_back((char)codepoint_);
98  else if (codepoint_ <= 0x7FF) {
99  str.push_back((char)(0xC0 | (codepoint_ >> 6)));
100  str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
101  } else if (codepoint_ <= 0xFFFF) {
102  str.push_back((char)(0xE0 | (codepoint_ >> 12)));
103  str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
104  str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
105  } else if (codepoint_ <= 0x1FFFFF) {
106  str.push_back((char)(0xF0 | (codepoint_ >> 18)));
107  str.push_back((char)(0x80 | ((codepoint_ >> 12) & 0x3F)));
108  str.push_back((char)(0x80 | ((codepoint_ >> 6) & 0x3F)));
109  str.push_back((char)(0x80 | (codepoint_ & 0x3F)));
110  }
111  }
112 };
113 
114 #endif
void append_codepoint(unsigned int codepoint_)
false true true true
Definition: bls_dkg.cpp:176
void push_back(unsigned char ch)
void push_back_u(unsigned int codepoint_)
JSONUTF8StringFilter(std::string &s)
Released under the MIT license