# If no exact match is found, show the longest prefix match --- stardict-2.4.8/src/lib/stddict.cpp 2006-07-03 22:15:51.000000000 -0400 +++ stardict-2.4.8/src/lib/stddict.cpp 2006-11-17 01:34:07.000000000 -0500 @@ -618,12 +618,26 @@ return get_key(idx); } +static inline gint prefix_match (const gchar *s1, const gchar *s2) +{ + gint ret=-1; + gunichar u1, u2; + do { + u1 = g_utf8_get_char(s1); + u2 = g_utf8_get_char(s2); + s1 = g_utf8_next_char(s1); + s2 = g_utf8_next_char(s2); + ret++; + } while (u1 && u1 == u2); + return ret; +} + bool offset_index::lookup(const char *str, glong &idx) { bool bFound=false; glong iFrom; glong iTo=npages-2; - gint cmpint; + gint cmpint, best, back; glong iThisIndex; if (stardict_strcmp(str, first.keystr.c_str())<0) { idx = 0; @@ -676,6 +690,18 @@ } else { idx*=ENTR_PER_PAGE; } + if (!bFound) { + best = prefix_match (str, page.entries[idx % ENTR_PER_PAGE].keystr); + for (;;) { + if ((iTo=idx-1) < 0) break; + if (idx % ENTR_PER_PAGE == 0) + iTo += load_page(iTo / ENTR_PER_PAGE) - ENTR_PER_PAGE; + back = prefix_match (str, page.entries[iTo % ENTR_PER_PAGE].keystr); + if (!back || back < best) break; + best = back; + idx = iTo; + } + } return bFound; }