/**
* MIT License (MIT)
*
* Copyright (c) 2015 Zhiyuan Wang
*/
#ifndef _STRING_BUFFER_H_
#define _STRING_BUFFER_H_
/**
* A buffer of C string.
*/
typedef struct StringBuffer StringBuffer;
/**
* Create a new buffer initialized to zero, return NULL if fail.
*/
extern StringBuffer *string_buffer_new();
/**
* Delete a StringBuffer.
*/
extern void string_buffer_delete(StringBuffer *self);
/**
* Get length of data in the buffer.
*/
extern size_t string_buffer_length(StringBuffer *self);
/**
* Get capacity of the buffer.
*/
extern size_t string_buffer_capacity(StringBuffer *self);
/**
* Get the string.
*/
extern char *string_buffer_get_string(StringBuffer *self);
/**
* Clear all data in the buffer.
*/
extern void string_buffer_clear(StringBuffer *self);
/**
* Add string to the buffer.
* Return length of str on success, -1 on failure.
*/
extern int string_buffer_append(StringBuffer *self, const char *str);
/**
* Add the first n bytes of str to the buffer.
* Return n on success, -1 on failure.
*/
extern int string_buffer_append_n(StringBuffer *self, const char *str, size_t n);
/**
* Add data to the buffer, the format specifier is the same with printf.
* Return value is the number of characters that has been written to the buffer,
* not counting the terminating null character.
*/
extern int string_buffer_appendf(StringBuffer *self, const char *format, ...);
/**
* Sets the n bytes of the tail of buffer to the specified value.
* Return n on sucess, -1 on failure.
*/
extern int string_buffer_append_set(StringBuffer *self, int value, size_t n);
#endif
/**
* MIT License (MIT)
*
* Copyright (c) 2015 Zhiyuan Wang
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "string_buffer.h"
#define STEP_LENGTH 512
#define EXPAND_BUFFER(self, n) \
if (expand_buffer(self, (n))) { \
return -1; \
} \
//结构体
struct StringBuffer {
size_t length;
size_t capacity;
char *buf;
};
//自动扩大容量
static
int expand_buffer(StringBuffer *self, size_t min)
{
char *new_buf = NULL;
size_t new_capacity = 0;
min = min + 1 - (self->capacity - self->length);
if (min <= 0) return 0;
new_capacity = self->capacity + (min / STEP_LENGTH + 1) * STEP_LENGTH;
new_buf = realloc(self->buf, new_capacity);
if (new_buf != NULL) {
self->buf = new_buf;
memset(self->buf + self->capacity, 0, new_capacity - self->capacity);
self->capacity = new_capacity;
return 0;
} else {
return -1;
}
}
//申请内存空间
StringBuffer *string_buffer_new()
{
StringBuffer *self = NULL;
self = calloc(1, sizeof(*self));
if (self != NULL) {
self->buf = calloc(1, STEP_LENGTH);
if (self->buf != NULL) {
self->capacity = STEP_LENGTH;
} else {
free(self);
self = NULL;
}
}
return self;
}
//释放内存空间
void string_buffer_delete(StringBuffer *self)
{
if (self) {
if (self->buf) {
free(self->buf);
}
free(self);
}
}
//获取实际占用长度
size_t string_buffer_length(StringBuffer *self)
{
return self ? self->length : 0;
}
//获取容量大小
size_t string_buffer_capacity(StringBuffer *self)
{
return self ? (self->capacity - 1) : 0;
}
//获取原始数据
char *string_buffer_get_string(StringBuffer *self)
{
return self ? self->buf : NULL;
}
//清空数据
void string_buffer_clear(StringBuffer *self)
{
if (self != NULL) {
memset(self->buf, 0, self->length);
self->length = 0;
}
}
//追加字符串
int string_buffer_append(StringBuffer *self, const char *str)
{
return string_buffer_append_n(self, str, strlen(str));
}
//追加限定长度字符串
int string_buffer_append_n(StringBuffer *self, const char *str, size_t n)
{
if (self == NULL) return -1;
EXPAND_BUFFER(self, n);
memcpy(self->buf + self->length, str, n);
self->length += n;
self->buf[self->length] = '\0';
return n;
}
//追加格式化字符串数据。
int string_buffer_appendf(StringBuffer *self, const char *format, ...)
{
size_t n = 0;
int ret = 0;
va_list args;
if (self == NULL) return -1;
while (1) {
n = self->capacity - self->length;
va_start(args, format);
ret = vsnprintf(self->buf + self->length, n, format, args);
va_end(args);
if (ret >= 0 && ret < n) {
self->length += ret;
return ret;
} else if (ret < 0) {
// error
return ret;
} else {
// no enough space in buffer
if (expand_buffer(self, ret - n)) {
// expand failed
return -1;
}
}
}
return ret;
}
//追加num个value
int string_buffer_append_set(StringBuffer *self, int value, size_t num)
{
if (self == NULL) return -1;
EXPAND_BUFFER(self, num);
memset(self->buf + self->length, value, num);
self->length += num;
self->buf[self->length] = '\0';
return num;
}
/**
* MIT License (MIT)
*
* Copyright (c) 2015 Zhiyuan Wang
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "string_buffer.h"
static
void test_new_delete()
{
StringBuffer *sb = string_buffer_new();
string_buffer_delete(sb);
string_buffer_delete(NULL);
}
static
void test_append()
{
int ret;
StringBuffer *sb;
sb = string_buffer_new();
ret = string_buffer_append(sb, "123456");
assert(ret == 6);
ret = string_buffer_append(sb, "123456");
assert(string_buffer_length(sb) == 12);
assert(strcmp(string_buffer_get_string(sb), "123456123456") == 0);
string_buffer_delete(sb);
}
static
void test_append_n()
{
int ret;
StringBuffer *sb;
sb = string_buffer_new();
ret = string_buffer_append_n(sb, "123456", 4);
assert(ret == 4);
string_buffer_append_n(sb, "56999", 2);
assert(string_buffer_length(sb) == 6);
assert(strcmp(string_buffer_get_string(sb), "123456") == 0);
string_buffer_delete(sb);
}
static
void test_appendf()
{
int ret;
StringBuffer *sb;
sb = string_buffer_new();
ret = string_buffer_appendf(sb, "%05d", 1);
assert(ret == 5);
assert(strcmp(string_buffer_get_string(sb), "00001") == 0);
string_buffer_appendf(sb, "%s", "hello");
assert(string_buffer_length(sb) == 10);
assert(strcmp(string_buffer_get_string(sb), "00001hello") == 0);
string_buffer_delete(sb);
}
static
void test_append_set()
{
int ret;
StringBuffer *sb;
sb = string_buffer_new();
ret = string_buffer_append_set(sb, ' ', 5);
assert(ret == 5);
assert(string_buffer_length(sb) == 5);
string_buffer_append_set(sb, 'a', 5);
assert(string_buffer_length(sb) == 10);
assert(strcmp(string_buffer_get_string(sb), " aaaaa") == 0);
string_buffer_delete(sb);
}
static
void test_clear()
{
StringBuffer *sb;
sb = string_buffer_new();
string_buffer_append(sb, "123456");
string_buffer_clear(sb);
assert(string_buffer_length(sb) == 0);
assert(strcmp(string_buffer_get_string(sb), "") == 0);
string_buffer_delete(sb);
}
static
void test_buffer()
{
int ret = 0;
char data[10240 + 1];
int i=0;
StringBuffer *sb = NULL;
sb = string_buffer_new();
for (i = 0; i < sizeof(data); ++i) {
memset(data, '1', i);
data[i] = '\0';
ret = string_buffer_append(sb, data);
assert(string_buffer_length(sb) == i);
assert(strcmp(string_buffer_get_string(sb), data) == 0);
assert(string_buffer_capacity(sb) == (i / 512 + 1) * 512 - 1);
string_buffer_clear(sb);
}
string_buffer_delete(sb);
}
int main(int argc, char const *argv[])
{
test_new_delete();
test_append();
test_append_n();
test_appendf();
test_append_set();
test_clear();
test_buffer();
printf("test ok\n");
return 0;
}
https://github.com/wzyplus/StringBuffer