fix edge cases in prefix matching

This commit is contained in:
Eric House 2012-08-28 07:35:11 -07:00
parent 145317426d
commit 4cb848582e
2 changed files with 17 additions and 6 deletions

View file

@ -194,13 +194,18 @@ findStartsWithChars( DictIter* iter, const XP_UCHAR* chars, XP_U16 charsOffset,
array_edge* edge, XP_U16 nTilesUsed )
{
XP_S16 result = -1;
if ( NULL == edge || ( '\0' == chars[charsOffset] ) ) {
XP_U16 charsLen = XP_STRLEN( &chars[charsOffset] );
if ( NULL == edge ) {
if ( 0 == charsLen ) {
iter->nEdges = nTilesUsed;
result = charsOffset;
}
} else if ( 0 == charsLen ) {
iter->nEdges = nTilesUsed;
result = charsOffset;
} else {
const DictionaryCtxt* dict = iter->dict;
XP_U16 nodeSize = dict->nodeSize;
XP_U16 charsLen = XP_STRLEN( &chars[charsOffset] );
for ( ; ; ) {
Tile tile = EDGETILE( dict, edge );
const XP_UCHAR* facep = dict_getTileString( dict, tile );
@ -650,8 +655,14 @@ dict_findStartsWith( DictIter* iter, const XP_UCHAR* prefix )
ASSERT_INITED( iter );
array_edge* edge = dict_getTopEdge( iter->dict );
XP_S16 offset = findStartsWithChars( iter, prefix, 0, edge, 0 );
if ( 0 < offset ) {
if ( nextWord( iter ) ) {
if ( 0 > offset ) {
/* not found; do nothing */
} else if ( 0 == offset ) {
if ( !firstWord( iter ) ) {
offset = -1;
}
} else {
if ( ACCEPT_ITER( iter, iter->nEdges ) || nextWord( iter ) ) {
DictPosition result = figurePosition( iter );
iter->position = result;
} else {

View file

@ -1306,7 +1306,7 @@ walk_dict_test( const LaunchParams* XP_UNUSED_DBG(params),
for ( ii = 0; ii < count; ++ii ) {
gchar* prefix = (gchar*)g_slist_nth_data( testPrefixes, ii );
XP_S16 lenMatched = dict_findStartsWith( &iter, prefix );
if ( 0 < lenMatched ) {
if ( 0 <= lenMatched ) {
XP_UCHAR buf[32];
XP_UCHAR bufPrev[32] = {0};
dict_wordToString( &iter, buf, VSIZE(buf) );
@ -1314,7 +1314,7 @@ walk_dict_test( const LaunchParams* XP_UNUSED_DBG(params),
XP_ASSERT( 0 == strncmp( buf, prefix, lenMatched ) );
DictPosition pos = dict_getPosition( &iter );
XP_ASSERT( 0 == strncmp( buf, words[pos], lenMatched ) );
XP_ASSERT( 0 == strcmp( buf, words[pos] ) );
if ( pos > 0 ) {
if ( !dict_getNthWord( &iter, pos-1, depth, &data ) ) {
XP_ASSERT( 0 );