This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/server/json_partial.cpp
T
2026-10-04 12:02:41 +02:00

179 lines
5.5 KiB
C++

#include "json_partial.h"
namespace rad {
namespace server {
namespace {
struct Scanner {
std::string_view s;
size_t i = 0;
void ws() {
while (i < s.size() && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')) ++i;
}
bool eof() const { return i >= s.size(); }
char peek() const { return s[i]; }
bool lit(std::string_view w) {
if (s.size() - i < w.size()) return false;
if (s.compare(i, w.size(), w) != 0) return false;
i += w.size();
return true;
}
bool str() {
if (eof() || s[i] != '"') return false;
++i;
while (!eof()) {
char c = s[i];
if (c == '\\') {
if (i + 1 >= s.size()) return false;
char e = s[i + 1];
if (e == 'u') {
if (i + 5 >= s.size()) return false;
i += 6;
} else {
i += 2;
}
continue;
}
if (c == '"') { ++i; return true; }
++i;
}
return false; /* unterminated */
}
bool num() {
size_t start = i;
if (!eof() && s[i] == '-') ++i;
size_t d0 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d0) { i = start; return false; }
if (!eof() && s[i] == '.') {
++i;
size_t d1 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d1) { i = start; return false; }
}
if (!eof() && (s[i] == 'e' || s[i] == 'E')) {
++i;
if (!eof() && (s[i] == '+' || s[i] == '-')) ++i;
size_t d2 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d2) { i = start; return false; }
}
return true;
}
bool value(int depth) {
/* A hard depth cap, because the input is model output and a runaway "[[[[[..." would
* otherwise recurse until the stack ends. 256 is far past any real tool schema. */
if (depth > 256) return false;
ws();
if (eof()) return false;
char c = peek();
if (c == '"') return str();
if (c == '{') return obj(depth + 1);
if (c == '[') return arr(depth + 1);
if (c == 't') return lit("true");
if (c == 'f') return lit("false");
if (c == 'n') return lit("null");
return num();
}
bool obj(int depth) {
++i; /* '{' */
ws();
if (eof()) return false;
if (peek() == '}') { ++i; return true; }
for (;;) {
ws();
if (!str()) return false;
ws();
if (eof() || peek() != ':') return false;
++i;
if (!value(depth)) return false;
ws();
if (eof()) return false;
if (peek() == ',') { ++i; continue; }
if (peek() == '}') { ++i; return true; }
return false;
}
}
bool arr(int depth) {
++i; /* '[' */
ws();
if (eof()) return false;
if (peek() == ']') { ++i; return true; }
for (;;) {
if (!value(depth)) return false;
ws();
if (eof()) return false;
if (peek() == ',') { ++i; continue; }
if (peek() == ']') { ++i; return true; }
return false;
}
}
};
} /* namespace */
bool json_complete(std::string_view s) {
Scanner sc{s};
if (!sc.value(0)) return false;
sc.ws();
return sc.eof();
}
size_t utf8_trunc(std::string_view s) {
size_t n = s.size();
if (n == 0) return 0;
/* Walk back over at most three continuation bytes to find the lead byte, then decide whether
* the sequence it starts is complete. */
size_t k = n;
size_t back = 0;
while (k > 0 && back < 4) {
unsigned char c = (unsigned char)s[k - 1];
if ((c & 0xC0) == 0x80) { --k; ++back; continue; }
size_t need = 1;
if ((c & 0x80) == 0x00) need = 1;
else if ((c & 0xE0) == 0xC0) need = 2;
else if ((c & 0xF0) == 0xE0) need = 3;
else if ((c & 0xF8) == 0xF0) need = 4;
else return n; /* not a lead byte: not our truncation to fix */
size_t have = n - (k - 1);
return have >= need ? n : k - 1;
}
return n;
}
size_t json_stable_prefix(std::string_view s) {
size_t n = utf8_trunc(s);
if (n == 0) return 0;
/* Back out of an unfinished escape. Count the run of backslashes immediately before the cut:
* an odd run means the last one is an opening escape whose payload has not arrived. */
size_t bs = 0;
while (bs < n && s[n - 1 - bs] == '\\') ++bs;
if (bs % 2 == 1) return n - 1;
/* Back out of an unfinished "\uXXXX". `k` is how many characters have arrived after the 'u',
* so the escape is complete at k == 4 and short before it. Only six bytes ever need looking
* at, which is why this is a loop and not a parser. */
for (size_t k = 0; k <= 4 && n >= k + 2; ++k) {
if (s[n - 1 - k] != 'u') continue;
if (s[n - 2 - k] != '\\') break;
size_t bs2 = 0, j = n - 2 - k;
while (j > 0 && s[j - 1] == '\\') { ++bs2; --j; }
if (bs2 % 2 == 1) break; /* the backslash was itself escaped */
if (k < 4) return n - 2 - k; /* fewer than four hex digits have arrived */
break;
}
return n;
}
} /* namespace server */
} /* namespace rad */