Add mg_strcasecmp()

CL: Add mg_strcasecmp()

PUBLISHED_FROM=cd2a26fa12473bfa0f5e7a0a1d34fb86562ee082
This commit is contained in:
Deomid Ryabkov 2019-12-29 22:29:52 +03:00 committed by Cesanta Bot
parent 10b11b03a8
commit 70dc6d8dc9
2 changed files with 25 additions and 2 deletions

View File

@ -1764,8 +1764,10 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK;
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) { int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0; size_t i = 0;
while (i < str1.len && i < str2.len) { while (i < str1.len && i < str2.len) {
if (str1.p[i] < str2.p[i]) return -1; int c1 = str1.p[i];
if (str1.p[i] > str2.p[i]) return 1; int c2 = str2.p[i];
if (c1 < c2) return -1;
if (c1 > c2) return 1;
i++; i++;
} }
if (i < str1.len) return 1; if (i < str1.len) return 1;
@ -1787,6 +1789,21 @@ int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n) {
return mg_strcmp(s1, s2); return mg_strcmp(s1, s2);
} }
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) WEAK;
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
int c1 = tolower((int) str1.p[i]);
int c2 = tolower((int) str2.p[i]);
if (c1 < c2) return -1;
if (c1 > c2) return 1;
i++;
}
if (i < str1.len) return 1;
if (i < str2.len) return -1;
return 0;
}
void mg_strfree(struct mg_str *s) WEAK; void mg_strfree(struct mg_str *s) WEAK;
void mg_strfree(struct mg_str *s) { void mg_strfree(struct mg_str *s) {
char *sp = (char *) s->p; char *sp = (char *) s->p;

View File

@ -2351,6 +2351,11 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
*/ */
int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n); int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
/*
* Compare two `mg_str`s ignoreing case; return value is the same as `strcmp`.
*/
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
/* /*
* Free the string (assuming it was heap allocated). * Free the string (assuming it was heap allocated).
*/ */
@ -6239,6 +6244,7 @@ int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
* Encodes a DNS name. * Encodes a DNS name.
*/ */
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len); int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
int mg_dns_encode_name_s(struct mbuf *io, struct mg_str name);
/* Low-level: parses a DNS response. */ /* Low-level: parses a DNS response. */
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg); int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);