mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 18:30:15 -04:00
smart case search in file search
This commit is contained in:
44
backend.c
44
backend.c
@@ -1,3 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* concat(const char *s1, const char *s2){
|
||||
const size_t len1 = strlen(s1);
|
||||
const size_t len2 = strlen(s2);
|
||||
@@ -6,5 +9,46 @@ char* concat(const char *s1, const char *s2){
|
||||
memcpy(result + len1, s2, len2 + 1);
|
||||
return result;
|
||||
}
|
||||
char* smartstrcasestr(char *haystack, const char *needle){
|
||||
char smart = 0;
|
||||
char *ret;
|
||||
char passes = 0;
|
||||
while (*needle) {
|
||||
if (*needle >= 'A' && *needle <= 'Z') {
|
||||
smart = 1;
|
||||
break;
|
||||
}
|
||||
passes++;
|
||||
needle++;
|
||||
}
|
||||
needle -= passes;
|
||||
if (smart == 0) {
|
||||
char *needle_case = malloc(strlen(needle)+1);
|
||||
strcpy(needle_case, needle);
|
||||
passes = 0;
|
||||
while (*needle_case) {
|
||||
*needle_case = *needle_case | ' ';
|
||||
needle_case++;
|
||||
passes++;
|
||||
}
|
||||
needle_case -= passes;
|
||||
|
||||
char *haystack_case = malloc(strlen(haystack)+1);
|
||||
strcpy(haystack_case, haystack);
|
||||
passes = 0;
|
||||
while (*haystack_case) {
|
||||
*haystack_case = *haystack_case | ' ';
|
||||
haystack_case++;
|
||||
passes++;
|
||||
}
|
||||
haystack_case -= passes;
|
||||
|
||||
ret = strstr(haystack_case, needle_case);
|
||||
free(needle_case);
|
||||
free(haystack_case);
|
||||
} else {
|
||||
ret = strstr(haystack, needle);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user