From 91b57aa9a869bbc2a9247735a2306538ce2ffdbe Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov <rojer@cesanta.com> Date: Mon, 29 Oct 2018 14:42:32 +0000 Subject: [PATCH] Add MG_MK_STR_N() and mg_str_starts_with() CL: Add MG_MK_STR_N() and mg_str_starts_with() PUBLISHED_FROM=f01003efc6aa6ff45fa99c7ad6e5319f5002f22d --- mongoose.c | 7 +++++++ mongoose.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/mongoose.c b/mongoose.c index 8b58d2e9d..7c4e691de 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1770,6 +1770,13 @@ struct mg_str mg_strstrip(struct mg_str s) { } return s; } + +int mg_str_starts_with(struct mg_str s, struct mg_str prefix) WEAK; +int mg_str_starts_with(struct mg_str s, struct mg_str prefix) { + const struct mg_str sp = MG_MK_STR_N(s.p, prefix.len); + if (s.len < prefix.len) return 0; + return (mg_strcmp(sp, prefix) == 0); +} #ifdef MG_MODULE_LINES #line 1 "common/str_util.c" #endif diff --git a/mongoose.h b/mongoose.h index 8fe13cd60..4b5b0db62 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2239,6 +2239,8 @@ struct mg_str mg_mk_str_n(const char *s, size_t len); /* Macro for initializing mg_str. */ #define MG_MK_STR(str_literal) \ { str_literal, sizeof(str_literal) - 1 } +#define MG_MK_STR_N(str_literal, len) \ + { str_literal, len } #define MG_NULL_STR \ { NULL, 0 } @@ -2286,6 +2288,9 @@ const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle); /* Strip whitespace at the start and the end of s */ struct mg_str mg_strstrip(struct mg_str s); +/* Returns 1 if s starts with the given prefix. */ +int mg_str_starts_with(struct mg_str s, struct mg_str prefix); + #ifdef __cplusplus } #endif -- GitLab