Annotation of researchv9/jtools/src/sux/string.c, revision 1.1

1.1     ! root        1: #include <jerq.h>
        !             2: #include "frame.h"
        !             3: 
        !             4: #define        MAXROOM 1024    /* maximum extra number of chars after insure() */
        !             5: 
        !             6: char *
        !             7: GCalloc(n, p)
        !             8:        int n;
        !             9:        char **p;
        !            10: {
        !            11:        gcalloc((unsigned long)n, p);
        !            12: }
        !            13: 
        !            14: insure(p, n)
        !            15:        register String *p;
        !            16:        register short n;
        !            17: {
        !            18:        register i;
        !            19:        char *old;
        !            20:        if (p->size < n) {      /* p needs to grow */
        !            21:                for (i = 1; i < n; i <<= 1)
        !            22:                        ;
        !            23:                if(i > n+MAXROOM)
        !            24:                        i = n+MAXROOM;
        !            25:                if(p->s==0)
        !            26:                        old=0;
        !            27:                else{
        !            28:                        GCalloc(p->n, &old);
        !            29:                        movstring(p->n, p->s, old);
        !            30:                        gcfree(p->s);
        !            31:                }
        !            32:                GCalloc(i, &p->s);
        !            33:                if(old){
        !            34:                        movstring(p->n, old, p->s);
        !            35:                        gcfree(old);
        !            36:                }
        !            37:                p->size = i;
        !            38:        }
        !            39: }
        !            40: #define        MINCHARS 64     /* for efficiency; min size of a string we'll alloc */
        !            41: strzero(p)
        !            42:        String *p;
        !            43: {
        !            44:        if(p->s && p->size>MINCHARS){
        !            45:                p->n=0;
        !            46:                p->size=0;      /* forces strinsure to free the old stuff */
        !            47:        }
        !            48:        insure(p, MINCHARS);
        !            49:        p->n = 0;
        !            50: }
        !            51: insstring(p, i, q)
        !            52:        register String *p, *q;
        !            53:        register short i;
        !            54: {
        !            55:        insure(p, p->n+q->n);
        !            56:        movstring(i-p->n, p->s+p->n, p->s+p->n+q->n);
        !            57:        movstring(q->n, q->s, p->s+i);
        !            58:        p->n += q->n;
        !            59: }
        !            60: delstring(p, i, j)
        !            61:        register String *p;
        !            62:        register short i, j;
        !            63: {
        !            64:        register n = j-i;
        !            65:        movstring(p->n-j, p->s+j, p->s+i);
        !            66:        p->n -= n;
        !            67: }
        !            68: movstring(i, s, d)
        !            69:        register short i;
        !            70:        register char *s, *d;
        !            71: {
        !            72:        if (i > 0)
        !            73:                do
        !            74:                        *d++ = *s++;
        !            75:                while (--i > 0);
        !            76:        else {
        !            77:                if (i++ < 0)            /* transfer -i chars in loop */
        !            78:                        do
        !            79:                                *--d = *--s;
        !            80:                        while (i++ < 0);
        !            81:        }
        !            82: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.