plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

Strinit.c (468B)


      1 #include "std.h"
      2 
      3 void
      4 Strdup(String *p, String s)
      5 {
      6 	Strzero(p);
      7 	Stradds(p, s);
      8 }
      9 
     10 void
     11 Strclose(String *s)
     12 {
     13 	free(s->s);
     14 	s->s = nil;
     15 	s->n = 0;
     16 	s->size = 0;
     17 }
     18 
     19 void
     20 Strinit(String *p)
     21 {
     22 	p->s = malloc(STRSIZ);
     23 	if (!p->s)
     24 		sysfatal("malloc: %r");
     25 	p->s[0] = '\0';
     26 	p->n = 0;
     27 	p->size = STRSIZ;
     28 }
     29 
     30 
     31 String
     32 Str(char *s)
     33 {
     34 	return !s ? Strn(nil, 0) : Strn(s, strlen(s));
     35 }
     36 
     37 String
     38 Strn(char *s, ulong n)
     39 {
     40 	String S;
     41 
     42 	S.s = s;
     43 	S.n = n;
     44 	S.size = 0;
     45 	return S;
     46 }