From e0881c558c92442c021307d207cc719016ab72e6 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 13 Feb 2023 22:31:18 +0100 Subject: [PATCH] strings+io: Make chars unsigned --- src/apfl.h | 16 ++++++++-------- src/format.c | 16 ++++++++-------- src/functional-test-runner.c | 2 +- src/globals.c | 6 +++--- src/main.c | 6 +++--- src/source_readers.c | 4 ++-- src/strings.c | 8 ++++---- src/tokenizer.c | 2 +- src/tokenizer_test.c | 4 ++-- webpage/playground/playground.c | 6 +++--- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/apfl.h b/src/apfl.h index 99bafd2..b88302c 100644 --- a/src/apfl.h +++ b/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; }; diff --git a/src/format.c b/src/format.c index 8510130..855de7e 100644 --- a/src/format.c +++ b/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 diff --git a/src/functional-test-runner.c b/src/functional-test-runner.c index fd5abca..33c262c 100644 --- a/src/functional-test-runner.c +++ b/src/functional-test-runner.c @@ -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)) { diff --git a/src/globals.c b/src/globals.c index 43ff68f..dde1b4d 100644 --- a/src/globals.c +++ b/src/globals.c @@ -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); } diff --git a/src/main.c b/src/main.c index 971e42b..73e6012 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } diff --git a/src/source_readers.c b/src/source_readers.c index f380a79..9515bc8 100644 --- a/src/source_readers.c +++ b/src/source_readers.c @@ -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; diff --git a/src/strings.c b/src/strings.c index 0686882..73c028a 100644 --- a/src/strings.c +++ b/src/strings.c @@ -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); } diff --git a/src/tokenizer.c b/src/tokenizer.c index d3b5da1..3388ff3 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -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; diff --git a/src/tokenizer_test.c b/src/tokenizer_test.c index f4948c0..0d97511 100644 --- a/src/tokenizer_test.c +++ b/src/tokenizer_test.c @@ -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); diff --git a/webpage/playground/playground.c b/webpage/playground/playground.c index 33dadba..44690d1 100644 --- a/webpage/playground/playground.c +++ b/webpage/playground/playground.c @@ -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;