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

@@ -261,11 +261,8 @@ static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp)
ip->bw_restlen += *lenp;
break;
}
if (n > 1) {
c = (unsigned)utf_ptr2char((char *)ip->bw_rest);
} else {
c = ip->bw_rest[0];
}
c = (n > 1) ? (unsigned)utf_ptr2char((char *)ip->bw_rest)
: ip->bw_rest[0];
if (n >= ip->bw_restlen) {
n -= ip->bw_restlen;
ip->bw_restlen = 0;
@@ -289,11 +286,8 @@ static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp)
(size_t)ip->bw_restlen);
break;
}
if (n > 1) {
c = (unsigned)utf_ptr2char(*bufp + wlen);
} else {
c = (uint8_t)(*bufp)[wlen];
}
c = n > 1 ? (unsigned)utf_ptr2char(*bufp + wlen)
: (uint8_t)(*bufp)[wlen];
}
if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) {
@@ -876,9 +870,7 @@ static int buf_write_make_backup(char *fname, bool append, FileInfo *file_info_o
// Change one character, just before the extension.
//
char *wp = *backupp + strlen(*backupp) - 1 - strlen(backup_ext);
if (wp < *backupp) { // empty file name ???
wp = *backupp;
}
wp = MAX(wp, *backupp); // empty file name ???
*wp = 'z';
while (*wp > 'a' && os_fileinfo(*backupp, &file_info_new)) {
(*wp)--;
@@ -993,9 +985,7 @@ nobackup:
// Change one character, just before the extension.
if (!p_bk && os_path_exists(*backupp)) {
p = *backupp + strlen(*backupp) - 1 - strlen(backup_ext);
if (p < *backupp) { // empty file name ???
p = *backupp;
}
p = MAX(p, *backupp); // empty file name ???
*p = 'z';
while (*p > 'a' && os_path_exists(*backupp)) {
(*p)--;
@@ -1255,9 +1245,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
status_redraw_all(); // redraw status lines later
}
if (end > buf->b_ml.ml_line_count) {
end = buf->b_ml.ml_line_count;
}
end = MIN(end, buf->b_ml.ml_line_count);
if (buf->b_ml.ml_flags & ML_EMPTY) {
start = end + 1;
}