+// Fast path to determine the longest prefix that can be left
+// unescaped in a string of sizeof(T) bytes packed in an integer of
+// type T.
+template <class T>
+size_t firstEscapableInWord(T s) {
+ static_assert(std::is_unsigned<T>::value, "Unsigned integer required");
+ static constexpr T kOnes = ~T() / 255; // 0x...0101
+ static constexpr T kMsbs = kOnes * 0x80; // 0x...8080
+
+ // Sets the MSB of bytes < b. Precondition: b < 128.
+ auto isLess = [](T w, uint8_t b) {
+ // A byte is < b iff subtracting b underflows, so we check that
+ // the MSB wasn't set before and it's set after the subtraction.
+ return (w - kOnes * b) & ~w & kMsbs;
+ };
+
+ auto isChar = [&](uint8_t c) {
+ // A byte is == c iff it is 0 if xored with c.
+ return isLess(s ^ (kOnes * c), 1);
+ };
+
+ // The following masks have the MSB set for each byte of the word
+ // that satisfies the corresponding condition.
+ auto isHigh = s & kMsbs; // >= 128
+ auto isLow = isLess(s, 0x20); // <= 0x1f
+ auto needsEscape = isHigh | isLow | isChar('\\') | isChar('"');
+
+ if (!needsEscape) {
+ return sizeof(T);
+ }
+
+ if (folly::kIsLittleEndian) {
+ return folly::findFirstSet(needsEscape) / 8 - 1;
+ } else {
+ return sizeof(T) - folly::findLastSet(needsEscape) / 8;
+ }
+}
+
+// Escape a string so that it is legal to print it in JSON text.
+void escapeString(
+ StringPiece input,
+ std::string& out,
+ const serialization_opts& opts) {
+ auto hexDigit = [] (uint8_t c) -> char {
+ return c < 10 ? c + '0' : c - 10 + 'a';
+ };
+
+ out.push_back('\"');
+
+ auto* p = reinterpret_cast<const unsigned char*>(input.begin());
+ auto* q = reinterpret_cast<const unsigned char*>(input.begin());
+ auto* e = reinterpret_cast<const unsigned char*>(input.end());
+
+ while (p < e) {
+ // Find the longest prefix that does not need escaping, and copy
+ // it literally into the output string.
+ auto firstEsc = p;
+ while (firstEsc < e) {
+ auto avail = e - firstEsc;
+ uint64_t word = 0;
+ if (avail >= 8) {
+ word = folly::loadUnaligned<uint64_t>(firstEsc);
+ } else {
+ memcpy(static_cast<void*>(&word), firstEsc, avail);
+ }
+ auto prefix = firstEscapableInWord(word);
+ DCHECK_LE(prefix, avail);
+ firstEsc += prefix;
+ if (prefix < 8) {
+ break;
+ }
+ }
+ if (firstEsc > p) {
+ out.append(reinterpret_cast<const char*>(p), firstEsc - p);
+ p = firstEsc;
+ // We can't be in the middle of a multibyte sequence, so we can reset q.
+ q = p;
+ if (p == e) {
+ break;
+ }
+ }
+
+ // Handle the next byte that may need escaping.
+
+ // Since non-ascii encoding inherently does utf8 validation
+ // we explicitly validate utf8 only if non-ascii encoding is disabled.
+ if ((opts.validate_utf8 || opts.skip_invalid_utf8)
+ && !opts.encode_non_ascii) {
+ // To achieve better spatial and temporal coherence
+ // we do utf8 validation progressively along with the
+ // string-escaping instead of two separate passes.
+
+ // As the encoding progresses, q will stay at or ahead of p.
+ CHECK_GE(q, p);
+
+ // As p catches up with q, move q forward.
+ if (q == p) {
+ // calling utf8_decode has the side effect of
+ // checking that utf8 encodings are valid
+ char32_t v = utf8ToCodePoint(q, e, opts.skip_invalid_utf8);
+ if (opts.skip_invalid_utf8 && v == U'\ufffd') {
+ out.append(u8"\ufffd");
+ p = q;
+ continue;
+ }
+ }
+ }
+ if (opts.encode_non_ascii && (*p & 0x80)) {
+ // note that this if condition captures utf8 chars
+ // with value > 127, so size > 1 byte
+ char32_t v = utf8ToCodePoint(p, e, opts.skip_invalid_utf8);
+ char buf[] = "\\u\0\0\0\0";
+ buf[2] = hexDigit(uint8_t(v >> 12));
+ buf[3] = hexDigit((v >> 8) & 0x0f);
+ buf[4] = hexDigit((v >> 4) & 0x0f);
+ buf[5] = hexDigit(v & 0x0f);
+ out.append(buf, 6);
+ } else if (*p == '\\' || *p == '\"') {
+ char buf[] = "\\\0";
+ buf[1] = char(*p++);
+ out.append(buf, 2);
+ } else if (*p <= 0x1f) {
+ switch (*p) {
+ case '\b': out.append("\\b"); p++; break;
+ case '\f': out.append("\\f"); p++; break;
+ case '\n': out.append("\\n"); p++; break;
+ case '\r': out.append("\\r"); p++; break;
+ case '\t': out.append("\\t"); p++; break;
+ default:
+ // Note that this if condition captures non readable chars
+ // with value < 32, so size = 1 byte (e.g control chars).
+ char buf[] = "\\u00\0\0";
+ buf[4] = hexDigit(uint8_t((*p & 0xf0) >> 4));
+ buf[5] = hexDigit(uint8_t(*p & 0xf));
+ out.append(buf, 6);
+ p++;
+ }
+ } else {
+ out.push_back(char(*p++));
+ }
+ }
+
+ out.push_back('\"');
+}
+
+std::string stripComments(StringPiece jsonC) {
+ std::string result;
+ enum class State {
+ None,
+ InString,
+ InlineComment,
+ LineComment
+ } state = State::None;
+
+ for (size_t i = 0; i < jsonC.size(); ++i) {
+ auto s = jsonC.subpiece(i);
+ switch (state) {
+ case State::None:
+ if (s.startsWith("/*")) {
+ state = State::InlineComment;
+ ++i;
+ continue;
+ } else if (s.startsWith("//")) {
+ state = State::LineComment;
+ ++i;
+ continue;
+ } else if (s[0] == '\"') {
+ state = State::InString;
+ }
+ result.push_back(s[0]);
+ break;
+ case State::InString:
+ if (s[0] == '\\') {
+ if (UNLIKELY(s.size() == 1)) {
+ throw std::logic_error("Invalid JSONC: string is not terminated");
+ }
+ result.push_back(s[0]);
+ result.push_back(s[1]);
+ ++i;
+ continue;
+ } else if (s[0] == '\"') {
+ state = State::None;
+ }
+ result.push_back(s[0]);
+ break;
+ case State::InlineComment:
+ if (s.startsWith("*/")) {
+ state = State::None;
+ ++i;
+ }
+ break;
+ case State::LineComment:
+ if (s[0] == '\n') {
+ // skip the line break. It doesn't matter.
+ state = State::None;
+ }
+ break;
+ default:
+ throw std::logic_error("Unknown comment state");
+ }
+ }
+ return result;
+}
+