Skip to content

Commit

Permalink
misc: refactor and sync csnippets
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Feb 19, 2025
1 parent f72b841 commit 9a5b9a9
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 156 deletions.
30 changes: 17 additions & 13 deletions contrib/csnippets/io/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ struct memory_stream {
};
ASSERT_SUPER(struct stream, struct memory_stream, s);

static int mem_direct_read(void *p, const void **buf, size_t *restrict len)
static int
mem_direct_read(void *p, const void **restrict buf, size_t *restrict len)
{
struct memory_stream *restrict m = p;
size_t n = *len;
Expand All @@ -35,7 +36,7 @@ static int mem_direct_read(void *p, const void **buf, size_t *restrict len)
return 0;
}

static int mem_write(void *p, const void *buf, size_t *restrict len)
static int mem_write(void *p, const void *restrict buf, size_t *restrict len)
{
struct memory_stream *restrict m = p;
size_t n = *len;
Expand Down Expand Up @@ -95,7 +96,7 @@ struct stream *io_memwriter(void *buf, const size_t bufsize, size_t *nwritten)
return s;
}

static int heap_write(void *p, const void *buf, size_t *restrict len)
static int heap_write(void *p, const void *restrict buf, size_t *restrict len)
{
struct stream *restrict s = p;
struct vbuffer *restrict *restrict pvbuf = s->data;
Expand All @@ -111,7 +112,7 @@ static int heap_write(void *p, const void *buf, size_t *restrict len)
static const struct stream_vftable vftable_heapwriter = {
.write = heap_write,
};
struct stream *io_heapwriter(struct vbuffer **pvbuf)
struct stream *io_heapwriter(struct vbuffer **restrict pvbuf)
{
if (pvbuf == NULL) {
return NULL;
Expand All @@ -124,7 +125,7 @@ struct stream *io_heapwriter(struct vbuffer **pvbuf)
return s;
}

int io_heapprintf(struct stream *s, const char *format, ...)
int io_heapprintf(struct stream *restrict s, const char *restrict format, ...)
{
assert(s->vftable == &vftable_heapwriter);
struct vbuffer *restrict *restrict pvbuf = s->data;
Expand All @@ -145,7 +146,8 @@ struct buffered_stream {
};
ASSERT_SUPER(struct stream, struct buffered_stream, s);

static int buf_direct_read(void *p, const void **buf, size_t *restrict len)
static int
buf_direct_read(void *p, const void **restrict buf, size_t *restrict len)
{
struct buffered_stream *restrict b = p;
if (b->pos == b->len) {
Expand All @@ -165,7 +167,7 @@ static int buf_direct_read(void *p, const void **buf, size_t *restrict len)
return err;
}

static int buf_read(void *p, void *buf, size_t *restrict len)
static int buf_read(void *p, void *restrict buf, size_t *restrict len)
{
struct buffered_stream *restrict b = p;
size_t nread = *len;
Expand Down Expand Up @@ -243,7 +245,7 @@ static int buf_flush(void *p)
return stream_flush(b->base);
}

static int buf_write(void *p, const void *buf, size_t *restrict len)
static int buf_write(void *p, const void *restrict buf, size_t *restrict len)
{
struct buffered_stream *restrict b = p;
const unsigned char *src = buf;
Expand Down Expand Up @@ -279,7 +281,7 @@ static int buf_write(void *p, const void *buf, size_t *restrict len)
return 0;
}

static int buf_vprintf(void *p, const char *format, va_list args)
static int buf_vprintf(void *p, const char *restrict format, va_list args)
{
struct buffered_stream *restrict b = DOWNCAST(
struct stream, struct buffered_stream, s, (struct stream *)p);
Expand Down Expand Up @@ -373,7 +375,7 @@ struct stream *io_bufwriter(struct stream *base, const size_t bufsize)
return s;
}

int io_bufprintf(struct stream *s, const char *format, ...)
int io_bufprintf(struct stream *restrict s, const char *restrict format, ...)
{
assert(s->vftable == &vftable_bufwriter);
va_list args;
Expand All @@ -390,7 +392,8 @@ struct metered_stream {
};
ASSERT_SUPER(struct stream, struct metered_stream, s);

static int metered_direct_read(void *p, const void **buf, size_t *restrict len)
static int
metered_direct_read(void *p, const void **restrict buf, size_t *restrict len)
{
struct metered_stream *restrict m = p;
const int ret = stream_direct_read(m->base, buf, len);
Expand All @@ -400,7 +403,7 @@ static int metered_direct_read(void *p, const void **buf, size_t *restrict len)
return ret;
}

static int metered_read(void *p, void *buf, size_t *restrict len)
static int metered_read(void *p, void *restrict buf, size_t *restrict len)
{
struct metered_stream *restrict m = p;
const int ret = stream_read(m->base, buf, len);
Expand All @@ -410,7 +413,8 @@ static int metered_read(void *p, void *buf, size_t *restrict len)
return ret;
}

static int metered_write(void *p, const void *buf, size_t *restrict len)
static int
metered_write(void *p, const void *restrict buf, size_t *restrict len)
{
struct metered_stream *restrict m = p;
const int ret = stream_write(m->base, buf, len);
Expand Down
18 changes: 12 additions & 6 deletions contrib/csnippets/io/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

#include <stddef.h>

int stream_direct_read(struct stream *s, const void **buf, size_t *len)
int stream_direct_read(
struct stream *restrict s, const void **restrict buf,
size_t *restrict len)
{
return s->vftable->direct_read(s, buf, len);
}

int stream_read(struct stream *s, void *buf, size_t *len)
int stream_read(
struct stream *restrict s, void *restrict buf, size_t *restrict len)
{
{
const io_reader read = s->vftable->read;
Expand Down Expand Up @@ -39,12 +42,14 @@ int stream_read(struct stream *s, void *buf, size_t *len)
return err;
}

int stream_write(struct stream *s, const void *buf, size_t *len)
int stream_write(
struct stream *restrict s, const void *restrict buf,
size_t *restrict len)
{
return s->vftable->write(s, buf, len);
}

int stream_flush(struct stream *s)
int stream_flush(struct stream *restrict s)
{
const io_flusher flush = s->vftable->flush;
if (flush == NULL) {
Expand All @@ -53,7 +58,7 @@ int stream_flush(struct stream *s)
return flush(s);
}

int stream_close(struct stream *s)
int stream_close(struct stream *restrict s)
{
const io_closer close = s->vftable->close;
if (close == NULL) {
Expand All @@ -64,7 +69,8 @@ int stream_close(struct stream *s)
}

int stream_copy(
struct stream *dst, struct stream *src, void *buf, const size_t bufsize)
struct stream *restrict dst, struct stream *restrict src,
void *restrict buf, const size_t bufsize)
{
size_t len;
do {
Expand Down
2 changes: 1 addition & 1 deletion contrib/csnippets/net/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stddef.h>
#include <string.h>

bool splithostport(char *str, char **host, char **port)
bool splithostport(char *str, char **restrict host, char **restrict port)
{
char *service = strrchr(str, ':');
if (service == NULL) {
Expand Down
8 changes: 4 additions & 4 deletions contrib/csnippets/net/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static const struct {
"The gateway did not receive a timely response from the upstream server or application." },
};

static char *skip_whitespace(char *s)
static char *skip_whitespace(char *restrict s)
{
while (*s == ' ' || *s == '\t') {
++s;
Expand Down Expand Up @@ -124,12 +124,12 @@ char *http_parsehdr(char *buf, char **key, char **value)
return next;
}

size_t http_date(char *buf, const size_t buf_size)
size_t http_date(char *restrict buf, const size_t buf_size)
{
/* RFC 7231: Section 7.1.1.1 */
static const char fmt[] = "%a, %d %b %Y %H:%M:%S GMT";
const time_t now = time(NULL);
const struct tm *gmt = gmtime(&now);
const struct tm *restrict gmt = gmtime(&now);
return strftime(buf, buf_size, fmt, gmt);
}

Expand All @@ -143,7 +143,7 @@ const char *http_status(const uint_least16_t code)
return NULL;
}

int http_error(char *buf, size_t buf_size, const uint_least16_t code)
int http_error(char *restrict buf, size_t buf_size, const uint_least16_t code)
{
const char *name = NULL, *info = NULL;
for (size_t i = 0; i < ARRAY_SIZE(http_resp); i++) {
Expand Down
24 changes: 12 additions & 12 deletions contrib/csnippets/net/mime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@
#define istspecial(c) (!!strchr("()<>@,;:\"/[]?=", (c)))
#define istoken(c) (!iscntrl(c) && !istspecial(c))

static inline char *strlower(char *s)
static inline char *strlower(char *restrict s)
{
for (unsigned char *restrict p = (unsigned char *)s; *p; p++) {
for (unsigned char *p = (unsigned char *)s; *p; p++) {
*p = tolower(*p);
}
return s;
}

static inline char *strtrimleftspace(char *s)
static inline char *strtrimleftspace(char *restrict s)
{
const unsigned char *restrict p = (unsigned char *)s;
const unsigned char *p = (unsigned char *)s;
while (*p && isspace(*p)) {
p++;
}
return (char *)p;
}

static inline char *strtrimrightspace(char *s)
static inline char *strtrimrightspace(char *restrict s)
{
unsigned char *restrict e = (unsigned char *)s + strlen(s) - 1;
unsigned char *e = (unsigned char *)s + strlen(s) - 1;
while ((unsigned char *)s < e && isspace(*e)) {
*e-- = '\0';
}
return s;
}

static inline char *strtrimspace(char *s)
static inline char *strtrimspace(char *restrict s)
{
return strtrimrightspace(strtrimleftspace(s));
}

char *mime_parse(char *s, char **type, char **subtype)
char *mime_parse(char *s, char **restrict type, char **restrict subtype)
{
char *next = strchr(s, ';');
if (next == NULL) {
Expand All @@ -60,15 +60,15 @@ char *mime_parse(char *s, char **type, char **subtype)
return next;
}

static char *next_token(char *s)
static char *next_token(char *restrict s)
{
char *restrict sep;
char *sep;
for (sep = s; *sep && istoken((unsigned char)*sep); sep++) {
}
return sep;
}

static char *parse_key(char *s, char **key)
static char *parse_key(char *s, char **restrict key)
{
*key = s;
s = next_token(s);
Expand All @@ -79,7 +79,7 @@ static char *parse_key(char *s, char **key)
return s + 1;
}

static char *parse_value(char *s, char **value)
static char *parse_value(char *s, char **restrict value)
{
if (*s != '\"') {
*value = s;
Expand Down
20 changes: 11 additions & 9 deletions contrib/csnippets/net/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdint.h>
#include <string.h>

static void hex(char *p, uint_fast8_t c)
static void hex(char *restrict p, const uint_fast8_t c)
{
static const char hex[] = "0123456789ABCDEF";
p[1] = hex[c & UINT8_C(0xF)];
Expand Down Expand Up @@ -150,15 +150,15 @@ size_t url_escape_path(char *buf, size_t buf_size, const char *path)
return escape(buf, buf_size, path, SIZE_MAX, "-_.~$&+,/:;=@", false);
}

size_t url_escape_query(char *buf, size_t buf_size, const char *restrict query)
size_t url_escape_query(char *buf, size_t buf_size, const char *query)
{
const size_t cap = buf_size;
for (;;) {
const char *restrict next = strchr(query, '&');
const char *next = strchr(query, '&');
if (next == NULL) {
next = query + strlen(query);
}
const char *restrict eq = memchr(query, '=', next - query);
const char *eq = memchr(query, '=', next - query);
if (eq == NULL) {
return 0;
}
Expand Down Expand Up @@ -270,9 +270,9 @@ static bool unescape(char *str, const bool space)
return true;
}

static inline char *strlower(char *s)
static inline char *strlower(char *restrict s)
{
for (unsigned char *restrict p = (unsigned char *)s; *p != '\0'; ++p) {
for (unsigned char *p = (unsigned char *)s; *p != '\0'; ++p) {
*p = tolower(*p);
}
return s;
Expand Down Expand Up @@ -372,7 +372,7 @@ bool url_parse(char *raw, struct url *restrict url)
return true;
}

bool url_path_segment(char **path, char **segment)
bool url_path_segment(char **restrict path, char **restrict segment)
{
char *s = *path;
while (*s == '/') {
Expand All @@ -391,7 +391,8 @@ bool url_path_segment(char **path, char **segment)
return true;
}

bool url_query_component(char **query, struct url_query_component *comp)
bool url_query_component(
char **restrict query, struct url_query_component *restrict comp)
{
char *s = *query;
char *next = strchr(s, '&');
Expand Down Expand Up @@ -420,7 +421,8 @@ bool url_query_component(char **query, struct url_query_component *comp)
return true;
}

bool url_unescape_userinfo(char *raw, char **username, char **password)
bool url_unescape_userinfo(
char *raw, char **restrict username, char **restrict password)
{
const char valid_chars[] = "-._:~!$&\'()*+,;=%@'";
char *colon = NULL;
Expand Down
Loading

0 comments on commit 9a5b9a9

Please sign in to comment.