add eina_strbuf_append_length()

SVN revision: 46516
This commit is contained in:
Gustavo Sverzut Barbieri 2010-02-26 20:09:36 +00:00
parent a4571eb6bb
commit f32672764d
3 changed files with 52 additions and 0 deletions

View File

@ -13,6 +13,7 @@ EAPI void eina_strbuf_reset(Eina_Strbuf *buf) EINA_ARG_NONNULL(1);
EAPI Eina_Bool eina_strbuf_append(Eina_Strbuf *buf, const char *str) EINA_ARG_NONNULL(1, 2);
EAPI Eina_Bool eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str) EINA_ARG_NONNULL(1, 2);
EAPI Eina_Bool eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen) EINA_ARG_NONNULL(1, 2);
EAPI Eina_Bool eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, unsigned int length) EINA_ARG_NONNULL(1, 2);
EAPI Eina_Bool eina_strbuf_append_char(Eina_Strbuf *buf, char c) EINA_ARG_NONNULL(1);
EAPI Eina_Bool eina_strbuf_insert(Eina_Strbuf *buf, const char *str,
size_t pos) EINA_ARG_NONNULL(1, 2);

View File

@ -117,8 +117,16 @@ eina_strbuf_reset(Eina_Strbuf *buf)
/**
* Append a string to a buffer, reallocating as necessary.
*
* This is slightly slower than eina_strbuf_append_length(), as it
* needs to strlen() the given pointer. If you know the size
* beforehand, consider using that variant.
*
* @param buf the Eina_Strbuf to append to
* @param str the string to append
*
* @see eina_strbuf_append()
* @see eina_strbuf_append_length()
*/
EAPI Eina_Bool
eina_strbuf_append(Eina_Strbuf *buf, const char *str)
@ -167,6 +175,9 @@ eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str)
* @param buf the Eina_Strbuf to append to
* @param str the string to append
* @param maxlen maximum number of chars to append
*
* @see eina_strbuf_append()
* @see eina_strbuf_append_length()
*/
EAPI Eina_Bool
eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen)
@ -187,6 +198,36 @@ eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen)
return EINA_TRUE;
}
/**
* Append a string of exact length to a buffer, reallocating as necessary.
*
* This is a slightly faster version that does not need to strlen()
* the whole string to know its size. Useful when dealing with strings
* of known size, such as eina_stringshare, see
* eina_stringshare_length().
*
* @param buf the Eina_Strbuf to append to
* @param str the string to append
* @param length the exact length to use.
*
* @see eina_strbuf_append()
* @see eina_strbuf_append_n()
*/
EAPI Eina_Bool
eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, unsigned int length)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE);
if (EINA_UNLIKELY(!_eina_strbuf_grow(buf, buf->len + length)))
return EINA_FALSE;
memcpy(buf->buf + buf->len, str, length);
buf->len += length;
buf->buf[buf->len] = '\0';
return EINA_TRUE;
}
/**
* Insert a string to a buffer, reallocating as necessary.
* @param buf the Eina_Strbuf to insert

View File

@ -139,6 +139,16 @@ START_TEST(strbuf_append)
fail_if(strcmp(eina_strbuf_string_get(buf), "a"));
eina_strbuf_reset(buf);
eina_strbuf_append_length(buf, "something", strlen("something"));
fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
fail_if(strcmp(eina_strbuf_string_get(buf), "something"));
eina_strbuf_reset(buf);
eina_strbuf_append_length(buf, "somethingELSE", strlen("something"));
fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
fail_if(strcmp(eina_strbuf_string_get(buf), "something"));
eina_strbuf_reset(buf);
eina_strbuf_free(buf);
eina_shutdown();