void compare_per_run() { int i, len; char utf8buf_expect[16]; char utf8buf_commit[16]; printf( "Expected: %s", expect_string_buf ); printf( "Committed: "); tested_word_count += (len = ueStrLen( expect_string_buf ) - 1); /* omit the suffix character */ for ( i = 0; i < len; i++ ) { ueStrNCpy( utf8buf_expect, ueStrSeek( expect_string_buf, i ), 1, STRNCPY_CLOSE ); ueStrNCpy( utf8buf_commit, ueStrSeek( commit_string_buf, i ), 1, STRNCPY_CLOSE ); if ( ! strcmp( utf8buf_expect, utf8buf_commit ) ) printf( "%s", utf8buf_commit ); else { printf( "\033[44;37m%s\033[m", utf8buf_commit ); failed_word_count++; } } memset( commit_string_buf, 0, MAXLEN ); printf( "\n\n" ); }
/* kpchen said, record is the index array of interval */ static void OutputRecordStr( ChewingData *pgdata, char *out_buf, int out_buf_len, int *record, int nRecord, uint16_t phoneSeq[], int nPhoneSeq, char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ], IntervalType selectInterval[], int nSelect, TreeDataType *ptd ) { PhraseIntervalType inter; int i; LoadChar( pgdata, out_buf, out_buf_len, phoneSeq, nPhoneSeq ); for ( i = 0; i < nRecord; i++ ) { inter = ptd->interval[ record[ i ] ]; ueStrNCpy( ueStrSeek( out_buf, inter.from ), ( inter.p_phr )->phrase, ( inter.to - inter.from ), -1); } for ( i = 0; i < nSelect; i++ ) { inter.from = selectInterval[ i ].from; inter.to = selectInterval[ i ].to ; ueStrNCpy( ueStrSeek( out_buf, inter.from ), selectStr[ i ], ( inter.to - inter.from ), -1); } }
int main () { char *u8string; int u8len; int len; char u8string2[16]; u8string = "HelloWorld"; u8len = ueStrLen(u8string); len = strlen(u8string); ok (u8len == len, "ueStrLen"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8len = ueStrLen(u8string); ok (u8len == 6, "ueStrLen"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ ueStrNCpy(u8string2, u8string, 3, STRNCPY_CLOSE); ok (!strcmp(u8string2, "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88" /* 測試計 */ ), "ueStrNCpy"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8string = ueStrSeek(u8string, 3); ok (!strcmp(u8string, "\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6" /* 算長度 */ ), "ueStrSeek"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8string = ueStrSeek(u8string, 0); ok (!strcmp(u8string, "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6" /* 測試計算長度 */ ), "ueStrSeek"); return exit_status(); }
void test_utf8() { char *u8string; int u8len; int len; char u8string2[16]; start_testcase(NULL, fd); u8string = "HelloWorld"; u8len = ueStrLen(u8string); len = strlen(u8string); ok(u8len == len, "ueStrLen"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8len = ueStrLen(u8string); ok(u8len == 6, "ueStrLen"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ ueStrNCpy(u8string2, u8string, 3, STRNCPY_CLOSE); ok(!strcmp(u8string2, "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88" /* 測試計 */ ), "ueStrNCpy"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8string = ueStrSeek(u8string, 3); ok(!strcmp(u8string, "\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6" /* 算長度 */ ), "ueStrSeek"); u8string = "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6"; /* 測試計算長度 */ u8string = ueStrSeek(u8string, 0); ok(!strcmp (u8string, "\xE6\xB8\xAC\xE8\xA9\xA6\xE8\xA8\x88\xE7\xAE\x97\xE9\x95\xB7\xE5\xBA\xA6" /* 測試計算長度 */ ), "ueStrSeek"); }
int main (int argc, char *argv[]) { char *u8string; int u8len; u8string = "HelloWorld"; u8len = ueStrLen(u8string); int len = strlen(u8string); ok (u8len == len, "ueStrLen"); u8string = "測試計算長度"; u8len = ueStrLen(u8string); ok (u8len == 6, "ueStrLen"); u8string = "測試計算長度"; char u8string2[16]; ueStrNCpy(u8string2, u8string, 3, STRNCPY_CLOSE); ok (!strcmp(u8string2, "測試計"), "ueStrNCpy"); u8string = "測試計算長度"; u8string = ueStrSeek(u8string, 3); ok (!strcmp(u8string, "算長度"), "ueStrSeek"); u8string = "測試計算長度"; u8string = ueStrSeek(u8string, 0); ok (!strcmp(u8string, "測試計算長度"), "ueStrSeek"); return exit_status(); }
/* * phrase is said to satisfy a choose interval if * their intersections are the same */ static int CheckChoose( ChewingData *pgdata, int ph_id, int from, int to, Phrase **pp_phr, char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ], IntervalType selectInterval[], int nSelect ) { IntervalType inte, c; int chno, len; Phrase *phrase = ALC( Phrase, 1 ); assert( phrase ); inte.from = from; inte.to = to; *pp_phr = NULL; /* if there exist one phrase satisfied all selectStr then return 1, else return 0. */ GetPhraseFirst( pgdata, phrase, ph_id ); do { for ( chno = 0; chno < nSelect; chno++ ) { c = selectInterval[ chno ]; if ( IsContain( inte, c ) ) { /* find a phrase of ph_id where the text contains * 'selectStr[chno]' test if not ok then return 0, if ok * then continue to test */ len = c.to - c.from; if ( memcmp( ueStrSeek( phrase->phrase, c.from - from ), selectStr[ chno ], ueStrNBytes( selectStr[ chno ], len ) ) ) break; } else if ( IsIntersect( inte, selectInterval[ chno ] ) ) { free( phrase ); return 0; } } if ( chno == nSelect ) { *pp_phr = phrase; return 1; } } while ( GetPhraseNext( pgdata, phrase ) ); free( phrase ); return 0; }
static int CheckUserChoose( ChewingData *pgdata, uint16_t *new_phoneSeq, int from , int to, Phrase **pp_phr, char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ], IntervalType selectInterval[], int nSelect ) { IntervalType inte, c; int chno, len; int user_alloc; UserPhraseData *pUserPhraseData; Phrase *p_phr = ALC( Phrase, 1 ); assert( p_phr ); inte.from = from; inte.to = to; *pp_phr = NULL; /* pass 1 * if these exist one selected interval which is not contained by inte * but has intersection with inte, then inte is an unacceptable interval */ for ( chno = 0; chno < nSelect; chno++ ) { c = selectInterval[ chno ]; if ( IsIntersect( inte, c ) && ! IsContain( inte, c ) ) { free( p_phr ); return 0; } } /* pass 2 * if there exist one phrase satisfied all selectStr then return 1, else return 0. * also store the phrase with highest freq */ pUserPhraseData = UserGetPhraseFirst( pgdata, new_phoneSeq ); p_phr->freq = -1; do { for ( chno = 0; chno < nSelect; chno++ ) { c = selectInterval[ chno ]; if ( IsContain( inte, c ) ) { /* * find a phrase of ph_id where the text contains * 'selectStr[chno]' test if not ok then return 0, * if ok then continue to test. */ len = c.to - c.from; if ( memcmp( ueStrSeek( pUserPhraseData->wordSeq, c.from - from ), selectStr[ chno ], ueStrNBytes( selectStr[ chno ], len ) ) ) break; } } if ( chno == nSelect ) { /* save phrase data to "pp_phr" */ if ( pUserPhraseData->userfreq > p_phr->freq ) { if ( ( user_alloc = ( to - from ) ) > 0 ) { ueStrNCpy( p_phr->phrase, pUserPhraseData->wordSeq, user_alloc, 1); } p_phr->freq = pUserPhraseData->userfreq; *pp_phr = p_phr; } } } while ( ( pUserPhraseData = UserGetPhraseNext( pgdata, new_phoneSeq ) ) != NULL ); if ( p_phr->freq != -1 ) return 1; free( p_phr ); return 0; }
CHEWING_API int chewing_handle_CtrlNum( ChewingContext *ctx, int key ) { ChewingData *pgdata = ctx->data; ChewingOutput *pgo = ctx->output; int keystrokeRtn = KEYSTROKE_ABSORB; int newPhraseLen; int i; uint16 addPhoneSeq[ MAX_PHONE_SEQ_LEN ]; char addWordSeq[ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ]; int phraseState; int cursor; CheckAndResetRange( pgdata ); if ( pgdata->bSelect ) return 0; CallPhrasing( pgdata ); newPhraseLen = key - '0'; if ( key == '0' || key == '1' ) { pgdata->bSelect = 1; pgdata->choiceInfo.oldChiSymbolCursor = pgdata->chiSymbolCursor; HaninSymbolInput( &( pgdata->choiceInfo ), &( pgdata->availInfo ), pgdata->phoneSeq, pgdata->config.candPerPage ); CallPhrasing( pgdata ); MakeOutputWithRtn( pgo, pgdata, keystrokeRtn ); return 0; } cursor = PhoneSeqCursor( pgdata ); if ( ! pgdata->config.bAddPhraseForward ) { if ( newPhraseLen >= 1 && cursor + newPhraseLen - 1 <= pgdata->nPhoneSeq ) { if ( NoSymbolBetween( pgdata, cursor, cursor + newPhraseLen - 1 ) ) { /* Manually add phrase to the user phrase database. */ memcpy( addPhoneSeq, &pgdata->phoneSeq[ cursor ], sizeof( uint16 ) * newPhraseLen ); addPhoneSeq[ newPhraseLen ] = 0; ueStrNCpy( addWordSeq, ueStrSeek( (char *) &pgdata->phrOut.chiBuf, cursor ), newPhraseLen, 1); phraseState = UserUpdatePhrase( addPhoneSeq, addWordSeq ); SetUpdatePhraseMsg( pgdata, addWordSeq, newPhraseLen, phraseState ); /* Clear the breakpoint between the New Phrase */ for ( i = 1; i < newPhraseLen; i++ ) pgdata->bUserArrBrkpt[ cursor + i ] = 0; } } } else { if ( newPhraseLen >= 1 && cursor - newPhraseLen >= 0 ) { if ( NoSymbolBetween( pgdata, cursor, cursor - newPhraseLen ) ) { /* Manually add phrase to the user phrase database. */ memcpy( addPhoneSeq, &pgdata->phoneSeq[ cursor - newPhraseLen ], sizeof( uint16 ) * newPhraseLen ); addPhoneSeq[ newPhraseLen ] = 0; ueStrNCpy( addWordSeq, ueStrSeek( (char *) &pgdata->phrOut.chiBuf, cursor - newPhraseLen ), newPhraseLen, 1); phraseState = UserUpdatePhrase( addPhoneSeq, addWordSeq ); SetUpdatePhraseMsg( pgdata, addWordSeq, newPhraseLen, phraseState ); /* Clear the breakpoint between the New Phrase */ for ( i = 1; i < newPhraseLen; i++ ) pgdata->bUserArrBrkpt[ cursor - newPhraseLen + i ] = 0; } } } CallPhrasing( pgdata ); MakeOutputWithRtn( pgo, pgdata, keystrokeRtn ); MakeOutputAddMsgAndCleanInterval( pgo, pgdata ); return 0; }