2017-07-03 17:44:58 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-07-03 17:44:58 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2018-06-12 18:24:45 +00:00
|
|
|
#include "state.h"
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Begin building a string */
|
2018-09-06 02:18:42 +00:00
|
|
|
uint8_t *janet_string_begin(int32_t length) {
|
|
|
|
char *data = janet_gcalloc(JANET_MEMORY_STRING, 2 * sizeof(int32_t) + length + 1);
|
2017-11-28 23:27:55 +00:00
|
|
|
uint8_t *str = (uint8_t *) (data + 2 * sizeof(int32_t));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_string_length(str) = length;
|
2018-01-06 16:09:15 +00:00
|
|
|
str[length] = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish building a string */
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_string_end(uint8_t *str) {
|
|
|
|
janet_string_hash(str) = janet_string_calchash(str, janet_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 */
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_string(const uint8_t *buf, int32_t len) {
|
|
|
|
int32_t hash = janet_string_calchash(buf, len);
|
|
|
|
char *data = janet_gcalloc(JANET_MEMORY_STRING, 2 * sizeof(int32_t) + len + 1);
|
2017-11-28 23:27:55 +00:00
|
|
|
uint8_t *str = (uint8_t *) (data + 2 * sizeof(int32_t));
|
2017-11-27 19:03:34 +00:00
|
|
|
memcpy(str, buf, len);
|
2018-01-06 16:09:15 +00:00
|
|
|
str[len] = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_string_length(str) = len;
|
|
|
|
janet_string_hash(str) = hash;
|
2017-11-27 19:03:34 +00:00
|
|
|
return str;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Compare two strings */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
|
|
|
|
int32_t xlen = janet_string_length(lhs);
|
|
|
|
int32_t ylen = janet_string_length(rhs);
|
2017-11-28 23:27:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Compare a janet string with a piece of memory */
|
|
|
|
int janet_string_equalconst(const uint8_t *lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t index;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t lhash = janet_string_hash(lhs);
|
|
|
|
int32_t llen = janet_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 */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_string_equal(const uint8_t *lhs, const uint8_t *rhs) {
|
|
|
|
return janet_string_equalconst(lhs, rhs,
|
|
|
|
janet_string_length(rhs), janet_string_hash(rhs));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load a c string */
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_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;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_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 */
|
2018-05-08 23:40:28 +00:00
|
|
|
#define BUFSIZE 64
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
static int32_t number_to_string_impl(uint8_t *buf, double x) {
|
2018-12-29 04:32:09 +00:00
|
|
|
int count = snprintf((char *) buf, BUFSIZE, "%g", x);
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) count;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
static void number_to_string_b(JanetBuffer *buffer, double x) {
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
2018-12-27 18:05:29 +00:00
|
|
|
buffer->count += number_to_string_impl(buffer->data + buffer->count, x);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
static const uint8_t *number_to_string(double x) {
|
2018-03-22 00:53:39 +00:00
|
|
|
uint8_t buf[BUFSIZE];
|
2018-12-27 18:05:29 +00:00
|
|
|
return janet_string(buf, number_to_string_impl(buf, x));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 23:14:02 +00:00
|
|
|
/* expects non positive x */
|
|
|
|
static int count_dig10(int32_t x) {
|
|
|
|
int result = 1;
|
|
|
|
for (;;) {
|
|
|
|
if (x > -10) return result;
|
|
|
|
if (x > -100) return result + 1;
|
|
|
|
if (x > -1000) return result + 2;
|
|
|
|
if (x > -10000) return result + 3;
|
|
|
|
x /= 10000;
|
|
|
|
result += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 21:25:24 +00:00
|
|
|
static int32_t integer_to_string_impl(uint8_t *buf, int32_t x) {
|
2018-09-17 23:14:02 +00:00
|
|
|
int32_t neg = 0;
|
|
|
|
int32_t len = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (x == 0) {
|
|
|
|
buf[0] = '0';
|
|
|
|
return 1;
|
2018-09-17 23:14:02 +00:00
|
|
|
} else if (x > 0) {
|
2017-07-03 17:44:58 +00:00
|
|
|
x = -x;
|
2018-09-17 23:14:02 +00:00
|
|
|
} else {
|
|
|
|
neg = 1;
|
|
|
|
*buf++ = '-';
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
2018-09-17 23:14:02 +00:00
|
|
|
len = count_dig10(x);
|
|
|
|
buf += len;
|
|
|
|
while (x) {
|
2018-01-25 23:48:12 +00:00
|
|
|
uint8_t digit = (uint8_t) -(x % 10);
|
2018-09-17 23:14:02 +00:00
|
|
|
*(--buf) = '0' + digit;
|
2017-07-03 17:44:58 +00:00
|
|
|
x /= 10;
|
|
|
|
}
|
2018-09-17 23:14:02 +00:00
|
|
|
return len + neg;
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void integer_to_string_b(JanetBuffer *buffer, int32_t x) {
|
|
|
|
janet_buffer_extra(buffer, 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
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define HEX(i) (((uint8_t *) janet_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++ = '<';
|
2018-05-08 23:40:28 +00:00
|
|
|
/* Maximum of 32 bytes for abstract type name */
|
|
|
|
for (i = 0; title[i] && i < 32; ++i)
|
2017-07-03 17:44:58 +00:00
|
|
|
*c++ = ((uint8_t *)title) [i];
|
|
|
|
*c++ = ' ';
|
|
|
|
*c++ = '0';
|
|
|
|
*c++ = 'x';
|
2018-09-06 02:18:42 +00:00
|
|
|
#if defined(JANET_64)
|
2018-03-22 21:38:37 +00:00
|
|
|
#define POINTSIZE 6
|
|
|
|
#else
|
|
|
|
#define POINTSIZE (sizeof(void *))
|
|
|
|
#endif
|
|
|
|
for (i = POINTSIZE; i > 0; --i) {
|
2017-07-03 17:44:58 +00:00
|
|
|
uint8_t byte = pbuf.bytes[i - 1];
|
|
|
|
*c++ = HEX(byte >> 4);
|
|
|
|
*c++ = HEX(byte & 0xF);
|
|
|
|
}
|
|
|
|
*c++ = '>';
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) (c - buf);
|
2018-03-22 21:38:37 +00:00
|
|
|
#undef POINTSIZE
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void string_description_b(JanetBuffer *buffer, const char *title, void *pointer) {
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
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
|
|
|
|
2018-05-20 01:16:00 +00:00
|
|
|
/* Describes a pointer with a title (string_description("bork", myp) returns
|
2017-11-06 03:05:47 +00:00
|
|
|
* a string "<bork 0x12345678>") */
|
|
|
|
static const uint8_t *string_description(const char *title, void *pointer) {
|
2018-03-22 00:53:39 +00:00
|
|
|
uint8_t buf[BUFSIZE];
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_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
|
2018-03-22 00:53:39 +00:00
|
|
|
#undef BUFSIZE
|
2017-07-03 17:44:58 +00:00
|
|
|
|
2018-05-20 01:16:00 +00:00
|
|
|
/* TODO - add more characters to escape.
|
2018-01-17 16:36:10 +00:00
|
|
|
*
|
|
|
|
* When more escapes are added, they must correspond
|
2018-09-06 02:18:42 +00:00
|
|
|
* to janet_escape_string_impl exactly or a buffer overrun could occur. */
|
|
|
|
static int32_t janet_escape_string_length(const uint8_t *str, int32_t slen) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 2;
|
|
|
|
int32_t i;
|
2018-01-17 16:36:10 +00:00
|
|
|
for (i = 0; i < slen; ++i) {
|
2017-11-01 21:53:43 +00:00
|
|
|
switch (str[i]) {
|
|
|
|
case '"':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\0':
|
2018-01-18 22:25:45 +00:00
|
|
|
case '\\':
|
2017-11-01 21:53:43 +00:00
|
|
|
len += 2;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-12 18:24:45 +00:00
|
|
|
if (str[i] < 32 || str[i] > 127)
|
|
|
|
len += 4;
|
|
|
|
else
|
|
|
|
len += 1;
|
2017-11-01 21:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_escape_string_impl(uint8_t *buf, const uint8_t *str, int32_t len) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, j;
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[0] = '"';
|
2018-01-17 16:36:10 +00:00
|
|
|
for (i = 0, j = 1; i < len; ++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;
|
2018-01-18 22:25:45 +00:00
|
|
|
case '\\':
|
|
|
|
buf[j++] = '\\';
|
|
|
|
buf[j++] = '\\';
|
|
|
|
break;
|
2017-07-03 17:44:58 +00:00
|
|
|
default:
|
2018-06-12 18:24:45 +00:00
|
|
|
if (c < 32 || c > 127) {
|
|
|
|
buf[j++] = '\\';
|
2018-08-23 01:41:25 +00:00
|
|
|
buf[j++] = 'x';
|
2018-09-06 02:18:42 +00:00
|
|
|
buf[j++] = janet_base64[(c >> 4) & 0xF];
|
|
|
|
buf[j++] = janet_base64[c & 0xF];
|
2018-06-12 18:24:45 +00:00
|
|
|
} else {
|
|
|
|
buf[j++] = c;
|
|
|
|
}
|
2017-07-03 17:44:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
buf[j++] = '"';
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_escape_string_b(JanetBuffer *buffer, const uint8_t *str) {
|
|
|
|
int32_t len = janet_string_length(str);
|
|
|
|
int32_t elen = janet_escape_string_length(str, len);
|
|
|
|
janet_buffer_extra(buffer, elen);
|
|
|
|
janet_escape_string_impl(buffer->data + buffer->count, str, len);
|
2018-01-17 16:36:10 +00:00
|
|
|
buffer->count += elen;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_escape_string(const uint8_t *str) {
|
|
|
|
int32_t len = janet_string_length(str);
|
|
|
|
int32_t elen = janet_escape_string_length(str, len);
|
|
|
|
uint8_t *buf = janet_string_begin(elen);
|
|
|
|
janet_escape_string_impl(buf, str, len);
|
|
|
|
return janet_string_end(buf);
|
2017-07-03 17:44:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_escape_buffer_b(JanetBuffer *buffer, JanetBuffer *bx) {
|
|
|
|
int32_t elen = janet_escape_string_length(bx->data, bx->count);
|
|
|
|
janet_buffer_push_u8(buffer, '@');
|
|
|
|
janet_buffer_extra(buffer, elen);
|
|
|
|
janet_escape_string_impl(
|
2018-01-17 16:36:10 +00:00
|
|
|
buffer->data + buffer->count,
|
|
|
|
bx->data,
|
|
|
|
bx->count);
|
|
|
|
buffer->count += elen;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_description_b(JanetBuffer *buffer, Janet x) {
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
case JANET_NIL:
|
|
|
|
janet_buffer_push_cstring(buffer, "nil");
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TRUE:
|
|
|
|
janet_buffer_push_cstring(buffer, "true");
|
2017-11-28 23:27:55 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_FALSE:
|
|
|
|
janet_buffer_push_cstring(buffer, "false");
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_NUMBER:
|
|
|
|
number_to_string_b(buffer, janet_unwrap_number(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
janet_buffer_push_bytes(buffer,
|
|
|
|
janet_unwrap_string(x),
|
|
|
|
janet_string_length(janet_unwrap_string(x)));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
janet_escape_string_b(buffer, janet_unwrap_string(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_BUFFER:
|
|
|
|
janet_escape_buffer_b(buffer, janet_unwrap_buffer(x));
|
2018-01-17 16:36:10 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_ABSTRACT:
|
2018-02-04 05:54:38 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
const char *n = janet_abstract_type(janet_unwrap_abstract(x))->name;
|
2018-08-06 01:13:14 +00:00
|
|
|
string_description_b(buffer,
|
|
|
|
n[0] == ':' ? n + 1 : n,
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_unwrap_abstract(x));
|
2018-08-06 01:13:14 +00:00
|
|
|
return;
|
2018-02-04 05:54:38 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_CFUNCTION:
|
2018-06-12 18:24:45 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet check = janet_table_get(janet_vm_registry, x);
|
2018-10-22 05:28:39 +00:00
|
|
|
if (janet_checktype(check, JANET_SYMBOL)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(buffer, "<cfunction ");
|
|
|
|
janet_buffer_push_bytes(buffer,
|
|
|
|
janet_unwrap_symbol(check),
|
|
|
|
janet_string_length(janet_unwrap_symbol(check)));
|
|
|
|
janet_buffer_push_u8(buffer, '>');
|
2018-06-12 18:24:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto fallthrough;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_FUNCTION:
|
2018-07-11 00:01:39 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFunction *fun = janet_unwrap_function(x);
|
|
|
|
JanetFuncDef *def = fun->def;
|
2018-07-11 00:01:39 +00:00
|
|
|
if (def->name) {
|
|
|
|
const uint8_t *n = def->name;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(buffer, "<function ");
|
|
|
|
janet_buffer_push_bytes(buffer, n, janet_string_length(n));
|
|
|
|
janet_buffer_push_u8(buffer, '>');
|
2018-07-11 00:01:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto fallthrough;
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
fallthrough:
|
2017-11-01 21:53:43 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
string_description_b(buffer, janet_type_names[janet_type(x)] + 1, janet_unwrap_pointer(x));
|
2017-11-01 21:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_to_string_b(JanetBuffer *buffer, Janet x) {
|
|
|
|
switch (janet_type(x)) {
|
2018-04-01 22:24:04 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_description_b(buffer, x);
|
2018-04-01 22:24:04 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_BUFFER:
|
|
|
|
janet_buffer_push_bytes(buffer,
|
|
|
|
janet_unwrap_buffer(x)->data,
|
|
|
|
janet_unwrap_buffer(x)->count);
|
2018-04-01 22:24:04 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
janet_buffer_push_bytes(buffer,
|
|
|
|
janet_unwrap_string(x),
|
|
|
|
janet_string_length(janet_unwrap_string(x)));
|
2018-04-01 22:24:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_description(Janet x) {
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
case JANET_NIL:
|
|
|
|
return janet_cstring("nil");
|
|
|
|
case JANET_TRUE:
|
|
|
|
return janet_cstring("true");
|
|
|
|
case JANET_FALSE:
|
|
|
|
return janet_cstring("false");
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_NUMBER:
|
|
|
|
return number_to_string(janet_unwrap_number(x));
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
return janet_unwrap_symbol(x);
|
|
|
|
case JANET_STRING:
|
|
|
|
return janet_escape_string(janet_unwrap_string(x));
|
|
|
|
case JANET_BUFFER:
|
2018-03-14 17:37:08 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer b;
|
2018-03-14 17:37:08 +00:00
|
|
|
const uint8_t *ret;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_init(&b, 3);
|
|
|
|
janet_escape_buffer_b(&b, janet_unwrap_buffer(x));
|
|
|
|
ret = janet_string(b.data, b.count);
|
|
|
|
janet_buffer_deinit(&b);
|
2018-03-14 17:37:08 +00:00
|
|
|
return ret;
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_ABSTRACT:
|
2018-03-14 17:37:08 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
const char *n = janet_abstract_type(janet_unwrap_abstract(x))->name;
|
2018-05-20 01:16:00 +00:00
|
|
|
return string_description(
|
2018-03-14 17:37:08 +00:00
|
|
|
n[0] == ':' ? n + 1 : n,
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_unwrap_abstract(x));
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_CFUNCTION:
|
2018-06-12 18:24:45 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet check = janet_table_get(janet_vm_registry, x);
|
|
|
|
if (janet_checktype(check, JANET_SYMBOL)) {
|
|
|
|
return janet_formatc("<cfunction %V>", check);
|
2018-06-12 18:24:45 +00:00
|
|
|
}
|
|
|
|
goto fallthrough;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_FUNCTION:
|
2018-07-11 00:01:39 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFunction *fun = janet_unwrap_function(x);
|
|
|
|
JanetFuncDef *def = fun->def;
|
2018-07-11 00:01:39 +00:00
|
|
|
if (def->name) {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_formatc("<function %S>", def->name);
|
2018-07-11 00:01:39 +00:00
|
|
|
}
|
|
|
|
goto fallthrough;
|
|
|
|
}
|
2018-06-12 18:24:45 +00:00
|
|
|
fallthrough:
|
2018-03-14 17:37:08 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
return string_description(janet_type_names[janet_type(x)] + 1, janet_unwrap_pointer(x));
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Convert any value to a janet string. Similar to description, but
|
2017-11-06 03:05:47 +00:00
|
|
|
* strings, symbols, and buffers will return their content. */
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *janet_to_string(Janet x) {
|
|
|
|
switch (janet_type(x)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_description(x);
|
|
|
|
case JANET_BUFFER:
|
|
|
|
return janet_string(janet_unwrap_buffer(x)->data, janet_unwrap_buffer(x)->count);
|
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
return janet_unwrap_string(x);
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:15:21 +00:00
|
|
|
/* Hold state for pretty printer. */
|
|
|
|
struct pretty {
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
int depth;
|
2018-11-19 07:41:52 +00:00
|
|
|
int indent;
|
2018-11-19 07:15:21 +00:00
|
|
|
JanetTable seen;
|
|
|
|
};
|
|
|
|
|
2018-11-19 07:41:52 +00:00
|
|
|
static void print_newline(struct pretty *S, int just_a_space) {
|
|
|
|
int i;
|
|
|
|
if (just_a_space) {
|
|
|
|
janet_buffer_push_u8(S->buffer, ' ');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
janet_buffer_push_u8(S->buffer, '\n');
|
|
|
|
for (i = 0; i < S->indent; i++) {
|
|
|
|
janet_buffer_push_u8(S->buffer, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:15:21 +00:00
|
|
|
/* Helper for pretty printing */
|
2018-12-06 22:26:59 +00:00
|
|
|
static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
|
2018-11-19 07:15:21 +00:00
|
|
|
/* Add to seen */
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
case JANET_NIL:
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_NUMBER:
|
2018-11-19 07:15:21 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_TRUE:
|
|
|
|
case JANET_FALSE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Janet seenid = janet_table_get(&S->seen, x);
|
2018-12-27 18:05:29 +00:00
|
|
|
if (janet_checktype(seenid, JANET_NUMBER)) {
|
2018-11-19 07:15:21 +00:00
|
|
|
janet_buffer_push_cstring(S->buffer, "<cycle ");
|
2018-11-30 16:46:05 +00:00
|
|
|
integer_to_string_b(S->buffer, janet_unwrap_integer(seenid));
|
2018-11-19 07:15:21 +00:00
|
|
|
janet_buffer_push_u8(S->buffer, '>');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
janet_table_put(&S->seen, x, janet_wrap_integer(S->seen.count));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
default:
|
|
|
|
janet_description_b(S->buffer, x);
|
2018-11-19 14:57:24 +00:00
|
|
|
break;
|
2018-11-19 07:15:21 +00:00
|
|
|
case JANET_ARRAY:
|
|
|
|
case JANET_TUPLE:
|
|
|
|
{
|
|
|
|
int isarray = janet_checktype(x, JANET_ARRAY);
|
|
|
|
janet_buffer_push_cstring(S->buffer, isarray ? "@[" : "(");
|
|
|
|
S->depth--;
|
2018-11-19 07:41:52 +00:00
|
|
|
S->indent += 2;
|
2018-11-19 07:15:21 +00:00
|
|
|
if (S->depth == 0) {
|
|
|
|
janet_buffer_push_cstring(S->buffer, "...");
|
|
|
|
} else {
|
|
|
|
int32_t i, len;
|
|
|
|
const Janet *arr;
|
|
|
|
janet_indexed_view(x, &arr, &len);
|
2018-11-19 07:41:52 +00:00
|
|
|
if (!isarray && len >= 5)
|
|
|
|
janet_buffer_push_u8(S->buffer, ' ');
|
2018-12-06 22:26:59 +00:00
|
|
|
if (is_dict_value && len >= 5) print_newline(S, 0);
|
2018-11-19 07:15:21 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-11-19 07:41:52 +00:00
|
|
|
if (i) print_newline(S, len < 5);
|
2018-12-06 22:26:59 +00:00
|
|
|
janet_pretty_one(S, arr[i], 0);
|
2018-11-19 07:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-19 07:41:52 +00:00
|
|
|
S->indent -= 2;
|
2018-11-19 07:15:21 +00:00
|
|
|
S->depth++;
|
|
|
|
janet_buffer_push_u8(S->buffer, isarray ? ']' : ')');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_STRUCT:
|
|
|
|
case JANET_TABLE:
|
|
|
|
{
|
|
|
|
int istable = janet_checktype(x, JANET_TABLE);
|
2018-11-26 14:21:24 +00:00
|
|
|
janet_buffer_push_cstring(S->buffer, istable ? "@" : "{");
|
|
|
|
|
|
|
|
/* For object-like tables, print class name */
|
|
|
|
if (istable) {
|
|
|
|
JanetTable *t = janet_unwrap_table(x);
|
|
|
|
JanetTable *proto = t->proto;
|
|
|
|
if (NULL != proto) {
|
|
|
|
Janet name = janet_table_get(proto, janet_csymbolv(":name"));
|
|
|
|
if (janet_checktype(name, JANET_SYMBOL)) {
|
|
|
|
const uint8_t *sym = janet_unwrap_symbol(name);
|
|
|
|
janet_buffer_push_bytes(S->buffer, sym, janet_string_length(sym));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
janet_buffer_push_cstring(S->buffer, "{");
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:15:21 +00:00
|
|
|
S->depth--;
|
2018-11-19 07:41:52 +00:00
|
|
|
S->indent += 2;
|
2018-11-19 07:15:21 +00:00
|
|
|
if (S->depth == 0) {
|
|
|
|
janet_buffer_push_cstring(S->buffer, "...");
|
|
|
|
} else {
|
|
|
|
int32_t i, len, cap;
|
|
|
|
int first_kv_pair = 1;
|
|
|
|
const JanetKV *kvs;
|
|
|
|
janet_dictionary_view(x, &kvs, &len, &cap);
|
2018-11-19 07:41:52 +00:00
|
|
|
if (!istable && len >= 4)
|
|
|
|
janet_buffer_push_u8(S->buffer, ' ');
|
2018-12-06 22:26:59 +00:00
|
|
|
if (is_dict_value && len >= 5) print_newline(S, 0);
|
2018-11-19 07:15:21 +00:00
|
|
|
for (i = 0; i < cap; i++) {
|
|
|
|
if (!janet_checktype(kvs[i].key, JANET_NIL)) {
|
|
|
|
if (first_kv_pair) {
|
|
|
|
first_kv_pair = 0;
|
|
|
|
} else {
|
2018-11-19 07:41:52 +00:00
|
|
|
print_newline(S, len < 4);
|
2018-11-19 07:15:21 +00:00
|
|
|
}
|
2018-12-06 22:26:59 +00:00
|
|
|
janet_pretty_one(S, kvs[i].key, 0);
|
2018-11-19 07:15:21 +00:00
|
|
|
janet_buffer_push_u8(S->buffer, ' ');
|
2018-12-06 22:26:59 +00:00
|
|
|
janet_pretty_one(S, kvs[i].value, 1);
|
2018-11-19 07:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 07:41:52 +00:00
|
|
|
S->indent -= 2;
|
2018-11-19 07:15:21 +00:00
|
|
|
S->depth++;
|
|
|
|
janet_buffer_push_u8(S->buffer, '}');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 14:57:24 +00:00
|
|
|
/* Remove from seen */
|
|
|
|
janet_table_remove(&S->seen, x);
|
2018-11-19 07:15:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for printing a janet value in a pretty form. Not meant to be used
|
|
|
|
* for serialization or anything like that. */
|
|
|
|
JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, Janet x) {
|
|
|
|
struct pretty S;
|
|
|
|
if (NULL == buffer) {
|
|
|
|
buffer = janet_buffer(0);
|
|
|
|
}
|
|
|
|
S.buffer = buffer;
|
|
|
|
S.depth = depth;
|
2018-11-19 07:41:52 +00:00
|
|
|
S.indent = 0;
|
2018-11-19 07:15:21 +00:00
|
|
|
janet_table_init(&S.seen, 10);
|
2018-12-06 22:26:59 +00:00
|
|
|
janet_pretty_one(&S, x, 0);
|
2018-11-19 07:15:21 +00:00
|
|
|
janet_table_deinit(&S.seen);
|
|
|
|
return S.buffer;
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Helper function for formatting strings. Useful for generating error messages and the like.
|
2018-09-06 02:18:42 +00:00
|
|
|
* Similiar to printf, but specialized for operating with janet. */
|
|
|
|
const uint8_t *janet_formatc(const char *format, ...) {
|
2017-11-21 02:39:44 +00:00
|
|
|
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;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer buffer;
|
|
|
|
JanetBuffer *bufp = &buffer;
|
2018-05-20 01:16:00 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Calculate length */
|
|
|
|
while (format[len]) len++;
|
|
|
|
|
|
|
|
/* Initialize buffer */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_init(bufp, len);
|
2017-11-21 02:39:44 +00:00
|
|
|
|
|
|
|
/* Start args */
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
/* Iterate length */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t c = format[i];
|
|
|
|
switch (c) {
|
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(bufp, c);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
{
|
|
|
|
if (i + 1 >= len)
|
|
|
|
break;
|
|
|
|
switch (format[++i]) {
|
2018-05-20 01:16:00 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(bufp, format[i]);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
2018-05-20 01:16:00 +00:00
|
|
|
case 'f':
|
2018-12-27 18:05:29 +00:00
|
|
|
number_to_string_b(bufp, va_arg(args, double));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
2018-05-20 01:16:00 +00:00
|
|
|
case 'd':
|
2018-01-12 21:25:24 +00:00
|
|
|
integer_to_string_b(bufp, va_arg(args, int32_t));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
const uint8_t *str = va_arg(args, const uint8_t *);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(bufp, str, janet_string_length(str));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's':
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(bufp, va_arg(args, const char *));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(bufp, (uint8_t) va_arg(args, long));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
{
|
2017-12-15 00:33:45 +00:00
|
|
|
const uint8_t *str = va_arg(args, const uint8_t *);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_escape_string_b(bufp, str);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 't':
|
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(bufp, janet_type_names[va_arg(args, JanetType)] + 1);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-20 01:16:00 +00:00
|
|
|
case 'V':
|
2018-04-01 22:24:04 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_to_string_b(bufp, va_arg(args, Janet));
|
2018-04-01 22:24:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-20 01:16:00 +00:00
|
|
|
case 'v':
|
2017-11-21 02:39:44 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_description_b(bufp, va_arg(args, Janet));
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-19 07:15:21 +00:00
|
|
|
case 'p':
|
|
|
|
{
|
|
|
|
janet_pretty(bufp, 4, va_arg(args, Janet));
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janet_string(buffer.data, buffer.count);
|
|
|
|
janet_buffer_deinit(&buffer);
|
2017-11-21 02:39:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print string to stdout */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_puts(const uint8_t *str) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t len = janet_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
|
|
|
}
|
2018-05-18 03:41:20 +00:00
|
|
|
|
2018-05-18 18:19:57 +00:00
|
|
|
/* Knuth Morris Pratt Algorithm */
|
|
|
|
|
|
|
|
struct kmp_state {
|
|
|
|
int32_t i;
|
|
|
|
int32_t j;
|
|
|
|
int32_t textlen;
|
|
|
|
int32_t patlen;
|
|
|
|
int32_t *lookup;
|
|
|
|
const uint8_t *text;
|
|
|
|
const uint8_t *pat;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kmp_init(
|
|
|
|
struct kmp_state *s,
|
|
|
|
const uint8_t *text, int32_t textlen,
|
|
|
|
const uint8_t *pat, int32_t patlen) {
|
|
|
|
int32_t *lookup = calloc(patlen, sizeof(int32_t));
|
|
|
|
if (!lookup) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
s->lookup = lookup;
|
|
|
|
s->i = 0;
|
|
|
|
s->j = 0;
|
|
|
|
s->text = text;
|
|
|
|
s->pat = pat;
|
|
|
|
s->textlen = textlen;
|
|
|
|
s->patlen = patlen;
|
|
|
|
/* Init state machine */
|
|
|
|
{
|
|
|
|
int32_t i, j;
|
|
|
|
for (i = 1, j = 0; i < patlen; i++) {
|
|
|
|
while (j && pat[j] != pat[i]) j = lookup[j - 1];
|
|
|
|
if (pat[j] == pat[i]) j++;
|
|
|
|
lookup[i] = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kmp_deinit(struct kmp_state *state) {
|
|
|
|
free(state->lookup);
|
|
|
|
}
|
|
|
|
|
2018-09-30 00:01:57 +00:00
|
|
|
static void kmp_seti(struct kmp_state *state, int32_t i) {
|
|
|
|
state->i = i;
|
|
|
|
state->j = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:19:57 +00:00
|
|
|
static int32_t kmp_next(struct kmp_state *state) {
|
|
|
|
int32_t i = state->i;
|
|
|
|
int32_t j = state->j;
|
|
|
|
int32_t textlen = state->textlen;
|
|
|
|
int32_t patlen = state->patlen;
|
|
|
|
const uint8_t *text = state->text;
|
|
|
|
const uint8_t *pat = state->pat;
|
|
|
|
int32_t *lookup = state->lookup;
|
|
|
|
while (i < textlen) {
|
|
|
|
if (text[i] == pat[j]) {
|
|
|
|
if (j == patlen - 1) {
|
|
|
|
state->i = i + 1;
|
|
|
|
state->j = lookup[j];
|
|
|
|
return i - j;
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (j > 0) {
|
|
|
|
j = lookup[j - 1];
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-18 03:41:20 +00:00
|
|
|
/* CFuns */
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_slice(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len, start, end;
|
|
|
|
const uint8_t *ret;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 3);
|
|
|
|
JANET_ARG_BYTES(data, len, args, 0);
|
2018-05-18 03:41:20 +00:00
|
|
|
/* Get start */
|
|
|
|
if (args.n < 2) {
|
|
|
|
start = 0;
|
|
|
|
} else {
|
2018-12-27 18:05:29 +00:00
|
|
|
JANET_ARG_INTEGER(start, args, 1);
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
/* Get end */
|
|
|
|
if (args.n < 3) {
|
|
|
|
end = -1;
|
|
|
|
} else {
|
2018-12-27 18:05:29 +00:00
|
|
|
JANET_ARG_INTEGER(end, args, 2);
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
if (start < 0) start = len + start;
|
|
|
|
if (end < 0) end = len + end + 1;
|
2018-11-25 20:58:50 +00:00
|
|
|
if (end < 0 || start < 0 || end > len || start > len)
|
|
|
|
JANET_THROW(args, "slice range out of bounds");
|
2018-05-18 03:41:20 +00:00
|
|
|
if (end >= start) {
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janet_string(data + start, end - start);
|
2018-05-18 03:41:20 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
ret = janet_cstring("");
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, ret);
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_repeat(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
uint8_t *newbuf, *p, *end;
|
|
|
|
int32_t len, rep;
|
|
|
|
int64_t mulres;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 2);
|
|
|
|
JANET_ARG_BYTES(data, len, args, 0);
|
|
|
|
JANET_ARG_INTEGER(rep, args, 1);
|
2018-05-18 03:41:20 +00:00
|
|
|
if (rep < 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "expected non-negative number of repetitions");
|
2018-05-18 03:41:20 +00:00
|
|
|
} else if (rep == 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_CSTRING(args, "");
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
mulres = (int64_t) rep * len;
|
|
|
|
if (mulres > INT32_MAX) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "result string is too long");
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
newbuf = janet_string_begin((int32_t) mulres);
|
2018-05-18 03:41:20 +00:00
|
|
|
end = newbuf + mulres;
|
|
|
|
for (p = newbuf; p < end; p += len) {
|
|
|
|
memcpy(p, data, len);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(newbuf));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_bytes(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *str;
|
|
|
|
int32_t strlen, i;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *tup;
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_BYTES(str, strlen, args, 0);
|
|
|
|
tup = janet_tuple_begin(strlen);
|
2018-05-18 03:41:20 +00:00
|
|
|
for (i = 0; i < strlen; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
tup[i] = janet_wrap_integer((int32_t) str[i]);
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_TUPLE(args, janet_tuple_end(tup));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_frombytes(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
int32_t i;
|
|
|
|
uint8_t *buf;
|
|
|
|
for (i = 0; i < args.n; i++) {
|
2018-12-27 18:05:29 +00:00
|
|
|
if (!janet_checkint(args.v[i])) {
|
|
|
|
JANET_THROW(args, "expected integer byte values");
|
|
|
|
}
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
buf = janet_string_begin(args.n);
|
2018-05-18 03:41:20 +00:00
|
|
|
for (i = 0; i < args.n; i++) {
|
|
|
|
int32_t c;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_INTEGER(c, args, i);
|
2018-05-18 03:41:20 +00:00
|
|
|
buf[i] = c & 0xFF;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_asciilower(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *str;
|
|
|
|
uint8_t *buf;
|
|
|
|
int32_t len, i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_BYTES(str, len, args, 0);
|
|
|
|
buf = janet_string_begin(len);
|
2018-05-18 03:41:20 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t c = str[i];
|
|
|
|
if (c >= 65 && c <= 90) {
|
|
|
|
buf[i] = c + 32;
|
|
|
|
} else {
|
|
|
|
buf[i] = c;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_asciiupper(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *str;
|
|
|
|
uint8_t *buf;
|
|
|
|
int32_t len, i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_BYTES(str, len, args, 0);
|
|
|
|
buf = janet_string_begin(len);
|
2018-05-18 03:41:20 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t c = str[i];
|
|
|
|
if (c >= 97 && c <= 122) {
|
|
|
|
buf[i] = c - 32;
|
|
|
|
} else {
|
|
|
|
buf[i] = c;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_reverse(JanetArgs args) {
|
2018-05-18 03:41:20 +00:00
|
|
|
const uint8_t *str;
|
|
|
|
uint8_t *buf;
|
|
|
|
int32_t len, i, j;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_ARG_BYTES(str, len, args, 0);
|
|
|
|
buf = janet_string_begin(len);
|
2018-05-18 03:41:20 +00:00
|
|
|
for (i = 0, j = len - 1; i < len; i++, j--) {
|
|
|
|
buf[i] = str[j];
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-05-18 03:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int findsetup(JanetArgs args, struct kmp_state *s, int32_t extra) {
|
2018-05-18 18:19:57 +00:00
|
|
|
const uint8_t *text, *pat;
|
|
|
|
int32_t textlen, patlen, start;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 2);
|
|
|
|
JANET_MAXARITY(args, 3 + extra);
|
|
|
|
JANET_ARG_BYTES(pat, patlen, args, 0);
|
|
|
|
JANET_ARG_BYTES(text, textlen, args, 1);
|
2018-05-18 20:24:09 +00:00
|
|
|
if (args.n >= 3) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_INTEGER(start, args, 2);
|
2018-05-18 18:19:57 +00:00
|
|
|
if (start < 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "expected non-negative start index");
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
kmp_init(s, text, textlen, pat, patlen);
|
|
|
|
s->i = start;
|
2018-09-06 02:18:42 +00:00
|
|
|
return JANET_SIGNAL_OK;
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_find(JanetArgs args) {
|
2018-05-18 18:19:57 +00:00
|
|
|
int32_t result;
|
|
|
|
struct kmp_state state;
|
2018-05-18 20:24:09 +00:00
|
|
|
int status = findsetup(args, &state, 0);
|
2018-05-18 18:19:57 +00:00
|
|
|
if (status) return status;
|
|
|
|
result = kmp_next(&state);
|
|
|
|
kmp_deinit(&state);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN(args, result < 0
|
|
|
|
? janet_wrap_nil()
|
|
|
|
: janet_wrap_integer(result));
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_findall(JanetArgs args) {
|
2018-05-18 18:19:57 +00:00
|
|
|
int32_t result;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array;
|
2018-05-18 18:19:57 +00:00
|
|
|
struct kmp_state state;
|
2018-05-18 20:24:09 +00:00
|
|
|
int status = findsetup(args, &state, 0);
|
2018-05-18 18:19:57 +00:00
|
|
|
if (status) return status;
|
2018-09-06 02:18:42 +00:00
|
|
|
array = janet_array(0);
|
2018-05-18 18:19:57 +00:00
|
|
|
while ((result = kmp_next(&state)) >= 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_push(array, janet_wrap_integer(result));
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
kmp_deinit(&state);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_ARRAY(args, array);
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct replace_state {
|
|
|
|
struct kmp_state kmp;
|
|
|
|
const uint8_t *subst;
|
|
|
|
int32_t substlen;
|
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int replacesetup(JanetArgs args, struct replace_state *s) {
|
2018-05-18 18:19:57 +00:00
|
|
|
const uint8_t *text, *pat, *subst;
|
|
|
|
int32_t textlen, patlen, substlen, start;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 3);
|
|
|
|
JANET_MAXARITY(args, 4);
|
|
|
|
JANET_ARG_BYTES(pat, patlen, args, 0);
|
|
|
|
JANET_ARG_BYTES(subst, substlen, args, 1);
|
|
|
|
JANET_ARG_BYTES(text, textlen, args, 2);
|
2018-05-18 18:19:57 +00:00
|
|
|
if (args.n == 4) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_INTEGER(start, args, 3);
|
2018-05-18 18:19:57 +00:00
|
|
|
if (start < 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "expected non-negative start index");
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
kmp_init(&s->kmp, text, textlen, pat, patlen);
|
|
|
|
s->kmp.i = start;
|
|
|
|
s->subst = subst;
|
|
|
|
s->substlen = substlen;
|
2018-09-06 02:18:42 +00:00
|
|
|
return JANET_SIGNAL_OK;
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_replace(JanetArgs args) {
|
2018-05-18 18:19:57 +00:00
|
|
|
int32_t result;
|
|
|
|
struct replace_state s;
|
|
|
|
uint8_t *buf;
|
|
|
|
int status = replacesetup(args, &s);
|
|
|
|
if (status) return status;
|
|
|
|
result = kmp_next(&s.kmp);
|
|
|
|
if (result < 0) {
|
|
|
|
kmp_deinit(&s.kmp);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string(s.kmp.text, s.kmp.textlen));
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
buf = janet_string_begin(s.kmp.textlen - s.kmp.patlen + s.substlen);
|
2018-05-18 20:24:09 +00:00
|
|
|
memcpy(buf, s.kmp.text, result);
|
2018-05-18 18:19:57 +00:00
|
|
|
memcpy(buf + result, s.subst, s.substlen);
|
|
|
|
memcpy(buf + result + s.substlen,
|
2018-05-18 20:24:09 +00:00
|
|
|
s.kmp.text + result + s.kmp.patlen,
|
|
|
|
s.kmp.textlen - result - s.kmp.patlen);
|
2018-05-18 18:19:57 +00:00
|
|
|
kmp_deinit(&s.kmp);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_replaceall(JanetArgs args) {
|
2018-05-18 18:19:57 +00:00
|
|
|
int32_t result;
|
|
|
|
struct replace_state s;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer b;
|
2018-05-18 18:19:57 +00:00
|
|
|
const uint8_t *ret;
|
|
|
|
int32_t lastindex = 0;
|
|
|
|
int status = replacesetup(args, &s);
|
|
|
|
if (status) return status;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_init(&b, s.kmp.textlen);
|
2018-05-18 18:19:57 +00:00
|
|
|
while ((result = kmp_next(&s.kmp)) >= 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(&b, s.kmp.text + lastindex, result - lastindex);
|
|
|
|
janet_buffer_push_bytes(&b, s.subst, s.substlen);
|
2018-05-18 20:24:09 +00:00
|
|
|
lastindex = result + s.kmp.patlen;
|
2018-09-30 00:01:57 +00:00
|
|
|
kmp_seti(&s.kmp, lastindex);
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(&b, s.kmp.text + lastindex, s.kmp.textlen - lastindex);
|
|
|
|
ret = janet_string(b.data, b.count);
|
|
|
|
janet_buffer_deinit(&b);
|
2018-05-18 18:19:57 +00:00
|
|
|
kmp_deinit(&s.kmp);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, ret);
|
2018-05-18 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_split(JanetArgs args) {
|
2018-05-18 20:24:09 +00:00
|
|
|
int32_t result;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array;
|
2018-05-18 20:24:09 +00:00
|
|
|
struct kmp_state state;
|
|
|
|
int32_t limit = -1, lastindex = 0;
|
|
|
|
if (args.n == 4) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_INTEGER(limit, args, 3);
|
2018-05-18 20:24:09 +00:00
|
|
|
}
|
|
|
|
int status = findsetup(args, &state, 1);
|
|
|
|
if (status) return status;
|
2018-09-06 02:18:42 +00:00
|
|
|
array = janet_array(0);
|
2018-05-18 20:24:09 +00:00
|
|
|
while ((result = kmp_next(&state)) >= 0 && limit--) {
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *slice = janet_string(state.text + lastindex, result - lastindex);
|
|
|
|
janet_array_push(array, janet_wrap_string(slice));
|
2018-05-18 20:24:09 +00:00
|
|
|
lastindex = result + state.patlen;
|
|
|
|
}
|
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *slice = janet_string(state.text + lastindex, state.textlen - lastindex);
|
|
|
|
janet_array_push(array, janet_wrap_string(slice));
|
2018-05-18 20:24:09 +00:00
|
|
|
}
|
|
|
|
kmp_deinit(&state);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_ARRAY(args, array);
|
2018-05-18 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_checkset(JanetArgs args) {
|
2018-05-20 01:16:00 +00:00
|
|
|
const uint8_t *set, *str;
|
|
|
|
int32_t setlen, strlen, i;
|
|
|
|
uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 2);
|
|
|
|
JANET_MAXARITY(args, 3);
|
|
|
|
JANET_ARG_BYTES(set, setlen, args, 0);
|
|
|
|
JANET_ARG_BYTES(str, strlen, args, 1);
|
2018-05-20 01:16:00 +00:00
|
|
|
/* Populate set */
|
|
|
|
for (i = 0; i < setlen; i++) {
|
|
|
|
int index = set[i] >> 5;
|
|
|
|
uint32_t mask = 1 << (set[i] & 7);
|
|
|
|
bitset[index] |= mask;
|
|
|
|
}
|
|
|
|
if (args.n == 3) {
|
|
|
|
int invert;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_BOOLEAN(invert, args, 2);
|
2018-05-20 01:16:00 +00:00
|
|
|
if (invert) {
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
bitset[i] = ~bitset[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check set */
|
|
|
|
for (i = 0; i < strlen; i++) {
|
|
|
|
int index = str[i] >> 5;
|
|
|
|
uint32_t mask = 1 << (str[i] & 7);
|
|
|
|
if (!(bitset[index] & mask)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_FALSE(args);
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_TRUE(args);
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int cfun_join(JanetArgs args) {
|
|
|
|
const Janet *parts;
|
2018-05-24 02:08:36 +00:00
|
|
|
const uint8_t *joiner;
|
|
|
|
uint8_t *buf, *out;
|
|
|
|
int32_t joinerlen, partslen, finallen, i;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 2);
|
|
|
|
JANET_ARG_INDEXED(parts, partslen, args, 0);
|
2018-05-24 02:08:36 +00:00
|
|
|
if (args.n == 2) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_BYTES(joiner, joinerlen, args, 1);
|
2018-05-24 02:08:36 +00:00
|
|
|
} else {
|
|
|
|
joiner = NULL;
|
|
|
|
joinerlen = 0;
|
|
|
|
}
|
|
|
|
/* Check args */
|
|
|
|
finallen = 0;
|
|
|
|
for (i = 0; i < partslen; i++) {
|
|
|
|
const uint8_t *chunk;
|
|
|
|
int32_t chunklen = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (!janet_bytes_view(parts[i], &chunk, &chunklen)) {
|
|
|
|
JANET_THROW(args, "expected string|symbol|buffer");
|
2018-05-24 02:08:36 +00:00
|
|
|
}
|
|
|
|
if (i) finallen += joinerlen;
|
|
|
|
finallen += chunklen;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
out = buf = janet_string_begin(finallen);
|
2018-05-24 02:08:36 +00:00
|
|
|
for (i = 0; i < partslen; i++) {
|
|
|
|
const uint8_t *chunk = NULL;
|
|
|
|
int32_t chunklen = 0;
|
|
|
|
if (i) {
|
|
|
|
memcpy(out, joiner, joinerlen);
|
|
|
|
out += joinerlen;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_bytes_view(parts[i], &chunk, &chunklen);
|
2018-05-24 02:08:36 +00:00
|
|
|
memcpy(out, chunk, chunklen);
|
|
|
|
out += chunklen;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_STRING(args, janet_string_end(buf));
|
2018-09-12 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 15:51:23 +00:00
|
|
|
static struct formatter {
|
2018-09-12 15:44:34 +00:00
|
|
|
const char *lead;
|
|
|
|
const char *f1;
|
|
|
|
const char *f2;
|
|
|
|
} formatters[] = {
|
|
|
|
{":g", "%g", "%.*g"},
|
|
|
|
{":G", "%G", "%.*G"},
|
|
|
|
{":e", "%e", "%.*e"},
|
|
|
|
{":E", "%E", "%.*E"},
|
|
|
|
{":f", "%f", "%.*f"},
|
|
|
|
{":F", "%F", "%.*F"}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cfun_number(JanetArgs args) {
|
|
|
|
struct formatter fmter = formatters[0];
|
|
|
|
char buf[100];
|
|
|
|
double x;
|
|
|
|
int formatNargs = 1;
|
|
|
|
int32_t precision = 0;
|
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 4);
|
|
|
|
JANET_ARG_NUMBER(x, args, 0);
|
|
|
|
if (args.n >= 2) {
|
|
|
|
const uint8_t *flag;
|
|
|
|
JANET_ARG_SYMBOL(flag, args, 1);
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
struct formatter fmttest = formatters[i];
|
|
|
|
if (!janet_cstrcmp(flag, fmttest.lead)) {
|
|
|
|
fmter = fmttest;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.n >= 3) {
|
|
|
|
JANET_ARG_INTEGER(precision, args, 2);
|
|
|
|
formatNargs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatNargs == 1) {
|
|
|
|
snprintf(buf, sizeof(buf), fmter.f1, x);
|
|
|
|
} else if (formatNargs == 2) {
|
|
|
|
snprintf(buf, sizeof(buf), fmter.f2, precision, x);
|
|
|
|
}
|
2018-05-24 02:08:36 +00:00
|
|
|
|
2018-09-12 15:44:34 +00:00
|
|
|
JANET_RETURN_CSTRING(args, buf);
|
2018-05-24 02:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:15:21 +00:00
|
|
|
static int cfun_pretty(JanetArgs args) {
|
|
|
|
JanetBuffer *buffer = NULL;
|
|
|
|
int32_t depth = 4;
|
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 3);
|
|
|
|
if (args.n > 1)
|
|
|
|
JANET_ARG_INTEGER(depth, args, 1);
|
|
|
|
if (args.n > 2)
|
|
|
|
JANET_ARG_BUFFER(buffer, args, 2);
|
|
|
|
buffer = janet_pretty(buffer, depth, args.v[0]);
|
|
|
|
JANET_RETURN_BUFFER(args, buffer);
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/slice", cfun_slice,
|
|
|
|
"(string/slice bytes [,start=0 [,end=(length str)]])\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns a substring from a byte sequence. The substring is from "
|
|
|
|
"index start inclusive to index end exclusive. All indexing "
|
|
|
|
"is from 0. 'start' and 'end' can also be negative to indicate indexing "
|
|
|
|
"from the end of the string."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/repeat", cfun_repeat,
|
|
|
|
"(string/repeat bytes n)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns a string that is n copies of bytes concatenated."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/bytes", cfun_bytes,
|
|
|
|
"(string/bytes str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns an array of integers that are the byte values of the string."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/from-bytes", cfun_frombytes,
|
|
|
|
"(string/from-bytes byte-array)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Creates a string from an array of integers with byte values. All integers "
|
|
|
|
"will be coerced to the range of 1 byte 0-255."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/ascii-lower", cfun_asciilower,
|
|
|
|
"(string/ascii-lower str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns a new string where all bytes are replaced with the "
|
|
|
|
"lowercase version of themselves in ascii. Does only a very simple "
|
|
|
|
"case check, meaning no unicode support."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/ascii-upper", cfun_asciiupper,
|
|
|
|
"(string/ascii-upper str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns a new string where all bytes are replaced with the "
|
|
|
|
"uppercase version of themselves in ascii. Does only a very simple "
|
|
|
|
"case check, meaning no unicode support."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/reverse", cfun_reverse,
|
|
|
|
"(string/reverse str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Returns a string that is the reversed version of str."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/find", cfun_find,
|
|
|
|
"(string/find patt str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Searches for the first instance of pattern patt in string "
|
|
|
|
"str. Returns the index of the first character in patt if found, "
|
|
|
|
"otherwise returns nil."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/find-all", cfun_findall,
|
|
|
|
"(string/find patt str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Searches for all instances of pattern patt in string "
|
|
|
|
"str. Returns an array of all indices of found patterns. Overlapping "
|
|
|
|
"instances of the pattern are not counted, meaning a byte in string "
|
|
|
|
"will only contribute to finding at most on occurrence of pattern. If no "
|
|
|
|
"occurrences are found, will return an empty array."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/replace", cfun_replace,
|
|
|
|
"(string/replace patt subst str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Replace the first occurrence of patt with subst in the the string str. "
|
|
|
|
"Will return the new string if patt is found, otherwise returns str."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/replace-all", cfun_replaceall,
|
|
|
|
"(string/replace-all patt subst str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Replace all instances of patt with subst in the string str. "
|
|
|
|
"Will return the new string if patt is found, otherwise returns str."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/split", cfun_split,
|
|
|
|
"(string/split delim str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Splits a string str with delimiter delim and returns an array of "
|
|
|
|
"substrings. The substrings will not contain the delimiter delim. If delim "
|
|
|
|
"is not found, the returned array will have one element."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/check-set", cfun_checkset,
|
|
|
|
"(string/check-set set str)\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Checks if any of the bytes in the string set appear in the string str. "
|
|
|
|
"Returns true if some bytes in set do appear in str, false if no bytes do."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/join", cfun_join,
|
|
|
|
"(string/join parts [,sep])\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Joins an array of strings into one string, optionally separated by "
|
|
|
|
"a separator string sep."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/number", cfun_number,
|
|
|
|
"(string/number x [,format [,maxlen [,precision]]])\n\n"
|
2018-11-16 21:24:10 +00:00
|
|
|
"Formats a number as string. The format parameter indicates how "
|
|
|
|
"to display the number, either as floating point, scientific, or "
|
|
|
|
"whichever representation is shorter. format can be:\n\n"
|
|
|
|
"\t:g - (default) shortest representation with lowercase e.\n"
|
|
|
|
"\t:G - shortest representation with uppercase E.\n"
|
|
|
|
"\t:e - scientific with lowercase e.\n"
|
|
|
|
"\t:E - scientific with uppercase E.\n"
|
|
|
|
"\t:f - floating point representation.\n"
|
|
|
|
"\t:F - same as :f\n\n"
|
|
|
|
"The programmer can also specify the max length of the output string "
|
|
|
|
"and the precision (number of places after decimal) in the output number. "
|
|
|
|
"Returns a string representation of x."
|
|
|
|
},
|
2018-12-01 03:49:21 +00:00
|
|
|
{"string/pretty", cfun_pretty,
|
|
|
|
"(string/pretty x [,depth=4 [,buffer=@\"\"]])\n\n"
|
2018-11-19 07:15:21 +00:00
|
|
|
"Pretty prints a value to a buffer. Optionally allwos setting max "
|
|
|
|
"recursion depth, as well as writing to a buffer. Returns the buffer."
|
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-05-18 03:41:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Module entry point */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_lib_string(JanetArgs args) {
|
|
|
|
JanetTable *env = janet_env(args);
|
|
|
|
janet_cfuns(env, NULL, cfuns);
|
2018-05-18 03:41:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|