Skip to content

Commit f34d4f9

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
Add mg_strcasecmp()
CL: Add mg_strcasecmp() PUBLISHED_FROM=cd2a26fa12473bfa0f5e7a0a1d34fb86562ee082
1 parent 2379272 commit f34d4f9

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

common/mg_str.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK;
9696
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
9797
size_t i = 0;
9898
while (i < str1.len && i < str2.len) {
99-
if (str1.p[i] < str2.p[i]) return -1;
100-
if (str1.p[i] > str2.p[i]) return 1;
99+
int c1 = str1.p[i];
100+
int c2 = str2.p[i];
101+
if (c1 < c2) return -1;
102+
if (c1 > c2) return 1;
101103
i++;
102104
}
103105
if (i < str1.len) return 1;
@@ -119,6 +121,21 @@ int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n) {
119121
return mg_strcmp(s1, s2);
120122
}
121123

124+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) WEAK;
125+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) {
126+
size_t i = 0;
127+
while (i < str1.len && i < str2.len) {
128+
int c1 = tolower((int) str1.p[i]);
129+
int c2 = tolower((int) str2.p[i]);
130+
if (c1 < c2) return -1;
131+
if (c1 > c2) return 1;
132+
i++;
133+
}
134+
if (i < str1.len) return 1;
135+
if (i < str2.len) return -1;
136+
return 0;
137+
}
138+
122139
void mg_strfree(struct mg_str *s) WEAK;
123140
void mg_strfree(struct mg_str *s) {
124141
char *sp = (char *) s->p;

common/mg_str.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
8585
*/
8686
int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
8787

88+
/*
89+
* Compare two `mg_str`s ignoreing case; return value is the same as `strcmp`.
90+
*/
91+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
92+
8893
/*
8994
* Free the string (assuming it was heap allocated).
9095
*/

mjs.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,11 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
20932093
*/
20942094
int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
20952095

2096+
/*
2097+
* Compare two `mg_str`s ignoreing case; return value is the same as `strcmp`.
2098+
*/
2099+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
2100+
20962101
/*
20972102
* Free the string (assuming it was heap allocated).
20982103
*/
@@ -5639,8 +5644,10 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK;
56395644
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
56405645
size_t i = 0;
56415646
while (i < str1.len && i < str2.len) {
5642-
if (str1.p[i] < str2.p[i]) return -1;
5643-
if (str1.p[i] > str2.p[i]) return 1;
5647+
int c1 = str1.p[i];
5648+
int c2 = str2.p[i];
5649+
if (c1 < c2) return -1;
5650+
if (c1 > c2) return 1;
56445651
i++;
56455652
}
56465653
if (i < str1.len) return 1;
@@ -5662,6 +5669,21 @@ int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n) {
56625669
return mg_strcmp(s1, s2);
56635670
}
56645671

5672+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) WEAK;
5673+
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) {
5674+
size_t i = 0;
5675+
while (i < str1.len && i < str2.len) {
5676+
int c1 = tolower((int) str1.p[i]);
5677+
int c2 = tolower((int) str2.p[i]);
5678+
if (c1 < c2) return -1;
5679+
if (c1 > c2) return 1;
5680+
i++;
5681+
}
5682+
if (i < str1.len) return 1;
5683+
if (i < str2.len) return -1;
5684+
return 0;
5685+
}
5686+
56655687
void mg_strfree(struct mg_str *s) WEAK;
56665688
void mg_strfree(struct mg_str *s) {
56675689
char *sp = (char *) s->p;

0 commit comments

Comments
 (0)