bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstring_t *str_new(pool_t pool, size_t initial_size)
93ae7fcd39c6982f7e338adfe71139942d9bbad1Timo Sirainen /* never allocate a 0 byte size buffer. this is especially important
93ae7fcd39c6982f7e338adfe71139942d9bbad1Timo Sirainen when str_c() is called on an empty string from a different stack
93ae7fcd39c6982f7e338adfe71139942d9bbad1Timo Sirainen frame (see the comment in buffer.c about this). */
93ae7fcd39c6982f7e338adfe71139942d9bbad1Timo Sirainen return buffer_create_dynamic(pool, I_MAX(initial_size, 1));
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainenstring_t *str_new_const(pool_t pool, const char *str, size_t len)
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen buffer_create_from_const_data(ret, str, len + 1);
cd466fe7b84b0223735a6469c7f7bc225f65996dTimo Sirainen return str_new(pool_datastack_create(), initial_size);
0e3f8c6edad565112d91f0a53568c0313d657e48Timo Sirainenstring_t *t_str_new_const(const char *str, size_t len)
0e3f8c6edad565112d91f0a53568c0313d657e48Timo Sirainen return str_new_const(pool_datastack_create(), str, len);
d6af1e63bc7824f1cc5b9b73a1c5f8f8789788d6Timo Sirainen /* remove the \0 - we don't want to keep it */
1f57716285d4c5bc9bf2fd5569e3c85fd496afd9Timo Sirainenbool str_equals(const string_t *str1, const string_t *str2)
1f57716285d4c5bc9bf2fd5569e3c85fd496afd9Timo Sirainen return memcmp(str1->data, str2->data, str1->used) == 0;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvoid str_append_n(string_t *str, const void *cstr, size_t max_len)
88bcf81213b292e58ddaa41643e4146299cc750fTimo Sirainen const char *p;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvoid str_printfa(string_t *str, const char *fmt, ...)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvoid str_vprintfa(string_t *str, const char *fmt, va_list args)
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen /* the format string is modified only if %m exists in it. it happens
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen only in error conditions, so don't try to t_push() here since it'll
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen just slow down the normal code path. */
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen fmt = printf_format_fix_get_len(fmt, &init_size);
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen /* @UNSAFE */
8b48c53a81bdc67f267ffbcc45ba9860cb49e977Timo Sirainen if (pos+init_size > buffer_get_writable_size(str) &&
22c1ec434d7323e125c150e3fd237316c74de6d5Timo Sirainen /* avoid growing buffer larger if possible. this is also
22c1ec434d7323e125c150e3fd237316c74de6d5Timo Sirainen required if buffer isn't dynamically growing. */
8b48c53a81bdc67f267ffbcc45ba9860cb49e977Timo Sirainen init_size = buffer_get_writable_size(str)-pos;
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen tmp = buffer_get_space_unsafe(str, pos, init_size);
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen /* didn't fit with the first guess. now we know the size,
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen so try again. */
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen tmp = buffer_get_space_unsafe(str, pos, ret + 1);
b09be485e9373be4288f5615bbce6ebed65a425aTimo Sirainen /* drop the unused data, including terminating NUL */