composetable: Add a prefix api

Add a function that computes the longest prefix
of a buffer for which a compose table has matches.
This commit is contained in:
Matthias Clasen 2021-07-29 08:23:40 -04:00
parent dbf5033f94
commit 914edb1472
2 changed files with 46 additions and 0 deletions

View File

@ -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,

View File

@ -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);