Skip to content

Commit

Permalink
refactor: fix issues reported by clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
swarn committed Dec 6, 2023
1 parent 19a027d commit 752a2e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/bonus.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include "match.h"


#define SCORE_GAP_LEADING -0.005
#define SCORE_GAP_TRAILING -0.005
#define SCORE_GAP_INNER -0.01
#define SCORE_MATCH_CONSECUTIVE 1.0
#define SCORE_MATCH_SLASH 0.9
#define SCORE_MATCH_WORD 0.8
#define SCORE_MATCH_CAPITAL 0.7
#define SCORE_MATCH_DOT 0.6
#define SCORE_GAP_LEADING (-0.005)
#define SCORE_GAP_TRAILING (-0.005)
#define SCORE_GAP_INNER (-0.01)
#define SCORE_MATCH_CONSECUTIVE (1.0)
#define SCORE_MATCH_SLASH (0.9)
#define SCORE_MATCH_WORD (0.8)
#define SCORE_MATCH_CAPITAL (0.7)
#define SCORE_MATCH_DOT (0.6)

// clang-format off
#define ASSIGN_LOWER(v) \
Expand Down
10 changes: 4 additions & 6 deletions src/fzy_native.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// The lua wrapper to the native C implementation of fzy
//

#include <stdbool.h>
#include <string.h>

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

#include "bonus.h"
#include "match.h"
Expand Down Expand Up @@ -51,7 +49,7 @@ static int score(lua_State * L)

// Given an array of `count` 0-based indices, push a table on to `L` with
// equivalent 1-based indices.
void push_indices(lua_State * L, index_t const * const indices, size_t count)
void push_indices(lua_State * L, index_t const * const indices, int count)
{
lua_createtable(L, count, 0);
for (int i = 0; i < count; i++)
Expand All @@ -73,19 +71,19 @@ static int positions(lua_State * L)
index_t result[MATCH_MAX_LEN];
score_t score = match_positions(needle, haystack, result, case_sensitive);

push_indices(L, result, strlen(needle));
push_indices(L, result, (int)strlen(needle));
lua_pushnumber(L, score);
return 2;
}

static int filter(lua_State * L)
{
char const * const needle = luaL_checkstring(L, 1);
size_t const needle_len = strlen(needle);
int const needle_len = (int)strlen(needle);

int const haystacks_idx = 2;
luaL_checktype(L, haystacks_idx, LUA_TTABLE);
int const haystacks_len = lua_rawlen(L, haystacks_idx);
int const haystacks_len = (int)lua_rawlen(L, haystacks_idx);

bool case_sensitive = false;
if (lua_gettop(L) > 2)
Expand Down
59 changes: 32 additions & 27 deletions src/match.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include "match.h"

#include <ctype.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -26,8 +23,8 @@ int has_match(char const * needle, char const * haystack, int case_sensitive)

if (! case_sensitive)
{
size_t const n = strlen(needle);
size_t const m = strlen(haystack);
int const n = (int)strlen(needle);
int const m = (int)strlen(haystack);

for (int i = 0; i < n; i++)
needle_lower[i] = (char)tolower(needle[i]);
Expand All @@ -43,7 +40,8 @@ int has_match(char const * needle, char const * haystack, int case_sensitive)

while (*needle)
{
if (! (haystack = strchr(haystack, *needle++)))
haystack = strchr(haystack, *needle++);
if (! haystack)
return 0;

haystack++;
Expand Down Expand Up @@ -125,7 +123,7 @@ static void setup_match_struct(
}

static inline void match_row(
const struct match_struct * match,
struct match_struct const * match,
int row,
score_t * curr_D,
score_t * curr_M,
Expand All @@ -150,12 +148,18 @@ static inline void match_row(
score_t score = SCORE_MIN;
if (i == 0)
{
// The match_bonus values are computed out to the length of the
// haystack in precompute_bonus. The index j is less than m,
// the length of the haystack. So the "garbage value" warning
// here is false.
// NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
score = (j * SCORE_GAP_LEADING) + match_bonus[j];
}
else if (j)
{
/* i > 0 && j > 0*/
score =
// NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
max(last_M[j - 1] + match_bonus[j],

/* consecutive match, doesn't stack with match_bonus */
Expand Down Expand Up @@ -249,9 +253,9 @@ score_t match_positions(

// D[][] Stores the best score for this position ending with a match.
// M[][] Stores the best possible score at this position.
typedef score_t(*score_grid_t)[MATCH_MAX_LEN];
score_grid_t D = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
score_grid_t M = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
typedef score_t score_row_t[MATCH_MAX_LEN];
score_row_t * const D = malloc(sizeof(score_row_t) * n);
score_row_t * const M = malloc(sizeof(score_row_t) * n);

score_t * last_D = NULL;
score_t * last_M = NULL;
Expand All @@ -270,27 +274,28 @@ score_t match_positions(
}

/* backtrace to find the positions of optimal matching */
if (positions)
int match_required = 0;
for (int i = n - 1, j = m - 1; i >= 0; i--)
{
int match_required = 0;
for (int i = n - 1, j = m - 1; i >= 0; i--)
for (; j >= 0; j--)
{
for (; j >= 0; j--)
// There may be multiple paths which result in the optimal
// weight.
//
// For simplicity, we will pick the first one we encounter,
// the latest in the candidate string.
// NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
if (D[i][j] != SCORE_MIN && (match_required || D[i][j] == M[i][j]))
{
// There may be multiple paths which result in the optimal
// weight.
//
// For simplicity, we will pick the first one we encounter,
// the latest in the candidate string.
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;
// 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;

if (positions)
positions[i] = j--;
break;
}

break;
}
}
}
Expand Down

0 comments on commit 752a2e3

Please sign in to comment.