refactor: collapse statements in single assignments

Problem:

Variables are often assigned multiple places in common patterns.

Solution:

Replace these common patterns with different patterns that reduce the
number of assignments.

Use `MAX` and `MIN`:
```c
if (x < y) {
  x = y;
}

// -->

x = MAX(x, y);
```

```c
if (x > y) {
  x = y;
}

// -->

x = MIN(x, y);
```

Use ternary:
```c
int a;
if (cond) {
  a = b;
} els {
  a = c;
}

// -->

int a = cond ? b : c;

```
This commit is contained in:
Lewis Russell
2024-07-19 11:00:13 +01:00
committed by Lewis Russell
parent 1b5a394ffd
commit d1bd3d643e
36 changed files with 419 additions and 1214 deletions

View File

@@ -276,7 +276,6 @@ static bool can_be_compound(trystate_T *sp, slang_T *slang, uint8_t *compflags,
static int score_wordcount_adj(slang_T *slang, int score, char *word, bool split)
{
int bonus;
int newscore;
hashitem_T *hi = hash_find(&slang->sl_wordcount, word);
if (HASHITEM_EMPTY(hi)) {
@@ -291,11 +290,8 @@ static int score_wordcount_adj(slang_T *slang, int score, char *word, bool split
} else {
bonus = SCORE_COMMON3;
}
if (split) {
newscore = score - bonus / 2;
} else {
newscore = score - bonus;
}
int newscore = split ? score - bonus / 2
: score - bonus;
if (newscore < 0) {
return 0;
}
@@ -449,7 +445,6 @@ void spell_suggest(int count)
suginfo_T sug;
suggest_T *stp;
bool mouse_used;
int limit;
int selected = count;
int badlen = 0;
int msg_scroll_save = msg_scroll;
@@ -481,9 +476,7 @@ void spell_suggest(int count)
badlen++;
end_visual_mode();
// make sure we don't include the NUL at the end of the line
if (badlen > get_cursor_line_len() - curwin->w_cursor.col) {
badlen = get_cursor_line_len() - curwin->w_cursor.col;
}
badlen = MIN(badlen, get_cursor_line_len() - curwin->w_cursor.col);
// Find the start of the badly spelled word.
} else if (spell_move_to(curwin, FORWARD, SMT_ALL, true, NULL) == 0
|| curwin->w_cursor.col > prev_cursor.col) {
@@ -519,11 +512,7 @@ void spell_suggest(int count)
// Get the list of suggestions. Limit to 'lines' - 2 or the number in
// 'spellsuggest', whatever is smaller.
if (sps_limit > Rows - 2) {
limit = Rows - 2;
} else {
limit = sps_limit;
}
int limit = MIN(sps_limit, Rows - 2);
spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
true, need_cap, true);
@@ -731,10 +720,7 @@ static void spell_find_suggest(char *badptr, int badlen, suginfo_T *su, int maxc
}
su->su_maxcount = maxcount;
su->su_maxscore = SCORE_MAXINIT;
if (su->su_badlen >= MAXWLEN) {
su->su_badlen = MAXWLEN - 1; // just in case
}
su->su_badlen = MIN(su->su_badlen, MAXWLEN - 1); // just in case
xmemcpyz(su->su_badword, su->su_badptr, (size_t)su->su_badlen);
spell_casefold(curwin, su->su_badptr, su->su_badlen, su->su_fbadword,
MAXWLEN);
@@ -1145,7 +1131,6 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun
idx_T *idxs, *fidxs, *pidxs;
int c, c2, c3;
int n = 0;
garray_T *gap;
idx_T arridx;
int fl = 0;
int tl;
@@ -1736,11 +1721,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun
// Done all bytes at this node, do next state. When still at
// already changed bytes skip the other tricks.
PROF_STORE(sp->ts_state)
if (sp->ts_fidx >= sp->ts_fidxtry) {
sp->ts_state = STATE_DEL;
} else {
sp->ts_state = STATE_FINAL;
}
sp->ts_state = sp->ts_fidx >= sp->ts_fidxtry ? STATE_DEL
: STATE_FINAL;
} else {
arridx += sp->ts_curi++;
c = byts[arridx];
@@ -2254,11 +2236,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun
// valid.
p = fword + sp->ts_fidx;
if (soundfold) {
gap = &slang->sl_repsal;
} else {
gap = &lp->lp_replang->sl_rep;
}
garray_T *gap = soundfold ? &slang->sl_repsal
: &lp->lp_replang->sl_rep;
while (sp->ts_curi < gap->ga_len) {
fromto_T *ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
if (*ftp->ft_from != *p) {
@@ -3126,11 +3105,9 @@ static void add_suggestion(suginfo_T *su, garray_T *gap, const char *goodword, i
// the best suggestions.
if (gap->ga_len > SUG_MAX_COUNT(su)) {
if (maxsf) {
su->su_sfmaxscore = cleanup_suggestions(gap,
su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
su->su_sfmaxscore = cleanup_suggestions(gap, su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
} else {
su->su_maxscore = cleanup_suggestions(gap,
su->su_maxscore, SUG_CLEAN_COUNT(su));
su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, SUG_CLEAN_COUNT(su));
}
}
}
@@ -3276,8 +3253,6 @@ static int soundalike_score(char *goodstart, char *badstart)
{
char *goodsound = goodstart;
char *badsound = badstart;
char *pl, *ps;
char *pl2, *ps2;
int score = 0;
// Adding/inserting "*" at the start (word starts with vowel) shouldn't be
@@ -3318,13 +3293,10 @@ static int soundalike_score(char *goodstart, char *badstart)
return SCORE_MAXMAX;
}
if (n > 0) {
pl = goodsound; // goodsound is longest
ps = badsound;
} else {
pl = badsound; // badsound is longest
ps = goodsound;
}
// n > 0 : goodsound is longest
// n <= 0 : badsound is longest
char *pl = n > 0 ? goodsound : badsound;
char *ps = n > 0 ? badsound : goodsound;
// Skip over the identical part.
while (*pl == *ps && *pl != NUL) {
@@ -3332,6 +3304,8 @@ static int soundalike_score(char *goodstart, char *badstart)
ps++;
}
char *pl2, *ps2;
switch (n) {
case -2:
case 2:
@@ -3552,19 +3526,13 @@ static int spell_edit_score(slang_T *slang, const char *badword, const char *goo
int pgc = wgoodword[j - 2];
if (bc == pgc && pbc == gc) {
int t = SCORE_SWAP + CNT(i - 2, j - 2);
if (t < CNT(i, j)) {
CNT(i, j) = t;
}
CNT(i, j) = MIN(CNT(i, j), t);
}
}
int t = SCORE_DEL + CNT(i - 1, j);
if (t < CNT(i, j)) {
CNT(i, j) = t;
}
CNT(i, j) = MIN(CNT(i, j), t);
t = SCORE_INS + CNT(i, j - 1);
if (t < CNT(i, j)) {
CNT(i, j) = t;
}
CNT(i, j) = MIN(CNT(i, j), t);
}
}
}