From 4cb848582e6677e32ade16575f3b75c1efac405a Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 28 Aug 2012 07:35:11 -0700 Subject: [PATCH] fix edge cases in prefix matching --- xwords4/common/dictiter.c | 19 +++++++++++++++---- xwords4/linux/linuxmain.c | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/xwords4/common/dictiter.c b/xwords4/common/dictiter.c index 9315fa0a4..fa0598abc 100644 --- a/xwords4/common/dictiter.c +++ b/xwords4/common/dictiter.c @@ -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 { diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index a831f0bf3..6eafff375 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -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 );