From 914edb1472b322c7bfe081e34671398f715476d4 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 29 Jul 2021 08:23:40 -0400 Subject: [PATCH] composetable: Add a prefix api Add a function that computes the longest prefix of a buffer for which a compose table has matches. --- gtk/gtkcomposetable.c | 41 +++++++++++++++++++++++++++++++++++++++++ gtk/gtkcomposetable.h | 5 +++++ 2 files changed, 46 insertions(+) diff --git a/gtk/gtkcomposetable.c b/gtk/gtkcomposetable.c index 40dd8b6e41..0d9488c214 100644 --- a/gtk/gtkcomposetable.c +++ b/gtk/gtkcomposetable.c @@ -1352,6 +1352,47 @@ gtk_compose_table_check (const GtkComposeTable *table, return FALSE; } +void +gtk_compose_table_get_prefix (const GtkComposeTable *table, + const guint16 *compose_buffer, + int n_compose, + int *prefix) +{ + int index_stride = table->max_seq_len + 1; + int p = 0; + + for (int idx = 0; idx < table->n_index_size; idx++) + { + const guint16 *seq_index = table->data + (idx * index_stride); + + if (seq_index[0] == compose_buffer[0]) + { + p = 1; + + for (int i = 1; i < table->max_seq_len; i++) + { + int len = i + 1; + + for (int j = seq_index[i]; j < seq_index[i + 1]; j += len) + { + int k; + + for (k = 0; k < MIN (len, n_compose) - 1; k++) + { + if (compose_buffer[k + 1] != (gunichar) table->data[j + k]) + break; + } + p = MAX (p, k + 1); + } + } + + break; + } + } + + *prefix = p; +} + void gtk_compose_table_foreach (const GtkComposeTable *table, GtkComposeSequenceCallback callback, diff --git a/gtk/gtkcomposetable.h b/gtk/gtkcomposetable.h index 1398559a66..e9adc50150 100644 --- a/gtk/gtkcomposetable.h +++ b/gtk/gtkcomposetable.h @@ -80,6 +80,11 @@ gboolean gtk_compose_table_check (const GtkComposeTable *table, gboolean *compose_match, GString *output); +void gtk_compose_table_get_prefix (const GtkComposeTable *table, + const guint16 *compose_buffer, + int n_compose, + int *prefix); + gboolean gtk_check_algorithmically (const guint16 *compose_buffer, int n_compose, gunichar *output);