str.c revision 88bcf81213b292e58ddaa41643e4146299cc750f
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2002-2016 Dovecot authors, see the included COPYING file */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenstring_t *str_new(pool_t pool, size_t initial_size)
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen /* never allocate a 0 byte size buffer. this is especially important
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen when str_c() is called on an empty string from a different stack
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen frame (see the comment in buffer.c about this). */
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen return buffer_create_dynamic(pool, I_MAX(initial_size, 1));
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainenstring_t *str_new_const(pool_t pool, const char *str, size_t len)
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen buffer_create_from_const_data(ret, str, len + 1);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return str_new(pool_datastack_create(), initial_size);
3f8303bae0f70df6db9337ad1d1476d290f9b1a3Timo Sirainenstring_t *t_str_new_const(const char *str, size_t len)
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen return str_new_const(pool_datastack_create(), str, len);
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen /* remove the \0 - we don't want to keep it */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenbool str_equals(const string_t *str1, const string_t *str2)
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen return memcmp(str1->data, str2->data, str1->used) == 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainenvoid str_append_n(string_t *str, const void *cstr, size_t max_len)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen const char *p;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenvoid str_printfa(string_t *str, const char *fmt, ...)
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenvoid str_vprintfa(string_t *str, const char *fmt, va_list args)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* the format string is modified only if %m exists in it. it happens
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen only in error conditions, so don't try to t_push() here since it'll
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen just slow down the normal code path. */
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen fmt = printf_format_fix_get_len(fmt, &init_size);
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainen /* @UNSAFE */
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen if (pos+init_size > buffer_get_writable_size(str) &&
9511a40d933181045343110c8101b75887062aaeTimo Sirainen /* avoid growing buffer larger if possible. this is also
2974dca6be5120e49279f06c8aa952e5fac56048Timo Sirainen required if buffer isn't dynamically growing. */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen init_size = buffer_get_writable_size(str)-pos;
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen tmp = buffer_get_space_unsafe(str, pos, init_size);
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen /* didn't fit with the first guess. now we know the size,
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen so try again. */