Implement case_insensitive_strcmp
This commit is contained in:
parent
5afa947d89
commit
35b88e235d
|
@ -52,22 +52,42 @@ void free_headers(struct http_header *headers) {
|
|||
free(headers);
|
||||
}
|
||||
|
||||
static char *str_lower(const char *s) {
|
||||
int length = strlen(s) + 1;
|
||||
char *lower = malloc(length * sizeof(char));
|
||||
char *lower_cursor = lower;
|
||||
for (const char *cursor = s; *cursor != '\0'; (cursor++, lower_cursor++)) {
|
||||
*lower_cursor = tolower(*cursor);
|
||||
}
|
||||
|
||||
return lower;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs strcmp on lowercase versions of a and b
|
||||
*
|
||||
* @param a A null terminated string.
|
||||
* @param b A null terminated string.
|
||||
*
|
||||
* @return strcmp(lower_a, lower_b)
|
||||
*/
|
||||
int case_insensitive_strcmp(const char *a, const char *b) {
|
||||
char *a_lower = str_lower(a);
|
||||
char *b_lower = str_lower(b);
|
||||
int cmp = strcmp(a, b);
|
||||
free(a_lower);
|
||||
free(b_lower);
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the name of a header to a given name, case insensitively
|
||||
*
|
||||
* @param name The name to compare against.
|
||||
*
|
||||
* @return The result of strcmp(lower_case_header_name, name)
|
||||
* @return The result of strcmp(lower_case_header_name, actual_header_name)
|
||||
*/
|
||||
int headercmp(const char *name) {
|
||||
int name_length = strlen(name) + 1;
|
||||
char *lower_name = malloc(name_length * sizeof(char));
|
||||
char *lower_cursor = lower_name;
|
||||
for (const char *cursor = name; *cursor != '\0'; (cursor++, lower_cursor++)) {
|
||||
*lower_cursor = tolower(*cursor);
|
||||
}
|
||||
int cmp_result = strcmp(lower_name, name);
|
||||
free(lower_name);
|
||||
|
||||
return cmp_result;
|
||||
int headercmp(struct http_header *header, const char *name) {
|
||||
return case_insensitive_strcmp(header->name, name);
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ struct http_header {
|
|||
|
||||
struct http_header *insert_header(const char *header_name, const char *header_value, struct http_header *existing_header);
|
||||
void free_headers(struct http_header *headers);
|
||||
int headercmp(const char *name);
|
||||
int case_insensitive_strcmp(const char *a, const char *b);
|
||||
int headercmp(struct http_header *header, const char *name);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue