Browse Source

[core] mark select buffer.[ch] funcs attr nonnull

master
Glenn Strauss 8 months ago
parent
commit
cfc8eeb5a7
  1. 18
      src/buffer.c
  2. 39
      src/buffer.h

18
src/buffer.c

@ -55,9 +55,6 @@ void buffer_free_ptr(buffer *b) {
void buffer_move(buffer * restrict b, buffer * restrict src) {
buffer tmp;
force_assert(NULL != b);
force_assert(NULL != src);
buffer_clear(b);
tmp = *src; *src = *b; *b = tmp;
}
@ -65,6 +62,7 @@ void buffer_move(buffer * restrict b, buffer * restrict src) {
/* make sure buffer is at least "size" big + 1 for '\0'. keep old data */
__attribute_cold__
__attribute_noinline__
__attribute_nonnull__
__attribute_returns_nonnull__
static char* buffer_realloc(buffer * const restrict b, const size_t len) {
#define BUFFER_PIECE_SIZE 64uL /*(must be power-of-2)*/
@ -80,6 +78,7 @@ static char* buffer_realloc(buffer * const restrict b, const size_t len) {
__attribute_cold__
__attribute_noinline__
__attribute_nonnull__
__attribute_returns_nonnull__
static char* buffer_alloc_replace(buffer * const restrict b, const size_t size) {
/*(discard old data so realloc() does not copy)*/
@ -100,6 +99,7 @@ char* buffer_string_prepare_copy(buffer * const b, const size_t size) {
__attribute_cold__
__attribute_noinline__
__attribute_nonnull__
__attribute_returns_nonnull__
static char* buffer_string_prepare_append_resize(buffer * const restrict b, const size_t size) {
if (b->used < 2) /* buffer_string_is_empty(b) */
@ -153,7 +153,6 @@ void buffer_string_set_length(buffer *b, uint32_t len) {
void buffer_commit(buffer *b, size_t size)
{
force_assert(NULL != b);
force_assert(b->size > 0);
if (0 == b->used) b->used = 1;
@ -279,6 +278,7 @@ void buffer_append_uint_hex_lc(buffer *b, uintmax_t value) {
}
}
__attribute_nonnull__
__attribute_returns_nonnull__
static char* utostr(char * const buf_end, uintmax_t val) {
char *cur = buf_end;
@ -291,6 +291,7 @@ static char* utostr(char * const buf_end, uintmax_t val) {
return cur;
}
__attribute_nonnull__
__attribute_returns_nonnull__
static char* itostr(char * const buf_end, intmax_t val) {
/* absolute value not defined for INTMAX_MIN, but can take absolute
@ -309,8 +310,6 @@ void buffer_append_int(buffer *b, intmax_t val) {
char* const buf_end = buf + sizeof(buf);
char *str;
force_assert(NULL != b);
str = itostr(buf_end, val);
force_assert(buf_end > str && str >= buf);
@ -411,14 +410,11 @@ int buffer_eq_slen(const buffer * const b, const char * const s, const size_t sl
*/
int buffer_is_equal(const buffer *a, const buffer *b) {
force_assert(NULL != a && NULL != b);
/* 1 = equal; 0 = not equal */
return (a->used == b->used && 0 == memcmp(a->ptr, b->ptr, a->used));
}
int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len) {
force_assert(NULL != a && NULL != s);
force_assert(b_len + 1 > b_len);
/* 1 = equal; 0 = not equal */
@ -673,8 +669,6 @@ void buffer_append_string_c_escaped(buffer * const restrict b, const char * cons
if (0 == s_len) return;
force_assert(NULL != s);
/* count to-be-encoded-characters */
for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
if ((*ds < 0x20) /* control character */
@ -813,8 +807,6 @@ void buffer_path_simplify(buffer *dest, buffer *src)
char c, pre1, pre2;
char *start, *slash, *walk, *out;
force_assert(NULL != dest && NULL != src);
if (buffer_string_is_empty(src)) {
buffer_copy_string_len(dest, CONST_STR_LEN(""));
return;

39
src/buffer.h

@ -43,6 +43,7 @@ buffer* buffer_init_string(const char *str); /* str can be NULL */
void buffer_free(buffer *b); /* b can be NULL */
/* reset b. if NULL != b && NULL != src, move src content to b. reset src. */
__attribute_nonnull__
void buffer_move(buffer * restrict b, buffer * restrict src);
/* make sure buffer is large enough to store a string of given size
@ -50,6 +51,7 @@ void buffer_move(buffer * restrict b, buffer * restrict src);
* sets b to an empty string, and may drop old content.
* @return b->ptr
*/
__attribute_nonnull__
__attribute_returns_nonnull__
char* buffer_string_prepare_copy(buffer *b, size_t size);
@ -59,6 +61,7 @@ char* buffer_string_prepare_copy(buffer *b, size_t size);
* "used" data is preserved; if not empty buffer must contain a
* zero terminated string.
*/
__attribute_nonnull__
__attribute_returns_nonnull__
char* buffer_string_prepare_append(buffer *b, size_t size);
@ -66,6 +69,7 @@ char* buffer_string_prepare_append(buffer *b, size_t size);
* buffer_string_prepare_append() which only ensures space is available)
* returns pointer to which callers should immediately write x bytes
*/
__attribute_nonnull__
__attribute_returns_nonnull__
char* buffer_extend(buffer * const restrict b, size_t x);
@ -74,6 +78,7 @@ char* buffer_extend(buffer * const restrict b, size_t x);
* requires enough space is present for the terminating zero (prepare with the
* same size to be sure).
*/
__attribute_nonnull__
void buffer_commit(buffer *b, size_t size);
/* sets string length:
@ -81,6 +86,7 @@ void buffer_commit(buffer *b, size_t size);
* - does not modify the string data apart from terminating zero
* - reallocates the buffer iff needed
*/
__attribute_nonnull__
void buffer_string_set_length(buffer *b, uint32_t len);
/* clear buffer
@ -88,6 +94,7 @@ void buffer_string_set_length(buffer *b, uint32_t len);
* - unsets used chars but does not modify existing ptr contents
* (b->ptr *is not* set to an empty, '\0'-terminated string "")
*/
__attribute_nonnull__
static inline void buffer_clear(buffer *b);
/* reset buffer
@ -97,12 +104,14 @@ static inline void buffer_clear(buffer *b);
* (b->ptr *is not* set to an empty, '\0'-terminated string "")
* - frees larger buffer (b->size > BUFFER_MAX_REUSE_SIZE)
*/
__attribute_nonnull__
static inline void buffer_reset(buffer *b);
/* free buffer ptr
* - invalidate buffer contents; free ptr; reset ptr, used, size to 0
*/
__attribute_cold__
__attribute_nonnull__
void buffer_free_ptr(buffer *b);
void buffer_copy_string(buffer * restrict b, const char * restrict s);
@ -120,10 +129,13 @@ struct const_iovec {
size_t iov_len;
};
__attribute_nonnull__
void buffer_append_iovec(buffer * restrict b, const struct const_iovec *iov, size_t n);
#define buffer_append_uint_hex(b,len) buffer_append_uint_hex_lc((b),(len))
__attribute_nonnull__
void buffer_append_uint_hex_lc(buffer *b, uintmax_t len);
__attribute_nonnull__
void buffer_append_int(buffer *b, intmax_t val);
void buffer_append_strftime(buffer * restrict b, const char * restrict format, const struct tm * restrict tm);
@ -131,12 +143,16 @@ void buffer_append_strftime(buffer * restrict b, const char * restrict format, c
/* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */
#define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100)
__attribute_nonnull__
size_t li_itostrn(char *buf, size_t buf_len, intmax_t val);
__attribute_nonnull__
size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val);
/* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */
#define li_tohex(buf,buf_len,s,s_len) li_tohex_lc((buf),(buf_len),(s),(s_len))
__attribute_nonnull__
void li_tohex_lc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
__attribute_nonnull__
void li_tohex_uc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
/* NULL buffer or empty buffer (used == 0);
@ -149,31 +165,41 @@ static inline int buffer_is_empty(const buffer *b);
__attribute_pure__
static inline int buffer_string_is_empty(const buffer *b);
__attribute_nonnull__
__attribute_pure__
int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len);
__attribute_nonnull__
__attribute_pure__
int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen);
__attribute_nonnull__
__attribute_pure__
int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen);
#define buffer_is_equal_caseless_string buffer_eq_icase_slen
__attribute_nonnull__
__attribute_pure__
int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen);
__attribute_nonnull__
__attribute_pure__
int buffer_is_equal(const buffer *a, const buffer *b);
__attribute_nonnull__
__attribute_pure__
int buffer_is_equal_right_len(const buffer *a, const buffer *b, size_t len);
__attribute_nonnull__
__attribute_pure__
int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len);
__attribute_nonnull__
void buffer_substr_replace (buffer * restrict b, size_t offset, size_t len, const buffer * restrict replace);
__attribute_nonnull__
void buffer_append_string_encoded_hex_lc(buffer * restrict b, const char * restrict s, size_t len);
__attribute_nonnull__
void buffer_append_string_encoded_hex_uc(buffer * restrict b, const char * restrict s, size_t len);
typedef enum {
@ -186,16 +212,22 @@ typedef enum {
void buffer_append_string_encoded(buffer * restrict b, const char * restrict s, size_t s_len, buffer_encoding_t encoding);
/* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */
__attribute_nonnull__
void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len);
__attribute_nonnull__
void buffer_urldecode_path(buffer *b);
__attribute_nonnull__
__attribute_pure__
int buffer_is_valid_UTF8(const buffer *b);
__attribute_nonnull__
void buffer_path_simplify(buffer *dest, buffer *src);
__attribute_nonnull__
void buffer_to_lower(buffer *b);
__attribute_nonnull__
void buffer_to_upper(buffer *b);
@ -239,15 +271,20 @@ static inline int light_isalnum(int c) {
__attribute_pure__
static inline uint32_t buffer_string_length(const buffer *b); /* buffer string length without terminating 0 */
__attribute_nonnull__
__attribute_pure__
static inline uint32_t buffer_string_space(const buffer *b); /* maximum length of string that can be stored without reallocating */
__attribute_nonnull__
static inline void buffer_append_slash(buffer *b); /* append '/' no non-empty strings not ending in '/' */
void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen); /* join strings with '/', if '/' not present */
__attribute_nonnull__
__attribute_pure__
static inline int buffer_has_slash_suffix (const buffer * const b);
__attribute_nonnull__
__attribute_pure__
static inline int buffer_has_pathsep_suffix (const buffer * const b);
@ -285,7 +322,7 @@ static inline uint32_t buffer_string_length(const buffer *b) {
}
static inline uint32_t buffer_string_space(const buffer *b) {
return NULL != b && b->size ? b->size - (b->used | (0 == b->used)) : 0;
return b->size ? b->size - (b->used | (0 == b->used)) : 0;
}
static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src) {

Loading…
Cancel
Save