… | |
… | |
3 | * Rights to this code are documented in doc/pod/license.pod. |
3 | * Rights to this code are documented in doc/pod/license.pod. |
4 | * |
4 | * |
5 | * String functions. |
5 | * String functions. |
6 | */ |
6 | */ |
7 | |
7 | |
8 | static char const rcsid[] = "$Id: string.C,v 1.3 2007/07/21 13:23:22 pippijn Exp $"; |
8 | static char const rcsid[] = "$Id: string.C,v 1.4 2007/08/28 17:08:12 pippijn Exp $"; |
9 | |
|
|
10 | #include <algorithm> |
|
|
11 | |
9 | |
12 | #include "atheme.h" |
10 | #include "atheme.h" |
|
|
11 | |
|
|
12 | char * |
|
|
13 | sstrdup (char const * const s) |
|
|
14 | { |
|
|
15 | char *t; |
|
|
16 | size_t len; |
|
|
17 | |
|
|
18 | if (s == NULL) |
|
|
19 | return NULL; |
|
|
20 | |
|
|
21 | t = alloc<char> (len = strlen (s) + 1); |
|
|
22 | |
|
|
23 | strlcpy (t, s, len); |
|
|
24 | return t; |
|
|
25 | } |
|
|
26 | |
|
|
27 | char * |
|
|
28 | sstrndup (char const *s, int len) |
|
|
29 | { |
|
|
30 | char *t; |
|
|
31 | |
|
|
32 | if (s == NULL) |
|
|
33 | return NULL; |
|
|
34 | |
|
|
35 | t = alloc<char> (len + 1); |
|
|
36 | |
|
|
37 | strlcpy (t, s, len); |
|
|
38 | return t; |
|
|
39 | } |
13 | |
40 | |
14 | void |
41 | void |
15 | assign (char *dst, char const *src, size_t maxlen) |
42 | assign (char *dst, char const *src, size_t maxlen) |
16 | { |
43 | { |
17 | if (!src) |
44 | if (!src) |
… | |
… | |
89 | |
116 | |
90 | return c; |
117 | return c; |
91 | } |
118 | } |
92 | |
119 | |
93 | int |
120 | int |
94 | string::append (char c) |
121 | dynstr::append (char c) |
95 | { |
122 | { |
96 | if (size - pos <= 1) |
123 | if (size - pos <= 1) |
97 | { |
124 | { |
98 | size_t new_size = std::max (size * 2, pos + 9); |
125 | size_t new_size = std::max (size * 2, pos + 9); |
99 | size = new_size; |
126 | size = new_size; |
… | |
… | |
104 | |
131 | |
105 | return size; |
132 | return size; |
106 | } |
133 | } |
107 | |
134 | |
108 | int |
135 | int |
109 | string::append_nonul (char c) |
136 | dynstr::append_nonul (char c) |
110 | { |
137 | { |
111 | if (size - pos <= 1) |
138 | if (size - pos <= 1) |
112 | { |
139 | { |
113 | size_t new_size = std::max (size * 2, pos + 9); |
140 | size_t new_size = std::max (size * 2, pos + 9); |
114 | size = new_size; |
141 | size = new_size; |
… | |
… | |
118 | |
145 | |
119 | return size; |
146 | return size; |
120 | } |
147 | } |
121 | |
148 | |
122 | int |
149 | int |
123 | string::append (char const *src, size_t size) |
150 | dynstr::append (char const *src, size_t len) |
124 | { |
151 | { |
125 | if (this->size - pos <= size) |
152 | if (size - pos <= len) |
126 | { |
153 | { |
127 | size_t new_size = std::max (this->size * 2, pos + size + 8); |
154 | size_t new_size = std::max (size * 2, pos + len + 8); |
128 | this->size = new_size; |
155 | size = new_size; |
129 | m_rep = new char[new_size]; |
156 | m_rep = new char[new_size]; |
130 | } |
157 | } |
131 | memcpy (m_rep + pos, src, size); |
158 | memcpy (m_rep + pos, src, len); |
132 | pos += size; |
159 | pos += len; |
133 | m_rep[pos] = '\0'; |
160 | m_rep[pos] = '\0'; |
134 | |
161 | |
135 | return size; |
162 | return len; |
136 | } |
163 | } |
137 | |
164 | |
138 | int |
165 | int |
139 | string::append_nonul (char const *src, size_t size) |
166 | dynstr::append_nonul (char const *src, size_t len) |
140 | { |
167 | { |
141 | if (this->size - pos <= size) |
168 | if (size - pos <= len) |
142 | { |
169 | { |
143 | size_t new_size = std::max (this->size * 2, pos + size + 8); |
170 | size_t new_size = std::max (size * 2, pos + len + 8); |
144 | this->size = new_size; |
171 | size = new_size; |
145 | m_rep = new char[new_size]; |
172 | m_rep = new char[new_size]; |
146 | } |
173 | } |
147 | memcpy (m_rep + pos, src, size); |
174 | memcpy (m_rep + pos, src, len); |
148 | pos += size; |
175 | pos += len; |
149 | |
176 | |
150 | return size; |
177 | return len; |
151 | } |
178 | } |
152 | |
179 | |
153 | int |
180 | int |
154 | string::append_unguarded (char c) |
181 | dynstr::append_unguarded (char c) |
155 | { |
182 | { |
156 | m_rep[pos] = c; |
183 | m_rep[pos] = c; |
157 | m_rep[++pos] = '\0'; |
184 | m_rep[++pos] = '\0'; |
158 | |
185 | |
159 | return size; |
186 | return size; |
160 | } |
187 | } |
161 | |
188 | |
162 | int |
189 | int |
163 | string::append_unguarded_nonul (char c) |
190 | dynstr::append_unguarded_nonul (char c) |
164 | { |
191 | { |
165 | m_rep[pos++] = c; |
192 | m_rep[pos++] = c; |
166 | |
193 | |
167 | return size; |
194 | return size; |
168 | } |
195 | } |
169 | |
196 | |
170 | int |
197 | int |
171 | string::append_unguarded (char const *src, size_t size) |
198 | dynstr::append_unguarded (char const *src, size_t len) |
172 | { |
199 | { |
173 | memcpy (m_rep + pos, src, size); |
200 | memcpy (m_rep + pos, src, len); |
174 | pos += size; |
201 | pos += len; |
175 | m_rep[pos] = '\0'; |
202 | m_rep[pos] = '\0'; |
176 | |
203 | |
177 | return size; |
204 | return len; |
178 | } |
205 | } |
179 | |
206 | |
180 | int |
207 | int |
181 | string::append_unguarded_nonul (char const *src, size_t size) |
208 | dynstr::append_unguarded_nonul (char const *src, size_t len) |
182 | { |
209 | { |
183 | memcpy (m_rep + pos, src, size); |
210 | memcpy (m_rep + pos, src, len); |
184 | pos += size; |
211 | pos += len; |
185 | |
212 | |
186 | return size; |
213 | return len; |
187 | } |
214 | } |