ucx/string.c

changeset 235
7cf1e41833a2
parent 234
7a63b4986b5b
child 236
ffc6d0910342
equal deleted inserted replaced
234:7a63b4986b5b 235:7cf1e41833a2
216 return result; 216 return result;
217 } 217 }
218 } 218 }
219 219
220 ssize_t nmax = *n; 220 ssize_t nmax = *n;
221 sstr_t* result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)); 221 size_t arrlen = 16;
222 sstr_t* result = (sstr_t*) almalloc(allocator, arrlen*sizeof(sstr_t));
222 223
223 if (result) { 224 if (result) {
224 sstr_t curpos = s; 225 sstr_t curpos = s;
225 ssize_t j = 1; 226 ssize_t j = 1;
226 while (1) { 227 while (1) {
248 curpos.ptr += processed; 249 curpos.ptr += processed;
249 curpos.length -= processed; 250 curpos.length -= processed;
250 251
251 /* allocate memory for the next string */ 252 /* allocate memory for the next string */
252 j++; 253 j++;
253 sstr_t* reallocated = (sstr_t*) 254 if (j > arrlen) {
254 alrealloc(allocator, result, j*sizeof(sstr_t)); 255 arrlen *= 2;
255 if (reallocated) { 256 sstr_t* reallocated = (sstr_t*) alrealloc(
256 result = reallocated; 257 allocator, result, arrlen*sizeof(sstr_t));
257 } else { 258 if (reallocated) {
258 for (ssize_t i = 0 ; i < j-1 ; i++) { 259 result = reallocated;
259 alfree(allocator, result[i].ptr); 260 } else {
261 for (ssize_t i = 0 ; i < j-1 ; i++) {
262 alfree(allocator, result[i].ptr);
263 }
264 alfree(allocator, result);
265 *n = -2;
266 return NULL;
260 } 267 }
261 alfree(allocator, result);
262 *n = -2;
263 return NULL;
264 } 268 }
265 } else { 269 } else {
266 /* nmax reached, copy the _full_ remaining string */ 270 /* nmax reached, copy the _full_ remaining string */
267 result[j-1] = sstrdup_a(allocator, curpos); 271 result[j-1] = sstrdup_a(allocator, curpos);
268 break; 272 break;

mercurial