mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 06:39:39 +00:00
Add string buffer util
This will help to build strings incrementally.
This commit is contained in:
parent
48fcfa96ab
commit
a41b39b23b
4 changed files with 161 additions and 0 deletions
|
@ -27,6 +27,7 @@ src = [
|
||||||
'src/util/log.c',
|
'src/util/log.c',
|
||||||
'src/util/net.c',
|
'src/util/net.c',
|
||||||
'src/util/process.c',
|
'src/util/process.c',
|
||||||
|
'src/util/strbuf.c',
|
||||||
'src/util/str_util.c',
|
'src/util/str_util.c',
|
||||||
'src/util/thread.c',
|
'src/util/thread.c',
|
||||||
'src/util/tick.c',
|
'src/util/tick.c',
|
||||||
|
@ -204,6 +205,10 @@ if get_option('buildtype') == 'debug'
|
||||||
['test_queue', [
|
['test_queue', [
|
||||||
'tests/test_queue.c',
|
'tests/test_queue.c',
|
||||||
]],
|
]],
|
||||||
|
['test_strbuf', [
|
||||||
|
'tests/test_strbuf.c',
|
||||||
|
'src/util/strbuf.c',
|
||||||
|
]],
|
||||||
['test_strutil', [
|
['test_strutil', [
|
||||||
'tests/test_strutil.c',
|
'tests/test_strutil.c',
|
||||||
'src/util/str_util.c',
|
'src/util/str_util.c',
|
||||||
|
|
77
app/src/util/strbuf.c
Normal file
77
app/src/util/strbuf.c
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
#include "strbuf.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
||||||
|
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
||||||
|
if (!buf->s) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf->len = 0;
|
||||||
|
buf->cap = init_cap;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
|
||||||
|
if (buf->len + len > buf->cap) {
|
||||||
|
fprintf(stderr, "realloc\n");
|
||||||
|
size_t new_cap = buf->cap * 3 / 2 + len;
|
||||||
|
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
||||||
|
if (!s) {
|
||||||
|
// Leave the old buf->s
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
buf->s = s;
|
||||||
|
buf->cap = new_cap;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len) {
|
||||||
|
assert(s);
|
||||||
|
assert(*s);
|
||||||
|
assert(strlen(s) >= len);
|
||||||
|
if (!sc_strbuf_reserve(buf, len)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(&buf->s[buf->len], s, len);
|
||||||
|
buf->len += len;
|
||||||
|
buf->s[buf->len] = '\0';
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append_char(struct sc_strbuf *buf, const char c) {
|
||||||
|
if (!sc_strbuf_reserve(buf, 1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf->s[buf->len] = c;
|
||||||
|
buf->len ++;
|
||||||
|
buf->s[buf->len] = '\0';
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n) {
|
||||||
|
if (!sc_strbuf_reserve(buf, n)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&buf->s[buf->len], c, n);
|
||||||
|
buf->len += n;
|
||||||
|
buf->s[buf->len] = '\0';
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
37
app/src/util/strbuf.h
Normal file
37
app/src/util/strbuf.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#ifndef SC_STRBUF_H
|
||||||
|
#define SC_STRBUF_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct sc_strbuf {
|
||||||
|
char *s;
|
||||||
|
size_t len;
|
||||||
|
size_t cap;
|
||||||
|
};
|
||||||
|
|
||||||
|
// buf->s must be manually freed by the caller
|
||||||
|
bool
|
||||||
|
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append(struct sc_strbuf *buf, const char *s, size_t len);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append_char(struct sc_strbuf *buf, const char c);
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_strbuf_append_n(struct sc_strbuf *buf, const char c, size_t n);
|
||||||
|
|
||||||
|
static inline bool
|
||||||
|
sc_strbuf_append_str(struct sc_strbuf *buf, const char *s) {
|
||||||
|
return sc_strbuf_append(buf, s, strlen(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append static string (i.e. the string size is known at compile time, for
|
||||||
|
// example a string literal)
|
||||||
|
#define sc_strbuf_append_staticstr(BUF, S) \
|
||||||
|
sc_strbuf_append(BUF, S, sizeof(S) - 1)
|
||||||
|
|
||||||
|
#endif
|
42
app/tests/test_strbuf.c
Normal file
42
app/tests/test_strbuf.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "util/strbuf.h"
|
||||||
|
|
||||||
|
static void test_strbuf_simple(void) {
|
||||||
|
struct sc_strbuf buf;
|
||||||
|
bool ok = sc_strbuf_init(&buf, 10);
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_staticstr(&buf, "Hello");
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_char(&buf, ' ');
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_staticstr(&buf, "world");
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_staticstr(&buf, "!\n");
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_staticstr(&buf, "This is a test");
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
ok = sc_strbuf_append_n(&buf, '.', 3);
|
||||||
|
assert(ok);
|
||||||
|
|
||||||
|
assert(!strcmp(buf.s, "Hello world!\nThis is a test..."));
|
||||||
|
free(buf.s);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
(void) argc;
|
||||||
|
(void) argv;
|
||||||
|
|
||||||
|
test_strbuf_simple();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue