jl_value_t *jl_readuntil(ios_t *s, uint8_t delim) { jl_array_t *a; // manually inlined common case char *pd = (char*)memchr(s->buf+s->bpos, delim, s->size - s->bpos); if (pd) { size_t n = pd-(s->buf+s->bpos)+1; a = jl_alloc_array_1d(jl_array_uint8_type, n); memcpy(jl_array_data(a), s->buf+s->bpos, n); s->bpos += n; } else { a = jl_alloc_array_1d(jl_array_uint8_type, 80); ios_t dest; jl_ios_mem(&dest, 0); ios_setbuf(&dest, a->data, 80, 0); size_t n = ios_copyuntil(&dest, s, delim); if (dest.buf != a->data) { a = jl_takebuf_array(&dest); } else { a->length = n; a->nrows = n; ((char*)a->data)[n] = '\0'; } } JL_GC_PUSH(&a); jl_struct_type_t* string_type = u8_isvalid(a->data, a->length) == 1 ? // ASCII jl_ascii_string_type : jl_utf8_string_type; jl_value_t *str = alloc_2w(); str->type = (jl_type_t*)string_type; jl_fieldref(str,0) = (jl_value_t*)a; JL_GC_POP(); return str; }
static void print_string(ios_t *f, char *str, size_t sz) { char buf[512]; size_t i = 0; uint8_t c; static char hexdig[] = "0123456789abcdef"; outc('"', f); if (!u8_isvalid(str, sz)) { // alternate print algorithm that preserves data if it's not UTF-8 for(i=0; i < sz; i++) { c = str[i]; if (c == '\\') outsn("\\\\", f, 2); else if (c == '"') outsn("\\\"", f, 2); else if (c >= 32 && c < 0x7f) outc(c, f); else { outsn("\\x", f, 2); outc(hexdig[c>>4], f); outc(hexdig[c&0xf], f); } } } else { while (i < sz) {
jl_value_t *jl_array_to_string(jl_array_t *a) { // TODO: check type of array? jl_struct_type_t* string_type = u8_isvalid(a->data, a->length) == 1 ? // ASCII jl_ascii_string_type : jl_utf8_string_type; return jl_apply((jl_function_t*)string_type, (jl_value_t**)&a, 1); }
static int reactor_isvalidutf8(lua_State* L) { int len = 0; const char* str = luaL_checklstring(L, 1, &len); lua_pushboolean(L, u8_isvalid(str, len)); return 1; }
jl_value_t *jl_array_to_string(jl_array_t *a) { // TODO: check type of array? jl_struct_type_t* string_type = u8_isvalid(a->data, a->length) == 1 ? // ASCII jl_ascii_string_type : jl_utf8_string_type; jl_value_t *s = alloc_2w(); s->type = (jl_type_t*)string_type; jl_set_nth_field(s, 0, (jl_value_t*)a); return s; }
jl_value_t *jl_array_to_string(jl_array_t *a) { if (!jl_typeis(a, jl_array_uint8_type)) jl_type_error("jl_array_to_string", (jl_value_t*)jl_array_uint8_type, (jl_value_t*)a); jl_datatype_t *string_type = u8_isvalid((char*)a->data, jl_array_len(a)) == 1 ? // ASCII jl_ascii_string_type : jl_utf8_string_type; jl_value_t *s = (jl_value_t*)jl_gc_alloc_1w(); jl_set_typeof(s, string_type); jl_set_nth_field(s, 0, (jl_value_t*)a); return s; }