str.h revision 53e9cbd970106be179f7586151fa4e434f7084f1
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch#ifndef STR_H
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch#define STR_H
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
9f8559a5ac7b53b83378c894de6578773dc1e392Timo Sirainen#include "buffer.h"
9f8559a5ac7b53b83378c894de6578773dc1e392Timo Sirainen
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstring_t *str_new(pool_t pool, size_t initial_size);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstring_t *t_str_new(size_t initial_size);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch/* Allocate a constant string using the given str as the input data.
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch str pointer is saved directly, so it must not be freed until the returned
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch string is no longer used. len must contain strlen(str). */
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstring_t *str_new_const(pool_t pool, const char *str, size_t len);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstring_t *t_str_new_const(const char *str, size_t len);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschvoid str_free(string_t **str);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschchar *str_free_without_data(string_t **str);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschconst char *str_c(string_t *str);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschchar *str_c_modifiable(string_t *str);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschbool str_equals(const string_t *str1, const string_t *str2) ATTR_PURE;
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline const unsigned char *str_data(const string_t *str)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch return str->data;
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch}
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline size_t str_len(const string_t *str)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch return str->used;
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch}
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch/* Append string/character */
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschvoid str_append_n(string_t *str, const void *cstr, size_t max_len);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline void str_append(string_t *str, const char *cstr)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch buffer_append(str, cstr, strlen(cstr));
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch}
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline void str_append_data(string_t *str, const void *data, size_t len)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch buffer_append(str, data, len);
d45ab3fff7c47f1719b9cd310228c0dac2bdd1b2Timo Sirainen}
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline void str_append_c(string_t *str, unsigned char chr)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch buffer_append_c(str, chr);
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch}
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschstatic inline void str_append_str(string_t *dest, const string_t *src)
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Bosch{
069def4dc35022852d569b7ab75a3b19d2cb0f1cTimo Sirainen buffer_append(dest, src->data, src->used);
069def4dc35022852d569b7ab75a3b19d2cb0f1cTimo Sirainen}
c911297407bdcc7c7936305eb8f791b450d6375cTimo Sirainen
c911297407bdcc7c7936305eb8f791b450d6375cTimo Sirainen/* Append printf()-like data */
5394bed8aaef2a6c1c870a34a23a7824e1f370bbStephan Boschvoid str_printfa(string_t *str, const char *fmt, ...)
ATTR_FORMAT(2, 3);
void str_vprintfa(string_t *str, const char *fmt, va_list args)
ATTR_FORMAT(2, 0);
static inline void str_insert(string_t *str, size_t pos, const char *cstr)
{
buffer_insert(str, pos, cstr, strlen(cstr));
}
static inline void str_delete(string_t *str, size_t pos, size_t len)
{
buffer_delete(str, pos, len);
}
static inline void str_truncate(string_t *str, size_t len)
{
buffer_set_used_size(str, len);
}
#endif