diff options
Diffstat (limited to 'lib/qstring.c')
-rw-r--r-- | lib/qstring.c | 793 |
1 files changed, 498 insertions, 295 deletions
diff --git a/lib/qstring.c b/lib/qstring.c index 5ca4d868..97f8e8b0 100644 --- a/lib/qstring.c +++ b/lib/qstring.c @@ -18,494 +18,697 @@ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ +#include "misc.h" +#include "stdio.h" #include "qstring.h" - #include "memory.h" -#include "zassert.h" /*============================================================================== */ /*------------------------------------------------------------------------------ - * Initialise qstring -- allocate if required. + * Create a new body or extend existing one to accommodate at least slen + 1. * - * If non-zero len is given, a body is allocated (for at least len + 1). + * Sets size to multiple of 8 (minimum 16), with at least 9 bytes free beyond + * the requested slen. * - * Returns: address of qstring + * Does not affect 'len' or 'cp'. * - * NB: assumes initialising a new structure. If not, then caller should - * use qs_reset() or qs_clear(). + * If 'keep_alias', ONLY sets 'b_size' & 'b_body'. + * + * If !'keep_alias', sets the elstring body & size & clears 'alias'. */ -extern qstring -qs_init_new(qstring qs, size_t len) +static inline void +qs_new_body(qstring qs, usize slen, bool keep_alias) { - if (qs == NULL) - qs = qs_new() ; - else - memset(qs, 0, sizeof(qstring_t)) ; /* see qs_new() */ + qs->b_size = (slen + 0x10) & ~(usize)(0x08 - 1) ; + qs->b_body = XREALLOC(MTYPE_STRING, qs->b_body, qs->b_size) ; - if (len != 0) - return qs_make_to_length(qs, len) ; - - return qs ; + if (!keep_alias) + qs_set_real_body_nn(qs) ; } ; /*------------------------------------------------------------------------------ - * Allocate or reallocate so that string is big enough for the given length. + * Create a new, empty qs * - * Allocate qstring if required. Returns with a body with size > 0. + * If non-zero slen is given, a body is allocated (size = slen + 1). + * If zero slen is given, no body is allocated. * - * Allocates to 16 byte boundaries with space for '\0' beyond given length. + * Sets 'len' = 'cp' = 0. * * Returns: address of qstring - * - * NB: allocates new body if the size == 0. - * - * If the qstring is a "dummy", its contents are now copied to the new - * real qstring body -- up to a maximum of the new length. */ extern qstring -qs_make_to_length(qstring qs, size_t len) +qs_new(usize slen) { - size_t size = (len + 0x10) & ~(size_t)(0x10 - 1) ; + qstring qs ; - if (qs == NULL) - qs = qs_new() ; + /* zeroising sets a completely empty qstring -- see qs_init_new() */ - if (size > qs->size) - { - if (qs->size == 0) - { - void* old ; - old = qs->body ; + qs = XCALLOC(MTYPE_QSTRING, sizeof(qstring_t)) ; - qs->size = size ; - qs->body = XMALLOC(MTYPE_QSTRING_BODY, qs->size) ; + confirm(QSTRING_INIT_ALL_ZEROS) ; - if ((qs->len != 0) && (old != NULL)) - memcpy(qs->body, old, (qs->len <= len) ? qs->len : len) ; - } - else - { - qs->size *= 2 ; - if (qs->size < size) - qs->size = size ; - qs->body = XREALLOC(MTYPE_QSTRING_BODY, qs->body, qs->size) ; - } ; - }; + if (slen != 0) + qs_new_body(qs, slen, false) ; return qs ; } ; /*------------------------------------------------------------------------------ - * Add 'n' to the current string length, allocating or extending the body as - * required. + * Create a new, empty qs with body + */ +extern qstring +qs_new_with_body(usize slen) +{ + return qs_new(slen | 1) ; +} ; + +/*------------------------------------------------------------------------------ + * Initialise qstring -- allocate if required. * - * Allocate qstring if required. Returns with a body with size > 0. + * If non-zero slen is given, a body is allocated (size = slen + 1). + * If zero slen is given, no body is allocated. * - * Allocates to 16 byte boundaries with space for '\0' beyond new length. + * Sets 'len' = 'cp' = 0. * * Returns: address of qstring * - * also: sets char** p_ep to point at the *end* of the new len. - * - * NB: allocates new body if the size == 0. - * - * If the qstring is a "dummy", its contents are now copied to the new - * real qstring body -- up to a maximum of the new length. + * NB: assumes initialising a new structure. If not, then caller should + * use qs_reset() or qs_clear(). */ extern qstring -qs_add_len(qstring qs, size_t n, char** p_ep) +qs_init_new(qstring qs, usize slen) { - size_t len ; - len = (qs != NULL) ? qs->len + n : n ; + if (qs == NULL) + return qs_new(slen) ; - qs = qs_make_to_length(qs, len) ; + memset(qs, 0, sizeof(qstring_t)) ; - qs->len = len ; + confirm(QSTRING_INIT_ALL_ZEROS) ; - *p_ep = (char*)qs->body + len ; + /* Zeroising has set: + * + * empty elstring -- no body, 'len' = 0 + * + * size = 0 -- no elstring body + * + * cp = 0 + * + * b_body = NULL -- no body buffer + * b_size = 0 -- no body buffer + * + * alias = false -- not an alias qstring + */ + + if (slen != 0) + qs_new_body(qs, slen, false) ; return qs ; } ; /*------------------------------------------------------------------------------ - * Free body of qstring -- zeroise size, len and cp + * Reset qstring -- free body and, if required, free the structure. * - * Does nothing if qstring is NULL + * If not freeing the structure, zeroise size, len and cp -- qs_free_body() + * + * Returns: NULL if freed the structure + * address of structure (if any), otherwise * * NB: frees the body if the size != 0. So, a "dummy" qstring will not retain * the old body. */ -extern void -qs_free_body(qstring qs) +extern qstring +qs_reset(qstring qs, free_keep_b free_structure) { if (qs != NULL) { - if (qs->size != 0) - XFREE(MTYPE_QSTRING_BODY, qs->body) ; /* sets qs->body = NULL */ + if (qs->b_body != NULL) + XFREE(MTYPE_STRING, qs->b_body) ; /* sets qs->b_body = NULL */ + + if (free_structure) + XFREE(MTYPE_QSTRING, qs) ; /* sets qs = NULL */ + else + memset(qs, 0, sizeof(qstring_t)) ; /* see qs_init_new */ - qs->size = 0 ; - qs->len = 0 ; - qs->cp = 0 ; + confirm(QSTRING_INIT_ALL_ZEROS) ; } ; + return qs ; } ; /*------------------------------------------------------------------------------ - * Reset qstring -- free body and, if required, free the structure. + * Allocate or reallocate body so that is big enough for the given 'slen'. * - * If not freeing the structure, zeroise size, len and cp -- qs_free_body() + * qstring may NOT be NULL. * - * Returns: NULL if freed the structure - * address of structure (if any), otherwise + * Returns with a body with size > 0 (even if 'slen' == 0). * - * NB: frees the body if the size != 0. So, a "dummy" qstring will not retain - * the old body. + * Expects, but does not require, that 'slen' > 'size'. + * + * Does not change 'cp' or 'len' (even if 'len' > 'slen'). + * + * If is an alias, copies min('len', 'alen', 'slen') characters to the body. */ -extern qstring -qs_reset(qstring qs, int free_structure) +Private void +qs_make_to_size(qstring qs, usize slen, usize alen) { - if (qs != NULL) + /* If body needs to be extended, do that now */ + if (slen >= qs->b_size) + qs_new_body(qs, slen, (qs->alias && (alen != 0))) ; + /* keeps alias if required */ + + /* Worry about alias. If we keep it, we keep all of it. */ + if (qs->alias) { - if (qs->size != 0) - XFREE(MTYPE_QSTRING_BODY, qs->body) ; /* sets qs->body = NULL */ + /* alias or empty */ + usize len = qs_len_nn(qs) ; - if (free_structure) - XFREE(MTYPE_QSTRING, qs) ; /* sets qs = NULL */ - else - { - qs->size = 0 ; - qs->len = 0 ; - qs->cp = 0 ; - } ; + if (alen >= len) + alen = len ; /* keep min(alen, len) */ + + if (alen > slen) + alen = slen ; /* keep only to new len. */ + + if (alen != 0) + memcpy(qs->b_body, qs_body_nn(qs), alen) ; + + qs_set_real_body_nn(qs) ; } ; - return qs ; } ; /*============================================================================== - * printf() and vprintf() type functions + * Setting value of qstring + * + * Copy the given string to the qstring, allocating qstring and/or extending + * it as required. + * + * Any alias is simply discarded. + * + * Sets 'len' to new length + * Sets 'cp' = 0 + * + * Returns: address of the qstring (allocated if required). */ /*------------------------------------------------------------------------------ - * Formatted print to qstring -- cf printf() + * Set qstring to be copy of the given string. * - * Allocate qstring if required. + * Treats src == NULL as an empty string. Otherwise src must be a '\0' + * terminated string. * - * Returns: address of qstring if OK - * NULL if failed (unlikely though that is) + * Does not copy or count the '\0' terminator. + * + * See notes above. */ extern qstring -qs_printf(qstring qs, const char* format, ...) +qs_set(qstring qs, const char* src) { - va_list args; + return qs_set_n(qs, src, (src != NULL ? strlen(src) : 0)) ; +} ; - va_start (args, format); - qs = qs_vprintf(qs, format, args); - va_end (args); +/*------------------------------------------------------------------------------ + * Set qstring to be copy of leading 'n' bytes of given string. + * + * If n == 0, src is ignored (and may be NULL) + * If n > 0, src string MUST be at least 'n' bytes long. + * + * See notes above. + */ +extern qstring +qs_set_n(qstring qs, const char* src, usize len) +{ + qs = qs_new_size(qs, len) ; /* ensures have body > len */ - return qs; + if (len != 0) + memcpy(qs_char_nn(qs), src, len) ; + + qs_set_len_nn(qs, len) ; + qs_set_cp_nn(qs, 0) ; + + return qs ; } ; /*------------------------------------------------------------------------------ - * Formatted print to qstring -- cf vprintf() + * Set qstring to be copy of given qstring contents. * - * Allocate qstring if required. + * If the given qstring is an alias, then the contents of the alias are copied + * (so the result is not an alias). See qs_copy() for the alternative. * - * Returns: address of qstring if OK - * NULL if failed (unlikely though that is) + * See notes above -- and note that 'cp' is set to 0. */ extern qstring -qs_vprintf(qstring qs, const char *format, va_list args) +qs_set_qs(qstring qs, qstring src) { - va_list ac ; - int len ; - qstring qqs ; + return qs_set_n(qs, qs_body(src), qs_len(src)) ; +} ; - qqs = qs ; - if (qs == NULL) - qs = qs_new() ; +/*------------------------------------------------------------------------------ + * Set qstring to be copy of given elstring contents. + * + * See notes above. + */ +extern qstring +qs_set_els(qstring qs, elstring src) +{ + return qs_set_n(qs, els_body(src), els_len(src)) ; +} ; - while (1) +/*------------------------------------------------------------------------------ + * Set qstring with given pattern to given length. + * + * Repeats the given pattern as many times as necessary to get to the given + * length -- using a final partial piece of the pattern as required. + * + * If the pattern is zero length, fills with spaces ! + * + * See notes above. + */ +extern qstring +qs_set_fill(qstring qs, usize len, const char* src) +{ + return qs_set_fill_n(qs, len, src, (src != NULL ? strlen(src) : 0)) ; +} ; + +/*------------------------------------------------------------------------------ + * Set qstring with given pattern to given length. + * + * Repeats the given pattern as many times as necessary to get to the given + * length -- using a final partial piece of the pattern as required. + * + * See notes above. + */ +extern qstring +qs_set_fill_n(qstring qs, usize len, const char* src, usize flen) +{ + char* p ; + char* q ; + usize left ; + + qs = qs_new_size(qs, len) ; /* ensures have body > len */ + + if (len != 0) { - /* Note that vsnprintf() returns the length of what it would like to have - * produced, if it had the space. That length does not include the - * trailing '\0'. - * - * Also note that given a zero length the string address may be NULL, and - * the result is still the length required. - */ - va_copy(ac, args); - qs->len = len = vsnprintf (qs->body, qs->size, format, ac) ; - va_end(ac); + if (flen == 0) + { + src = " " ; + flen = strlen(src) ; + } ; + + if (len < flen) + flen = len ; + + q = p = qs_char_nn(qs) ; + memcpy(p, src, flen) ; + p += flen ; + left = len - flen ; - if (len < 0) - break ; + while (left > 0) + { + if (left < flen) + flen = left ; - if (len < (int)qs->size) - return qs ; + memcpy(p, q, flen) ; + p += flen ; + left -= flen ; - qs_make_to_length(qs, len) ; + flen += flen ; + } ; } ; - if (qqs == NULL) - qs_reset_free(qs) ; /* discard what was allocated */ - else - qs->len = 0 ; + qs_set_len_nn(qs, len) ; + qs_set_cp_nn(qs, 0) ; - return NULL ; + return qs ; } ; /*============================================================================== - * Other operations + * Appending to a qstring + * + * Copy the given string to the end of the given qstring (at 'len'), + * allocating qstring and/or extending it as required. + * + * If this is an alias, it is copied to before being appended to (even if + * appending nothing). + * + * Can append to NULL or empty qstring. + * + * Sets 'len' to new length. + * Does not affect 'cp'. + * + * Will work even if the stuff being appended is somewhere in the body of the + * qstring !! + * + * Returns: address of the qstring (allocated if required). */ /*------------------------------------------------------------------------------ - * Set qstring to be copy of the given string. + * Append given qstring to a qstring. * - * Allocates a qstring, if required. + * See notes above. + */ +extern qstring +qs_append(qstring qs, qstring src) +{ + return qs_append_str_n(qs, qs_body(src), qs_len(src)) ; +} ; + +/*------------------------------------------------------------------------------ + * Append given string to a qstring. * - * Sets qs->len to the length of the string (excluding trailing '\0') + * Treats src == NULL as an empty string. Otherwise src must be a '\0' + * terminated string. * - * NB: if src == NULL, sets qstring to be zero length string. + * Does not copy or count the '\0' terminator. * - * Returns: address of the qstring copied to. + * See notes above. + */ +extern qstring qs_append_str(qstring qs, const char* src) +{ + return qs_append_str_n(qs, src, (src != NULL) ? strlen(src) : 0) ; +} ; + +/*------------------------------------------------------------------------------ + * Append leading 'n' bytes of given string to a qstring. + * + * If n == 0, src may be NULL + * If n > 0, src string MUST be at least 'n' bytes long. * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * See notes above. */ extern qstring -qs_set(qstring qs, const char* src) +qs_append_str_n(qstring qs, const char* src, usize n) { - qs = qs_set_len(qs, (src != NULL) ? strlen(src) : 0) ; - if (qs->len != 0) - memcpy(qs->body, src, qs->len + 1) ; - else - *((char*)qs->body) = '\0' ; + qs = qs_extend(qs, n) ; /* allocate, copy any alias, extend body, + set new length, etc */ + + if (n != 0) + memmove(qs_ep_char_nn(qs) - n, src, n) ; return qs ; } ; /*------------------------------------------------------------------------------ - * Set qstring to be leading 'n' bytes of given string. + * Append given elstring to a qstring. + * + * See notes above. + */ +extern qstring +qs_append_els(qstring qs, elstring src) +{ + return qs_append_str_n(qs, els_body(src), els_len(src)) ; +} ; + +/*============================================================================== + * Setting of alias. + * + * Does NOT copy the given string, but sets the qstring to be a pointer to it. + * This means that: * - * Allocates qstring if required. + * NB: it is the caller's responsibility to ensure that the original string + * stays put for however long the qstring is an alias for it. * - * Inserts '\0' terminator after the 'n' bytes copied. + * It is also the caller's responsibility to see that the original string + * is discarded as required (once the alias is no longer required.) * - * Returns: address of the qstring copied to. + * NB: if the qstring is changed in any way, a copy of the aliased string will + * be made first. * - * NB: src string MUST be at least 'n' bytes long. + * NB: if a pointer to the body of the qstring is taken, then while that is in + * use, the qstring must not be released, so that the alias is not + * released. * - * NB: src may not be NULL unless n == 0. + * Preserves any existing qstring body. * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * Returns: address of the qstring (allocated if required). + */ + +/*------------------------------------------------------------------------------ + * Set qstring to be an alias for the given string. + * + * Treats src == NULL as an empty string. Otherwise src must be a '\0' + * terminated string. + * + * Does not count the '\0' terminator. + * + * See notes above. */ extern qstring -qs_set_n(qstring qs, const char* src, size_t n) +qs_set_alias(qstring qs, const char* src) { - qs = qs_set_len(qs, n) ; /* ensures have body > n */ - if (n != 0) - memcpy(qs->body, src, n) ; + return qs_set_alias_n(qs, src, (src != NULL) ? strlen(src) : 0) ; +} ; - *((char*)qs->body + n) = '\0' ; +/*------------------------------------------------------------------------------ + * Set qstring to be an alias for the leading 'n' bytes of given string. + * + * If n == 0, src may be NULL + * If n > 0, src string MUST be at least 'n' bytes long. + * + * See notes above. + */ +extern qstring +qs_set_alias_n(qstring qs, const char* src, usize n) +{ + if (qs == NULL) + qs = qs_new(0) ; + + /* Make the alias. Note that any existing b_body and b_size are preserved, + * so that any current body can be reused at a later date. + */ + qs_set_body_nn(qs, (n != 0) ? src : "") ; + qs_set_len_nn(qs, n) ; + qs_set_cp_nn(qs, 0) ; + qs->alias = true ; + qs->size = 0 ; /* => empty or alias */ return qs ; } ; /*------------------------------------------------------------------------------ - * Append given string to a qstring. + * Set qstring to be an alias for the given qstring. * - * Allocates a qstring, if required. + * If the src is not an alias, then the qstring is an alias for the body of + * src -- so must be careful not to disturb that ! * - * Sets qs->len to the length of the result (excluding trailing '\0') + * If the src is an alias, then the qstring is another alias. * - * NB: if src == NULL, appends nothing -- but result will be '\0' terminated. + * See notes above. + */ +extern qstring +qs_set_alias_qs(qstring qs, qstring src) +{ + return qs_set_alias_n(qs, qs_body(src), qs_len(src)) ; +} ; + +/*------------------------------------------------------------------------------ + * Construct a qstring which is an alias for the given elstring. * - * Returns: address of the qstring copied to. + * If n == 0, src may be NULL + * If n > 0, src string MUST be at least 'n' bytes long. * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * See notes above. */ -extern qstring qs_append(qstring qs, const char* src) +extern qstring +qs_set_alias_els(qstring qs, elstring src) { - size_t n ; - char* ep ; - - n = (src != NULL) ? strlen(src) : 0 ; - - qs = qs_add_len(qs, n, &ep) ; - ep = (char*)qs->body + qs->len ; - - if (n != 0) - memcpy(ep - n, src, n + 1) ; - else - *ep = '\0' ; - - return qs ; + return qs_set_alias_n(qs, els_body(src), els_len(src)) ; } ; +/*============================================================================== + * Copying of qstring + */ + /*------------------------------------------------------------------------------ - * Set qstring to be leading 'n' bytes of given string. + * Copy one qstring to another -- allocating/extending as required. * - * Allocates qstring if required. - * - * Returns: address of the qstring copied to. + * If both are NULL, returns NULL. + * Otherwise if dst is NULL, creates a new qstring. * - * NB: src string MUST be at least 'n' bytes long. + * If src is NULL it is treated as zero length, with 'cp' == 0. * - * NB: src may not be NULL unless n == 0. + * If src is not an alias, a copy is made to dst. + * If src is an alias, dst becomes another alias for the same thing. * - * NB: if n == 0, appends nothing -- but result will be '\0' terminated. + * If dst is an alias, that is discarded. * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * Copies the src 'cp' to the dst. * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * Returns: the destination qstring (allocated if required). */ extern qstring -qs_append_n(qstring qs, const char* src, size_t n) +qs_copy(qstring dst, qstring src) { - char* ep ; - - qs = qs_add_len(qs, n, &ep) ; + if (src == NULL) + { + qs_clear(dst) ; /* if dst not NULL, clear it */ + return dst ; + } ; - if (n != 0) - memcpy(ep - n, src, n) ; + if (src->alias) + dst = qs_set_alias_qs(dst, src) ; + else + dst = qs_set_qs(dst, src) ; - *ep = '\0' ; + qs_set_cp_nn(dst, qs_cp_nn(src)) ; /* copy in the src cp. */ - return qs ; + return dst ; } ; -/*------------------------------------------------------------------------------ - * Copy one qstring to another +/*============================================================================== + * printf() and vprintf() type functions * - * If both are NULL, returns NULL. + * Allocate and/or extend qstring as required. * - * Otherwise if dst is NULL, creates a new qstring. + * Any alias is discarded. * - * Sets dst: body = copy of src->len bytes of src->body -- '\0' terminated. - * cp = src->cp - * len = src->len + * Sets 'len' = length of the '\0' terminated result (less the '\0'). + * Sets 'cp' = 0 * - * Where a NULL src has zero cp and len. + * If fails and qs != NULL: * - * If not NULL, the destination is guaranteed to have a body, and that will be - * '\0' terminated. + * Sets 'len' = 0 + * Sets 'cp' = 0 + * But retains any existing body. * - * Returns: the destination qstring + * Returns: address of qstring if OK + * NULL if failed (unlikely though that is) + */ + +/*------------------------------------------------------------------------------ + * Formatted print to qstring -- cf printf() * - * NB: if copying to a dummy qstring, the old body is simply discarded. + * See notes above. */ extern qstring -qs_copy(qstring dst, qstring src) +qs_printf(qstring qs, const char* format, ...) { - size_t n ; + va_list args; - if (src == NULL) - { - if (dst == NULL) - return dst ; + va_start (args, format); + qs = qs_vprintf(qs, format, args); + va_end (args); + + return qs; +} ; + +/*------------------------------------------------------------------------------ + * Formatted print to qstring -- cf vprintf() + * + * See notes above. + */ +extern qstring +qs_vprintf(qstring qs, const char *format, va_list args) +{ + va_list ac ; + int slen ; + qstring qqs ; - n = 0 ; - dst->cp = 0 ; - } + qqs = qs ; /* NULL => need to make qs */ + if (qs == NULL) + qqs = qs_new(0) ; /* Sets size, cp & len = 0 */ else + qs_clear(qqs) ; /* Sets cp & len = 0, discard any alias, but + keep existing body */ + + while (1) { - if (dst == NULL) - dst = qs_new() ; + /* Note that vsnprintf() returns the length of what it would like to have + * produced, if it had the space. That length does not include the + * trailing '\0'. + * + * Also note that given a zero length the string address may be NULL, and + * the result is still the length required. + */ + va_copy(ac, args); + slen = vsnprintf(qs_body_nn(qqs), qqs->size, format, ac) ; + va_end(ac); - n = src->len ; - dst->cp = src->cp ; - } ; + if (slen < 0) + break ; /* failed */ - qs_set_len(dst, n) ; + if ((usize)slen < qqs->size) + { + qs_set_len_nn(qqs, slen) ; + return qqs ; /* succeeded */ + } ; - if (n > 0) - memcpy(dst->body, src->body, n) ; + qs_make_to_size(qqs, slen, 0) ; /* need space for slen */ + } ; - *((char*)dst->body + n) = '\0' ; + /* Failed... discard anything that has been allocated. */ + if (qs == NULL) + qs_reset(qqs, free_it) ; - return dst ; + return NULL ; } ; +/*============================================================================== + * Other operations + */ + /*------------------------------------------------------------------------------ - * Compare significant parts of two qstrings. + * Replace 'r' bytes at 'cp' by 'n' bytes -- extending if required. + * + * May increase or decrease 'len'. but does not affect 'cp'. * - * By significant, mean excluding leading/trailing isspace() and treating - * multiple isspace() as single isspace(). + * If the given src is NULL, do not insert anything, just leave the space + * ready for it. * - * Compares the 'len' portions of the strings. + * Returns: number of bytes beyond 'cp' that now exist. * - * If either is NULL, it is deemed to be an empty string. + * qstring MUST NOT be NULL * - * Returns: -1 => a < b - * 0 => a == b - * +1 => a > b + * If 'cp' > 'len', then sets 'len' = 'cp' first -- which will introduce + * one or more undefined bytes. + * + * If this is a aliased qstring, a copy is made, so is no longer an alias. */ -extern int -qs_cmp_sig(qstring a, qstring b) +extern usize +qs_replace(qstring qs, usize r, const void* src, usize n) { - const unsigned char* p_a ; - const unsigned char* e_a ; - const unsigned char* p_b ; - const unsigned char* e_b ; + usize cp, len, nlen, after ; + const char* ap ; + char* np ; - /* Set up pointers and dispense with leading and trailing isspace() - * - * Dummy up if NULL - */ - if (a != NULL) - { - p_a = a->body ; - e_a = p_a + a->len ; - - while ((p_a < e_a) && isspace(*p_a)) - ++p_a ; - while ((p_a < e_a) && isspace(*(e_a - 1))) - --e_a ; - } - else - { - p_a = NULL ; - e_a = NULL ; - } + len = qs_len_nn(qs) ; + cp = qs_cp_nn(qs) ; - if (b != NULL) - { - p_b = b->body ; - e_b = p_b + b->len ; - - while ((p_b < e_b) && isspace(*p_b)) - ++p_b ; - while ((p_b < e_b) && isspace(*(e_b - 1))) - --e_b ; - } + if ((cp + r) >= len) + /* Replacing up to or beyond the end of the string */ + after = 0 ; else - { - p_b = NULL ; - e_b = NULL ; - } ; + /* Replacing section short of the end of the string */ + after = len - (cp + r) ; - /* Now set about finding the first difference */ - while ((p_a != e_a) && (p_b != e_b)) + nlen = cp + n + after ; + if (nlen >= qs->b_size) + qs_new_body(qs, nlen, qs->alias) ; /* keeping any alias */ + + ap = np = qs->b_body ; + + if (qs->alias) { - if (isspace(*p_a) && isspace(*p_b)) - { - do { ++p_a ; } while (isspace(*p_a)) ; - do { ++p_b ; } while (isspace(*p_b)) ; - } ; + ap = qs_body_nn(qs) ; /* copy from the alias */ + + uint before = cp ; + if (before > len) + before = len ; - if (*p_a != *p_b) - return (*p_a < *p_b) ? -1 : +1 ; + if (before > 0) + memmove(np, ap, before) ; - ++p_a ; - ++p_b ; + qs_set_real_body_nn(qs) ; } ; - /* No difference before ran out of one or both */ - if (p_a != e_a) - return +1 ; - else if (p_b != e_b) - return -1 ; - else - return 0 ; + if (after > 0) /* move the after part before inserting */ + memmove(np + cp + n, ap + cp + r, after) ; + + if ((n > 0) && (src != NULL)) /* insert */ + memmove(np + cp, src, n) ; + + /* Set new 'len' */ + qs_set_len_nn(qs, nlen) ; + + return n + after ; } ; |