Esempio n. 1
0
/**
 * Finalizes searching trie if it was not finalized.
 */
static inline int php_ahocorasick_finalize(ahocorasick_master_t * ahoMaster){
    if (ahoMaster == NULL
        || ahoMaster->init_ok != 1
        || ahoMaster->ac_finalized == 1)
    {
        return 0;
    }

    ahoMaster->ac_finalized = 1;
    //*** 5. Finalize automata (no more patterns will be added).
    ac_trie_finalize (ahoMaster->acap);
    return 1;
}
void AhoCorasickPlus::finalize ()
{
    ac_trie_finalize (m_automata);
}
int main (int argc, char **argv)
{
    unsigned int i;
    AC_TRIE_t *trie;

    /* Get a new trie */
    trie = ac_trie_create ();

    /* Add patterns to the trie */
    for (i = 0; i < PATTERN_COUNT; i++)
    {
        if (ac_trie_add (trie, &patterns[i], 0) != ACERR_SUCCESS)
            printf("Failed to add pattern \"%.*s\"\n",
                   (int)patterns[i].ptext.length, patterns[i].ptext.astring);
    }

    /* Finalize the trie */
    ac_trie_finalize (trie);

    printf("Normal replace mode:\n");

    for (i = 0; i < CHUNK_COUNT; i++)
        /* Replace */
        multifast_replace (trie,
                           &input_chunks[i], MF_REPLACE_MODE_NORMAL, listener, 0);

    /* Flush the buffer */
    multifast_rep_flush (trie, 0);

    /* After the last chunk you must call the rep_flush() function in order to
     * receive the final result in your call-back function. The last rep_flush
     * call must be done with 0 in its second parameter.
     *
     * It is possible to receive intermediate results by calling rep_flush with
     * a non-zero value in the second parameter. The intermediate results may
     * not be as you expect, Because the replacement algorithm keeps the
     * prefixes in a backlog buffer.
     */

    printf("\nLazy replace mode:\n");

    /* There are two replacement modes:
     *      1. Normal
     *      2. Lazy
     *
     * In the normal mode:
     *      - Any pattern occurrence is replaced
     *      - Factor patterns are ignored
     *
     * In the lazy mode:
     *      - The first occurrence is replaced
     *      - If the first occurrence has a common factor with a successor
     *        pattern, then the successor is ignored
     *
     * Example:
     *
     * Patterns and replacements:
     *      abc -> x
     *      cb -> y
     *      b -> z
     *
     * Input text and replacement result:
     *
     *  - Normal mode:
     *      abc => x
     *      abcb => xy
     *
     *  - Lazy mode:
     *      abc => azc
     *      abcb => azy
     *
     */

    for (i = 0; i < CHUNK_COUNT; i++)
        /* Replace */
        multifast_replace (trie,
                           &input_chunks[i], MF_REPLACE_MODE_LAZY, listener, 0);

    /* Flush the buffer */
    multifast_rep_flush (trie, 0);

    printf("\n");

    /* Release the trie */
    ac_trie_release (trie);

    return 0;
}