xref: /plan9-contrib/sys/src/cmd/sam/string.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include "sam.h"
2 
3 #define	MINSIZE	16		/* minimum number of chars allocated */
4 #define	MAXSIZE	256		/* maximum number of chars for an empty string */
5 
6 
7 void
8 Strinit(String *p)
9 {
10 	p->s = emalloc(MINSIZE*RUNESIZE);
11 	p->n = 0;
12 	p->size = MINSIZE;
13 }
14 
15 void
16 Strinit0(String *p)
17 {
18 	p->s = emalloc(MINSIZE*RUNESIZE);
19 	p->s[0] = 0;
20 	p->n = 1;
21 	p->size = MINSIZE;
22 }
23 
24 void
25 Strclose(String *p)
26 {
27 	free(p->s);
28 }
29 
30 void
31 Strzero(String *p)
32 {
33 	if(p->size > MAXSIZE){
34 		p->s = erealloc(p->s, RUNESIZE*MAXSIZE); /* throw away the garbage */
35 		p->size = MAXSIZE;
36 	}
37 	p->n = 0;
38 }
39 
40 int
41 Strlen(Rune *r)
42 {
43 	Rune *s;
44 
45 	for(s=r; *s; s++)
46 		;
47 	return s-r;
48 }
49 
50 void
51 Strdupl(String *p, Rune *s)	/* copies the null */
52 {
53 	p->n = Strlen(s)+1;
54 	Strinsure(p, p->n);
55 	memmove(p->s, s, p->n*RUNESIZE);
56 }
57 
58 void
59 Strduplstr(String *p, String *q)	/* will copy the null if there's one there */
60 {
61 	Strinsure(p, q->n);
62 	p->n = q->n;
63 	memmove(p->s, q->s, q->n*RUNESIZE);
64 }
65 
66 void
67 Straddc(String *p, int c)
68 {
69 	Strinsure(p, p->n+1);
70 	p->s[p->n++] = c;
71 }
72 
73 void
74 Strinsure(String *p, ulong n)
75 {
76 	if(n > STRSIZE)
77 		error(Etoolong);
78 	if(p->size < n){	/* p needs to grow */
79 		n += 100;
80 		p->s = erealloc(p->s, n*RUNESIZE);
81 		p->size = n;
82 	}
83 }
84 
85 void
86 Strinsert(String *p, String *q, Posn p0)
87 {
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);
91 	p->n += q->n;
92 }
93 
94 void
95 Strdelete(String *p, Posn p1, Posn p2)
96 {
97 	memmove(p->s+p1, p->s+p2, (p->n-p2)*RUNESIZE);
98 	p->n -= p2-p1;
99 }
100 
101 int
102 Strcmp(String *a, String *b)
103 {
104 	int i, c;
105 
106 	for(i=0; i<a->n && i<b->n; i++)
107 		if(c = (a->s[i] - b->s[i]))	/* assign = */
108 			return c;
109 	/* damn NULs confuse everything */
110 	i = a->n - b->n;
111 	if(i == 1){
112 		if(a->s[a->n-1] == 0)
113 			return 0;
114 	}else if(i == -1){
115 		if(b->s[b->n-1] == 0)
116 			return 0;
117 	}
118 	return i;
119 }
120 
121 char*
122 Strtoc(String *s)
123 {
124 	int i;
125 	char *c, *d;
126 	Rune *r;
127 
128 	c = emalloc(s->n*UTFmax + 1);  /* worst case UTFmax bytes per rune, plus NUL */
129 	d = c;
130 	r = s->s;
131 	for(i=0; i<s->n; i++)
132 		d += runetochar(d, r++);
133 	if(d==c || d[-1]!=0)
134 		*d = 0;
135 	return c;
136 
137 }
138 
139 /*
140  * Build very temporary String from Rune*
141  */
142 String*
143 tmprstr(Rune *r, int n)
144 {
145 	static String p;
146 
147 	p.s = r;
148 	p.n = n;
149 	p.size = n;
150 	return &p;
151 }
152 
153 /*
154  * Convert null-terminated char* into String
155  */
156 String*
157 tmpcstr(char *s)
158 {
159 	String *p;
160 	Rune *r;
161 	int i, n;
162 
163 	n = utflen(s);	/* don't include NUL */
164 	p = emalloc(sizeof(String));
165 	r = emalloc(n*RUNESIZE);
166 	p->s = r;
167 	for(i=0; i<n; i++,r++)
168 		s += chartorune(r, s);
169 	p->n = n;
170 	p->size = n;
171 	return p;
172 }
173 
174 void
175 freetmpstr(String *s)
176 {
177 	free(s->s);
178 	free(s);
179 }
180