fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
stralloc.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include "byte.h"
3#include "str.h"
4#include "stralloc.h"
5#include "alloc.h"
6
14int stralloc_starts(stralloc *sa,const char *s)
15{
16 int len;
17 len = str_len(s);
18 return (sa->len >= len) && byte_equal(s,len,sa->s);
19}
20
21int stralloc_cat(stralloc *sato,stralloc *safrom)
22{
23 return stralloc_catb(sato,safrom->s,safrom->len);
24}
25
26int stralloc_catb(stralloc *sa,const char *s,unsigned int n)
27{
28 if (!sa->s) return stralloc_copyb(sa,s,n);
29 if (!stralloc_readyplus(sa,n + 1)) return 0;
30 byte_copy(sa->s + sa->len,n,s);
31 sa->len += n;
32 sa->s[sa->len] = 'Z'; /* ``offensive programming'' */
33 return 1;
34}
35
36int stralloc_cats(stralloc *sa,const char *s)
37{
38 return stralloc_catb(sa,s,str_len(s));
39}
40
41int stralloc_copy(stralloc *sato,stralloc *safrom)
42{
43 return stralloc_copyb(sato,safrom->s,safrom->len);
44}
45
46
47int stralloc_ready(stralloc *sa,size_t len)
48{
49 size_t wanted = len+(len>>3)+30; /* heuristic from djb */
50 if (wanted<len) wanted = len;
51 if (!sa->s || sa->a<len) {
52 char* tmp;
53 if (!(tmp = realloc(sa->s,wanted))) // !!! needs stdlib (realloc)
54 return 0;
55 sa->a = wanted;
56 sa->s = tmp;
57 }
58 return 1;
59}
60
61int stralloc_readyplus(stralloc *sa,size_t len)
62{
63 if (sa->s) {
64 if (sa->len + len < len) return 0; /* catch integer overflow */
65 return stralloc_ready(sa,sa->len+len);
66 } else
67 return stralloc_ready(sa,len);
68}
69
70int stralloc_copyb(stralloc *sa,const char *s,unsigned int n)
71{
72 if (!stralloc_ready(sa,n + 1)) return 0;
73 byte_copy(sa->s,n,s);
74 sa->len = n;
75 sa->s[n] = 'Z'; /* ``offensive programming'' */
76 return 1;
77}
78
79int stralloc_copys(stralloc *sa,const char *s)
80{
81 return stralloc_copyb(sa,s,str_len(s));
82}
83
84int stralloc_catulong0(stralloc *sa,unsigned long u,unsigned int n)
85{
86 unsigned int len;
87 unsigned long q;
88 char *s;
89
90 len = 1;
91 q = u;
92 while (q > 9) { ++len; q /= 10; }
93 if (len < n) len = n;
94
95 if (!stralloc_readyplus(sa,len)) return 0;
96 s = sa->s + sa->len;
97 sa->len += len;
98 while (len) { s[--len] = '0' + (u % 10); u /= 10; }
99
100 return 1;
101}
102
103int stralloc_catlong0(stralloc *sa,long l,unsigned int n)
104{
105 if (l < 0) {
106 if (!stralloc_append(sa,"-")) return 0;
107 l = -l;
108 }
109 return stralloc_catulong0(sa,l,n);
110}
111
112int stralloc_append(stralloc *sa,const char *in)
113{
114 if (stralloc_readyplus(sa,1)) {
115 sa->s[sa->len] = *in;
116 ++sa->len;
117 return 1;
118 }
119 return 0;
120}
121
123 if (sa->s) free(sa->s);
124 sa->s = 0;
125 sa->a = sa->len = 0;
126}
int stralloc_copyb(stralloc *sa, const char *s, unsigned int n)
Definition: stralloc.c:70
int stralloc_catb(stralloc *sa, const char *s, unsigned int n)
Definition: stralloc.c:26
int stralloc_cat(stralloc *sato, stralloc *safrom)
Definition: stralloc.c:21
int stralloc_copys(stralloc *sa, const char *s)
Definition: stralloc.c:79
int stralloc_cats(stralloc *sa, const char *s)
Definition: stralloc.c:36
int stralloc_append(stralloc *sa, const char *in)
Definition: stralloc.c:112
int stralloc_catlong0(stralloc *sa, long l, unsigned int n)
Definition: stralloc.c:103
int stralloc_ready(stralloc *sa, size_t len)
Definition: stralloc.c:47
int stralloc_copy(stralloc *sato, stralloc *safrom)
Definition: stralloc.c:41
int stralloc_catulong0(stralloc *sa, unsigned long u, unsigned int n)
Definition: stralloc.c:84
int stralloc_readyplus(stralloc *sa, size_t len)
Definition: stralloc.c:61
int stralloc_starts(stralloc *sa, const char *s)
Definition: stralloc.c:14
void stralloc_free(stralloc *sa)
Definition: stralloc.c:122
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
#define byte_equal(s, n, t)
Definition: byte.h:18
unsigned int str_len(const char *)
Definition: str.c:68
size_t len
Definition: stralloc.h:19
size_t a
Definition: stralloc.h:20
char * s
Definition: stralloc.h:18