Skip to content

Commit e05f1b2

Browse files
add custom fossil_media_strncasecmp
1 parent 210081d commit e05f1b2

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

code/logic/fossil/media/media.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ extern "C"
4242
* Common Utility Declarations
4343
* =============================== */
4444

45+
/**
46+
* @brief Case-insensitive string comparison up to n characters.
47+
*
48+
* Compares two strings ignoring case, up to n characters.
49+
*
50+
* @param s1 First string.
51+
* @param s2 Second string.
52+
* @param n Maximum number of characters to compare.
53+
* @return 0 if equal, <0 if s1 < s2, >0 if s1 > s2.
54+
*/
55+
int fossil_media_strncasecmp(const char *s1, const char *s2, size_t n);
56+
4557
/**
4658
* @brief Allocates and returns a null-terminated string containing a
4759
* copy of the given input.

code/logic/html.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int parse_html_string(const char *input, fossil_media_html_doc_t **out_do
104104
continue;
105105
}
106106
// Handle DOCTYPE <!DOCTYPE ...>
107-
if (strncasecmp(p+2, "DOCTYPE", 7) == 0) {
107+
if (fossil_media_strncasecmp(p+2, "DOCTYPE", 7) == 0) {
108108
const char *end = strchr(p, '>');
109109
if (!end) break;
110110
size_t len = (size_t)(end - (p+2));

code/logic/media.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@
2828
#include <string.h>
2929
#include <ctype.h>
3030

31+
32+
/**
33+
* @brief Case-insensitive string comparison up to n characters.
34+
*
35+
* Compares two strings ignoring case, up to n characters.
36+
*
37+
* @param s1 First string.
38+
* @param s2 Second string.
39+
* @param n Maximum number of characters to compare.
40+
* @return 0 if equal, <0 if s1 < s2, >0 if s1 > s2.
41+
*/
42+
int fossil_media_strncasecmp(const char *s1, const char *s2, size_t n) {
43+
size_t i;
44+
for (i = 0; i < n; i++) {
45+
unsigned char c1 = (unsigned char)s1[i];
46+
unsigned char c2 = (unsigned char)s2[i];
47+
if (c1 == '\0' || c2 == '\0') {
48+
return c1 - c2;
49+
}
50+
if (tolower(c1) != tolower(c2)) {
51+
return tolower(c1) - tolower(c2);
52+
}
53+
}
54+
return 0;
55+
}
56+
3157
/* -------------------------------------------------------------
3258
* fossil_media_strdup
3359
* -------------------------------------------------------------

0 commit comments

Comments
 (0)