Browse Source

+ f2b_buf_splice()

master
Alex 'AdUser' Z 4 years ago
parent
commit
9cff184288
  1. 21
      src/buf.c
  2. 1
      src/buf.h
  3. 20
      t/t_buf.c

21
src/buf.c

@ -40,7 +40,7 @@ f2b_buf_append(f2b_buf_t *buf, const char *str, size_t len) {
}
/**
* @brief Extracts line terminated by '\r', '\n'
* @brief Extracts line terminated by delimiter
* @return Pointer to extracted string on success or NULL otherwise
* @note Use only with 'read' buffer type
*/
@ -65,10 +65,23 @@ f2b_buf_extract(f2b_buf_t *buf, const char *end) {
/* shift data inside buffer */
len += strlen(end);
assert(buf->used >= len);
f2b_buf_splice(buf, len);
return s;
}
size_t
f2b_buf_splice(f2b_buf_t *buf, size_t len) {
assert(buf != NULL);
if (len == 0)
return len;
if (buf->used <= len)
len = buf->used;
buf->used -= len,
memmove(buf->data, &buf->data[len], buf->used);
buf->data[buf->used] = '\0';
return s;
return len;
}

1
src/buf.h

@ -11,5 +11,6 @@ bool f2b_buf_alloc(f2b_buf_t *buf, size_t max);
void f2b_buf_free(f2b_buf_t *buf);
bool f2b_buf_append(f2b_buf_t *buf, const char *str, size_t size);
char * f2b_buf_extract(f2b_buf_t *buf, const char *end);
size_t f2b_buf_splice(f2b_buf_t *buf, size_t len);
#endif /* F2B_BUF_H_ */

20
t/t_buf.c

@ -5,6 +5,7 @@ int main() {
f2b_buf_t buf;
char *line;
bool ret;
int len;
memset(&buf, 0x0, sizeof(buf));
@ -39,6 +40,25 @@ int main() {
assert(buf.used == 0);
free(line);
f2b_buf_append(&buf, "test4\n\n", 6);
assert(buf.used == 6);
assert(strcmp(buf.data, "test4\n") == 0);
len = f2b_buf_splice(&buf, 0);
assert(len == 0);
assert(buf.used == 6);
assert(strcmp(buf.data, "test4\n") == 0);
len = f2b_buf_splice(&buf, 2);
assert(len == 2);
assert(buf.used == 4);
assert(strcmp(buf.data, "st4\n") == 0);
len = f2b_buf_splice(&buf, 6);
assert(len == 4);
assert(buf.used == 0);
assert(buf.data[0] == '\0');
f2b_buf_free(&buf);
assert(buf.used == 0);
assert(buf.size == 0);

Loading…
Cancel
Save