2017-07-03 17:44:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Begin building a string */
|
2017-11-28 23:27:55 +00:00
|
|
|
uint8_t *dst_string_begin(int32_t length) {
|
|
|
|
char *data = dst_alloc(DST_MEMORY_STRING, 2 * sizeof(int32_t) + length);
|
|
|
|
uint8_t *str = (uint8_t *) (data + 2 * sizeof(int32_t));
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_string_length(str) = length;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish building a string */
|
2017-11-06 03:05:47 +00:00
|
|
|
const uint8_t *dst_string_end(uint8_t *str) {
|
|
|
|
dst_string_hash(str) = dst_string_calchash(str, dst_string_length(str));
|
2017-11-27 19:03:34 +00:00
|
|
|
return str;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a buffer as a string */
|
2017-11-28 23:27:55 +00:00
|
|
|
const uint8_t *dst_string(const uint8_t *buf, int32_t len) {
|
|
|
|
int32_t hash = dst_string_calchash(buf, len);
|
|
|
|
char *data = dst_alloc(DST_MEMORY_STRING, 2 * sizeof(int32_t) + len);
|
|
|
|
uint8_t *str = (uint8_t *) (data + 2 * sizeof(int32_t));
|
2017-11-27 19:03:34 +00:00
|
|
|
memcpy(str, buf, len);
|
|
|
|
dst_string_length(str) = len;
|
|
|
|
dst_string_hash(str) = hash;
|
|
|
|
return str;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Compare two strings */
|
|
|
|
int dst_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t xlen = dst_string_length(lhs);
|
|
|
|
int32_t ylen = dst_string_length(rhs);
|
|
|
|
int32_t len = xlen > ylen ? ylen : xlen;
|
|
|
|
int32_t i;
|
2017-11-27 19:03:34 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (lhs[i] == rhs[i]) {
|
|
|
|
continue;
|
|
|
|
} else if (lhs[i] < rhs[i]) {
|
|
|
|
return -1; /* x is less than y */
|
|
|
|
} else {
|
|
|
|
return 1; /* y is less than x */
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 19:03:34 +00:00
|
|
|
if (xlen == ylen) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return xlen < ylen ? -1 : 1;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Compare a dst string with a piece of memory */
|
2017-11-28 23:27:55 +00:00
|
|
|
int dst_string_equalconst(const uint8_t *lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash) {
|
|
|
|
int32_t index;
|
|
|
|
int32_t lhash = dst_string_hash(lhs);
|
|
|
|
int32_t llen = dst_string_length(lhs);
|
2017-11-27 19:03:34 +00:00
|
|
|
if (lhs == rhs)
|
|
|
|
return 1;
|
|
|
|
if (lhash != rhash || llen != rlen)
|
|
|
|
return 0;
|
|
|
|
for (index = 0; index < llen; index++) {
|
|
|
|
if (lhs[index] != rhs[index])
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-27 19:03:34 +00:00
|
|
|
return 1;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Check if two strings are equal */
|
|
|
|
int dst_string_equal(const uint8_t *lhs, const uint8_t *rhs) {
|
|
|
|
return dst_string_equalconst(lhs, rhs,
|
|
|
|
dst_string_length(rhs), dst_string_hash(rhs));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a c string */
|
2017-11-06 03:05:47 +00:00
|
|
|
const uint8_t *dst_cstring(const char *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
while (str[len]) ++len;
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_string((const uint8_t *)str, len);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-07-03 17:44:58 +00:00
|
|
|
|
|
|
|
/* Temporary buffer size */
|
2017-09-09 18:39:51 +00:00
|
|
|
#define DST_BUFSIZE 36
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t real_to_string_impl(uint8_t *buf, double x) {
|
2017-11-27 00:31:40 +00:00
|
|
|
int count = snprintf((char *) buf, DST_BUFSIZE, "%.21g", x);
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) count;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static void real_to_string_b(DstBuffer *buffer, double x) {
|
|
|
|
dst_buffer_ensure(buffer, buffer->count + DST_BUFSIZE);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->count += real_to_string_impl(buffer->data + buffer->count, x);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static const uint8_t *real_to_string(double x) {
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t buf[DST_BUFSIZE];
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_string(buf, real_to_string_impl(buf, x));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t integer_to_string_impl(uint8_t *buf, int64_t x) {
|
2017-07-03 17:44:58 +00:00
|
|
|
int neg = 0;
|
|
|
|
uint8_t *hi, *low;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t count = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (x == 0) {
|
|
|
|
buf[0] = '0';
|
|
|
|
return 1;
|
|
|
|
}
|
2017-07-03 17:44:58 +00:00
|
|
|
if (x < 0) {
|
|
|
|
neg = 1;
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
while (x > 0) {
|
|
|
|
uint8_t digit = x % 10;
|
|
|
|
buf[count++] = '0' + digit;
|
|
|
|
x /= 10;
|
|
|
|
}
|
|
|
|
if (neg)
|
|
|
|
buf[count++] = '-';
|
|
|
|
/* Reverse */
|
|
|
|
hi = buf + count - 1;
|
|
|
|
low = buf;
|
|
|
|
while (hi > low) {
|
|
|
|
uint8_t temp = *low;
|
|
|
|
*low++ = *hi;
|
|
|
|
*hi-- = temp;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
return count;
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static void integer_to_string_b(DstBuffer *buffer, int64_t x) {
|
|
|
|
dst_buffer_extra(buffer, DST_BUFSIZE);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->count += integer_to_string_impl(buffer->data + buffer->count, x);
|
|
|
|
}
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static const uint8_t *integer_to_string(int64_t x) {
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t buf[DST_BUFSIZE];
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_string(buf, integer_to_string_impl(buf, x));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
#define HEX(i) (((uint8_t *) dst_base64)[(i)])
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Returns a string description for a pointer. Truncates
|
|
|
|
* title to 12 characters */
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t string_description_impl(uint8_t *buf, const char *title, void *pointer) {
|
2017-07-03 17:44:58 +00:00
|
|
|
uint8_t *c = buf;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2017-07-03 17:44:58 +00:00
|
|
|
union {
|
|
|
|
uint8_t bytes[sizeof(void *)];
|
|
|
|
void *p;
|
|
|
|
} pbuf;
|
|
|
|
|
|
|
|
pbuf.p = pointer;
|
|
|
|
*c++ = '<';
|
2017-11-01 21:53:43 +00:00
|
|
|
for (i = 0; title[i] && i < 12; ++i)
|
2017-07-03 17:44:58 +00:00
|
|
|
*c++ = ((uint8_t *)title) [i];
|
|
|
|
*c++ = ' ';
|
|
|
|
*c++ = '0';
|
|
|
|
*c++ = 'x';
|
|
|
|
for (i = sizeof(void *); i > 0; --i) {
|
|
|
|
uint8_t byte = pbuf.bytes[i - 1];
|
|
|
|
if (!byte) continue;
|
|
|
|
*c++ = HEX(byte >> 4);
|
|
|
|
*c++ = HEX(byte & 0xF);
|
|
|
|
}
|
|
|
|
*c++ = '>';
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) (c - buf);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static void string_description_b(DstBuffer *buffer, const char *title, void *pointer) {
|
|
|
|
dst_buffer_ensure(buffer, buffer->count + DST_BUFSIZE);
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->count += string_description_impl(buffer->data + buffer->count, title, pointer);
|
|
|
|
}
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Describes a pointer with a title (string_description("bork", myp) returns
|
|
|
|
* a string "<bork 0x12345678>") */
|
|
|
|
static const uint8_t *string_description(const char *title, void *pointer) {
|
2017-11-01 21:53:43 +00:00
|
|
|
uint8_t buf[DST_BUFSIZE];
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_string(buf, string_description_impl(buf, title, pointer));
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
#undef HEX
|
2017-09-09 18:39:51 +00:00
|
|
|
#undef DST_BUFSIZE
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t dst_escape_string_length(const uint8_t *str) {
|
|
|
|
int32_t len = 2;
|
|
|
|
int32_t i;
|
2017-09-09 18:39:51 +00:00
|
|
|
for (i = 0; i < dst_string_length(str); ++i) {
|
2017-11-01 21:53:43 +00:00
|
|
|
switch (str[i]) {
|
|
|
|
case '"':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\0':
|
|
|
|
len += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dst_escape_string_impl(uint8_t *buf, const uint8_t *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, j;
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[0] = '"';
|
|
|
|
for (i = 0, j = 1; i < dst_string_length(str); ++i) {
|
2017-07-03 17:44:58 +00:00
|
|
|
uint8_t c = str[i];
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '\\';
|
|
|
|
buf[j++] = '"';
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '\\';
|
|
|
|
buf[j++] = 'n';
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
case '\r':
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '\\';
|
|
|
|
buf[j++] = 'r';
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
case '\0':
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '\\';
|
|
|
|
buf[j++] = '0';
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = c;
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '"';
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_escape_string_b(DstBuffer *buffer, const uint8_t *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = dst_escape_string_length(str);
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_buffer_extra(buffer, len);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_escape_string_impl(buffer->data + buffer->count, str);
|
|
|
|
buffer->count += len;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
const uint8_t *dst_escape_string(const uint8_t *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = dst_escape_string_length(str);
|
2017-11-06 03:05:47 +00:00
|
|
|
uint8_t *buf = dst_string_begin(len);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_escape_string_impl(buf, str);
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_string_end(buf);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns a string pointer with the description of the string */
|
2017-11-06 03:05:47 +00:00
|
|
|
const uint8_t *dst_short_description(DstValue x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(x)) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_NIL:
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_cstring("nil");
|
2017-11-28 23:27:55 +00:00
|
|
|
case DST_TRUE:
|
|
|
|
return dst_cstring("true");
|
|
|
|
case DST_FALSE:
|
|
|
|
return dst_cstring("false");
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_REAL:
|
2017-11-28 23:27:55 +00:00
|
|
|
return real_to_string(dst_unwrap_real(x));
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_INTEGER:
|
2017-11-28 23:27:55 +00:00
|
|
|
return integer_to_string(dst_unwrap_integer(x));
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_SYMBOL:
|
2017-11-28 23:27:55 +00:00
|
|
|
return dst_unwrap_string(x);
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_STRING:
|
2017-11-28 23:27:55 +00:00
|
|
|
return dst_escape_string(dst_unwrap_string(x));
|
2017-09-09 18:39:51 +00:00
|
|
|
case DST_USERDATA:
|
2017-11-28 23:27:55 +00:00
|
|
|
return string_description(dst_userdata_type(dst_unwrap_pointer(x))->name, dst_unwrap_pointer(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
default:
|
2017-11-28 23:27:55 +00:00
|
|
|
return string_description(dst_type_names[dst_type(x)], dst_unwrap_pointer(x));
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_short_description_b(DstBuffer *buffer, DstValue x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(x)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_NIL:
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_buffer_push_cstring(buffer, "nil");
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-28 23:27:55 +00:00
|
|
|
case DST_TRUE:
|
|
|
|
dst_buffer_push_cstring(buffer, "true");
|
|
|
|
return;
|
|
|
|
case DST_FALSE:
|
|
|
|
dst_buffer_push_cstring(buffer, "false");
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
case DST_REAL:
|
2017-11-28 23:27:55 +00:00
|
|
|
real_to_string_b(buffer, dst_unwrap_real(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
case DST_INTEGER:
|
2017-11-28 23:27:55 +00:00
|
|
|
integer_to_string_b(buffer, dst_unwrap_integer(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
case DST_SYMBOL:
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_buffer_push_bytes(buffer, dst_unwrap_string(x), dst_string_length(dst_unwrap_string(x)));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
case DST_STRING:
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_escape_string_b(buffer, dst_unwrap_string(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
case DST_USERDATA:
|
2017-11-28 23:27:55 +00:00
|
|
|
string_description_b(buffer, dst_userdata_type(dst_unwrap_pointer(x))->name, dst_unwrap_pointer(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
|
|
|
default:
|
2017-11-28 23:27:55 +00:00
|
|
|
string_description_b(buffer, dst_type_names[dst_type(x)], dst_unwrap_pointer(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
/* Helper structure for stringifying nested structures */
|
2017-11-21 02:39:44 +00:00
|
|
|
typedef struct DstPrinter DstPrinter;
|
|
|
|
struct DstPrinter {
|
|
|
|
DstBuffer buffer;
|
|
|
|
DstTable seen;
|
|
|
|
uint32_t flags;
|
2017-11-29 20:17:56 +00:00
|
|
|
uint32_t state;
|
|
|
|
int32_t next;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t indent;
|
|
|
|
int32_t indent_size;
|
|
|
|
int32_t token_line_limit;
|
|
|
|
int32_t depth;
|
2017-11-21 02:39:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DST_PRINTFLAG_INDENT 1
|
|
|
|
#define DST_PRINTFLAG_TABLEPAIR 2
|
|
|
|
#define DST_PRINTFLAG_COLORIZE 4
|
|
|
|
#define DST_PRINTFLAG_ALLMAN 8
|
|
|
|
|
|
|
|
/* Go to next line for printer */
|
|
|
|
static void dst_print_indent(DstPrinter *p) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, len;
|
2017-11-21 02:39:44 +00:00
|
|
|
len = p->indent_size * p->indent;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
dst_buffer_push_u8(&p->buffer, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if a value is a print atom (not a printable data structure) */
|
|
|
|
static int is_print_ds(DstValue v) {
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(v)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
default: return 0;
|
|
|
|
case DST_ARRAY:
|
|
|
|
case DST_STRUCT:
|
|
|
|
case DST_TUPLE:
|
|
|
|
case DST_TABLE: return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VT100 Colors for types */
|
|
|
|
static const char *dst_type_colors[15] = {
|
|
|
|
"\x1B[35m",
|
|
|
|
"\x1B[33m",
|
|
|
|
"\x1B[33m",
|
|
|
|
"\x1B[35m",
|
|
|
|
"\x1B[32m",
|
|
|
|
"\x1B[36m",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"\x1B[37m",
|
|
|
|
"\x1B[37m",
|
|
|
|
"\x1B[37m",
|
|
|
|
"\x1B[37m",
|
|
|
|
"\x1B[37m"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
static void dst_description_helper(DstPrinter *p, DstValue x);
|
|
|
|
|
|
|
|
/* Print a hastable view inline */
|
2017-11-28 23:27:55 +00:00
|
|
|
static void dst_print_hashtable_inner(DstPrinter *p, const DstValue *data, int32_t len, int32_t cap) {
|
|
|
|
int32_t i;
|
2017-11-21 02:39:44 +00:00
|
|
|
int doindent = 0;
|
|
|
|
if (p->flags & DST_PRINTFLAG_INDENT) {
|
|
|
|
if (len <= p->token_line_limit) {
|
|
|
|
for (i = 0; i < cap; i += 2) {
|
|
|
|
if (is_print_ds(data[i]) || is_print_ds(data[i + 1])) {
|
|
|
|
doindent = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
doindent = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (doindent) {
|
|
|
|
dst_buffer_push_u8(&p->buffer, '\n');
|
|
|
|
p->indent++;
|
|
|
|
for (i = 0; i < cap; i += 2) {
|
2017-11-29 20:17:56 +00:00
|
|
|
if (!dst_checktype(data[i], DST_NIL)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_print_indent(p);
|
|
|
|
dst_description_helper(p, data[i]);
|
|
|
|
dst_buffer_push_u8(&p->buffer, ' ');
|
|
|
|
dst_description_helper(p, data[i + 1]);
|
|
|
|
dst_buffer_push_u8(&p->buffer, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->indent--;
|
|
|
|
dst_print_indent(p);
|
|
|
|
} else {
|
|
|
|
int isfirst = 1;
|
|
|
|
for (i = 0; i < cap; i += 2) {
|
2017-11-29 20:17:56 +00:00
|
|
|
if (!dst_checktype(data[i], DST_NIL)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
if (isfirst)
|
|
|
|
isfirst = 0;
|
|
|
|
else
|
|
|
|
dst_buffer_push_u8(&p->buffer, ' ');
|
|
|
|
dst_description_helper(p, data[i]);
|
|
|
|
dst_buffer_push_u8(&p->buffer, ' ');
|
|
|
|
dst_description_helper(p, data[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Help print a sequence */
|
2017-11-28 23:27:55 +00:00
|
|
|
static void dst_print_seq_inner(DstPrinter *p, const DstValue *data, int32_t len) {
|
|
|
|
int32_t i;
|
2017-11-21 02:39:44 +00:00
|
|
|
int doindent = 0;
|
|
|
|
if (p->flags & DST_PRINTFLAG_INDENT) {
|
|
|
|
if (len <= p->token_line_limit) {
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (is_print_ds(data[i])) {
|
|
|
|
doindent = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
doindent = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (doindent) {
|
|
|
|
dst_buffer_push_u8(&p->buffer, '\n');
|
|
|
|
p->indent++;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
dst_print_indent(p);
|
|
|
|
dst_description_helper(p, data[i]);
|
|
|
|
dst_buffer_push_u8(&p->buffer, '\n');
|
|
|
|
}
|
|
|
|
p->indent--;
|
|
|
|
dst_print_indent(p);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
dst_description_helper(p, data[i]);
|
|
|
|
if (i != len - 1)
|
|
|
|
dst_buffer_push_u8(&p->buffer, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 17:44:58 +00:00
|
|
|
/* Static debug print helper */
|
2017-11-21 02:39:44 +00:00
|
|
|
static void dst_description_helper(DstPrinter *p, DstValue x) {
|
2017-11-29 20:17:56 +00:00
|
|
|
const char *open;
|
|
|
|
const char *close;
|
|
|
|
int32_t len, cap;
|
|
|
|
const DstValue *data;
|
|
|
|
DstValue check;
|
2017-11-21 02:39:44 +00:00
|
|
|
p->depth--;
|
2017-11-29 20:17:56 +00:00
|
|
|
switch (dst_type(x)) {
|
|
|
|
default:
|
|
|
|
if (p->flags & DST_PRINTFLAG_COLORIZE) {
|
|
|
|
dst_buffer_push_cstring(&p->buffer, dst_type_colors[dst_type(x)]);
|
|
|
|
dst_short_description_b(&p->buffer, x);
|
|
|
|
dst_buffer_push_cstring(&p->buffer, "\x1B[0m");
|
|
|
|
} else {
|
|
|
|
dst_short_description_b(&p->buffer, x);
|
|
|
|
}
|
|
|
|
p->depth++;
|
|
|
|
return;
|
|
|
|
case DST_STRUCT:
|
|
|
|
open = "{"; close = "}";
|
|
|
|
break;
|
|
|
|
case DST_TABLE:
|
|
|
|
open = "@{"; close = "}";
|
|
|
|
break;
|
|
|
|
case DST_TUPLE:
|
|
|
|
open = "("; close = ")";
|
|
|
|
break;
|
|
|
|
case DST_ARRAY:
|
|
|
|
open = "["; close = "]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!p->state) {
|
|
|
|
dst_table_init(&p->seen, 10);
|
|
|
|
p->state = 1;
|
|
|
|
}
|
|
|
|
check = dst_table_get(&p->seen, x);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(check, DST_INTEGER)) {
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_buffer_push_cstring(&p->buffer, "<cycle ");
|
2017-11-28 23:27:55 +00:00
|
|
|
integer_to_string_b(&p->buffer, dst_unwrap_integer(check));
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_buffer_push_cstring(&p->buffer, ">");
|
2017-11-29 20:17:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst_table_put(&p->seen, x, dst_wrap_integer(p->next++));
|
|
|
|
dst_buffer_push_cstring(&p->buffer, open);
|
|
|
|
if (p->depth == 0) {
|
|
|
|
dst_buffer_push_cstring(&p->buffer, "...");
|
|
|
|
} else if (dst_hashtable_view(x, &data, &len, &cap)) {
|
|
|
|
dst_print_hashtable_inner(p, data, len, cap);
|
|
|
|
} else if (dst_seq_view(x, &data, &len)) {
|
|
|
|
dst_print_seq_inner(p, data, len);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
dst_buffer_push_cstring(&p->buffer, close);
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Remove from seen as we know that printing completes, we
|
|
|
|
* can print in multiple times and we know we are not recursing */
|
|
|
|
dst_table_remove(&p->seen, x);
|
|
|
|
p->depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Init printer to defaults */
|
|
|
|
static void dst_printer_defaults(DstPrinter *p) {
|
|
|
|
p->next = 0;
|
|
|
|
p->flags = DST_PRINTFLAG_INDENT;
|
|
|
|
p->depth = 4;
|
|
|
|
p->indent = 0;
|
|
|
|
p->indent_size = 2;
|
|
|
|
p->token_line_limit = 5;
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Debug print. Returns a description of an object as a string. */
|
2017-11-06 03:05:47 +00:00
|
|
|
const uint8_t *dst_description(DstValue x) {
|
2017-11-21 02:39:44 +00:00
|
|
|
DstPrinter printer;
|
|
|
|
const uint8_t *ret;
|
|
|
|
|
|
|
|
dst_printer_defaults(&printer);
|
2017-11-29 20:17:56 +00:00
|
|
|
printer.state = 0;
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_buffer_init(&printer.buffer, 0);
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Only print description up to a depth of 4 */
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_description_helper(&printer, x);
|
|
|
|
ret = dst_string(printer.buffer.data, printer.buffer.count);
|
|
|
|
|
|
|
|
dst_buffer_deinit(&printer.buffer);
|
2017-11-29 20:17:56 +00:00
|
|
|
if (printer.state)
|
|
|
|
dst_table_deinit(&printer.seen);
|
2017-11-21 02:39:44 +00:00
|
|
|
return ret;
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Convert any value to a dst string. Similar to description, but
|
|
|
|
* strings, symbols, and buffers will return their content. */
|
|
|
|
const uint8_t *dst_to_string(DstValue x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(x)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
default:
|
2017-11-21 02:39:44 +00:00
|
|
|
return dst_short_description(x);
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_STRING:
|
|
|
|
case DST_SYMBOL:
|
2017-11-28 23:27:55 +00:00
|
|
|
return dst_unwrap_string(x);
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for formatting strings. Useful for generating error messages and the like.
|
|
|
|
* Similiar to printf, but specialized for operating with dst. */
|
|
|
|
const uint8_t *dst_formatc(const char *format, ...) {
|
|
|
|
va_list args;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
|
|
|
int32_t i;
|
2017-11-21 02:39:44 +00:00
|
|
|
const uint8_t *ret;
|
|
|
|
DstPrinter printer;
|
|
|
|
DstBuffer *bufp = &printer.buffer;
|
2017-11-29 20:17:56 +00:00
|
|
|
printer.state = 0;
|
2017-11-21 02:39:44 +00:00
|
|
|
|
|
|
|
/* Calculate length */
|
|
|
|
while (format[len]) len++;
|
|
|
|
|
|
|
|
/* Initialize buffer */
|
|
|
|
dst_buffer_init(bufp, len);
|
|
|
|
|
|
|
|
/* Start args */
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
/* Iterate length */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t c = format[i];
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
dst_buffer_push_u8(bufp, c);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
{
|
|
|
|
if (i + 1 >= len)
|
|
|
|
break;
|
|
|
|
switch (format[++i]) {
|
|
|
|
default:
|
|
|
|
dst_buffer_push_u8(bufp, format[i]);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
real_to_string_b(bufp, va_arg(args, double));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
integer_to_string_b(bufp, va_arg(args, int64_t));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
const uint8_t *str = va_arg(args, const uint8_t *);
|
|
|
|
dst_buffer_push_bytes(bufp, str, dst_string_length(str));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
dst_buffer_push_cstring(bufp, va_arg(args, const char *));
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
dst_buffer_push_u8(bufp, va_arg(args, int64_t));
|
|
|
|
break;
|
2017-11-29 20:17:56 +00:00
|
|
|
case 'v':
|
2017-11-21 02:39:44 +00:00
|
|
|
{
|
|
|
|
dst_printer_defaults(&printer);
|
|
|
|
dst_description_helper(&printer, va_arg(args, DstValue));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q':
|
|
|
|
{
|
|
|
|
const uint8_t *str = dst_to_string(va_arg(args, DstValue));
|
|
|
|
dst_escape_string_b(bufp, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 't':
|
|
|
|
{
|
|
|
|
dst_buffer_push_cstring(bufp, dst_type_names[va_arg(args, DstType)]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V':
|
|
|
|
{
|
2017-11-29 20:17:56 +00:00
|
|
|
dst_short_description_b(bufp, va_arg(args, DstValue));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
ret = dst_string(printer.buffer.data, printer.buffer.count);
|
|
|
|
dst_buffer_deinit(&printer.buffer);
|
2017-11-29 20:17:56 +00:00
|
|
|
if (printer.state)
|
|
|
|
dst_table_deinit(&printer.seen);
|
2017-11-21 02:39:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print string to stdout */
|
|
|
|
void dst_puts(const uint8_t *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t len = dst_string_length(str);
|
2017-11-21 02:39:44 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
putc(str[i], stdout);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|