File: sstateinsert.c

package info (click to toggle)
yodl 4.05.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,724 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 163
file content (34 lines) | stat: -rw-r--r-- 1,469 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "subst.ih"

char const *s_state_insert(register State *sp, char const *key,
                                               char const *subst)
{
    while (*key)
    {
        register char const *cp;
                                        /* if key is in the set, switch to  */
        if ((cp = strchr(sp->d_str, *key)) != 0)         /*  that state  */
            sp = sp->d_next[cp - sp->d_str];
        else
        {                               /* if not, add a new state to the   */
            size_t last = string_length(&sp->d_set);      /*          set */

            string_addchar(&sp->d_set, (char)*key); /* add key's 1st char */
            sp->d_str = string_str(&sp->d_set); /* and set the state's ptr  */

                                                /* add a new state          */
            new_size(&sp->d_next, last + 1, last, sizeof(State *));
            sp = sp->d_next[last] = s_state_new();
        }
        key++;                          /* inspect the next key char        */
    }

                                        /* all key chars exhausted: insert  */
                                        /* replacement text                 */

    if (sp->d_replacement)              /* oops, it's already there         */
        return sp->d_replacement;       /* return the one found             */

    sp->d_replacement = new_str(subst);
    return 0;                           /* here 0 indicates SUCCESS         */
}