strings+io: Make chars unsigned
This commit is contained in:
parent
01dfec2e32
commit
e0881c558c
10 changed files with 35 additions and 35 deletions
16
src/apfl.h
16
src/apfl.h
|
|
@ -54,12 +54,12 @@ bool apfl_position_eq(struct apfl_position, struct apfl_position);
|
|||
// Strings
|
||||
|
||||
struct apfl_string_view {
|
||||
const char *bytes;
|
||||
const unsigned char *bytes;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct apfl_string {
|
||||
char *bytes;
|
||||
unsigned char *bytes;
|
||||
size_t len;
|
||||
|
||||
// TODO: Not sure, if it's a good idea to expose this. We now need the actual
|
||||
|
|
@ -101,7 +101,7 @@ bool apfl_string_copy(struct apfl_allocator allocator, struct apfl_string *dst,
|
|||
|
||||
struct apfl_string_builder {
|
||||
struct apfl_allocator allocator;
|
||||
char *bytes;
|
||||
unsigned char *bytes;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
};
|
||||
|
|
@ -110,8 +110,8 @@ struct apfl_string_builder apfl_string_builder_init(struct apfl_allocator alloca
|
|||
void apfl_string_builder_deinit(struct apfl_string_builder *);
|
||||
bool apfl_string_builder_prealloc(struct apfl_string_builder *, size_t newcap);
|
||||
bool apfl_string_builder_append(struct apfl_string_builder *, struct apfl_string_view);
|
||||
bool apfl_string_builder_append_byte(struct apfl_string_builder *, char byte);
|
||||
bool apfl_string_builder_append_bytes(struct apfl_string_builder *, const char *bytes, size_t len);
|
||||
bool apfl_string_builder_append_byte(struct apfl_string_builder *, unsigned char byte);
|
||||
bool apfl_string_builder_append_bytes(struct apfl_string_builder *, const unsigned char *bytes, size_t len);
|
||||
struct apfl_string apfl_string_builder_move_string(struct apfl_string_builder *);
|
||||
|
||||
#define apfl_string_builder_append_cstr(builder, cstr) (apfl_string_builder_append((builder), apfl_string_view_from_cstr((cstr))))
|
||||
|
|
@ -127,7 +127,7 @@ struct apfl_string_view apfl_string_view_rtrim(struct apfl_string_view sv);
|
|||
struct apfl_string_view apfl_string_view_trim(struct apfl_string_view sv);
|
||||
|
||||
struct apfl_io_writer {
|
||||
bool (*write)(void *, const char *buf, size_t len);
|
||||
bool (*write)(void *, const unsigned char *buf, size_t len);
|
||||
void *opaque;
|
||||
};
|
||||
|
||||
|
|
@ -137,7 +137,7 @@ struct apfl_io_writer apfl_io_string_writer(struct apfl_string_builder *sb);
|
|||
|
||||
bool apfl_io_write_string_view(struct apfl_io_writer, struct apfl_string_view);
|
||||
#define apfl_io_write_string(w, s) apfl_io_write_string_view((w), apfl_string_view_from(s))
|
||||
bool apfl_io_write_byte(struct apfl_io_writer, char);
|
||||
bool apfl_io_write_byte(struct apfl_io_writer, unsigned char);
|
||||
|
||||
bool apfl_format_put_int(struct apfl_io_writer, int);
|
||||
bool apfl_format_put_hexbyte(struct apfl_io_writer, unsigned char);
|
||||
|
|
@ -525,7 +525,7 @@ struct apfl_source_reader {
|
|||
* implementing a reader for a REPL to indicate to the user if they need
|
||||
* to type more code (e.g. by changing the prompt).
|
||||
*/
|
||||
bool (*callback)(void *opaque, char *buf, size_t *len, bool need);
|
||||
bool (*callback)(void *opaque, unsigned char *buf, size_t *len, bool need);
|
||||
|
||||
void *opaque;
|
||||
};
|
||||
|
|
|
|||
16
src/format.c
16
src/format.c
|
|
@ -10,7 +10,7 @@
|
|||
#define TRY FMT_TRY
|
||||
|
||||
static bool
|
||||
write(struct apfl_io_writer w, const char *buf, size_t len)
|
||||
write(struct apfl_io_writer w, const unsigned char *buf, size_t len)
|
||||
{
|
||||
return len == 0
|
||||
? true
|
||||
|
|
@ -18,14 +18,14 @@ write(struct apfl_io_writer w, const char *buf, size_t len)
|
|||
}
|
||||
|
||||
static bool
|
||||
write_file(void *opaque, const char *buf, size_t len)
|
||||
write_file(void *opaque, const unsigned char *buf, size_t len)
|
||||
{
|
||||
FILE *f = opaque;
|
||||
return fwrite(buf, len, 1, f) == 1;
|
||||
}
|
||||
|
||||
static bool
|
||||
write_string(void *opaque, const char *buf, size_t len)
|
||||
write_string(void *opaque, const unsigned char *buf, size_t len)
|
||||
{
|
||||
struct apfl_string_builder *sb = opaque;
|
||||
return apfl_string_builder_append(sb, (struct apfl_string_view) {.bytes = buf, .len = len});
|
||||
|
|
@ -63,11 +63,11 @@ apfl_format_put_int(struct apfl_io_writer w, int i)
|
|||
char buf[PUT_INT_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_INT_BUFSIZE, "%d", i);
|
||||
assert(len < PUT_INT_BUFSIZE);
|
||||
return write(w, buf, len);
|
||||
return write(w, (const unsigned char *)buf, len);
|
||||
}
|
||||
|
||||
bool
|
||||
apfl_io_write_byte(struct apfl_io_writer w, char c)
|
||||
apfl_io_write_byte(struct apfl_io_writer w, unsigned char c)
|
||||
{
|
||||
return write(w, &c, 1);
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ apfl_format_put_hexbyte(struct apfl_io_writer w, unsigned char c)
|
|||
char buf[PUT_HEXBYTE_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_HEXBYTE_BUFSIZE, "%x", (unsigned) c);
|
||||
assert(len < PUT_HEXBYTE_BUFSIZE);
|
||||
return write(w, buf, len);
|
||||
return write(w, (const unsigned char *)buf, len);
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
@ -108,7 +108,7 @@ apfl_format_put_number(struct apfl_io_writer w, apfl_number number)
|
|||
{
|
||||
char buf[PUT_NUMBER_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_NUMBER_BUFSIZE, "%.12G", number);
|
||||
TRY(write(w, buf, len));
|
||||
TRY(write(w, (const unsigned char *)buf, len));
|
||||
if (len >= PUT_NUMBER_BUFSIZE) {
|
||||
TRY(apfl_io_write_string(w, "[...]"));
|
||||
}
|
||||
|
|
@ -123,7 +123,7 @@ apfl_format_put_poiner(struct apfl_io_writer w, void *ptr)
|
|||
char buf[PUT_POINTER_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_POINTER_BUFSIZE, "%p", ptr);
|
||||
assert(len < PUT_POINTER_BUFSIZE);
|
||||
return write(w, buf, len);
|
||||
return write(w, (const unsigned char *)buf, len);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ file_get_contents(const char *filename, struct apfl_string *str, struct apfl_all
|
|||
|
||||
struct apfl_string_builder sb = apfl_string_builder_init(allocator);
|
||||
|
||||
char buf[BUFSIZE];
|
||||
unsigned char buf[BUFSIZE];
|
||||
while (!feof(f)) {
|
||||
size_t len = fread(buf, 1, BUFSIZE, f);
|
||||
if (!apfl_string_builder_append_bytes(&sb, buf, len)) {
|
||||
|
|
|
|||
|
|
@ -401,9 +401,9 @@ static const char *
|
|||
getcstring(apfl_ctx ctx, apfl_stackidx index)
|
||||
{
|
||||
apfl_move_to_top_of_stack(ctx, index);
|
||||
apfl_push_string_view_copy(ctx, (struct apfl_string_view) { .bytes = (char [1]) {'\0'}, .len = 1, });
|
||||
apfl_push_string_view_copy(ctx, (struct apfl_string_view) { .bytes = (unsigned char [1]) {'\0'}, .len = 1, });
|
||||
apfl_concat_strings(ctx, -2, -1);
|
||||
return apfl_get_string(ctx, -1).bytes;
|
||||
return (const char *)apfl_get_string(ctx, -1).bytes;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -462,7 +462,7 @@ impl_fread(apfl_ctx ctx)
|
|||
|
||||
size_t size = (size_t)presize;
|
||||
|
||||
char *buf = ALLOC_BYTES(ctx->gc.allocator, size);
|
||||
unsigned char *buf = ALLOC_BYTES(ctx->gc.allocator, size);
|
||||
if (buf == NULL) {
|
||||
apfl_raise_alloc_error(ctx);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ enum repl_mode {
|
|||
};
|
||||
|
||||
static bool
|
||||
repl_source_reader_cb(void *context, char *buf, size_t *len, bool need)
|
||||
repl_source_reader_cb(void *context, unsigned char *buf, size_t *len, bool need)
|
||||
{
|
||||
(void)context;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ repl_source_reader_cb(void *context, char *buf, size_t *len, bool need)
|
|||
|
||||
size_t maxlen = *len;
|
||||
|
||||
if (fgets(buf, maxlen, stdin) == NULL) {
|
||||
if (fgets((char *)buf, maxlen, stdin) == NULL) {
|
||||
if (feof(stdin)) {
|
||||
*len = 0;
|
||||
return true;
|
||||
|
|
@ -31,7 +31,7 @@ repl_source_reader_cb(void *context, char *buf, size_t *len, bool need)
|
|||
}
|
||||
}
|
||||
|
||||
*len = strlen(buf);
|
||||
*len = strlen((char *)buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include "apfl.h"
|
||||
|
||||
static bool
|
||||
string_reader_callback(void *opaque, char *buf, size_t *len, bool need)
|
||||
string_reader_callback(void *opaque, unsigned char *buf, size_t *len, bool need)
|
||||
{
|
||||
(void)need;
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ struct apfl_source_reader apfl_string_source_reader(struct apfl_string_source_re
|
|||
}
|
||||
|
||||
static bool
|
||||
stdio_reader_callback(void *opaque, char *buf, size_t *len, bool need)
|
||||
stdio_reader_callback(void *opaque, unsigned char *buf, size_t *len, bool need)
|
||||
{
|
||||
(void)need;
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ struct apfl_string_view
|
|||
apfl_string_view_from_cstr(char *cstr)
|
||||
{
|
||||
return (struct apfl_string_view) {
|
||||
.bytes = cstr,
|
||||
.bytes = (unsigned char *)cstr,
|
||||
.len = strlen(cstr),
|
||||
};
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ struct apfl_string_view
|
|||
apfl_string_view_from_const_cstr(const char *cstr)
|
||||
{
|
||||
return (struct apfl_string_view) {
|
||||
.bytes = cstr,
|
||||
.bytes = (unsigned char *)cstr,
|
||||
.len = strlen(cstr),
|
||||
};
|
||||
}
|
||||
|
|
@ -122,7 +122,7 @@ apfl_string_builder_deinit(struct apfl_string_builder *builder)
|
|||
}
|
||||
|
||||
bool
|
||||
apfl_string_builder_append_bytes(struct apfl_string_builder *builder, const char *bytes, size_t len)
|
||||
apfl_string_builder_append_bytes(struct apfl_string_builder *builder, const unsigned char *bytes, size_t len)
|
||||
{
|
||||
return apfl_resizable_append(
|
||||
builder->allocator,
|
||||
|
|
@ -152,7 +152,7 @@ apfl_string_builder_append(struct apfl_string_builder *builder, struct apfl_stri
|
|||
}
|
||||
|
||||
bool
|
||||
apfl_string_builder_append_byte(struct apfl_string_builder *builder, char byte)
|
||||
apfl_string_builder_append_byte(struct apfl_string_builder *builder, unsigned char byte)
|
||||
{
|
||||
return apfl_string_builder_append_bytes(builder, &byte, 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ static_assert(INT_MAX >= BUFSIZE, "BUFSIZE is too large for type buf_offset");
|
|||
struct apfl_tokenizer {
|
||||
struct apfl_allocator allocator;
|
||||
struct apfl_source_reader source_reader;
|
||||
char *buf;
|
||||
unsigned char *buf;
|
||||
buf_offset buf_pos;
|
||||
buf_offset buf_len;
|
||||
|
||||
|
|
|
|||
|
|
@ -296,11 +296,11 @@ TEST(all_tokens, t) {
|
|||
|
||||
TEST(strings_with_binary_data, t) {
|
||||
struct tokenizer_test *tt = new_tokenizer_test_sv(t, (struct apfl_string_view) {
|
||||
.bytes = (char []){'"', '\x00', '\xFF', '\\', 'x', '0', '0', '\\', 'x', '2', 'a', '"'},
|
||||
.bytes = (unsigned char []){'"', '\x00', '\xFF', '\\', 'x', '0', '0', '\\', 'x', '2', 'a', '"'},
|
||||
.len = 12,
|
||||
});
|
||||
expect_text_token_sv(tt, 1, 1, APFL_TOK_STRING, (struct apfl_string_view) {
|
||||
.bytes = (char []){'\x00', '\xFF', '\x00', '\x2A'},
|
||||
.bytes = (unsigned char []){'\x00', '\xFF', '\x00', '\x2A'},
|
||||
.len = 4
|
||||
});
|
||||
expect_eof(tt);
|
||||
|
|
|
|||
|
|
@ -23,12 +23,12 @@ EM_ASYNC_JS(char *, get_input, (int need), {
|
|||
return stringOnWasmHeap;
|
||||
});
|
||||
|
||||
EM_JS(void, web_writer, (int error, const char* str, int size), {
|
||||
EM_JS(void, web_writer, (int error, const unsigned char* str, int size), {
|
||||
window.playground_write(!!error, UTF8ToString(str, size));
|
||||
});
|
||||
|
||||
static bool
|
||||
web_fmt_write(void *opaque, const char *buf, size_t len)
|
||||
web_fmt_write(void *opaque, const unsigned char *buf, size_t len)
|
||||
{
|
||||
int error = (int)opaque;
|
||||
web_writer(error, buf, (int)len);
|
||||
|
|
@ -36,7 +36,7 @@ web_fmt_write(void *opaque, const char *buf, size_t len)
|
|||
}
|
||||
|
||||
static bool
|
||||
playground_source_reader_cb(void *context, char *buf, size_t *len, bool need)
|
||||
playground_source_reader_cb(void *context, unsigned char *buf, size_t *len, bool need)
|
||||
{
|
||||
struct playground_source_reader_data *r = context;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue