This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further ANSI changes now builds and passes (most) tests
[perl5.git] / x2p / str.c
CommitLineData
79072805 1/* $RCSfile: str.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:26 $
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
d48672a2
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8
LW
7 *
8 * $Log: str.c,v $
8d063cd8
LW
9 */
10
8d063cd8 11#include "EXTERN.h"
8d063cd8 12#include "a2p.h"
9c8d0b29 13#include "util.h"
8d063cd8 14
9c8d0b29 15void
f0f333f4 16str_numset(register STR *str, double num)
8d063cd8
LW
17{
18 str->str_nval = num;
19 str->str_pok = 0; /* invalidate pointer */
20 str->str_nok = 1; /* validate number */
21}
22
23char *
f0f333f4 24str_2ptr(register STR *str)
8d063cd8
LW
25{
26 register char *s;
27
28 if (!str)
29 return "";
30 GROWSTR(&(str->str_ptr), &(str->str_len), 24);
31 s = str->str_ptr;
32 if (str->str_nok) {
33 sprintf(s,"%.20g",str->str_nval);
34 while (*s) s++;
35 }
36 *s = '\0';
37 str->str_cur = s - str->str_ptr;
38 str->str_pok = 1;
39#ifdef DEBUGGING
40 if (debug & 32)
68dc0745 41 fprintf(stderr,"0x%lx ptr(%s)\n",(unsigned long)str,str->str_ptr);
8d063cd8
LW
42#endif
43 return str->str_ptr;
44}
45
46double
f0f333f4 47str_2num(register STR *str)
8d063cd8
LW
48{
49 if (!str)
50 return 0.0;
51 if (str->str_len && str->str_pok)
52 str->str_nval = atof(str->str_ptr);
53 else
54 str->str_nval = 0.0;
55 str->str_nok = 1;
56#ifdef DEBUGGING
57 if (debug & 32)
68dc0745 58 fprintf(stderr,"0x%lx num(%g)\n",(unsigned long)str,str->str_nval);
8d063cd8
LW
59#endif
60 return str->str_nval;
61}
62
9c8d0b29 63void
f0f333f4 64str_sset(STR *dstr, register STR *sstr)
8d063cd8
LW
65{
66 if (!sstr)
67 str_nset(dstr,No,0);
68 else if (sstr->str_nok)
69 str_numset(dstr,sstr->str_nval);
70 else if (sstr->str_pok)
71 str_nset(dstr,sstr->str_ptr,sstr->str_cur);
72 else
73 str_nset(dstr,"",0);
74}
75
9c8d0b29 76void
f0f333f4 77str_nset(register STR *str, register char *ptr, register int len)
8d063cd8
LW
78{
79 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
80 bcopy(ptr,str->str_ptr,len);
81 str->str_cur = len;
82 *(str->str_ptr+str->str_cur) = '\0';
83 str->str_nok = 0; /* invalidate number */
84 str->str_pok = 1; /* validate pointer */
85}
86
9c8d0b29 87void
f0f333f4 88str_set(register STR *str, register char *ptr)
8d063cd8
LW
89{
90 register int len;
91
92 if (!ptr)
93 ptr = "";
94 len = strlen(ptr);
95 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
96 bcopy(ptr,str->str_ptr,len+1);
97 str->str_cur = len;
98 str->str_nok = 0; /* invalidate number */
99 str->str_pok = 1; /* validate pointer */
100}
101
9c8d0b29 102void
f0f333f4
NIS
103str_chop(register STR *str, register char *ptr) /* like set but assuming ptr is in str */
104
105
8d063cd8
LW
106{
107 if (!(str->str_pok))
108 str_2ptr(str);
109 str->str_cur -= (ptr - str->str_ptr);
110 bcopy(ptr,str->str_ptr, str->str_cur + 1);
111 str->str_nok = 0; /* invalidate number */
112 str->str_pok = 1; /* validate pointer */
113}
114
9c8d0b29 115void
f0f333f4 116str_ncat(register STR *str, register char *ptr, register int len)
8d063cd8
LW
117{
118 if (!(str->str_pok))
119 str_2ptr(str);
120 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
121 bcopy(ptr,str->str_ptr+str->str_cur,len);
122 str->str_cur += len;
123 *(str->str_ptr+str->str_cur) = '\0';
124 str->str_nok = 0; /* invalidate number */
125 str->str_pok = 1; /* validate pointer */
126}
127
9c8d0b29 128void
f0f333f4 129str_scat(STR *dstr, register STR *sstr)
8d063cd8
LW
130{
131 if (!(sstr->str_pok))
132 str_2ptr(sstr);
133 if (sstr)
134 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
135}
136
9c8d0b29 137void
f0f333f4 138str_cat(register STR *str, register char *ptr)
8d063cd8
LW
139{
140 register int len;
141
142 if (!ptr)
143 return;
144 if (!(str->str_pok))
145 str_2ptr(str);
146 len = strlen(ptr);
147 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
148 bcopy(ptr,str->str_ptr+str->str_cur,len+1);
149 str->str_cur += len;
150 str->str_nok = 0; /* invalidate number */
151 str->str_pok = 1; /* validate pointer */
152}
153
154char *
f0f333f4 155str_append_till(register STR *str, register char *from, register int delim, char *keeplist)
8d063cd8
LW
156{
157 register char *to;
158 register int len;
159
160 if (!from)
161 return Nullch;
162 len = strlen(from);
163 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
164 str->str_nok = 0; /* invalidate number */
165 str->str_pok = 1; /* validate pointer */
166 to = str->str_ptr+str->str_cur;
167 for (; *from; from++,to++) {
168 if (*from == '\\' && from[1] && delim != '\\') {
169 if (!keeplist) {
170 if (from[1] == delim || from[1] == '\\')
171 from++;
172 else
173 *to++ = *from++;
174 }
a0d0e21e 175 else if (strchr(keeplist,from[1]))
8d063cd8
LW
176 *to++ = *from++;
177 else
178 from++;
179 }
180 else if (*from == delim)
181 break;
182 *to = *from;
183 }
184 *to = '\0';
185 str->str_cur = to - str->str_ptr;
186 return from;
187}
188
189STR *
f0f333f4 190str_new(int len)
8d063cd8
LW
191{
192 register STR *str;
193
194 if (freestrroot) {
195 str = freestrroot;
196 freestrroot = str->str_link.str_next;
197 }
198 else {
199 str = (STR *) safemalloc(sizeof(STR));
200 bzero((char*)str,sizeof(STR));
201 }
202 if (len)
203 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
204 return str;
205}
206
207void
f0f333f4 208str_grow(register STR *str, int len)
8d063cd8
LW
209{
210 if (len && str)
211 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
212}
213
214/* make str point to what nstr did */
215
216void
f0f333f4 217str_replace(register STR *str, register STR *nstr)
8d063cd8 218{
9c8d0b29 219 Safefree(str->str_ptr);
8d063cd8
LW
220 str->str_ptr = nstr->str_ptr;
221 str->str_len = nstr->str_len;
222 str->str_cur = nstr->str_cur;
223 str->str_pok = nstr->str_pok;
224 if (str->str_nok = nstr->str_nok)
225 str->str_nval = nstr->str_nval;
226 safefree((char*)nstr);
227}
228
229void
f0f333f4 230str_free(register STR *str)
8d063cd8
LW
231{
232 if (!str)
233 return;
234 if (str->str_len)
235 str->str_ptr[0] = '\0';
236 str->str_cur = 0;
237 str->str_nok = 0;
238 str->str_pok = 0;
239 str->str_link.str_next = freestrroot;
240 freestrroot = str;
241}
242
9c8d0b29 243int
f0f333f4 244str_len(register STR *str)
8d063cd8
LW
245{
246 if (!str)
247 return 0;
248 if (!(str->str_pok))
249 str_2ptr(str);
250 if (str->str_len)
251 return str->str_cur;
252 else
253 return 0;
254}
255
256char *
f0f333f4 257str_gets(register STR *str, register FILE *fp)
8d063cd8 258{
c2960299
AD
259#if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
260 /* Here is some breathtakingly efficient cheating */
8d063cd8
LW
261
262 register char *bp; /* we're going to steal some values */
263 register int cnt; /* from the stdio struct and put EVERYTHING */
378cc40b 264 register STDCHAR *ptr; /* in the innermost loop into registers */
8d063cd8
LW
265 register char newline = '\n'; /* (assuming at least 6 registers) */
266 int i;
267 int bpx;
268
44a8e56a
PP
269#if defined(VMS)
270 /* An ungetc()d char is handled separately from the regular
271 * buffer, so we getc() it back out and stuff it in the buffer.
272 */
273 i = getc(fp);
274 if (i == EOF) return Nullch;
275 *(--((*fp)->_ptr)) = (unsigned char) i;
276 (*fp)->_cnt++;
277#endif
278
16d20bd9 279 cnt = FILE_cnt(fp); /* get count into register */
8d063cd8
LW
280 str->str_nok = 0; /* invalidate number */
281 str->str_pok = 1; /* validate pointer */
282 if (str->str_len <= cnt) /* make sure we have the room */
283 GROWSTR(&(str->str_ptr), &(str->str_len), cnt+1);
284 bp = str->str_ptr; /* move these two too to registers */
16d20bd9 285 ptr = FILE_ptr(fp);
8d063cd8 286 for (;;) {
378cc40b
LW
287 while (--cnt >= 0) {
288 if ((*bp++ = *ptr++) == newline)
289 if (bp <= str->str_ptr || bp[-2] != '\\')
290 goto thats_all_folks;
291 else {
292 line++;
293 bp -= 2;
294 }
8d063cd8
LW
295 }
296
16d20bd9
AD
297 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
298 FILE_ptr(fp) = ptr;
44a8e56a 299 i = getc(fp); /* get more characters */
16d20bd9
AD
300 cnt = FILE_cnt(fp);
301 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
8d063cd8
LW
302
303 bpx = bp - str->str_ptr; /* prepare for possible relocation */
304 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1);
305 bp = str->str_ptr + bpx; /* reconstitute our pointer */
306
307 if (i == newline) { /* all done for now? */
308 *bp++ = i;
309 goto thats_all_folks;
310 }
311 else if (i == EOF) /* all done for ever? */
312 goto thats_all_folks;
313 *bp++ = i; /* now go back to screaming loop */
314 }
315
316thats_all_folks:
16d20bd9
AD
317 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
318 FILE_ptr(fp) = ptr;
8d063cd8
LW
319 *bp = '\0';
320 str->str_cur = bp - str->str_ptr; /* set length */
321
c2960299
AD
322#else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
323 /* The big, slow, and stupid way */
8d063cd8
LW
324
325 static char buf[4192];
326
327 if (fgets(buf, sizeof buf, fp) != Nullch)
328 str_set(str, buf);
329 else
330 str_set(str, No);
331
c2960299 332#endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
8d063cd8
LW
333
334 return str->str_cur ? str->str_ptr : Nullch;
335}
336
337void
f0f333f4 338str_inc(register STR *str)
8d063cd8
LW
339{
340 register char *d;
341
342 if (!str)
343 return;
344 if (str->str_nok) {
345 str->str_nval += 1.0;
346 str->str_pok = 0;
347 return;
348 }
349 if (!str->str_pok) {
350 str->str_nval = 1.0;
351 str->str_nok = 1;
352 return;
353 }
354 for (d = str->str_ptr; *d && *d != '.'; d++) ;
355 d--;
356 if (!isdigit(*str->str_ptr) || !isdigit(*d) ) {
357 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
358 return;
359 }
360 while (d >= str->str_ptr) {
361 if (++*d <= '9')
362 return;
363 *(d--) = '0';
364 }
365 /* oh,oh, the number grew */
366 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2);
367 str->str_cur++;
368 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
369 *d = d[-1];
370 *d = '1';
371}
372
373void
f0f333f4 374str_dec(register STR *str)
8d063cd8
LW
375{
376 register char *d;
377
378 if (!str)
379 return;
380 if (str->str_nok) {
381 str->str_nval -= 1.0;
382 str->str_pok = 0;
383 return;
384 }
385 if (!str->str_pok) {
386 str->str_nval = -1.0;
387 str->str_nok = 1;
388 return;
389 }
390 for (d = str->str_ptr; *d && *d != '.'; d++) ;
391 d--;
392 if (!isdigit(*str->str_ptr) || !isdigit(*d) || (*d == '0' && d == str->str_ptr)) {
393 str_numset(str,atof(str->str_ptr) - 1.0); /* punt */
394 return;
395 }
396 while (d >= str->str_ptr) {
397 if (--*d >= '0')
398 return;
399 *(d--) = '9';
400 }
401}
402
403/* make a string that will exist for the duration of the expression eval */
404
405STR *
f0f333f4 406str_mortal(STR *oldstr)
8d063cd8
LW
407{
408 register STR *str = str_new(0);
409 static long tmps_size = -1;
410
411 str_sset(str,oldstr);
412 if (++tmps_max > tmps_size) {
413 tmps_size = tmps_max;
414 if (!(tmps_size & 127)) {
415 if (tmps_size)
416 tmps_list = (STR**)saferealloc((char*)tmps_list,
417 (tmps_size + 128) * sizeof(STR*) );
418 else
419 tmps_list = (STR**)safemalloc(128 * sizeof(char*));
420 }
421 }
422 tmps_list[tmps_max] = str;
423 return str;
424}
425
426STR *
f0f333f4 427str_make(char *s)
8d063cd8
LW
428{
429 register STR *str = str_new(0);
430
431 str_set(str,s);
432 return str;
433}
434
435STR *
f0f333f4 436str_nmake(double n)
8d063cd8
LW
437{
438 register STR *str = str_new(0);
439
440 str_numset(str,n);
441 return str;
442}