From caa16deb1cca045e88065b86ac39826fcbee84fb Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 10 Oct 2021 14:40:51 +0100 Subject: [PATCH] bidi.c: update the API. The input length field is now a size_t rather than an int, on general principles. The return value is now void (we weren't using the previous return value at all). And we now require the client to have previously allocated a BidiContext, which will allow allocated storage to be reused between runs, saving a lot of churn on malloc. (However, the current BidiContext doesn't contain anything interesting. I could have moved the existing mallocs into it, but there's no point, since I'm about to rewrite the whole thing anyway.) --- defs.h | 2 ++ putty.h | 4 +++- terminal/bidi.c | 22 +++++++++++++++++++--- terminal/terminal.c | 6 +++++- terminal/terminal.h | 2 ++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/defs.h b/defs.h index b8205c10..e5d3fefe 100644 --- a/defs.h +++ b/defs.h @@ -170,6 +170,8 @@ typedef struct SessionSpecial SessionSpecial; typedef struct StripCtrlChars StripCtrlChars; +typedef struct BidiContext BidiContext; + /* * A small structure wrapping up a (pointer, length) pair so that it * can be conveniently passed to or from a function. diff --git a/putty.h b/putty.h index 334b588b..6bff90bb 100644 --- a/putty.h +++ b/putty.h @@ -2359,7 +2359,9 @@ typedef struct bidi_char { unsigned int origwc, wc; unsigned short index, nchars; } bidi_char; -int do_bidi(bidi_char *line, int count); +BidiContext *bidi_new_context(void); +void bidi_free_context(BidiContext *ctx); +void do_bidi(BidiContext *ctx, bidi_char *line, size_t count); int do_shape(bidi_char *line, bidi_char *to, int count); bool is_rtl(int c); diff --git a/terminal/bidi.c b/terminal/bidi.c index 4c55b86e..15ed23d0 100644 --- a/terminal/bidi.c +++ b/terminal/bidi.c @@ -1116,6 +1116,22 @@ int do_shape(bidi_char *line, bidi_char *to, int count) return 1; } +struct BidiContext { + int dummy; +}; + +BidiContext *bidi_new_context(void) +{ + BidiContext *ctx = snew(BidiContext); + memset(ctx, 0, sizeof(BidiContext)); + return ctx; +} + +void bidi_free_context(BidiContext *ctx) +{ + sfree(ctx); +} + /* * The Main Bidi Function, and the only function that should * be used by the outside world. @@ -1124,7 +1140,7 @@ int do_shape(bidi_char *line, bidi_char *to, int count) * the Bidirectional algorithm to. */ -int do_bidi(bidi_char *line, int count) +void do_bidi(BidiContext *ctx, bidi_char *line, size_t count) { unsigned char* types; unsigned char* levels; @@ -1145,7 +1161,7 @@ int do_bidi(bidi_char *line, int count) } } if (!yes) - return L; + return; /* Initialize types, levels */ types = snewn(count, unsigned char); @@ -1565,7 +1581,7 @@ int do_bidi(bidi_char *line, int count) */ sfree(types); sfree(levels); - return R; + return; } diff --git a/terminal/terminal.c b/terminal/terminal.c index ddeb1e94..dcdbfc6a 100644 --- a/terminal/terminal.c +++ b/terminal/terminal.c @@ -2048,6 +2048,8 @@ Terminal *term_init(Conf *myconf, struct unicode_data *ucsdata, TermWin *win) term->win_scrollbar_update_pending = false; term->win_palette_pending = false; + term->bidi_ctx = bidi_new_context(); + palette_reset(term, false); return term; @@ -2107,6 +2109,8 @@ void term_free(Terminal *term) sfree(term->window_title); sfree(term->icon_title); + bidi_free_context(term->bidi_ctx); + sfree(term); } @@ -5662,7 +5666,7 @@ static termchar *term_bidi_line(Terminal *term, struct termline *ldata, } if(!term->no_bidi) - do_bidi(term->wcFrom, nbc); + do_bidi(term->bidi_ctx, term->wcFrom, nbc); if(!term->no_arabicshaping) { do_shape(term->wcFrom, term->wcTo, nbc); diff --git a/terminal/terminal.h b/terminal/terminal.h index c463f7a8..75d08de7 100644 --- a/terminal/terminal.h +++ b/terminal/terminal.h @@ -353,6 +353,8 @@ struct terminal_tag { char *window_title, *icon_title; bool minimised; + BidiContext *bidi_ctx; + /* Multi-layered colour palette. The colours from Conf (plus the * default xterm-256 ones that don't have Conf ids at all) have * lowest priority, followed by platform overrides if any,