Changeset 1922

Show
Ignore:
Timestamp:
03/15/07 23:24:22 (1 year ago)
Author:
karpet
Message:

don't bother with the ucdata libraries after all. we'll make due with native isw*() functions and some naive tokenizing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/words.c

    r1921 r1922  
    3939static int      is_ignore_end(wint_t c); 
    4040static int      is_ignore_word(wint_t c); 
     41static int      bytes_in_char(wint_t c); 
    4142static void     set_debug(); 
    4243 
     
    146147        return 0; 
    147148         
    148     if( !c || 
     149    return ( !c || 
    149150        isspace(c) || 
    150151        iscntrl(c) || 
    151152        ispunct(c) 
    152153        ) 
    153         return 1; 
    154          
    155     return 0; 
     154    ? 1 : 0; 
    156155} 
    157156 
     
    204203 
    205204static int 
    206 bytes_in_char(wchar_t c) 
    207 
    208     int len; 
    209     char *mb; 
     205bytes_in_char(wint_t ch) 
     206
     207    int len = 0; 
    210208     
    211     mb = swish_xmalloc(sizeof(xmlChar)*sizeof(wchar_t)); 
    212     wctomb(mb, c); 
    213     len = mblen(mb, sizeof(wchar_t)); 
    214     swish_xfree(mb); 
     209    if (ch < 0x80) { 
     210        len = 1; 
     211    } 
     212    if (ch < 0x800) { 
     213        len = 2; 
     214    } 
     215    if (ch < 0x10000) { 
     216        len = 3; 
     217    } 
     218    if (ch < 0x110000) { 
     219        len = 4; 
     220    } 
    215221     
    216222    if( WORD_DEBUG > 5 ) 
    217         swish_debug_msg(" %lc is %d bytes long", c, len); 
     223        swish_debug_msg(" %lc is %d bytes long", ch, len); 
    218224         
    219225    return len; 
    220226 
    221227} 
     228 
    222229 
    223230static swish_WordList * 
     
    236243    xmlChar * utf8_str; 
    237244 
    238     /* convert xmlChar str into a widechar string for comparing against tables */ 
     245    /* convert xmlChar str into a widechar string for comparing against isw*() functions. 
     246     * the returned pointer must be freed eventually. 
     247     */ 
    239248    wchar_t        *wide = swish_locale_to_wchar(str); 
    240249 
     
    259268        c = (int) towlower(wide[i]); 
    260269        nextc = (int) towlower(wide[i + 1]); 
    261         byte_count += bytes_in_char(c); 
     270        byte_count += bytes_in_char((wint_t)c); 
    262271 
    263272        if (WORD_DEBUG > 10) 
     
    345354 
    346355                    /* turn off flag */ 
    347                     in_word = 0;                     
     356                    in_word     = 0;                     
    348357                     
    349                     word[w] = '\0'; 
    350                     wl = strip_wide_chars(word, w); 
    351                     utf8_str = swish_wchar_to_locale((wchar_t *) word); 
     358                    word[w]     = '\0'; 
     359                    wl          = strip_wide_chars(word, w); 
     360                    utf8_str    = swish_wchar_to_locale((wchar_t *) word); 
    352361 
    353362                    if (wl >= minwordlen) 
     
    396405} 
    397406 
     407/************************************************ 
     408*   mimic the Swish-e WordCharacters lookup tables 
     409*   using the default is*() functions. 
     410*************************************************/ 
     411 
     412static int ascii_tables_created = 0; 
     413static char ascii_word_table[128]; 
     414static char ascii_start_table[128]; 
     415static char ascii_end_table[128]; 
     416 
     417static void 
     418make_ascii_tables() 
     419{ 
     420    int i; 
     421        for (i = 0; i < 127; i++) 
     422    { 
     423        if (is_ignore_word_ascii(i)) 
     424                ascii_word_table[i] = 0; 
     425        else 
     426            ascii_word_table[i] = 1; 
     427             
     428        if (is_ignore_end_ascii(i)) 
     429            ascii_end_table[i] = 0; 
     430        else 
     431            ascii_end_table[i] = 1; 
     432             
     433        if (is_ignore_start_ascii(i)) 
     434            ascii_start_table[i] = 0; 
     435        else 
     436            ascii_start_table[i] = 1; 
     437             
     438    } 
     439    ascii_tables_created = 1; 
     440} 
     441 
     442 
     443 
    398444/************************************************************ 
    399 *   no attempt is made here to support the old Swish-e 
    400 *   WordCharacters et al method of a 256-char lookup table.  
    401 *   Instead we just use the native 
    402 *   is*() functions -- the ascii versions, not the isw*() wide 
    403 *   versions. This should save some overhead for the common case 
    404 *   of all ascii text in a string. 
     445*   This should save some overhead compared to the utf8 version 
     446*   for the common case of all ascii text in a string. 
    405447*   Just like all the Swish3 tokenizing code, this is just a sane 
    406448*   fallback function. We expect and encourage users to write their 
     
    408450* 
    409451**************************************************************/ 
     452 
    410453 
    411454static swish_WordList * 
     
    433476        swish_debug_msg("parsing string: '%s' into words", str); 
    434477 
    435     for (i = 0; str[i] != '\0'; i++) 
    436     { 
    437         c = (int) tolower(str[i]); 
    438         nextc = (int) tolower(str[i + 1]); 
     478     
     479    /* build tables if this is first time through */ 
     480    if (!ascii_tables_created) 
     481        make_ascii_tables(); 
     482 
     483 
     484    for (i = 0; str[i] != NULL; i++) 
     485    { 
     486        c       = (int) tolower(str[i]); 
     487        nextc   = (int) tolower(str[i + 1]); 
    439488        byte_count++; 
    440489 
     
    455504             
    456505             
    457         if (is_ignore_word_ascii(c)
     506        if (!ascii_word_table[(int)c]
    458507        { 
    459508 
     
    468517                /* add NULL */ 
    469518                word[w] = NULL; 
    470                 wl = strip_ascii_chars(word, w); 
     519                wl      = strip_ascii_chars(word, w); 
    471520 
    472521                if (wl >= minwordlen) 
     
    697746    { 
    698747 
    699         if (is_ignore_end_ascii(word[i])
    700         { 
    701             word[i] = '\0'
     748        if (!ascii_end_table[word[i]]
     749        { 
     750            word[i] = NULL
    702751            end++; 
    703752        } 
     
    714763    { 
    715764        k = i; 
    716         if (!is_ignore_start_ascii(word[k])
     765        if (ascii_start_table[word[k]]
    717766        { 
    718767            break;