adding mg_split function

This commit is contained in:
Hooman Behnejad 2022-05-31 14:11:14 +04:30
parent e2fcf5f92c
commit 499401d457
4 changed files with 18 additions and 10 deletions

View File

@ -4766,9 +4766,9 @@ bool mg_globmatch(const char *s1, size_t n1, const char *s2, size_t n2) {
} }
static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff, static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
size_t *klen, size_t *voff, size_t *vlen) { size_t *klen, size_t *voff, size_t *vlen, char delim) {
size_t kvlen, kl; size_t kvlen, kl;
for (kvlen = 0; ofs + kvlen < n && s[ofs + kvlen] != ',';) kvlen++; for (kvlen = 0; ofs + kvlen < n && s[ofs + kvlen] != delim;) kvlen++;
for (kl = 0; kl < kvlen && s[ofs + kl] != '=';) kl++; for (kl = 0; kl < kvlen && s[ofs + kl] != '=';) kl++;
if (koff != NULL) *koff = ofs; if (koff != NULL) *koff = ofs;
if (klen != NULL) *klen = kl; if (klen != NULL) *klen = kl;
@ -4778,16 +4778,20 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
return ofs > n ? n : ofs; return ofs > n ? n : ofs;
} }
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) { bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim) {
size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0; size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0;
if (s->ptr == NULL || s->len == 0) return 0; if (s->ptr == NULL || s->len == 0) return 0;
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen); off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, delim);
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen); if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen); if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen);
*s = mg_str_n(s->ptr + off, s->len - off); *s = mg_str_n(s->ptr + off, s->len - off);
return off > 0; return off > 0;
} }
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
return mg_split(s, k, v, ',');
}
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
va_list ap; va_list ap;
size_t n; size_t n;

View File

@ -720,7 +720,7 @@ const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps); bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n); bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap); size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...) PRINTF_LIKE(3, 4); size_t mg_snprintf(char *, size_t, const char *fmt, ...) PRINTF_LIKE(3, 4);
char *mg_hex(const void *buf, size_t len, char *dst); char *mg_hex(const void *buf, size_t len, char *dst);

View File

@ -134,9 +134,9 @@ bool mg_globmatch(const char *s1, size_t n1, const char *s2, size_t n2) {
} }
static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff, static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
size_t *klen, size_t *voff, size_t *vlen) { size_t *klen, size_t *voff, size_t *vlen, char delim) {
size_t kvlen, kl; size_t kvlen, kl;
for (kvlen = 0; ofs + kvlen < n && s[ofs + kvlen] != ',';) kvlen++; for (kvlen = 0; ofs + kvlen < n && s[ofs + kvlen] != delim;) kvlen++;
for (kl = 0; kl < kvlen && s[ofs + kl] != '=';) kl++; for (kl = 0; kl < kvlen && s[ofs + kl] != '=';) kl++;
if (koff != NULL) *koff = ofs; if (koff != NULL) *koff = ofs;
if (klen != NULL) *klen = kl; if (klen != NULL) *klen = kl;
@ -146,16 +146,20 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
return ofs > n ? n : ofs; return ofs > n ? n : ofs;
} }
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) { bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim) {
size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0; size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0;
if (s->ptr == NULL || s->len == 0) return 0; if (s->ptr == NULL || s->len == 0) return 0;
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen); off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, delim);
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen); if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen); if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen);
*s = mg_str_n(s->ptr + off, s->len - off); *s = mg_str_n(s->ptr + off, s->len - off);
return off > 0; return off > 0;
} }
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
return mg_split(s, k, v, ',');
}
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) { size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
va_list ap; va_list ap;
size_t n; size_t n;

View File

@ -30,7 +30,7 @@ const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps); bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n); bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap); size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...) PRINTF_LIKE(3, 4); size_t mg_snprintf(char *, size_t, const char *fmt, ...) PRINTF_LIKE(3, 4);
char *mg_hex(const void *buf, size_t len, char *dst); char *mg_hex(const void *buf, size_t len, char *dst);