Skip to content

Commit

Permalink
Add xstr_s and xstr_n
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Jan 2, 2025
1 parent 387afdd commit 36ef3cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ Usage example:
```c
// Assume that hm->uri holds /foo/bar. Then we can match the requested URI:
struct xstr caps[3]; // Two wildcard symbols '*' plus 1
struct xstr str = { "/hello/world", 12 };
struct xstr pattern = { "/*/*", 4 };
struct xstr str = xstr_s("/hello/world");
struct xstr pattern = xstr_s{ "/*/*");
if (xmatch(str, pattern, caps)) {
// caps[0] holds `hello`, caps[1] holds `world`.
}
Expand Down
17 changes: 16 additions & 1 deletion str.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ size_t fmt_b64(void (*fn)(char, void *), void *arg, va_list *ap);
size_t fmt_esc(void (*fn)(char, void *), void *arg, va_list *ap);

// Utility functions
struct xstr { char *buf; size_t len; };
struct xstr {
char *buf;
size_t len;
};
struct xstr xstr_n(const char *s, size_t n);
struct xstr xstr_s(const char *s);
bool xmatch(struct xstr s, struct xstr p, struct xstr *caps);
void xhexdump(void (*fn)(char, void *), void *arg, const void *buf, size_t len);
size_t xb64_decode(const char *src, size_t slen, char *dst, size_t dlen);
Expand Down Expand Up @@ -743,6 +748,16 @@ void xhexdump(void (*fn)(char, void *), void *a, const void *buf, size_t len) {
fn('\n', a);
}

struct xstr xstr_n(const char *s, size_t n) {
struct xstr str = {(char *) s, n};
return str;
}

struct xstr xstr_s(const char *s) {
struct xstr str = {(char *) s, strlen(s)};
return str;
}

bool xmatch(struct xstr s, struct xstr p, struct xstr *caps) {
size_t i = 0, j = 0, ni = 0, nj = 0;
if (caps) caps->buf = NULL, caps->len = 0;
Expand Down
20 changes: 5 additions & 15 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,14 @@ static void test_base64(void) {
assert(strcmp(b, "hi") == 0);
}

static struct xstr xstr_n(const char *s, size_t n) {
struct xstr str = {(char *) s, n};
return str;
}

static struct xstr xstr(const char *s) {
struct xstr str = {(char *) s, strlen(s)};
return str;
}

static void test_xmatch(void) {
struct xstr caps[3];
assert(xmatch(xstr_n("", 0), xstr_n("", 0), NULL) == true);
assert(xmatch(xstr("//a.c"), xstr("#.c"), NULL) == true);
assert(xmatch(xstr("a"), xstr("#"), caps) == true);
assert(xmatch(xstr("//a.c"), xstr("#.c"), caps) == true);
assert(xmatch(xstr("a_b_c_"), xstr("a*b*c"), caps) == false);
assert(xmatch(xstr("a__b_c"), xstr("a*b*c"), caps) == true);
assert(xmatch(xstr_s("//a.c"), xstr_s("#.c"), NULL) == true);
assert(xmatch(xstr_s("a"), xstr_s("#"), caps) == true);
assert(xmatch(xstr_s("//a.c"), xstr_s("#.c"), caps) == true);
assert(xmatch(xstr_s("a_b_c_"), xstr_s("a*b*c"), caps) == false);
assert(xmatch(xstr_s("a__b_c"), xstr_s("a*b*c"), caps) == true);
}

int main(void) {
Expand Down

0 comments on commit 36ef3cb

Please sign in to comment.