From 3deb118d4bf638e5daf631bca56a8856d2a7b1c8 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 1 Jan 2003 17:03:27 +0000 Subject: [PATCH] Having painstakingly generated those reverse mapping tables in sbcsdat.c, it would seem a shame not to actually use them. Ahem. Thanks to Ben, without whose checkin in this area I'd have forgotten completely :-) [originally from svn r2404] --- charset/sbcs.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/charset/sbcs.c b/charset/sbcs.c index c92bdea6..8e2a2274 100644 --- a/charset/sbcs.c +++ b/charset/sbcs.c @@ -28,18 +28,26 @@ void write_sbcs(charset_spec const *charset, long int input_chr, void (*emit)(void *ctx, long int output), void *emitctx) { const struct sbcs_data *sd = charset->data; - int i; + int i, j, k, c; UNUSEDARG(state); /* - * FIXME: this should work, but it's ludicrously inefficient. - * We should be using the ucs2sbcs table. + * Binary-search in the ucs2sbcs table. */ - for (i = 0; i < 256; i++) - if (sd->sbcs2ucs[i] == input_chr) { - emit(emitctx, i); + i = -1; + j = sd->nvalid; + while (i+1 < j) { + k = (i+j)/2; + c = sd->ucs2sbcs[k]; + if (input_chr < sd->sbcs2ucs[c]) + j = k; + else if (input_chr > sd->sbcs2ucs[c]) + i = k; + else { + emit(emitctx, c); return; } + } emit(emitctx, ERROR); } -- 2.45.2