39#define C0_VERSION "0.2.0"
98#define C0_WARN_NO_SYN 0x01u
99#define C0_WARN_NO_EOT 0x02u
100#define C0_WARN_UNKNOWN_KEY 0x04u
101#define C0_WARN_DUPLICATE_KEY 0x08u
102#define C0_WARN_UNKNOWN_SHAPE 0x10u
103#define C0_WARN_UNKNOWN_VERSION 0x20u
710#ifdef C0_IMPLEMENTATION
717static const unsigned char c0__assigned[32] = {
718 0, 1, 1, 1, 1, 1, 0, 1,
719 0, 0, 0, 0, 0, 0, 0, 0,
720 1, 0, 0, 0, 0, 0, 1, 1,
721 1, 1, 1, 0, 1, 1, 1, 1
724static const unsigned char c0__text_escaped[32] = {
725 0, 0, 1, 1, 1, 0, 0, 0,
726 0, 0, 0, 0, 0, 0, 0, 0,
727 1, 0, 0, 0, 0, 0, 1, 1,
728 0, 1, 0, 0, 0, 0, 0, 0
731int c0_is_assigned(uint8_t
byte) {
return byte < 0x20 && c0__assigned[byte]; }
737 if (len == 0)
return 1;
739 for (i = 0; i < len; i++) {
740 if (p[i] < 0x20)
return 0;
755 size_t s = 0, e = raw.
len;
763size_t c0_decode_text(
const uint8_t *in,
size_t len, uint8_t *out) {
777static void c0__fail(
c0_error *err,
c0_status code,
size_t pos,
const char *msg) {
785static void c0__err_clear(
c0_error *err) {
793static c0_bytes c0__slice(
const uint8_t *buf,
size_t s,
size_t e) {
796 b.
len = e > s ? e - s : 0;
805static size_t c0__layout(
const uint8_t *buf,
size_t pos,
size_t stop) {
810static size_t c0__plain(
const uint8_t *buf,
size_t pos,
size_t stop) {
811 while (pos < stop && buf[pos] >= 0x20) pos++;
816static size_t c0__text(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
820 uint8_t b = buf[pos];
828 c0__fail(err,
C0_ERR_SYNTAX, pos,
"DLE before a byte that is not one of the seven");
835 c0__fail(err,
C0_ERR_SYNTAX, pos,
"STX inside text (text does not nest)");
838 c0__fail(err,
C0_ERR_SYNTAX, pos,
"framing inside open text");
849static size_t c0__level(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
850 size_t start = pos, depth = 1;
853 uint8_t b = buf[pos];
857 pos = c0__text(buf, pos, stop, err);
858 if (err->
code)
return stop;
865 if (depth == 0)
return pos;
867 c0__fail(err,
C0_ERR_SYNTAX, pos,
"framing inside open level");
877static size_t c0__comment(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err);
878static size_t c0__record_end(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err);
879static size_t c0__keyed_unit_end(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err);
883static int c0__bel_kills_element(
const uint8_t *buf,
size_t pos,
size_t stop) {
884 size_t p2 = c0__layout(buf, pos + 1, stop);
885 return p2 < stop && buf[p2] < 0x20 && buf[p2] !=
C0_STX && buf[p2] !=
C0_DLE;
889static int c0__soh_is_header(
const uint8_t *buf,
size_t pos,
size_t stop) {
890 size_t p2 = c0__layout(buf, pos + 1, stop);
891 return p2 < stop && buf[p2] ==
C0_RS;
896static void c0__sub_run(
const uint8_t *buf,
size_t pos,
size_t stop,
size_t *run,
size_t *next) {
898 while (pos < stop && buf[pos] ==
C0_SUB) {
900 pos = c0__layout(buf, pos + 1, stop);
906static size_t c0__unit(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err);
911static size_t c0__skip_subs(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
913 size_t run, nx, p = c0__layout(buf, pos, stop);
914 if (!(p < stop && buf[p] ==
C0_SUB))
return pos;
915 c0__sub_run(buf, p, stop, &run, &nx);
916 if (!(nx < stop && buf[nx] ==
C0_US))
return pos;
917 pos = c0__unit(buf, nx + 1, stop, err);
918 if (err->
code)
return stop;
925static size_t c0__unit(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
927 uint8_t b = buf[pos];
931 pos = c0__text(buf, pos, stop, err);
932 if (err->
code)
return stop;
934 pos = c0__level(buf, pos, stop, err);
935 if (err->
code)
return stop;
939 if (c0__bel_kills_element(buf, pos, stop))
return pos;
940 pos = c0__comment(buf, pos, stop, err);
941 if (err->
code)
return stop;
951static size_t c0__comment(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
953 pos = c0__layout(buf, pos + 1, stop);
959 if (b >= 0x20)
return c0__plain(buf, pos, stop);
960 if (b ==
C0_STX)
return c0__text(buf, pos, stop, err);
961 if (b ==
C0_DLE)
return c0__level(buf, pos, stop, err);
962 if (b ==
C0_RS)
return c0__record_end(buf, pos + 1, stop, err);
964 size_t p2 = c0__layout(buf, pos + 1, stop);
965 if (p2 < stop && buf[p2] ==
C0_RS)
return c0__record_end(buf, p2 + 1, stop, err);
966 return c0__skip_subs(buf, c0__keyed_unit_end(buf, pos + 1, stop, err), stop, err);
968 if (b ==
C0_GS)
return c0__plain(buf, c0__layout(buf, pos + 1, stop), stop);
971 c0__sub_run(buf, pos, stop, &run, &nx);
972 if (nx < stop && buf[nx] ==
C0_GS)
return c0__plain(buf, c0__layout(buf, nx + 1, stop), stop);
973 c0__fail(err,
C0_ERR_SYNTAX, pos,
"BEL before a code that is not an element");
976 if (b ==
C0_FS)
return c0__plain(buf, pos + 1, stop);
977 c0__fail(err,
C0_ERR_SYNTAX, pos,
"BEL before a code that is not an element");
984static size_t c0__record_end(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
986 uint8_t b = buf[pos];
990 if (c0__soh_is_header(buf, pos, stop))
return pos;
994 c0__sub_run(buf, pos, stop, &run, &nx);
995 if (nx < stop && buf[nx] ==
C0_US) pos = nx;
998 pos = c0__text(buf, pos, stop, err);
999 if (err->
code)
return stop;
1000 }
else if (b ==
C0_DLE) {
1001 pos = c0__level(buf, pos, stop, err);
1002 if (err->
code)
return stop;
1003 }
else if (b ==
C0_BEL) {
1004 size_t p2 = c0__layout(buf, pos + 1, stop);
1005 if (p2 < stop && (buf[p2] ==
C0_RS || buf[p2] ==
C0_GS || buf[p2] ==
C0_FS || buf[p2] ==
C0_SUB))
return pos;
1006 if (p2 < stop && buf[p2] ==
C0_SOH && c0__soh_is_header(buf, p2, stop))
return pos;
1007 pos = c0__comment(buf, pos, stop, err);
1008 if (err->
code)
return stop;
1017static size_t c0__keyed_unit_end(
const uint8_t *buf,
size_t pos,
size_t stop,
c0_error *err) {
1018 pos = c0__layout(buf, pos, stop);
1019 pos = c0__plain(buf, pos, stop);
1020 if (!(pos < stop && buf[pos] ==
C0_US)) {
1021 c0__fail(err,
C0_ERR_SYNTAX, pos,
"key without a value (no US)");
1024 return c0__unit(buf, pos + 1, stop, err);
1027static size_t c0__trim_end(
const uint8_t *buf,
size_t start,
size_t stop) {
1028 while (stop > start &&
c0_is_layout(buf[stop - 1])) stop--;
1032#define C0__SCAN_WRAP(name, inner) \
1033 size_t name(const uint8_t *buf, size_t pos, size_t stop, c0_error *err) { \
1034 c0_error local = {C0_OK, 0, ""}; \
1035 if (!err) err = &local; \
1036 c0__err_clear(err); \
1037 return inner(buf, pos, stop, err); \
1039size_t c0_scan_layout(
const uint8_t *buf,
size_t pos,
size_t stop) {
return c0__layout(buf, pos, stop); }
1040size_t c0_scan_plain(
const uint8_t *buf,
size_t pos,
size_t stop) {
return c0__plain(buf, pos, stop); }
1047static int c0__eq_str(
c0_bytes b,
const char *s) {
1048 size_t n = strlen(s);
1049 return b.
len == n && (n == 0 || memcmp(b.
ptr, s, n) == 0);
1068 pos = c0__layout(t.
ptr, pos, t.
len);
1069 while (pos < t.
len) {
1071 pos = c0__layout(t.
ptr, c0__comment(t.
ptr, pos, t.
len, &err), t.
len);
1086 size_t stop = c0__text(t.
ptr, 0, t.
len, &err);
1089 st = c0__only_notes_after(t, stop);
1092 out->
len = stop - 2;
1093 for (i = 0; i < out->
len; i++) {
1104 return c0__only_notes_after(t, 0);
1109 for (i = 0; i < t.
len; i++) {
1110 uint8_t b = t.
ptr[i];
1112 c0_status st = c0__only_notes_after(t, i);
1137 stop = c0__level(t.
ptr, 0, t.
len, &err);
1141 out->
stop = stop - 1;
1151 size_t p2 = c0__layout(r.
buf, p + 1, r.
stop);
1154 p = c0__layout(r.
buf, c0__comment(r.
buf, p, r.
stop, &err), r.
stop);
1155 if (err.
code)
return 0;
1166 c0__err_clear(&it.
err);
1170static c0_unit c0__unit_at(
const uint8_t *buf,
size_t s,
size_t e,
size_t subs_end,
size_t depth) {
1172 u.
raw = c0__slice(buf, s, e);
1182 if (it->
done)
return 0;
1184 e = c0__unit(it->
buf, s, it->
stop, &it->
err);
1191 *out = c0__unit_at(it->
buf, s, e, nx, 0);
1192 if (nx < it->stop && it->
buf[nx] ==
C0_US) {
1211 if (i == n)
return C0_OK;
1223 c0__err_clear(&it.
err);
1231 const uint8_t *buf = it->
buf;
1232 size_t run, nx, s, e, p;
1234 p = c0__layout(buf, it->
pos, it->
stop);
1235 if (!(p < it->stop && buf[p] ==
C0_SUB)) {
1239 c0__sub_run(buf, p, it->
stop, &run, &nx);
1240 if (!(nx < it->stop && buf[nx] ==
C0_US) || run != it->
depth + 1) {
1246 e = c0__unit(buf, s, it->
stop, &it->
err);
1253 size_t q = c0__layout(buf, p, it->
stop), r2, n2;
1254 if (!(q < it->stop && buf[q] ==
C0_SUB))
break;
1255 c0__sub_run(buf, q, it->
stop, &r2, &n2);
1256 if (r2 <= run || !(n2 < it->stop && buf[n2] ==
C0_US))
break;
1257 p = c0__unit(buf, n2 + 1, it->
stop, &it->
err);
1263 *out = c0__unit_at(buf, s, e, p, run);
1288 if (i == n)
return C0_OK;
1294static c0_pair_iter c0__pairs(
const uint8_t *buf,
size_t start,
size_t stop) {
1299 c0__err_clear(&it.
err);
1306 const uint8_t *buf = it->
buf;
1308 uint8_t b = buf[it->
pos];
1310 size_t ks = c0__layout(buf, it->
pos + 1, it->
stop);
1311 size_t ke = c0__plain(buf, ks, it->
stop), vs, ve, nx;
1312 if (!(ke < it->stop && buf[ke] ==
C0_US)) {
1318 ve = c0__unit(buf, vs, it->
stop, &it->
err);
1320 if (!it->
err.
code) nx = c0__skip_subs(buf, ve, it->
stop, &it->
err);
1326 *key =
c0_trim(c0__slice(buf, ks, ke));
1327 *out = c0__unit_at(buf, vs, ve, nx, 0);
1329 }
else if (b ==
C0_BEL) {
1353 if (k.
len == klen && (klen == 0 || memcmp(k.
ptr, key, klen) == 0)) {
1367 return c0_record_get(r, (
const uint8_t *)key, strlen(key), raw);
1376 if (k.
len == klen && (klen == 0 || memcmp(k.
ptr, key, klen) == 0)) {
1395typedef enum { C0__EL_END = 0, C0__EL_RECORD, C0__EL_HEADER, C0__EL_GROUP, C0__EL_FILE } c0__el_kind;
1406static void c0__el_record(
const uint8_t *buf,
size_t rs,
size_t stop, c0__el *el, c0__el_kind kind,
c0_error *err) {
1407 size_t e = c0__record_end(buf, rs, stop, err);
1410 el->b = c0__trim_end(buf, rs, e);
1414static void c0__next_el(
const uint8_t *buf,
size_t pos,
size_t stop, c0__el *el,
c0_error *err) {
1419 pos = c0__layout(buf, pos, stop);
1421 el->kind = C0__EL_END;
1423 if (err->
code || pos >= stop)
return;
1426 size_t p2 = c0__layout(buf, pos + 1, stop);
1431 if (buf[p2] >= 0x20 || buf[p2] ==
C0_STX) {
1432 pos = c0__comment(buf, pos, stop, err);
1439 c0__fail(err,
C0_ERR_SYNTAX, pos,
"a nested level is not an item");
1445 c0__sub_run(buf, pos, stop, &run, &p2);
1446 if (!(p2 < stop && buf[p2] ==
C0_GS)) {
1447 c0__fail(err,
C0_ERR_SYNTAX, pos,
"SUB before a byte that is not GS or US");
1455 c0__el_record(buf, pos + 1, stop, el, C0__EL_RECORD, err);
1459 size_t p2 = c0__layout(buf, pos + 1, stop);
1460 if (p2 < stop && buf[p2] ==
C0_RS) {
1461 c0__el_record(buf, p2 + 1, stop, el, C0__EL_HEADER, err);
1464 c0__fail(err,
C0_ERR_SYNTAX, pos,
"keyed unit outside a record");
1469 pos = c0__layout(buf, pos + 1, stop);
1470 el->kind = C0__EL_GROUP;
1472 el->b = el->next = c0__plain(buf, pos, stop);
1476 el->kind = C0__EL_FILE;
1477 el->a = c0__layout(buf, pos + 1, stop);
1478 el->b = el->next = c0__plain(buf, el->a, stop);
1482 c0__fail(err,
C0_ERR_SYNTAX, pos,
"BEL before a code that is not an element");
1486 pos = c0__plain(buf, c0__layout(buf, pos + 1, stop), stop);
1491 c0__fail(err,
C0_ERR_SYNTAX, pos,
"CAN with no open level");
1492 }
else if (b < 0x20 && !c0__assigned[b]) {
1495 c0__fail(err,
C0_ERR_SYNTAX, pos,
"unexpected byte at element level");
1504static size_t c0__skip_dead_group(
const uint8_t *buf,
const c0__el *dead,
size_t stop,
c0_error *err) {
1505 size_t pos = dead->next;
1508 c0__next_el(buf, pos, stop, &el, err);
1509 if (el.kind == C0__EL_END || el.kind == C0__EL_FILE)
return el.at;
1510 if (el.kind == C0__EL_GROUP && el.depth <= dead->depth)
return el.at;
1517static size_t c0__skip_dead_file(
const uint8_t *buf,
const c0__el *dead,
size_t stop,
c0_error *err) {
1518 size_t pos = dead->next;
1521 c0__next_el(buf, pos, stop, &el, err);
1522 if (el.kind == C0__EL_END || el.kind == C0__EL_FILE)
return el.at;
1539 c0__err_clear(&it.
err);
1545static void c0__part_end(
const uint8_t *buf,
size_t pos,
size_t stop, c0__el *el,
int *has,
c0_error *err) {
1548 c0__next_el(buf, pos, stop, el, err);
1549 if (el->kind == C0__EL_END || el->kind == C0__EL_FILE || err->
code)
return;
1551 pos = el->kind == C0__EL_GROUP ? c0__skip_dead_group(buf, el, stop, err) : el->next;
1567 c0__part_end(it->
buf, it->
pos, it->
stop, &el, &has, &it->
err);
1575 if (el.kind != C0__EL_FILE) {
1582 out->
start = el.next;
1583 c0__part_end(it->
buf, el.next, it->
stop, &el, &has, &it->
err);
1588 while (el.kind == C0__EL_FILE && el.dead && !it->
err.
code) {
1589 size_t p = c0__skip_dead_file(it->
buf, &el, it->
stop, &it->
err);
1590 c0__next_el(it->
buf, p, it->
stop, &el, &it->
err);
1593 if (el.kind == C0__EL_END) {
1608static c0_group_iter c0__groups(
const uint8_t *buf,
size_t start,
size_t stop) {
1615 c0__err_clear(&it.
err);
1625static void c0__group_end(
const uint8_t *buf,
size_t pos,
size_t stop, c0__el *el,
int *has,
c0_error *err) {
1628 c0__next_el(buf, pos, stop, el, err);
1629 if (el->kind == C0__EL_END || el->kind == C0__EL_GROUP || el->kind == C0__EL_FILE || err->
code)
return;
1630 if (!el->dead) *has = 1;
1642 size_t start = it->
pos;
1644 c0__group_end(it->
buf, start, it->
stop, &el, &has, &it->
err);
1646 if (el.kind == C0__EL_END) it->
done = 1;
1656 if (el.kind == C0__EL_END || it->
err.
code) {
1660 if (el.kind == C0__EL_FILE) {
1662 it->
pos = c0__skip_dead_file(it->
buf, &el, it->
stop, &it->
err);
1670 it->
pos = c0__skip_dead_group(it->
buf, &el, it->
stop, &it->
err);
1673 out->
depth = el.depth;
1676 out->
start = el.next;
1677 c0__group_end(it->
buf, el.next, it->
stop, &el, &has, &it->
err);
1680 if (el.kind == C0__EL_END) it->
done = 1;
1689 if (n.
len == nlen && (nlen == 0 || memcmp(n.
ptr, name, nlen) == 0))
return C0_OK;
1710 if (n > 1 || g.
depth != 0)
return 0;
1717 size_t pos = g.
start;
1720 c0__next_el(g.
buf, pos, g.
stop, &el, &err);
1721 if (el.kind == C0__EL_END || err.
code)
return 0;
1722 if (el.kind == C0__EL_HEADER && !el.dead) {
1738 c0__err_clear(&it.
err);
1755 c0__err_clear(&it.
err);
1761 if (it->
done)
return 0;
1764 if (el.kind == C0__EL_END || it->
err.
code) {
1769 if (el.kind == C0__EL_RECORD && !el.dead) {
1786 if (
c0_value(raw, NULL, &v) ==
C0_OK && v.
len == nlen && (nlen == 0 || memcmp(v.
ptr, name, nlen) == 0)) {
1808 c0__err_clear(&items->
err);
1834#define C0__MAX_DEPTH 512
1835#define C0__GS_DEPTH_MAX 256
1841 size_t dead_elements;
1846#define C0__P_ERR(p) ((p)->err.code != C0_OK)
1848enum { C0__PH_START = 0, C0__PH_BODY, C0__PH_AFTER_EOT, C0__PH_EM };
1850static void c0__p_init(c0__parser *p,
const uint8_t *buf,
int canonical) {
1852 p->canonical = canonical;
1854 p->dead_elements = 0;
1856 c0__err_clear(&p->err);
1859static void c0__p_nc(c0__parser *p) { p->ok = 0; }
1861static void c0__p_fail(c0__parser *p,
c0_status code,
size_t pos,
const char *msg) { c0__fail(&p->err, code, pos, msg); }
1863static size_t c0__p_layout(c0__parser *p,
size_t pos,
size_t stop) {
1864 size_t p2 = c0__layout(p->buf, pos, stop);
1865 if (p2 != pos) c0__p_nc(p);
1882 s->
phase = C0__PH_START;
1884 c0__st_body_reset(s, 0);
1887static size_t c0__p_body(c0__parser *p,
c0_parse_state *s,
size_t pos,
size_t stop);
1888static size_t c0__p_record(c0__parser *p,
size_t pos,
size_t stop);
1889static size_t c0__p_value(c0__parser *p,
size_t pos,
size_t stop,
int heading);
1890static size_t c0__p_level(c0__parser *p,
size_t pos,
size_t stop);
1891static size_t c0__p_reference(c0__parser *p,
size_t pos,
size_t stop);
1894static size_t c0__p_note_content(c0__parser *p,
size_t pos,
size_t stop) {
1895 return p->buf[pos] ==
C0_STX ? c0__text(p->buf, pos, stop, &p->err) : c0__plain(p->buf, pos, stop);
1899static size_t c0__p_trailing_notes(c0__parser *p,
size_t pos,
size_t stop) {
1900 const uint8_t *buf = p->buf;
1901 while (pos < stop && buf[pos] ==
C0_BEL) {
1902 size_t p2 = c0__layout(buf, pos + 1, stop);
1909 if (!(c >= 0x20 || c ==
C0_STX))
break;
1911 pos = c0__p_note_content(p, p2, stop);
1912 if (C0__P_ERR(p))
return stop;
1913 pos = c0__p_layout(p, pos, stop);
1918static int c0__value_start(
const uint8_t *buf,
size_t pos,
size_t stop) {
1920 if (pos >= stop)
return 0;
1926static size_t c0__p_name(c0__parser *p,
size_t pos,
size_t stop,
size_t *ns,
size_t *ne) {
1927 const uint8_t *buf = p->buf;
1929 pos = c0__p_layout(p, pos, stop);
1930 if (pos < stop && buf[pos] ==
C0_STX) {
1935 pos = c0__plain(buf, pos, stop);
1936 e = c0__trim_end(buf, *ns, pos);
1937 if (e != pos) c0__p_nc(p);
1943static size_t c0__p_header_row(c0__parser *p,
size_t pos,
size_t stop) {
1944 const uint8_t *buf = p->buf;
1947 pos = c0__p_name(p, pos, stop, &ns, &ne);
1948 if (C0__P_ERR(p))
return stop;
1949 pos = c0__p_trailing_notes(p, c0__p_layout(p, pos, stop), stop);
1950 if (C0__P_ERR(p))
return stop;
1951 if (!(pos < stop && buf[pos] ==
C0_US))
break;
1954 if (pos < stop && buf[pos] ==
C0_SUB) {
1956 c0__sub_run(buf, pos, stop, &run, &p2);
1957 if (p2 < stop && buf[p2] ==
C0_US) {
1958 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a header row admits no sub-units");
1967static size_t c0__p_value(c0__parser *p,
size_t pos,
size_t stop,
int heading) {
1968 const uint8_t *buf = p->buf;
1971 pos = c0__p_layout(p, pos, stop);
1972 if (pos >= stop)
return pos;
1975 pos = c0__plain(buf, pos, stop);
1976 if (buf[pos - 1] ==
C0_SP) c0__p_nc(p);
1977 }
else if (b ==
C0_STX) {
1979 pos = c0__text(buf, pos, stop, &p->err);
1980 if (C0__P_ERR(p))
return stop;
1982 size_t n = pos - start - 2, i;
1984 for (i = 0; plain && i < n; i++) {
1985 if (buf[start + 1 + i] < 0x20) plain = 0;
1987 if (n == 0 || plain) c0__p_nc(p);
1989 }
else if (b ==
C0_DLE) {
1990 pos = c0__p_level(p, pos, stop);
1991 if (C0__P_ERR(p))
return stop;
1992 }
else if (b ==
C0_ENQ) {
1994 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"reference in a heading");
1997 pos = c0__p_reference(p, pos, stop);
1998 if (C0__P_ERR(p))
return stop;
1999 }
else if (b ==
C0_BEL) {
2000 size_t p2 = c0__layout(buf, pos + 1, stop);
2009 pos = c0__p_level(p, p2, stop);
2010 if (C0__P_ERR(p))
return stop;
2011 }
else if (c >= 0x20 || c ==
C0_STX) {
2012 pos = c0__p_trailing_notes(p, pos, stop);
2013 if (C0__P_ERR(p))
return stop;
2014 if (c0__value_start(buf, pos, stop)) {
2015 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"value after a note in the same unit");
2025 after = c0__p_trailing_notes(p, c0__p_layout(p, pos, stop), stop);
2026 if (C0__P_ERR(p))
return stop;
2027 if (c0__value_start(buf, after, stop)) {
2028 c0__p_fail(p,
C0_ERR_SYNTAX, after,
"two values in one unit");
2035static size_t c0__p_keyed_unit(c0__parser *p,
size_t pos,
size_t stop,
int heading,
size_t *ks,
size_t *ke,
size_t *vs) {
2036 pos = c0__p_name(p, pos, stop, ks, ke);
2037 if (C0__P_ERR(p))
return stop;
2038 if (!(pos < stop && p->buf[pos] ==
C0_US)) {
2039 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"key without a value (no US)");
2043 return c0__p_value(p, *vs, stop, heading);
2047static int c0__p_keyed_start(c0__parser *p,
size_t pos,
size_t stop) {
2048 const uint8_t *buf = p->buf;
2052 pos = c0__layout(buf, pos, stop);
2053 if (pos >= stop)
return 0;
2055 if (b ==
C0_SOH)
return !c0__soh_is_header(buf, pos, stop);
2056 if (b !=
C0_BEL)
return 0;
2057 p2 = c0__layout(buf, pos + 1, stop);
2058 if (p2 >= stop)
return 0;
2059 if (buf[p2] ==
C0_SOH)
return !c0__soh_is_header(buf, p2, stop);
2060 if (!(buf[p2] >= 0x20 || buf[p2] ==
C0_STX))
return 0;
2061 pos = c0__p_note_content(p, p2, stop);
2062 if (C0__P_ERR(p))
return 0;
2071static size_t c0__p_sub_units(c0__parser *p,
size_t pos,
size_t stop) {
2072 const uint8_t *buf = p->buf;
2075 size_t start, run = 0, nx;
2077 start = c0__p_layout(p, pos, stop);
2078 if (!(start < stop && buf[start] ==
C0_SUB))
return pos;
2080 while (nx < stop && buf[nx] ==
C0_SUB) {
2082 nx = c0__p_layout(p, nx + 1, stop);
2085 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB at the end of input");
2090 if (run > prev + 1) {
2091 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"depth skipped: a sub-unit is at most one deeper than the sub-unit before it");
2095 pos = c0__p_value(p, nx + 1, stop, 0);
2096 if (C0__P_ERR(p))
return stop;
2099 }
else if (c ==
C0_SOH) {
2100 if (c0__soh_is_header(buf, nx, stop)) c0__p_fail(p,
C0_ERR_SYNTAX, start,
"a header row admits no SUB");
2101 else c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before SOH is reserved (a sub-key)");
2103 }
else if (c ==
C0_BEL) {
2107 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before a byte that is not a separator");
2113static size_t c0__p_positional(c0__parser *p,
size_t pos,
size_t stop) {
2114 const uint8_t *buf = p->buf;
2117 pos = c0__p_value(p, pos, stop, 0);
2118 if (C0__P_ERR(p))
return stop;
2119 pos = c0__p_sub_units(p, pos, stop);
2120 if (C0__P_ERR(p))
return stop;
2121 pos = c0__p_layout(p, pos, stop);
2122 if (pos >= stop)
break;
2126 }
else if (b ==
C0_SUB) {
2128 }
else if (b ==
C0_SOH) {
2129 if (c0__soh_is_header(buf, pos, stop))
break;
2130 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"keyed unit in a positional record (mixed record)");
2140static void c0__p_check_keys(c0__parser *p,
size_t start,
size_t stop) {
2146 if (k2.
len == k.
len && (k.
len == 0 || memcmp(k.
ptr, k2.
ptr, k.
len) == 0)) c0__p_nc(p);
2151static size_t c0__p_keyed(c0__parser *p,
size_t pos,
size_t stop) {
2152 const uint8_t *buf = p->buf;
2153 size_t start = pos, ks, ke, vs;
2156 pos = c0__p_layout(p, pos, stop);
2157 if (pos >= stop)
break;
2160 if (c0__soh_is_header(buf, pos, stop))
break;
2161 pos = c0__p_keyed_unit(p, pos + 1, stop, 0, &ks, &ke, &vs);
2162 if (C0__P_ERR(p))
return stop;
2163 pos = c0__p_sub_units(p, pos, stop);
2164 if (C0__P_ERR(p))
return stop;
2165 }
else if (b ==
C0_BEL) {
2166 size_t p2 = c0__layout(buf, pos + 1, stop);
2174 if (c0__soh_is_header(buf, p2, stop))
break;
2176 pos = c0__p_keyed_unit(p, p2 + 1, stop, 0, &ks, &ke, &vs);
2177 if (C0__P_ERR(p))
return stop;
2178 pos = c0__p_sub_units(p, pos, stop);
2179 if (C0__P_ERR(p))
return stop;
2180 }
else if (c >= 0x20 || c ==
C0_STX) {
2181 pos = c0__p_trailing_notes(p, pos, stop);
2182 if (C0__P_ERR(p))
return stop;
2183 }
else if (c ==
C0_SUB) {
2184 c0__p_fail(p,
C0_ERR_SYNTAX, p2,
"BEL before a sub-unit: a unit cannot be dead");
2189 }
else if (b ==
C0_SUB) {
2195 if (pos < stop && buf[pos] ==
C0_US) {
2196 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"US after a keyed value (mixed record)");
2199 if (p->canonical) c0__p_check_keys(p, start, pos);
2204static size_t c0__p_record(c0__parser *p,
size_t pos,
size_t stop) {
2205 return c0__p_keyed_start(p, pos, stop) ? c0__p_keyed(p, pos, stop) : c0__p_positional(p, pos, stop);
2209static size_t c0__p_level(c0__parser *p,
size_t pos,
size_t stop) {
2210 const uint8_t *buf = p->buf;
2214 if (++p->depth > C0__MAX_DEPTH) {
2218 pos = c0__p_layout(p, pos + 1, stop);
2229 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"DLE must be followed by a structural code");
2232 c0__st_body_reset(&st, 1);
2233 pos = c0__p_body(p, &st, pos, stop);
2234 if (C0__P_ERR(p))
return stop;
2235 pos = c0__p_layout(p, pos, stop);
2240 if (buf[pos] !=
C0_CAN) {
2241 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"unexpected byte inside level");
2252static void c0__p_check_pattern(c0__parser *p,
const uint8_t *buf,
size_t start,
size_t stop,
int inner,
size_t at) {
2260 size_t ngroups = 0, prev = 0, nrecs = 0, i;
2262 memset(&last, 0,
sizeof(last));
2268 c0__p_fail(p,
C0_ERR_SYNTAX, at, inner ?
"an empty level continues nothing" :
"an empty level is not a reference");
2272 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a pattern is one branch: two files");
2277 if (ngroups == 0 && !inner && g.
depth == 0) {
2278 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a pattern begins with a file or group line");
2282 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a header row is not a step of a pattern");
2285 if (have_prev && g.
depth <= prev) {
2286 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a pattern is one branch: two groups at one depth");
2292 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"nothing follows the record in a pattern");
2302 c0__p_fail(p,
C0_ERR_SYNTAX, at, inner ?
"an empty level continues nothing" :
"an empty level is not a reference");
2308 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a pattern is one branch: two records");
2312 if (!inner) c0__p_nc(p);
2325 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a keyed record in a pattern holds one keyed unit");
2329 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a sub-unit is not a step of a pattern");
2333 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a keyed unit in a pattern has no value");
2337 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a step of a pattern is a name");
2341 if (n == 0) c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a keyed record in a pattern holds one keyed unit");
2351 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a pattern record holds at most a record id, a field name and a level");
2355 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a sub-unit is not a step of a pattern");
2361 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"the third unit of a pattern record is a level");
2364 c0__p_check_pattern(p, lv.
buf, lv.
start, lv.
stop, 1, at);
2365 if (C0__P_ERR(p))
return;
2368 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a step of a pattern is a name");
2372 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a step of a pattern is a name");
2381static size_t c0__p_reference(c0__parser *p,
size_t pos,
size_t stop) {
2382 const uint8_t *buf = p->buf;
2385 pos = c0__p_layout(p, pos + 1, stop);
2387 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"reference without a name");
2392 size_t dead0 = p->dead_elements, after;
2393 after = c0__p_level(p, pos, stop);
2394 if (C0__P_ERR(p))
return stop;
2395 if (p->dead_elements != dead0) {
2396 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a dead element is not a step of a pattern");
2399 c0__p_check_pattern(p, buf, pos + 1, after - 1, 0, pos);
2400 return C0__P_ERR(p) ? stop : after;
2403 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a reference is a name or a pattern, not text");
2408 pos = c0__p_name(p, pos, stop, &ns, &ne);
2410 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"reference without a name");
2415 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"reference without a name");
2422static size_t c0__p_body(c0__parser *p,
c0_parse_state *s,
size_t pos,
size_t stop) {
2423 const uint8_t *buf = p->buf;
2426 size_t mark_at = pos;
2429 pos = c0__p_layout(p, pos, stop);
2430 if (pos >= stop)
break;
2434 pos = c0__p_record(p, pos + 1, stop);
2435 if (C0__P_ERR(p))
return stop;
2445 size_t p2 = c0__layout(buf, pos + 1, stop);
2446 if (!(p2 < stop && buf[p2] ==
C0_RS)) {
2447 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"keyed unit outside a record");
2451 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"header row not at the start of its group");
2454 pos = c0__p_header_row(p, p2 + 1, stop);
2455 if (C0__P_ERR(p))
return stop;
2466 size_t depth = 1 + prefix, ns, ne;
2468 if (depth >= C0__GS_DEPTH_MAX) {
2473 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"depth skipped: a group is at most one deeper than the group line before it");
2477 pos = c0__p_name(p, pos + 1, stop, &ns, &ne);
2478 if (C0__P_ERR(p))
return stop;
2492 pos = c0__p_name(p, pos + 1, stop, &ns, &ne);
2493 if (C0__P_ERR(p))
return stop;
2514 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"BEL before a code that is not an element");
2517 p2 = c0__layout(buf, pos + 1, stop);
2523 if (c >= 0x20 || c ==
C0_STX) {
2525 pos = c0__p_note_content(p, p2, stop);
2526 if (C0__P_ERR(p))
return stop;
2532 }
else if (c ==
C0_SUB) {
2535 c0__sub_run(buf, p2, stop, &run, &p3);
2536 if (p3 < stop && buf[p3] ==
C0_GS) {
2541 }
else if (p3 < stop && buf[p3] ==
C0_US) {
2542 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"BEL before a sub-unit: a unit cannot be dead");
2545 c0__p_fail(p,
C0_ERR_SYNTAX, p2,
"BEL before a code that is not an element");
2548 }
else if (c ==
C0_DLE) {
2549 c0__p_fail(p,
C0_ERR_SYNTAX, p2,
"a nested level is not an item");
2552 c0__p_fail(p,
C0_ERR_SYNTAX, p2,
"BEL before a code that is not an element");
2561 size_t start = pos, run = 0;
2563 while (pos < stop && buf[pos] ==
C0_SUB) {
2565 pos = c0__p_layout(p, pos + 1, stop);
2568 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB at the end of input");
2574 }
else if (c ==
C0_RS) {
2575 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before RS is reserved (a sub-record)");
2577 }
else if (c ==
C0_FS) {
2578 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before FS is reserved (a sub-file)");
2580 }
else if (c ==
C0_SOH) {
2581 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before SOH is reserved (a sub-key); a header row admits none");
2583 }
else if (c ==
C0_US) {
2586 }
else if (c ==
C0_BEL) {
2590 c0__p_fail(p,
C0_ERR_SYNTAX, start,
"SUB before a byte that is not a separator");
2597 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"commit inside a nested level");
2601 c0__p_fail(p,
C0_ERR_SYNTAX, mark_at,
"BEL before ETB: framing cannot be commented out");
2605 pos = c0__plain(buf, c0__p_layout(p, pos + 1, stop), stop);
2612 c0__p_fail(p,
C0_ERR_SYNTAX, pos, b ==
C0_CAN ?
"CAN with no open level" :
"framing inside open level");
2616 c0__p_fail(p,
C0_ERR_SYNTAX, mark_at,
"BEL before framing");
2627 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a nested level is not an item");
2630 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a reference outside a record");
2633 if (b >= 0x20) c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"plain text outside a record");
2647static void c0__p_check_schema(c0__parser *p,
c0_bytes rawv,
size_t at) {
2661 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema level holds one keyed record");
2666 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema level holds one keyed record");
2672 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema level holds one keyed record");
2677 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema level holds a keyed record, not a positional one");
2683 if (!(c0__eq_str(k,
"name") || c0__eq_str(k,
"url") || c0__eq_str(k,
"hash"))) {
2688 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema name, url or hash is a name");
2692 if (n == 0) c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema record needs name, url or hash");
2696 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema name cannot be text");
2700 c0__p_fail(p,
C0_ERR_SYNTAX, at,
"a schema name is a name");
2705static size_t c0__p_heading(c0__parser *p,
size_t pos,
size_t stop) {
2706 const uint8_t *buf = p->buf;
2710 pos = c0__p_layout(p, pos, stop);
2711 if (pos >= stop)
return pos;
2714 if (c0__soh_is_header(buf, pos, stop))
return pos;
2715 pos = c0__p_keyed_unit(p, pos + 1, stop, 1, &ks, &ke, &vs);
2716 if (C0__P_ERR(p))
return stop;
2717 if (c0__eq_str(c0__slice(buf, ks, ke),
"schema")) c0__p_check_schema(p, c0__slice(buf, vs, pos), vs);
2718 if (C0__P_ERR(p))
return stop;
2719 }
else if (b ==
C0_BEL) {
2720 size_t p2 = c0__layout(buf, pos + 1, stop);
2728 if (c0__soh_is_header(buf, p2, stop))
return pos;
2730 pos = c0__p_keyed_unit(p, p2 + 1, stop, 1, &ks, &ke, &vs);
2731 if (C0__P_ERR(p))
return stop;
2732 }
else if (c >= 0x20 || c ==
C0_STX) {
2734 pos = c0__p_note_content(p, p2, stop);
2735 if (C0__P_ERR(p))
return stop;
2739 c0__p_fail(p,
C0_ERR_SYNTAX, p2,
"BEL before a code that is not an element");
2742 }
else if (b ==
C0_SUB) {
2743 size_t p2 = c0__layout(buf, pos + 1, stop);
2745 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"SUB at the end of input");
2748 if (buf[p2] ==
C0_US) {
2749 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"the heading's values take no sub-units");
2753 }
else if (b >= 0x20) {
2754 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"plain text directly after SYN");
2765static size_t c0__p_run(c0__parser *p,
c0_parse_state *st,
size_t pos,
size_t stop) {
2766 const uint8_t *buf = p->buf;
2769 if (st->
phase == C0__PH_EM)
return stop;
2770 pos = c0__p_layout(p, pos, stop);
2771 if (pos >= stop || C0__P_ERR(p))
return pos;
2773 switch (st->
phase) {
2777 st->
phase = C0__PH_EM;
2782 pos = c0__p_heading(p, pos + 1, stop);
2783 if (C0__P_ERR(p))
return stop;
2784 }
else if (!st->
first) {
2785 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"a document after the first must begin with SYN");
2789 st->
phase = C0__PH_BODY;
2790 c0__st_body_reset(st, 0);
2793 pos = c0__p_body(p, st, pos, stop);
2794 if (C0__P_ERR(p))
return stop;
2795 if (pos >= stop)
return pos;
2800 st->
phase = C0__PH_AFTER_EOT;
2801 }
else if (b ==
C0_SYN) {
2802 st->
phase = C0__PH_START;
2805 st->
phase = C0__PH_EM;
2809 case C0__PH_AFTER_EOT:
2811 st->
phase = C0__PH_START;
2812 }
else if (b ==
C0_EM) {
2814 st->
phase = C0__PH_EM;
2817 c0__p_fail(p,
C0_ERR_SYNTAX, pos,
"unexpected byte after EOT");
2833 memset(out, 0,
sizeof(*out));
2835 c0__p_init(&p, t.
ptr, 0);
2836 after = c0__p_reference(&p, 0, t.
len);
2837 if (C0__P_ERR(&p))
return p.err.code;
2839 pos = c0__layout(t.
ptr, 1, t.
len);
2842 size_t end = c0__level(t.
ptr, pos, t.
len, &err);
2847 out->
name = c0__slice(t.
ptr, pos, pos);
2854static size_t c0__ref_body_path(
c0_body body,
c0_bytes *steps,
size_t max,
size_t n) {
2868 memset(&last, 0,
sizeof(last));
2877 if (!have)
return n;
2884 if (n < max) steps[n] = k;
2896 if (n < max) steps[n] = v;
2909 if (max > 0) steps[0] = r->
name;
2912 return c0__ref_body_path(r->
pattern, steps, max, 0);
2923 if (name) *name = r->
name;
2942 const uint8_t *buf = p->buf;
2943 size_t pos, dead0 = p->dead_elements;
2945 memset(out, 0,
sizeof(*out));
2948 pos = c0__p_layout(p, start, len);
2954 pos = c0__p_heading(p, pos, len);
2955 if (C0__P_ERR(p))
return p->err.code;
2963 c0__st_body_reset(&st, 0);
2964 pos = c0__p_body(p, &st, pos, len);
2965 if (C0__P_ERR(p))
return p->err.code;
2967 pos = c0__p_layout(p, pos, len);
2983 if (!c0__eq_str(k,
"shape") && !c0__eq_str(k,
"schema") && !c0__eq_str(k,
"version") && !c0__eq_str(k,
"blob")) {
3008 c0__p_init(&p, buf, 0);
3009 st = c0__read_document(&p, len, start, out);
3010 if (err) *err = p.err;
3018 c0__p_init(&p, buf, 0);
3019 c0__st_body_reset(&st, 0);
3020 pos = c0__p_body(&p, &st, 0, len);
3021 if (!C0__P_ERR(&p)) {
3022 pos = c0__layout(buf, pos, len);
3023 if (pos < len) c0__p_fail(&p,
C0_ERR_SYNTAX, pos,
"unexpected byte in body");
3025 if (err) *err = p.err;
3026 if (p.err.code)
return p.err.
code;
3040 return c0_heading_get(d, (
const uint8_t *)key, strlen(key), raw);
3057 for (i = 0; i < v.
len; i++) {
3058 if (v.
ptr[i] <
'0' || v.
ptr[i] >
'9')
return 1;
3059 n = n * 10 + (v.
ptr[i] -
'0');
3081 c0__err_clear(&it.
err);
3087 if (it->
done)
return 0;
3088 pos = c0__p_layout(p, it->
pos, it->
len);
3089 if (pos >= it->
len) {
3101 c0__fail(&it->
err,
C0_ERR_SYNTAX, pos,
"a document after the first must begin with SYN");
3105 if (c0__read_document(p, it->
len, pos, out) !=
C0_OK) {
3121 c0__p_init(&p, it->
buf, 0);
3122 return c0__next_document(it, out, &p);
3141 if (err) *err = it.
err;
3143 if (has_em) *has_em = it.
has_em;
3149static c0_status c0__run_container(c0__parser *p,
size_t len) {
3152 while (c0__next_document(&it, &d, p)) {
3154 if (it.
err.
code && !p->err.code) p->err = it.
err;
3160 c0__p_init(&p, buf, 0);
3161 c0__run_container(&p, len);
3162 if (err) *err = p.err;
3168 c0__p_init(&p, buf, 1);
3169 c0__run_container(&p, len);
3170 return p.err.code ==
C0_OK && p.ok;
3175static int c0__to_size(
c0_bytes v,
size_t *out) {
3177 if (v.
len == 0)
return 0;
3178 for (i = 0; i < v.
len; i++) {
3179 if (v.
ptr[i] <
'0' || v.
ptr[i] >
'9')
return 0;
3180 n = n * 10 + (size_t)(v.
ptr[i] -
'0');
3195 size_t ni = 0, oi = 1, li = 2;
3199 if (err) *err = it.
err;
3207 if (err) *err = it.
err;
3223 if (!(v.
len == nlen && (nlen == 0 || memcmp(v.
ptr, name, nlen) == 0)))
continue;
3226 if (off > tail.
len || length > tail.
len - off)
continue;
3227 out->
ptr = tail.
ptr + off;
3235 return c0_blob(buf, len, (
const uint8_t *)name, strlen(name), out, err);
3240enum { C0__B_TOP = 0, C0__B_HEADING = 1, C0__B_RECORD = 2 };
3242static int c0__b_reserve(
c0_builder *b,
size_t extra) {
3244 if (b->
len + extra > b->
cap) {
3245 size_t ncap = b->
cap ? b->
cap : 64;
3247 while (ncap < b->len + extra) ncap *= 2;
3248 nd = (uint8_t *)realloc(b->
data, ncap);
3259static void c0__b_byte(
c0_builder *b, uint8_t x) {
3260 if (c0__b_reserve(b, 1)) b->
data[b->
len++] = x;
3263static void c0__b_raw(
c0_builder *b,
const uint8_t *p,
size_t n) {
3264 if (n && c0__b_reserve(b, n)) {
3265 memcpy(b->
data + b->
len, p, n);
3270static void c0__b_name(
c0_builder *b,
const uint8_t *p,
size_t n) {
3279static void c0__b_value(
c0_builder *b,
const uint8_t *p,
size_t n) {
3286 for (i = 0; i < n; i++) {
3288 c0__b_byte(b, p[i]);
3294 b->
state = C0__B_TOP;
3299static void c0__b_first_value(
c0_builder *b,
const uint8_t *p,
size_t n) {
3300 c0__b_value(b, p, n);
3313static void c0__b_key_head(
c0_builder *b,
const uint8_t *key,
size_t klen) {
3318static c0_bytes c0__cstr(
const char *s) {
3320 b.
ptr = (
const uint8_t *)s;
3333 for (i = 0; i < count; i++) out[i] = c0__cstr(strs[i]);
3338 memset(b, 0,
sizeof(*b));
3345 memset(b, 0,
sizeof(*b));
3360 c0__b_end_record(b);
3361 c0__b_byte(b,
C0_FS);
3362 c0__b_name(b, name, nlen);
3366 c0__b_end_record(b);
3367 c0__b_byte(b,
C0_GS);
3372 c0__b_name(b, name, nlen);
3376 c0__b_byte(b,
C0_RS);
3377 b->
state = C0__B_RECORD;
3383 c0__b_first_value(b, value, vlen);
3387 c0__b_byte(b,
C0_US);
3392 c0__b_byte(b,
C0_US);
3393 c0__b_value(b, value, vlen);
3404 c0__b_name(b, name, nlen);
3410 int *ns = (
int *)realloc(b->
stack, ncap *
sizeof(
int) * 2);
3421 b->
state = C0__B_TOP;
3427 if (b->
depth == 0) {
3445 c0__b_value(b, text, tlen);
3451 c0__b_name(b, name, nlen);
3460 c0__b_end_record(b);
3462 b->
state = C0__B_HEADING;
3466 c0__b_end_record(b);
3471 c0__b_end_record(b);
3477 c0__b_name(b, payload, plen);
3481 c0__b_end_record(b);
3482 c0__b_byte(b,
C0_EM);
3483 c0__b_raw(b, tail, tlen);
3513 c0__b_end_record(b);
3514 for (i = 1; i < depth; i++) c0__b_byte(b,
C0_SUB);
3522 for (i = 0; i < count; i++) {
3523 if (!
c0_is_name(names[i].ptr, names[i].len)) {
3528 c0__b_end_record(b);
3531 for (i = 0; i < count; i++) {
3533 c0__b_raw(b, names[i].ptr, names[i].len);
3535 c0__b_end_record(b);
3541 for (i = 0; i < count; i++)
c0_build_field(b, fields[i].ptr, fields[i].len);
3546 else if (b->
units == 0) c0__b_first_value(b, value, vlen);
3561 for (i = 0; i < depth; i++) c0__b_byte(b,
C0_SUB);
3562 c0__b_byte(b,
C0_US);
3563 c0__b_value(b, value, vlen);
3569 for (i = 0; i < count; i++)
c0_build_key(b, keys[i].ptr, keys[i].len, values[i].ptr, values[i].len);
3573 c0__b_key_head(b, key, klen);
3583 c0__b_key_head(b, key, klen);
3596 for (i = 0; i < count; i++)
c0_build_field(b, items[i].ptr, items[i].len);
3611void c0_build_ref_record(
c0_builder *b,
const uint8_t *group,
size_t glen,
const uint8_t *record,
size_t rlen,
const uint8_t *field,
size_t flen) {
3612 if (glen == 0 || rlen == 0 || (field && flen == 0)) {
3619 c0__b_name(b, record, rlen);
3623 c0__b_name(b, field, flen);
3640 c0_bytes p = payload ? c0__cstr(payload) : c0__cstr(
"");
3647 c0_bytes *v = c0__cstrs(b, names, count);
3653 c0_bytes *v = c0__cstrs(b, fields, count);
3669 c0_bytes k = c0__cstr(key), v = c0__cstr(value);
3674 c0_bytes *v = c0__cstrs(b, items, count);
3681 c0_bytes g = c0__cstr(group), r = c0__cstr(record), f = field ? c0__cstr(field) : c0__cstr(
"");
3692static size_t c0__payload_end(
const uint8_t *buf,
size_t pos,
size_t len) {
3693 size_t p = c0__layout(buf, pos, len);
3694 if (p < len && buf[p] >= 0x20) {
3695 p = c0__plain(buf, p, len);
3696 while (p > pos && buf[p - 1] ==
C0_SP) p--;
3702static c0_block_iter c0__blocks(
const uint8_t *buf,
size_t stop) {
3710 c0__st_init(&it.
st);
3718static int c0__block_sound(
c0_parse_state *st,
const uint8_t *buf,
size_t start,
size_t stop) {
3721 c0__p_init(&p, buf, 0);
3722 c0__p_run(&p, &tmp, start, stop);
3723 if (C0__P_ERR(&p))
return 0;
3729 const uint8_t *buf = it->
buf;
3731 uint8_t b = buf[it->
pos];
3737 }
else if (b ==
C0_ETX) {
3739 }
else if (b ==
C0_ETB) {
3748 }
else if (b ==
C0_ETB) {
3750 }
else if (b ==
C0_SYN && it->
pos > 0) {
3757 if (damaged) *damaged = !sound;
3758 it->
pos = c0__payload_end(buf, it->
pos + 1, it->
stop);
3770static int c0__torn_tail(
const uint8_t *buf,
size_t pos,
size_t len) {
3774 pos = c0__layout(buf, pos, len);
3775 if (pos >= len)
return 0;
3779 }
else if (b ==
C0_EM) {
3781 }
else if (b ==
C0_BEL) {
3782 if (c0__bel_kills_element(buf, pos, len))
return 1;
3783 pos = c0__comment(buf, pos, len, &err);
3784 if (err.
code)
return 1;
3823static size_t c0__stream_copy(
const c0_stream *s, uint8_t *out,
int stop_at_damage) {
3830 if (stop_at_damage)
break;
3833 if (blk.
len) memcpy(out + n, blk.
ptr, blk.
len);
3847 size_t dles = 0, j = i;
3848 while (j > 0 && buf[j - 1] ==
C0_DLE) {
3852 if (dles % 2 == 0)
return c0__plain(buf, i + 1, len);
3867static int c0__sb_reserve(c0__sb *s,
size_t extra) {
3868 if (s->oom)
return 0;
3869 if (s->n + extra > s->cap) {
3870 size_t ncap = s->cap ? s->cap : 64;
3872 while (ncap < s->n + extra) ncap *= 2;
3873 nd = (uint8_t *)realloc(s->d, ncap);
3884static void c0__sb_byte(c0__sb *s, uint8_t b) {
3885 if (c0__sb_reserve(s, 1)) s->d[s->n++] = b;
3888static void c0__sb_raw(c0__sb *s,
const uint8_t *p,
size_t n) {
3889 if (n && c0__sb_reserve(s, n)) {
3890 memcpy(s->d + s->n, p, n);
3895static void c0__sb_cstr(c0__sb *s,
const char *p) { c0__sb_raw(s, (
const uint8_t *)p, strlen(p)); }
3898static void c0__sb_glyph(c0__sb *s, uint8_t
byte) {
3899 c0__sb_byte(s, 0xE2);
3900 c0__sb_byte(s, 0x90);
3901 c0__sb_byte(s, (uint8_t)(0x80 |
byte));
3904static void c0__sb_spaces(c0__sb *s,
size_t n) {
3905 while (n--) c0__sb_byte(s,
' ');
3923static void c0__pf_push(c0__pf *f,
size_t v) {
3924 if (f->sn == f->scap) {
3925 size_t ncap = f->scap ? f->scap * 2 : 16;
3926 size_t *ns = (
size_t *)realloc(f->stack, ncap *
sizeof(
size_t));
3928 c0__fail(f->err,
C0_ERR_OOM, 0,
"out of memory");
3934 f->stack[f->sn++] = v;
3937static size_t c0__pf_pop(c0__pf *f) {
return f->sn ? f->stack[--f->sn] : 0; }
3941static void c0__check_glyphs(c0__pf *f,
const uint8_t *p,
size_t n) {
3943 for (i = 0; i + 2 < n; i++) {
3944 if (p[i] == 0xE2 && p[i + 1] == 0x90 && p[i + 2] >= 0x80 && p[i + 2] <= 0x9F) {
3951static void c0__pf_data(c0__pf *f,
c0_bytes d) {
3952 c0__check_glyphs(f, d.
ptr, d.
len);
3953 c0__sb_raw(f->o, d.
ptr, d.
len);
3958static void c0__pf_text(c0__pf *f,
c0_bytes t) {
3960 c0__check_glyphs(f, t.
ptr, t.
len);
3961 c0__sb_glyph(f->o,
C0_STX);
3962 for (i = 1; i + 1 < t.
len; i++) {
3963 uint8_t b = t.
ptr[i];
3964 if (b < 0x20 && b !=
C0_LF && b !=
C0_CR && b !=
C0_HT) c0__sb_glyph(f->o, b);
3965 else c0__sb_byte(f->o, b);
3967 c0__sb_glyph(f->o,
C0_ETX);
3970static size_t c0__pf_plain(c0__pf *f,
size_t pos) {
3972 pos = c0__layout(f->buf, pos, f->len);
3974 pos = c0__plain(f->buf, pos, f->len);
3975 c0__pf_data(f,
c0_trim(c0__slice(f->buf, start, pos)));
3979static void c0__pf_indent(c0__pf *f,
size_t depth) {
3980 while (depth--) c0__sb_raw(f->o, (
const uint8_t *)f->indent, f->ilen);
3983static void c0__pf_nl(c0__pf *f) {
3984 if (!f->ls) c0__sb_byte(f->o,
'\n');
3988static void c0__pf_inline_level(c0__pf *f,
c0_bytes raw) {
3990 while (i < raw.len && !f->err->
code) {
3991 uint8_t b = raw.
ptr[i];
3993 size_t stop = c0__text(raw.
ptr, i, raw.
len, f->err);
3994 c0__pf_text(f, c0__slice(raw.
ptr, i, stop));
3996 }
else if (b >= 0x20) {
3998 i = c0__plain(raw.
ptr, i, raw.
len);
3999 c0__pf_data(f,
c0_trim(c0__slice(raw.
ptr, start, i)));
4003 c0__sb_glyph(f->o, b);
4012static size_t c0__pf_units(c0__pf *f,
size_t pos) {
4013 const uint8_t *buf = f->buf;
4014 size_t len = f->len;
4015 while (pos < len && !f->err->
code) {
4016 uint8_t b = buf[pos];
4019 pos = c0__plain(buf, pos, len);
4020 c0__pf_data(f,
c0_trim(c0__slice(buf, start, pos)));
4024 c0__sb_glyph(f->o, b);
4026 }
else if (b ==
C0_SUB) {
4030 c0__sub_run(buf, pos, len, &run, &p2);
4031 if (!(p2 < len && buf[p2] ==
C0_US))
break;
4032 while (run--) c0__sb_glyph(f->o,
C0_SUB);
4034 }
else if (b ==
C0_SOH) {
4035 size_t p2 = c0__layout(buf, pos + 1, len);
4036 if (p2 < len && buf[p2] ==
C0_RS)
break;
4037 c0__sb_glyph(f->o,
C0_SOH);
4039 }
else if (b ==
C0_STX) {
4040 size_t stop = c0__text(buf, pos, len, f->err);
4041 c0__pf_text(f, c0__slice(buf, pos, stop));
4043 }
else if (b ==
C0_DLE) {
4044 c0__sb_glyph(f->o,
C0_DLE);
4045 c0__sb_byte(f->o,
'\n');
4046 c0__pf_push(f, f->gdepth);
4050 }
else if (b ==
C0_BEL) {
4051 size_t p2 = c0__layout(buf, pos + 1, len);
4052 if (p2 < len && (buf[p2] >= 0x20 || buf[p2] ==
C0_STX || buf[p2] ==
C0_DLE)) {
4053 c0__sb_glyph(f->o,
C0_BEL);
4058 }
else if (b ==
C0_ETB) {
4059 c0__sb_glyph(f->o,
C0_ETB);
4060 pos = c0__pf_plain(f, pos + 1);
4071static size_t c0__pf_element(c0__pf *f,
size_t pos) {
4072 const uint8_t *buf = f->buf;
4073 size_t len = f->len;
4074 uint8_t b = buf[pos];
4077 c0__sb_glyph(f->o,
C0_BEL);
4078 pos = c0__layout(buf, pos + 1, len);
4079 if (pos < len && (buf[pos] >= 0x20 || buf[pos] ==
C0_STX)) {
4080 pos = c0__pf_units(f, pos);
4081 c0__sb_byte(f->o,
'\n');
4085 if (pos >= len)
return pos;
4086 return c0__pf_element(f, pos);
4087 }
else if (b ==
C0_SUB) {
4089 c0__sb_glyph(f->o,
C0_SUB);
4090 pos = c0__layout(buf, pos + 1, len);
4091 if (pos >= len)
return pos;
4092 return c0__pf_element(f, pos);
4093 }
else if (b ==
C0_SOH) {
4094 c0__sb_glyph(f->o,
C0_SOH);
4095 pos = c0__layout(buf, pos + 1, len);
4096 if (pos < len && buf[pos] ==
C0_RS) {
4097 c0__sb_glyph(f->o,
C0_RS);
4100 pos = c0__pf_units(f, pos);
4101 }
else if (b ==
C0_RS) {
4102 c0__sb_glyph(f->o,
C0_RS);
4103 pos = c0__pf_units(f, pos + 1);
4104 }
else if (b ==
C0_ETB) {
4105 c0__sb_glyph(f->o,
C0_ETB);
4106 pos = c0__pf_plain(f, pos + 1);
4109 c0__sb_byte(f->o,
'\n');
4115static size_t c0__pf_heading(c0__pf *f,
size_t pos) {
4116 const uint8_t *buf = f->buf;
4117 size_t len = f->len;
4118 while (pos < len && !f->err->
code) {
4119 uint8_t b = buf[pos];
4121 size_t p2 = c0__layout(buf, pos + 1, len);
4122 if (p2 < len && buf[p2] ==
C0_RS)
break;
4123 c0__sb_glyph(f->o,
C0_SOH);
4124 pos = c0__pf_plain(f, p2);
4125 if (pos < len && buf[pos] ==
C0_US) {
4126 c0__sb_glyph(f->o,
C0_US);
4127 pos = c0__layout(buf, pos + 1, len);
4128 if (pos < len && buf[pos] ==
C0_STX) {
4129 size_t stop = c0__text(buf, pos, len, f->err);
4130 c0__pf_text(f, c0__slice(buf, pos, stop));
4132 }
else if (pos < len && buf[pos] ==
C0_DLE) {
4133 size_t stop = c0__level(buf, pos, len, f->err);
4134 c0__pf_inline_level(f, c0__slice(buf, pos, stop));
4137 pos = c0__pf_plain(f, pos);
4140 }
else if (b ==
C0_BEL) {
4141 size_t p2 = c0__layout(buf, pos + 1, len);
4142 if (p2 < len && (buf[p2] >= 0x20 || buf[p2] ==
C0_STX)) {
4143 c0__sb_glyph(f->o,
C0_BEL);
4145 if (buf[pos] ==
C0_STX) {
4146 size_t stop = c0__text(buf, pos, len, f->err);
4147 c0__pf_text(f, c0__slice(buf, pos, stop));
4150 pos = c0__pf_plain(f, pos);
4152 }
else if (p2 < len && buf[p2] ==
C0_SOH) {
4153 c0__sb_glyph(f->o,
C0_BEL);
4167static void c0__pf_compact(c0__pf *f) {
4168 const uint8_t *buf = f->buf;
4169 size_t len = f->len, pos = 0;
4173 while (pos < len && !f->err->
code) {
4174 uint8_t b = buf[pos];
4180 pos = c0__plain(buf, pos, len);
4181 c0__pf_data(f, c0__slice(buf, start, pos));
4189 c0__sb_glyph(f->o,
C0_SYN);
4190 pos = c0__pf_heading(f, pos + 1);
4191 c0__sb_byte(f->o,
'\n');
4198 c0__pf_indent(f, f->depth);
4201 c0__sb_glyph(f->o,
C0_FS);
4202 pos = c0__pf_plain(f, pos + 1);
4203 c0__sb_byte(f->o,
'\n');
4210 c0__pf_indent(f, f->depth);
4213 c0__sb_glyph(f->o,
C0_GS);
4214 pos = c0__pf_plain(f, pos + 1);
4215 c0__sb_byte(f->o,
'\n');
4222 c0__sub_run(buf, pos, len, &run, &p2);
4223 if (p2 < len && (buf[p2] ==
C0_GS || buf[p2] ==
C0_FS)) {
4225 c0__pf_indent(f, f->depth);
4226 while (run--) c0__sb_glyph(f->o,
C0_SUB);
4233 c0__pf_indent(f, f->depth + f->gdepth);
4234 pos = c0__pf_element(f, pos);
4242 c0__pf_indent(f, f->depth + f->gdepth);
4243 pos = c0__pf_element(f, pos);
4246 if (f->depth > 0) f->depth--;
4247 f->gdepth = c0__pf_pop(f);
4249 c0__pf_indent(f, f->depth + f->gdepth);
4250 c0__sb_glyph(f->o,
C0_CAN);
4253 pos = c0__pf_units(f, pos);
4255 c0__sb_byte(f->o,
'\n');
4261 c0__sb_glyph(f->o,
C0_EOT);
4262 c0__sb_byte(f->o,
'\n');
4267 size_t tail = len - pos - 1;
4269 c0__sb_glyph(f->o,
C0_EM);
4272 size_t k =
sizeof(num);
4275 num[--k] = (char)(
'0' + tail % 10);
4278 c0__sb_byte(f->o,
' ');
4279 c0__sb_cstr(f->o, num + k);
4280 c0__sb_cstr(f->o,
" bytes");
4282 c0__sb_byte(f->o,
'\n');
4290 pos = c0__pf_units(f, pos);
4291 c0__sb_byte(f->o,
'\n');
4299 if (!f->ls) c0__sb_byte(f->o,
'\n');
4304static int c0__glyph_at(
c0_bytes s,
size_t i, uint8_t *code) {
4305 if (i + 2 < s.
len && s.
ptr[i] == 0xE2 && s.
ptr[i + 1] == 0x90 && (s.
ptr[i + 2] & 0xE0) == 0x80) {
4306 *code = (uint8_t)(s.
ptr[i + 2] & 0x1F);
4312static int c0__starts_glyph(
c0_bytes s, uint8_t code) {
4314 return c0__glyph_at(s, 0, &c) && c == code;
4317static size_t c0__cp_count(
c0_bytes s) {
4319 for (i = 0; i < s.
len; i++) {
4320 if ((s.
ptr[i] & 0xC0) != 0x80) n++;
4325static size_t c0__utf8_adv(
c0_bytes s,
size_t i) {
4326 uint8_t c = s.
ptr[i];
4328 if ((c >> 5) == 0x6) adv = 2;
4329 else if ((c >> 4) == 0xE) adv = 3;
4330 else if ((c >> 3) == 0x1E) adv = 4;
4331 if (i + adv > s.
len) adv = s.
len - i;
4343static int c0__tl_push(c0__tline *t,
c0_bytes field,
int sep) {
4344 if (t->n == t->cap) {
4345 size_t ncap = t->cap ? t->cap * 2 : 8;
4350 ns = (
int *)realloc(t->sep_us, ncap *
sizeof(
int));
4355 t->fields[t->n] =
c0_trim(field);
4356 t->sep_us[t->n] = sep;
4364static int c0__parse_table_line(
c0_bytes text,
size_t line, c0__tline *t) {
4365 size_t i = 0, field_start;
4367 int in_text = 0, keyed;
4368 memset(t, 0,
sizeof(*t));
4370 while (i < text.
len && (text.
ptr[i] ==
' ' || text.
ptr[i] ==
'\t')) i++;
4371 if (i >= text.
len)
return 0;
4372 if (c0__glyph_at(text, i, &code) && code ==
C0_SOH) {
4374 if (!(c0__glyph_at(text, i, &code) && code ==
C0_RS))
return 0;
4376 }
else if (c0__glyph_at(text, i, &code) && code ==
C0_RS) {
4381 t->prefix = c0__slice(text.
ptr, 0, i);
4382 keyed = c0__glyph_at(text, i, &code) && code ==
C0_SOH;
4384 while (i < text.
len) {
4385 size_t adv = c0__utf8_adv(text, i);
4386 int is_glyph = c0__glyph_at(text, i, &code);
4388 if (is_glyph && code ==
C0_ETX) in_text = 0;
4390 if (is_glyph && code ==
C0_DLE && i < text.
len) i += c0__utf8_adv(text, i);
4391 }
else if (is_glyph && code ==
C0_STX) {
4394 }
else if (is_glyph && (code ==
C0_DLE || code ==
C0_CAN)) {
4396 }
else if (is_glyph && code ==
C0_US) {
4397 if (!c0__tl_push(t, c0__slice(text.
ptr, field_start, i), 1))
goto oom;
4400 }
else if (keyed && is_glyph && code ==
C0_SOH && i > t->prefix.len) {
4401 if (!c0__tl_push(t, c0__slice(text.
ptr, field_start, i), 0))
goto oom;
4408 if (in_text)
goto not_table;
4409 if (!c0__tl_push(t, c0__slice(text.
ptr, field_start, text.
len), 1))
goto oom;
4410 if (t->n < 2)
goto not_table;
4415 memset(t, 0,
sizeof(*t));
4420 memset(t, 0,
sizeof(*t));
4424static int c0__is_prefix_glyph(uint8_t c) {
4430static int c0__align(
const uint8_t *pretty,
size_t plen,
int spaced, c0__sb *out) {
4432 size_t nlines = 0, i, s = 0;
4433 c0__tline *tl = NULL;
4434 size_t ntl = 0, tlcap = 0;
4435 size_t *gstart = NULL;
4436 size_t ng = 0, gcap = 0;
4438 c0__sb *repl = NULL;
4440 const char *sp = spaced ?
" " :
"";
4443 for (i = 0; i <= plen; i++) {
4444 if (i == plen || pretty[i] ==
'\n') {
4445 if (i == plen && i == s)
break;
4451 lines[nlines++] = c0__slice(pretty, s, i);
4455 repl = (c0__sb *)calloc(nlines ? nlines : 1,
sizeof(c0__sb));
4456 if (!repl)
goto done;
4459 for (i = 0; i < nlines; i++) {
4462 if (trimmed.
len == 0 || c0__starts_glyph(trimmed,
C0_GS) || c0__starts_glyph(trimmed,
C0_FS) ||
4463 c0__starts_glyph(trimmed,
C0_EOT) || c0__starts_glyph(trimmed,
C0_SYN) || c0__starts_glyph(trimmed,
C0_CAN)) {
4465 }
else if (c0__starts_glyph(trimmed,
C0_SOH) || c0__starts_glyph(trimmed,
C0_RS)) {
4467 int r = c0__parse_table_line(lines[i], i, &t);
4468 if (r < 0)
goto done;
4473 size_t ncap = tlcap ? tlcap * 2 : 16;
4474 c0__tline *nt = (c0__tline *)realloc(tl, ncap *
sizeof(c0__tline));
4485 size_t ncap = gcap ? gcap * 2 : 16;
4486 size_t *ngs = (
size_t *)realloc(gstart, ncap *
sizeof(
size_t));
4503 if (close) in_group = 0;
4507 for (i = 0; i < ng; i++) {
4508 size_t from = gstart[i], to = (i + 1 < ng) ? gstart[i + 1] : ntl, j, c, cols = 0;
4510 for (j = from; j < to; j++) {
4511 if (tl[j].n > cols) cols = tl[j].n;
4513 widths = (
size_t *)calloc(cols ? cols : 1,
sizeof(
size_t));
4514 if (!widths)
goto done;
4515 for (j = from; j < to; j++) {
4516 for (c = 0; c < tl[j].n; c++) {
4517 size_t w = c0__cp_count(tl[j].fields[c]);
4518 if (w > widths[c]) widths[c] = w;
4521 for (j = from; j < to; j++) {
4522 c0__sb *o = &repl[tl[j].line];
4523 c0__sb_raw(o, tl[j].prefix.ptr, tl[j].prefix.len);
4525 for (c = 0; c < tl[j].n; c++) {
4526 c0__sb_raw(o, tl[j].fields[c].ptr, tl[j].fields[c].len);
4527 if (c + 1 < tl[j].n) {
4528 c0__sb_spaces(o, widths[c] - c0__cp_count(tl[j].fields[c]));
4529 if (tl[j].sep_us[c]) {
4531 c0__sb_glyph(o,
C0_US);
4534 c0__sb_byte(o,
' ');
4547 for (i = 0; i < nlines; i++) {
4549 size_t ws_end = 0, glyph_end;
4552 c0__sb *o = &repl[i];
4554 if (
c0_trim(text).len == 0)
continue;
4555 while (ws_end < text.
len && (text.
ptr[ws_end] ==
' ' || text.
ptr[ws_end] ==
'\t')) ws_end++;
4557 while (c0__glyph_at(text, glyph_end, &code) && c0__is_prefix_glyph(code)) glyph_end += 3;
4558 if (glyph_end == ws_end || glyph_end >= text.
len)
continue;
4559 rest = c0__slice(text.
ptr, glyph_end, text.
len);
4564 c0__sb_raw(o, text.
ptr, glyph_end);
4566 c0__sb_raw(o, rest.
ptr, rest.
len);
4567 if (o->oom)
goto done;
4570 for (i = 0; i < nlines; i++) {
4571 if (repl[i].d) c0__sb_raw(out, repl[i].d, repl[i].n);
4572 else c0__sb_raw(out, lines[i].ptr, lines[i].len);
4573 c0__sb_byte(out,
'\n');
4577 for (i = 0; i < ntl; i++) {
4584 for (i = 0; i < nlines; i++) free(repl[i].d);
4592 c0__sb o = {NULL, 0, 0, 0}, aligned = {NULL, 0, 0, 0};
4595 if (!err) err = &local;
4597 memset(&f, 0,
sizeof(f));
4601 f.indent = indent ? indent :
" ";
4602 f.ilen = strlen(f.indent);
4606 if (o.oom) c0__fail(err,
C0_ERR_OOM, 0,
"out of memory");
4615 c0__fail(err,
C0_ERR_OOM, 0,
"out of memory");
4624 c0__fail(err,
C0_ERR_OOM, 0,
"out of memory");
4627 if (outlen) *outlen = o.n - 1;
4632static unsigned c0__utf8(
const uint8_t *s,
size_t len,
size_t i,
size_t *adv) {
4637 }
else if ((c >> 5) == 0x6 && i + 1 < len) {
4639 return (
unsigned)((c & 0x1F) << 6) | (s[i + 1] & 0x3F);
4640 }
else if ((c >> 4) == 0xE && i + 2 < len) {
4642 return (
unsigned)((c & 0x0F) << 12) | ((
unsigned)(s[i + 1] & 0x3F) << 6) | (s[i + 2] & 0x3F);
4643 }
else if ((c >> 3) == 0x1E && i + 3 < len) {
4645 return (
unsigned)((c & 0x07) << 18) | ((
unsigned)(s[i + 1] & 0x3F) << 12) |
4646 ((unsigned)(s[i + 2] & 0x3F) << 6) | (s[i + 3] & 0x3F);
4654static size_t c0__pp_text(
const uint8_t *str,
size_t len,
size_t i, c0__sb *o) {
4657 unsigned cp = c0__utf8(str, len, i, &adv);
4658 if (cp >= 0x2400 && cp <= 0x241F) {
4659 uint8_t code = (uint8_t)(cp - 0x2400);
4660 c0__sb_byte(o, code);
4662 if (code ==
C0_DLE && i < len) {
4663 unsigned nx = c0__utf8(str, len, i, &adv);
4664 if (nx >= 0x2400 && nx <= 0x241F) {
4665 c0__sb_byte(o, (uint8_t)(nx - 0x2400));
4668 }
else if (code ==
C0_ETX) {
4672 c0__sb_raw(o, str + i, adv);
4679uint8_t *
c0_pretty_parse(
const char *str_,
size_t len,
size_t *outlen) {
4680 const uint8_t *str = (
const uint8_t *)str_;
4681 c0__sb o = {NULL, 0, 0, 0}, pending = {NULL, 0, 0, 0};
4686 unsigned cp = c0__utf8(str, len, i, &adv);
4687 if (cp >= 0x2400 && cp <= 0x241F) {
4688 uint8_t code = (uint8_t)(cp - 0x2400);
4690 c0__sb_byte(&o, code);
4693 i = c0__pp_text(str, len, i, &o);
4694 }
else if (code ==
C0_EM) {
4695 while (i < len && str[i] !=
'\n') i++;
4698 }
else if (cp ==
'\n' || cp ==
'\r') {
4702 }
else if (cp ==
' ' || cp ==
'\t') {
4703 if (!after_code) c0__sb_byte(&pending, (uint8_t)cp);
4707 c0__sb_raw(&o, pending.d, pending.n);
4711 c0__sb_raw(&o, str + i, adv);
4716 if (o.oom || pending.oom) {
4720 if (!o.d) o.d = (uint8_t *)malloc(1);
4721 if (outlen) *outlen = o.n;
c0_status c0_record_unit_get(c0_record r, const uint8_t *key, size_t klen, c0_unit *out)
The unit under a key with its sub-units (last wins), or C0_ERR_NOT_FOUND.
c0_status c0_record_unit(c0_record r, size_t n, c0_unit *out)
Unit n (0-based) with its sub-units, or C0_ERR_NOT_FOUND.
void c0_builder_gs_str(c0_builder *b, const char *name)
void c0_build_group(c0_builder *b, const uint8_t *name, size_t nlen)
GS name.
c0_status c0_value(c0_bytes raw, uint8_t *scratch, c0_bytes *out)
The logical value of a raw unit (as yielded by c0_next_unit, c0_next_pair, c0_record_field …): for a ...
void c0_builder_em(c0_builder *b, const uint8_t *tail, size_t tlen)
EM and the raw tail; nothing may follow.
void c0_builder_soh(c0_builder *b)
SOH alone: before RS, a header row.
int c0_body_bare(c0_body b)
Whether the body is a bare record stream: one unnamed file, at most the unnamed group.
c0_pretty_mode
Definition c0.h:685
@ C0_PRETTY_COMPACT
one element per line, no padding
Definition c0.h:686
@ C0_PRETTY_ALIGNED
table columns aligned
Definition c0.h:687
@ C0_PRETTY_SPACED
aligned, plus a space after each prefix and around ␟
Definition c0.h:688
size_t c0_record_count(c0_record r)
Number of positional units.
void c0_builder_bel_text(c0_builder *b, const uint8_t *text, size_t tlen)
BEL and a note; empty text is C0_ERR_BAD_STATE.
int c0_next_file(c0_file_iter *it, c0_file *out)
c0_bytes c0_trim(c0_bytes raw)
Layout trimmed from both ends of a raw slice.
void c0_build_field(c0_builder *b, const uint8_t *value, size_t vlen)
next unit of the open record (opens one if none)
c0_bytes c0_builder_bytes(const c0_builder *b)
char * c0_pretty_format(const uint8_t *buf, size_t len, const char *indent, c0_pretty_mode mode, size_t *outlen, c0_error *err)
Format compact bytes as pretty form: a malloc'd, NUL-terminated UTF-8 string the caller frees; *outle...
c0_status c0_container_tail(const uint8_t *buf, size_t len, c0_bytes *tail, int *has_em, c0_error *err)
Scan the whole container: *tail gets the raw region after EM (empty when there is none,...
void c0_build_key_level_open_str(c0_builder *b, const char *key)
#define C0_WARN_NO_SYN
Loud warnings a document reader raises (DESIGN.md "Warnings"), as bits.
Definition c0.h:98
c0_status c0_validate(const uint8_t *buf, size_t len, c0_error *err)
Check a whole buffer (one or more documents, an optional EM tail) against GRAMMAR....
c0_status c0_heading_get_str(const c0_document *d, const char *key, c0_bytes *raw)
#define C0_BEL
Comment: a note, or marks the next element dead.
Definition c0.h:47
int c0_next_unit_with_subs(c0_unit_iter *it, c0_unit *out)
The next positional unit with its sub-units (the walk of c0_next_unit).
void c0_build_ref(c0_builder *b, const uint8_t *name, size_t nlen)
ENQ name as the next unit: a group reference.
void c0_build_record(c0_builder *b, const c0_bytes *fields, size_t count)
RS and count units; count 0 opens an empty record.
void c0_builder_enq_str(c0_builder *b, const char *name)
void c0_builder_gs_name(c0_builder *b, const uint8_t *name, size_t nlen)
GS name; ends any open record.
void c0_builder_init(c0_builder *b)
#define C0_CR
Definition c0.h:62
size_t c0_decode_text(const uint8_t *inside, size_t len, uint8_t *out)
Decode the inside of a text (the bytes between STX and ETX): DLE escapes removed.
#define C0_GS
Group separator.
Definition c0.h:55
c0_status c0_record_get_str(c0_record r, const char *key, c0_bytes *raw)
c0_group_iter c0_file_groups(c0_file f)
Groups of one file.
void c0_build_field_str(c0_builder *b, const char *value)
void c0_build_etb(c0_builder *b, const uint8_t *payload, size_t plen)
ETB and an optional plain payload (NULL for none).
c0_body c0_body_view(const uint8_t *buf, size_t len)
A body over a whole buffer (a slice with no SYN: records, groups, files), unchecked: pair it with c0_...
size_t c0_scan_text(const uint8_t *buf, size_t pos, size_t stop, c0_error *err)
At an STX: the position after the matching ETX.
void c0_build_ref_record(c0_builder *b, const uint8_t *group, size_t glen, const uint8_t *record, size_t rlen, const uint8_t *field, size_t flen)
␅␐␝group␞record[␟field]␘ as the next unit (field NULL to omit)
int c0_ref_group(const c0_ref *r, c0_bytes *name)
Whether the reference names a group as a whole (a plain name, or a pattern that is a bare group line)...
c0_status c0_blob(const uint8_t *buf, size_t len, const uint8_t *name, size_t nlen, c0_bytes *out, c0_error *err)
The bytes of a named blob from the tail, located by the first document's blob table (columns matched ...
int c0_canonical(const uint8_t *buf, size_t len)
Whether bytes are a canonical span (GRAMMAR.md "Canonical constraints"): well-formed,...
c0_record_iter c0_body_records(c0_body b)
Records of a bare body (the unnamed group's).
#define C0_WARN_DUPLICATE_KEY
a duplicate heading key (last wins)
Definition c0.h:101
int c0_next_group(c0_group_iter *it, c0_group *out)
void c0_build_header(c0_builder *b, const c0_bytes *names, size_t count)
SOH RS name US name …
void c0_build_syn(c0_builder *b)
SYN; keyed units until the next element are the heading.
void c0_build_section_str(c0_builder *b, const char *name, size_t depth)
int c0_is_name(const uint8_t *p, size_t len)
Whether bytes are a legal name (label, key, header name): same test as plain.
c0_sub_iter c0_unit_subs(c0_unit u)
void c0_build_key_level_open(c0_builder *b, const uint8_t *key, size_t klen)
SOH key US DLE.
void c0_build_list_str(c0_builder *b, const char *const *items, size_t count)
void c0_build_record_str(c0_builder *b, const char *const *fields, size_t count)
void c0_build_key(c0_builder *b, const uint8_t *key, size_t klen, const uint8_t *value, size_t vlen)
SOH key US value, in the open record or the heading.
size_t c0_scan_comment(const uint8_t *buf, size_t pos, size_t stop, c0_error *err)
At a BEL: past the note, or past the whole dead element's own line.
c0_record_iter c0_group_records(c0_group g)
c0_stream c0_stream_read(const uint8_t *buf, size_t len)
#define C0_LF
Definition c0.h:61
#define C0_ENQ
Reference: a group name, or a pattern (ENQ DLE … CAN).
Definition c0.h:46
c0_status c0_heading_blob(const c0_document *d, c0_body *out)
The blob table (a level) as a body, or C0_ERR_NOT_FOUND / C0_ERR_NOT_LEVEL.
#define C0_EOT
End of document.
Definition c0.h:45
#define C0_FS
File separator.
Definition c0.h:54
size_t c0_unit_sub_count(c0_unit u)
Number of direct sub-units.
void c0_builder_us_str(c0_builder *b, const char *value)
c0_status c0_body_read(const uint8_t *buf, size_t len, c0_body *out, c0_error *err)
Parse a slice as a body (no SYN, heading or EOT): the grammar is applied to all of it; C0_OK and *out...
c0_status c0_builder_status(const c0_builder *b)
uint8_t * c0_pretty_parse(const char *str, size_t len, size_t *outlen)
Parse pretty form back to compact bytes: glyphs become codes; outside text whitespace touching a glyp...
c0_pair_iter c0_record_pairs(c0_record r)
Keyed units in written order, duplicates included, dead units skipped.
void c0_builder_soh_str(c0_builder *b, const char *name)
int c0_group_header(c0_group g, c0_record *out)
The group's header row as a record (its units are the header names).
c0_bytes c0_doc_iter_tail(const c0_doc_iter *it)
After the cursor is exhausted: the raw bytes after EM (empty without EM).
#define C0_SOH
Heading: a key for the value after it; SOH RS a header row.
Definition c0.h:42
void c0_build_map_str(c0_builder *b, const char *const *keys, const char *const *values, size_t count)
#define C0_WARN_NO_EOT
no EOT and the shape is not stream
Definition c0.h:99
void c0_build_em(c0_builder *b, const uint8_t *tail, size_t tlen)
EM and the raw tail; nothing may follow.
void c0_builder_rs_value(c0_builder *b, const uint8_t *value, size_t vlen)
RS and the record's first value.
void c0_builder_rs(c0_builder *b)
RS: open a record.
int c0_next_record(c0_record_iter *it, c0_record *out)
#define C0_ETX
End of text.
Definition c0.h:44
void c0_builder_fs_str(c0_builder *b, const char *name)
c0_status c0_record_field(c0_record r, size_t n, c0_bytes *raw)
Raw unit n (0-based), or C0_ERR_NOT_FOUND.
void c0_builder_enq(c0_builder *b, const uint8_t *name, size_t nlen)
ENQ name: a group reference.
int c0_next_document(c0_doc_iter *it, c0_document *out)
c0_unit_kind
What a raw unit holds, by its first non-layout byte.
Definition c0.h:165
@ C0_UNIT_REF
ENQ name, or ENQ DLE … CAN (a pattern).
Definition c0.h:169
@ C0_UNIT_TEXT
STX … ETX.
Definition c0.h:167
@ C0_UNIT_LEVEL
DLE … CAN.
Definition c0.h:168
@ C0_UNIT_PLAIN
a plain value, the empty value, or a unit holding only a note
Definition c0.h:166
void c0_builder_rs_str(c0_builder *b, const char *value)
#define C0_CAN
Close a nested level.
Definition c0.h:51
void c0_build_note(c0_builder *b, const uint8_t *text, size_t tlen)
BEL and the note (plain, or text when it must be).
void c0_builder_dle(c0_builder *b)
DLE: open a level (saves the record state).
c0_status c0_blob_str(const uint8_t *buf, size_t len, const char *name, c0_bytes *out, c0_error *err)
c0_bytes c0_bytes_of(const uint8_t *ptr, size_t len)
A view over len bytes at ptr.
c0_bytes c0_heading_shape(const c0_document *d)
The shape value (decoded, must be plain), or an empty slice when absent.
#define C0_SP
Definition c0.h:63
c0_bytes c0_file_name(c0_file f)
void c0_builder_etb_str(c0_builder *b, const char *payload)
void c0_build_item(c0_builder *b, const uint8_t *text, size_t tlen)
a list item: the next unit
c0_bytes c0_stream_committed(const c0_stream *s)
size_t c0_ref_path(const c0_ref *r, c0_bytes *steps, size_t max)
The steps of a reference in walking order: the group name alone, or the pattern's file,...
int c0_next_block(c0_block_iter *it, c0_bytes *out, int *damaged)
int c0_is_plain(const uint8_t *p, size_t len)
Whether a value can be written plain: no byte below 0x20, no edge whitespace.
int c0_is_text_escaped(uint8_t byte)
Whether a byte is one of the seven codes that take a DLE inside text (STX ETX DLE ETB EOT SYN EM).
int c0_next_pair(c0_pair_iter *it, c0_bytes *key, c0_bytes *raw)
c0_status
Status codes shared by every part of the library.
Definition c0.h:72
@ C0_ERR_NOT_LEVEL
c0_unit_level on a unit that is not DLE … CAN
Definition c0.h:79
@ C0_ERR_TOO_DEEP
nesting deeper than the parser's limit
Definition c0.h:77
@ C0_ERR_NOT_REFERENCE
c0_unit_ref on a unit that is not ENQ …
Definition c0.h:81
@ C0_ERR_NOT_SCALAR
c0_value on a level or reference
Definition c0.h:78
@ C0_ERR_UNASSIGNED
an unassigned control code outside text
Definition c0.h:75
@ C0_ERR_NOT_LIST
c0_unit_list on a level that is not a list
Definition c0.h:80
@ C0_ERR_SYNTAX
malformed input (see c0_error.msg / .pos)
Definition c0.h:74
@ C0_ERR_BAD_NAME
builder: a name with a control byte or edge space
Definition c0.h:84
@ C0_OK
Definition c0.h:73
@ C0_ERR_BAD_STATE
builder: e.g.
Definition c0.h:85
@ C0_ERR_UNEXPECTED_END
input ended inside text, a level, or after DLE/BEL
Definition c0.h:76
@ C0_ERR_NOT_FOUND
no such group / key / field / blob
Definition c0.h:83
@ C0_ERR_OOM
allocation failed
Definition c0.h:86
@ C0_ERR_NEEDS_DECODE
c0_value without scratch on text with escapes
Definition c0.h:82
void c0_build_level_close(c0_builder *b)
CAN.
void c0_build_block_str(c0_builder *b, const char *text)
size_t c0_scan_level(const uint8_t *buf, size_t pos, size_t stop, c0_error *err)
At a DLE: the position after the matching CAN.
c0_status c0_document_read(const uint8_t *buf, size_t len, size_t start, c0_document *out, c0_error *err)
Read the document starting at start (after any layout), applying the whole grammar to it: C0_OK,...
void c0_build_sub_unit(c0_builder *b, const uint8_t *value, size_t vlen, size_t depth)
SUB×depth US value: a sub-unit of the unit just written (depth 2: of the sub-unit before); not a unit...
void c0_build_map(c0_builder *b, const c0_bytes *keys, const c0_bytes *values, size_t count)
RS and keyed units in the order given.
c0_bytes c0_stream_tail(const c0_stream *s)
c0_bytes c0_group_name(c0_group g)
size_t c0_scan_plain(const uint8_t *buf, size_t pos, size_t stop)
Skip a run of bytes >= 0x20 (a label, key, payload or plain value with its layout).
void c0_builder_bel_str(c0_builder *b, const char *text)
c0_status c0_record_unit_get_str(c0_record r, const char *key, c0_unit *out)
void c0_builder_soh_name(c0_builder *b, const uint8_t *name, size_t nlen)
SOH key.
int c0_next_unit(c0_unit_iter *it, c0_bytes *raw)
size_t c0_stream_last_commit_end(const uint8_t *buf, size_t len)
Position just past the last commit, found by a backward scan (a shortcut for well-formed input; c0_st...
size_t c0_scan_record_end(const uint8_t *buf, size_t pos, size_t stop, c0_error *err)
From just after an RS: the end of the record (a sub-unit, a SUB run then US, stays inside; a SUB run ...
#define C0_RS
Record separator.
Definition c0.h:56
#define C0_EM
End of medium: a raw blob follows.
Definition c0.h:52
size_t c0_stream_before_damage(const c0_stream *s, uint8_t *out)
The same, stopping at the first damaged block: the default reading, which delivers the records before...
void c0_builder_us_value(c0_builder *b, const uint8_t *value, size_t vlen)
US and the value that follows it.
#define C0_US
Unit separator.
Definition c0.h:57
void c0_builder_sub(c0_builder *b)
SUB: the element after the next separator is subordinate to the one before (before GS depth,...
void c0_build_group_str(c0_builder *b, const char *name)
void c0_build_note_str(c0_builder *b, const char *text)
c0_status c0_group_column(c0_group g, const uint8_t *name, size_t nlen, size_t *index)
Index of a header name in the group's header row, or C0_ERR_NOT_FOUND.
c0_status c0_body_find_group_str(c0_body b, const char *name, c0_group *out)
c0_file_iter c0_body_files(c0_body b)
void c0_build_header_str(c0_builder *b, const char *const *names, size_t count)
void c0_builder_etb(c0_builder *b)
ETB alone.
void c0_builder_gs(c0_builder *b)
GS alone: an empty-named group (␝␝b is group "" then group b).
void c0_build_ref_open(c0_builder *b)
ENQ DLE as the next unit: a pattern; write its lines, then c0_build_level_close.
void c0_builder_em_str(c0_builder *b, const char *tail)
void c0_builder_etb_payload(c0_builder *b, const uint8_t *payload, size_t plen)
ETB and a plain payload.
c0_unit_iter c0_record_units(c0_record r)
The record's positional units.
void c0_build_block(c0_builder *b, const uint8_t *text, size_t tlen)
a paragraph: a record of one unit
#define C0_WARN_UNKNOWN_SHAPE
a shape other than diff or stream
Definition c0.h:102
void c0_builder_can(c0_builder *b)
CAN: close the innermost level; C0_ERR_BAD_STATE if none is open.
void c0_build_key_str(c0_builder *b, const char *key, const char *value)
int c0_record_keyed(c0_record r)
Whether the record is keyed (it holds a keyed unit, live or dead).
int c0_is_layout(uint8_t byte)
Whether a byte is layout outside text (SP, HT, LF, CR).
#define C0_HT
Definition c0.h:60
void c0_build_sub_unit_str(c0_builder *b, const char *value, size_t depth)
void c0_build_file_str(c0_builder *b, const char *name)
size_t c0_stream_undamaged(const c0_stream *s, uint8_t *out)
Copy the undamaged committed blocks, without their ETB markers and payloads, into out (room for commi...
void c0_build_etb_str(c0_builder *b, const char *payload)
c0_pair_iter c0_document_heading(const c0_document *d)
The heading's keyed units, in written order (dead units skipped).
c0_group_iter c0_body_groups(c0_body b)
Groups of every file of a body, in order (FS lines are skipped).
void c0_build_level_open(c0_builder *b)
DLE: a nested level as the next unit; inside, the builder is at the top of a fresh body.
void c0_builder_bel(c0_builder *b)
BEL alone: the element that follows is dead.
#define C0_WARN_UNKNOWN_VERSION
a version other than 1
Definition c0.h:103
c0_status c0_unit_ref(c0_bytes raw, c0_ref *out)
A raw unit as a reference.
void c0_builder_us(c0_builder *b)
US alone (an empty unit, or before DLE / ENQ).
#define C0_STX
Start of text (a literal value).
Definition c0.h:43
void c0_builder_syn(c0_builder *b)
SYN; soh_name + us_value pairs that follow are the heading.
c0_status c0_unit_list(c0_bytes raw, c0_unit_iter *items)
A raw unit as a flat list: a level holding at most one positional record.
int c0_next_pair_with_subs(c0_pair_iter *it, c0_bytes *key, c0_unit *out)
The same, the value as a unit with its sub-units.
#define C0_DLE
Recurse (opens a nested level) outside text; escape inside.
Definition c0.h:48
#define C0_WARN_UNKNOWN_KEY
a heading key the reader does not know
Definition c0.h:100
void c0_build_eot(c0_builder *b)
EOT.
int c0_heading_version(const c0_document *d)
The version value as an integer; 1 when absent or not a number.
c0_block_iter c0_stream_blocks(const c0_stream *s)
size_t c0_scan_layout(const uint8_t *buf, size_t pos, size_t stop)
Skip layout bytes.
#define C0_ETB
Commit marker (stream mode).
Definition c0.h:50
void c0_builder_fs(c0_builder *b, const uint8_t *name, size_t nlen)
FS name; ends any open record.
void c0_build_item_str(c0_builder *b, const char *text)
size_t c0_scan_unit(const uint8_t *buf, size_t pos, size_t stop, c0_error *err)
From just after a US (or a key's US): the end of the unit's value (its sub-units, SUB … US …,...
void c0_builder_free(c0_builder *b)
c0_status c0_record_get(c0_record r, const uint8_t *key, size_t klen, c0_bytes *raw)
Raw value for a key (last wins), or C0_ERR_NOT_FOUND.
c0_unit_kind c0_unit_type(c0_bytes raw)
void c0_builder_eot(c0_builder *b)
EOT.
int c0_next_sub(c0_sub_iter *it, c0_unit *out)
void c0_build_ref_record_str(c0_builder *b, const char *group, const char *record, const char *field)
void c0_build_section(c0_builder *b, const uint8_t *name, size_t nlen, size_t depth)
SUB×(depth−1) GS name: a group at depth (1 is a plain GS line).
c0_status c0_unit_level(c0_bytes raw, c0_body *out)
A raw unit as a nested level: the body between its DLE and CAN.
void c0_builder_enq_dle(c0_builder *b)
ENQ DLE: a pattern; write its lines, then c0_builder_can.
c0_status c0_body_find_group(c0_body b, const uint8_t *name, size_t nlen, c0_group *out)
First group with the given name in the body (any file), or C0_ERR_NOT_FOUND.
#define C0_SYN
Start of document, then the heading.
Definition c0.h:49
void c0_build_file(c0_builder *b, const uint8_t *name, size_t nlen)
FS name.
c0_status c0_heading_get(const c0_document *d, const uint8_t *key, size_t klen, c0_bytes *raw)
Raw heading value for a key (last wins), or C0_ERR_NOT_FOUND.
void c0_build_ref_str(c0_builder *b, const char *name)
#define C0_SUB
Subordinate: the element after is a child of the element before (GS: subsection, US: sub-unit).
Definition c0.h:53
c0_doc_iter c0_documents(const uint8_t *buf, size_t len)
void c0_build_list(c0_builder *b, const c0_bytes *items, size_t count)
a flat list as the next unit: DLE RS item US item … CAN, or DLE CAN
int c0_is_assigned(uint8_t byte)
Whether a byte is one of the sixteen assigned codes.
Cursor over the committed blocks (previous commit to ETB, marker and payload excluded); *damaged is s...
Definition c0.h:655
int in_text
Definition c0.h:660
size_t index
Definition c0.h:661
size_t pos
Definition c0.h:658
c0_parse_state st
Definition c0.h:662
const uint8_t * buf
Definition c0.h:656
size_t stop
Definition c0.h:657
size_t block_start
Definition c0.h:659
A body: a document body or the inside of a nested level, as a range of the buffer.
Definition c0.h:188
size_t stop
Definition c0.h:191
const uint8_t * buf
Definition c0.h:189
size_t start
Definition c0.h:190
Writes C0DATA in canonical compact form into a growable buffer it owns; call c0_builder_free when don...
Definition c0.h:508
size_t stack_cap
internal
Definition c0.h:517
size_t depth
internal: open levels
Definition c0.h:516
int * stack
internal: saved state per open level
Definition c0.h:515
size_t units
internal: units written in the open record
Definition c0.h:514
size_t len
Definition c0.h:510
uint8_t * data
Definition c0.h:509
int state
internal: top / heading / record
Definition c0.h:513
c0_status status
Definition c0.h:512
size_t cap
Definition c0.h:511
A zero-copy view into a buffer: not NUL-terminated.
Definition c0.h:66
const uint8_t * ptr
Definition c0.h:67
size_t len
Definition c0.h:68
Cursor over the documents of a container (a file or message): each begins with SYN except possibly th...
Definition c0.h:450
const uint8_t * buf
Definition c0.h:451
size_t em_pos
Definition c0.h:455
c0_error err
Definition c0.h:457
size_t pos
Definition c0.h:453
int done
Definition c0.h:456
size_t len
Definition c0.h:452
int has_em
Definition c0.h:454
One document: an optional SYN heading, a body, an optional EOT.
Definition c0.h:419
c0_body body
Definition c0.h:427
size_t stop
after the EOT, or where the document ends
Definition c0.h:422
size_t start
Definition c0.h:421
const uint8_t * buf
Definition c0.h:420
size_t dead_elements
dead records, header rows, groups and files met in the body
Definition c0.h:429
size_t heading_start
the keyed units after SYN …
Definition c0.h:425
int has_syn
Definition c0.h:423
size_t heading_end
… up to the first body code
Definition c0.h:426
int has_eot
Definition c0.h:424
unsigned warnings
C0_WARN_* bits.
Definition c0.h:428
Error detail: the code, the byte offset it was found at, and a static message.
Definition c0.h:91
const char * msg
Definition c0.h:94
size_t pos
Definition c0.h:93
c0_status code
Definition c0.h:92
Cursor over the files of a body as the result model has them.
Definition c0.h:355
size_t stop
Definition c0.h:358
int done
Definition c0.h:360
size_t pos
Definition c0.h:357
c0_error err
Definition c0.h:361
int first
Definition c0.h:359
const uint8_t * buf
Definition c0.h:356
A file within a body: its name and the range of its contents.
Definition c0.h:326
const uint8_t * buf
Definition c0.h:327
size_t start
Definition c0.h:330
size_t name_end
Definition c0.h:329
size_t name_start
Definition c0.h:328
int leading
Definition c0.h:332
size_t stop
Definition c0.h:331
Cursor over groups: every GS line at any depth, in order, plus the unnamed leading group of a file or...
Definition c0.h:371
size_t pos
Definition c0.h:373
int first
Definition c0.h:375
size_t stop
Definition c0.h:374
int done
Definition c0.h:376
c0_error err
Definition c0.h:377
const uint8_t * buf
Definition c0.h:372
A group: its depth (one plus the SUB prefix of its GS line: ␝a 1, ␚␝b 2; 0 for the unnamed group at t...
Definition c0.h:339
size_t stop
Definition c0.h:345
size_t name_end
Definition c0.h:343
size_t name_start
Definition c0.h:342
size_t depth
Definition c0.h:341
const uint8_t * buf
Definition c0.h:340
size_t start
Definition c0.h:344
Cursor over keyed units (a keyed record, or a document heading).
Definition c0.h:296
c0_error err
Definition c0.h:300
size_t stop
Definition c0.h:299
size_t pos
Definition c0.h:298
const uint8_t * buf
Definition c0.h:297
Parser state carried from one committed block to the next (every field is internal).
Definition c0.h:618
int phase
Definition c0.h:619
size_t nfiles
Definition c0.h:628
int header_ok
Definition c0.h:626
int first
Definition c0.h:620
int leading
Definition c0.h:622
int dead_file
Definition c0.h:623
int dead_depth
Definition c0.h:624
int in_level
Definition c0.h:621
int file_has_groups
Definition c0.h:627
int prev_depth
Definition c0.h:625
Cursor over the live records of a group (header rows, notes, commits and dead records skipped).
Definition c0.h:400
c0_error err
Definition c0.h:405
size_t stop
Definition c0.h:403
int done
Definition c0.h:404
size_t pos
Definition c0.h:402
const uint8_t * buf
Definition c0.h:401
A record: the bytes after its RS, trailing layout excluded.
Definition c0.h:232
const uint8_t * buf
Definition c0.h:233
size_t stop
Definition c0.h:235
size_t start
Definition c0.h:234
A parsed reference (DESIGN.md "References"): a group name written plain, or a pattern,...
Definition c0.h:208
c0_bytes name
the group name of a plain reference (empty for a pattern)
Definition c0.h:209
c0_body pattern
the pattern's body (the inside of its level) when is_pattern
Definition c0.h:210
int is_pattern
Definition c0.h:211
A scan of an append-only log.
Definition c0.h:636
size_t len
Definition c0.h:638
int torn
Definition c0.h:640
size_t first_damaged
index of the first damaged block
Definition c0.h:644
const uint8_t * buf
Definition c0.h:637
size_t damaged_count
Definition c0.h:642
size_t resume_offset
the offset after that block's ETB and payload, where reading can go on
Definition c0.h:645
size_t committed_end
just past the last ETB and its payload
Definition c0.h:639
size_t block_count
Definition c0.h:641
int has_damaged
whether any block is damaged
Definition c0.h:643
Cursor over the direct sub-units of a unit (those one deeper than it), in written order; each comes w...
Definition c0.h:277
size_t depth
the parent's depth
Definition c0.h:281
c0_error err
Definition c0.h:282
size_t stop
Definition c0.h:280
const uint8_t * buf
Definition c0.h:278
size_t pos
Definition c0.h:279
Cursor over the positional units of a record, or the items of a list.
Definition c0.h:242
c0_error err
Definition c0.h:247
const uint8_t * buf
Definition c0.h:243
size_t stop
Definition c0.h:245
int done
Definition c0.h:246
size_t pos
Definition c0.h:244
A unit with its sub-units (DESIGN.md "Subordinates"): SUB before US makes the unit after a child of t...
Definition c0.h:262
c0_bytes raw
the value with its layout: pass to c0_value / c0_unit_level / c0_unit_ref
Definition c0.h:263
const uint8_t * buf
Definition c0.h:264
size_t subs_start
its sub-units: [subs_start, subs_end), SUB runs each followed by US and a value
Definition c0.h:265
size_t depth
0 for a unit of a record; a sub-unit's depth is the length of its SUB run
Definition c0.h:267
size_t subs_end
Definition c0.h:266