From c033a6886e4d6efb948782fbc389fe5f673acf36 Mon Sep 17 00:00:00 2001 From: Kristaps Dz Date: Sun, 15 Sep 2024 12:47:45 -0700 Subject: [PATCH] Tweak previous. Reference #140 --- gemini.c | 13 ++++++------- term.c | 15 +++++++++------ tree.c | 15 +++++++++------ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/gemini.c b/gemini.c index a9eab51..7758eff 100644 --- a/gemini.c +++ b/gemini.c @@ -167,10 +167,12 @@ rndr_link_ref(const struct gemini *st, static int rndr_escape(struct lowdown_buf *out, const char *buf, size_t sz) { - size_t i, start = 0; + size_t i, start = 0; + unsigned char ch; for (i = 0; i < sz; i++) { - if (buf[i] == '\n') { + ch = (unsigned char)buf[i]; + if (ch == '\n') { if (!hbuf_put(out, buf + start, i - start)) return 0; if (out->size && @@ -180,18 +182,15 @@ rndr_escape(struct lowdown_buf *out, const char *buf, size_t sz) if (!hbuf_putc(out, ' ')) return 0; start = i + 1; - } else if ((unsigned char) buf[i] < 0x80 && iscntrl((unsigned char)buf[i])) { - fprintf(stderr, "this is a control character: %x %x\n", (unsigned char)buf[i], buf[i]); + } else if (ch < 0x80 && iscntrl(ch)) { if (!hbuf_put(out, buf + start, i - start)) return 0; start = i + 1; } } - if (start < sz && - !hbuf_put(out, buf + start, sz - start)) + if (start < sz && !hbuf_put(out, buf + start, sz - start)) return 0; - return 1; } diff --git a/term.c b/term.c index 68dce25..59c3358 100644 --- a/term.c +++ b/term.c @@ -181,15 +181,17 @@ static ssize_t rndr_escape(struct term *term, struct lowdown_buf *out, const char *buf, size_t sz) { - size_t i, start = 0, cols = 0; - ssize_t ret; + size_t i, start = 0, cols = 0; + ssize_t ret; + unsigned char ch; /* Don't allow control characters through. */ - for (i = 0; i < sz; i++) - if ((unsigned char)buf[i] < 0x80 && iscntrl((unsigned char)buf[i])) { - ret = rndr_mbswidth - (term, buf + start, i - start); + for (i = 0; i < sz; i++) { + ch = (unsigned char)buf[i]; + if (ch < 0x80 && iscntrl(ch)) { + ret = rndr_mbswidth (term, buf + start, + i - start); if (ret < 0) return -1; cols += ret; @@ -197,6 +199,7 @@ rndr_escape(struct term *term, struct lowdown_buf *out, return -1; start = i + 1; } + } /* Remaining bytes. */ diff --git a/tree.c b/tree.c index e3b5ac6..88229d6 100644 --- a/tree.c +++ b/tree.c @@ -82,25 +82,28 @@ rndr_indent(struct lowdown_buf *ob, size_t indent) static int rndr_short(struct lowdown_buf *ob, const struct lowdown_buf *b) { - size_t i; + size_t i; + unsigned char ch; - for (i = 0; i < 20 && i < b->size; i++) - if (b->data[i] == '\n') { + for (i = 0; i < 20 && i < b->size; i++) { + ch = (unsigned char)b->data[i]; + if (ch == '\n') { if (!HBUF_PUTSL(ob, "\\n")) return 0; - } else if (b->data[i] == '\r') { + } else if (ch == '\r') { if (!HBUF_PUTSL(ob, "\\r")) return 0; - } else if (b->data[i] == '\t') { + } else if (ch == '\t') { if (!HBUF_PUTSL(ob, "\\t")) return 0; - } else if ((unsigned char)b->data[i] < 0x80 && iscntrl((unsigned char)b->data[i])) { + } else if (ch < 0x80 && iscntrl(ch)) { if (!hbuf_putc(ob, '?')) return 0; } else { if (!hbuf_putc(ob, b->data[i])) return 0; } + } if (i < b->size && !HBUF_PUTSL(ob, "...")) return 0;