X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=phpstr%2Fphpstr.c;fp=phpstr%2Fphpstr.c;h=eb2f87a715d29d6975f0528e479ee81c18da9bac;hp=addbf34362b369190227ec175ef614627e3c62d7;hb=ac3d2543d5be5cc11352451c4dcaca363569dc66;hpb=650dad8564d53bd3fb6c9867fa81e0abf17f6f7b diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index addbf34..eb2f87a 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -212,7 +212,7 @@ PHPSTR_API phpstr *phpstr_sub(const phpstr *buf, size_t offset, size_t length) if (offset >= buf->used) { return NULL; } else { - size_t need = (length + offset) > buf->used ? (buf->used - offset) : (length - offset); + size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset)); phpstr *sub = phpstr_init_ex(NULL, need, PHPSTR_INIT_PREALLOC | (buf->pmem ? PHPSTR_INIT_PERSISTENT:0)); if (sub) { if (PHPSTR_NOMEM == phpstr_append(sub, buf->data + offset, need)) { @@ -326,7 +326,7 @@ PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_ *chunk = NULL; if (!*s) { - *s = phpstr_init_ex(NULL, chunk_size * 2, chunk_size ? PHPSTR_INIT_PREALLOC : 0); + *s = phpstr_init_ex(NULL, chunk_size << 1, chunk_size ? PHPSTR_INIT_PREALLOC : 0); } storage = *s; @@ -340,12 +340,10 @@ PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_ return chunk_size; } - if (storage->used >= storage->size/2) { - phpstr *avail = phpstr_left(storage, storage->size/2); - *chunk = estrndup(PHPSTR_VAL(avail), PHPSTR_LEN(avail)); - phpstr_free(&avail); - phpstr_cut(storage, 0, storage->size/2); - return storage->size/2; + if (storage->used >= (chunk_size = storage->size >> 1)) { + *chunk = estrndup(storage->data, chunk_size); + phpstr_cut(storage, 0, chunk_size); + return chunk_size; } return 0;