summaryrefslogtreecommitdiff
path: root/match.c
diff options
context:
space:
mode:
Diffstat (limited to 'match.c')
-rw-r--r--match.c93
1 files changed, 46 insertions, 47 deletions
diff --git a/match.c b/match.c
index f915110..3eafda1 100644
--- a/match.c
+++ b/match.c
@@ -9,16 +9,16 @@
#include "config.h"
-char *strcasechr(const char *s, char c){
+char *strcasechr(const char *s, char c) {
const char accept[3] = {c, toupper(c), 0};
return strpbrk(s, accept);
}
-int has_match(const char *needle, const char *haystack){
- while(*needle){
+int has_match(const char *needle, const char *haystack) {
+ while (*needle) {
char nch = *needle++;
- if(!(haystack = strcasechr(haystack, nch))){
+ if (!(haystack = strcasechr(haystack, nch))) {
return 0;
}
haystack++;
@@ -29,22 +29,22 @@ int has_match(const char *needle, const char *haystack){
#define max(a, b) (((a) > (b)) ? (a) : (b))
/* print one of the internal matrices */
-void mat_print(score_t *mat, const char *needle, const char *haystack){
+void mat_print(score_t *mat, const char *needle, const char *haystack) {
int n = strlen(needle);
int m = strlen(haystack);
int i, j;
fprintf(stderr, " ");
- for(j = 0; j < m; j++){
+ for (j = 0; j < m; j++) {
fprintf(stderr, " %c", haystack[j]);
}
fprintf(stderr, "\n");
- for(i = 0; i < n; i++){
+ for (i = 0; i < n; i++) {
fprintf(stderr, " %c |", needle[i]);
- for(j = 0; j < m; j++){
- score_t val = mat[i*m + j];
- if(val == SCORE_MIN){
+ for (j = 0; j < m; j++) {
+ score_t val = mat[i * m + j];
+ if (val == SCORE_MIN) {
fprintf(stderr, " -inf");
- }else{
+ } else {
fprintf(stderr, " % .2f", val);
}
}
@@ -53,14 +53,14 @@ void mat_print(score_t *mat, const char *needle, const char *haystack){
fprintf(stderr, "\n\n");
}
-score_t calculate_score(const char *needle, const char *haystack, size_t *positions){
- if(!*haystack || !*needle)
+score_t calculate_score(const char *needle, const char *haystack, size_t *positions) {
+ if (!*haystack || !*needle)
return SCORE_MIN;
int n = strlen(needle);
int m = strlen(haystack);
- if(m > 1024){
+ if (m > 1024) {
/*
* Unreasonably large candidate: return no score
* If it is a valid match it will still be returned, it will
@@ -79,23 +79,20 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
/* Which positions are beginning of words */
char last_ch = '\0';
- for(int i = 0; i < m; i++){
+ for (int i = 0; i < m; i++) {
char ch = haystack[i];
score_t score = 0;
- if(isalnum(ch)){
- if(!last_ch || last_ch == '/'){
+ if (isalnum(ch)) {
+ if (!last_ch || last_ch == '/') {
score = SCORE_MATCH_SLASH;
- }else if(last_ch == '-' ||
- last_ch == '_' ||
- last_ch == ' ' ||
- (last_ch >= '0' && last_ch <= '9')){
+ } else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' ||
+ (last_ch >= '0' && last_ch <= '9')) {
score = SCORE_MATCH_WORD;
- }else if(last_ch >= 'a' && last_ch <= 'z' &&
- ch >= 'A' && ch <= 'Z'){
+ } else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
/* CamelCase */
score = SCORE_MATCH_CAPITAL;
- }else if(last_ch == '.'){
+ } else if (last_ch == '.') {
score = SCORE_MATCH_DOT;
}
}
@@ -104,21 +101,20 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
last_ch = ch;
}
- for(int i = 0; i < n; i++){
+ for (int i = 0; i < n; i++) {
score_t prev_score = SCORE_MIN;
- score_t gap_score = i == n-1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
- for(int j = 0; j < m; j++){
+ score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
+ for (int j = 0; j < m; j++) {
score_t score = SCORE_MIN;
- if(tolower(needle[i]) == tolower(haystack[j])){
- if(!i){
+ if (tolower(needle[i]) == tolower(haystack[j])) {
+ if (!i) {
score = (j * SCORE_GAP_LEADING) + match_bonus[j];
- }else if(j){
+ } else if (j) {
score = max(
- M[i-1][j-1] + match_bonus[j],
+ M[i - 1][j - 1] + match_bonus[j],
- /* consecutive match, doesn't stack with match_bonus */
- D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE
- );
+ /* consecutive match, doesn't stack with match_bonus */
+ D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
}
}
D[i][j] = score;
@@ -134,10 +130,10 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
#endif
/* backtrace to find the positions of optimal matching */
- if(positions){
+ if (positions) {
int match_required = 0;
- for(int i = n-1, j = m-1; i >= 0; i--){
- for(; j >= 0; j--){
+ for (int i = n - 1, j = m - 1; i >= 0; i--) {
+ for (; j >= 0; j--) {
/*
* There may be multiple paths which result in
* the optimal weight.
@@ -146,12 +142,15 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
* we encounter, the latest in the candidate
* string.
*/
- if(D[i][j] != SCORE_MIN && (match_required || D[i][j] == M[i][j])){
+ if (D[i][j] != SCORE_MIN &&
+ (match_required || D[i][j] == M[i][j])) {
/* If this score was determined using
* SCORE_MATCH_CONSECUTIVE, the
* previous character MUST be a match
*/
- match_required = i && j && M[i][j] == D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE;
+ match_required =
+ i && j &&
+ M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
positions[i] = j--;
break;
}
@@ -159,24 +158,24 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
}
}
- return M[n-1][m-1];
+ return M[n - 1][m - 1];
}
-score_t match_positions(const char *needle, const char *haystack, size_t *positions){
- if(!*needle){
+score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
+ if (!*needle) {
return SCORE_MAX;
- }else if(!strcasecmp(needle, haystack)){
- if(positions){
+ } else if (!strcasecmp(needle, haystack)) {
+ if (positions) {
int n = strlen(needle);
- for(int i = 0; i < n; i++)
+ for (int i = 0; i < n; i++)
positions[i] = i;
}
return SCORE_MAX;
- }else{
+ } else {
return calculate_score(needle, haystack, positions);
}
}
-score_t match(const char *needle, const char *haystack){
+score_t match(const char *needle, const char *haystack) {
return match_positions(needle, haystack, NULL);
}