3 #define MINSIZE 16 /* minimum number of chars allocated */
4 #define MAXSIZE 256 /* maximum number of chars for an empty string */
10 p->s = emalloc(MINSIZE*RUNESIZE);
18 p->s = emalloc(MINSIZE*RUNESIZE);
33 if(p->size > MAXSIZE){
34 p->s = erealloc(p->s, RUNESIZE*MAXSIZE); /* throw away the garbage */
51 Strdupl(String *p, Rune *s) /* copies the null */
55 memmove(p->s, s, p->n*RUNESIZE);
59 Strduplstr(String *p, String *q) /* will copy the null if there's one there */
63 memmove(p->s, q->s, q->n*RUNESIZE);
67 Straddc(String *p, int c)
74 Strinsure(String *p, ulong n)
78 if(p->size < n){ /* p needs to grow */
80 p->s = erealloc(p->s, n*RUNESIZE);
86 Strinsert(String *p, String *q, Posn p0)
88 Strinsure(p, p->n+q->n);
89 memmove(p->s+p0+q->n, p->s+p0, (p->n-p0)*RUNESIZE);
90 memmove(p->s+p0, q->s, q->n*RUNESIZE);
95 Strdelete(String *p, Posn p1, Posn p2)
97 memmove(p->s+p1, p->s+p2, (p->n-p2)*RUNESIZE);
102 Strcmp(String *a, String *b)
106 for(i=0; i<a->n && i<b->n; i++)
107 if(c = (a->s[i] - b->s[i])) /* assign = */
109 /* damn NULs confuse everything */
112 if(a->s[a->n-1] == 0)
115 if(b->s[b->n-1] == 0)
122 Strispre(String *a, String *b)
126 for(i=0; i<a->n && i<b->n; i++){
127 if(a->s[i] - b->s[i]){ /* assign = */
142 c = emalloc(s->n*UTFmax + 1); /* worst case UTFmax bytes per rune, plus NUL */
145 for(i=0; i<s->n; i++)
146 d += runetochar(d, r++);
154 * Build very temporary String from Rune*
157 tmprstr(Rune *r, int n)
168 * Convert null-terminated char* into String
177 n = utflen(s); /* don't include NUL */
178 p = emalloc(sizeof(String));
179 r = emalloc(n*RUNESIZE);
181 for(i=0; i<n; i++,r++)
182 s += chartorune(r, s);
189 freetmpstr(String *s)