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

@@ -1132,17 +1132,11 @@ static void serialize_pos(bufinfo_T *bi, pos_T pos)
static void unserialize_pos(bufinfo_T *bi, pos_T *pos)
{
pos->lnum = undo_read_4c(bi);
if (pos->lnum < 0) {
pos->lnum = 0;
}
pos->lnum = MAX(pos->lnum, 0);
pos->col = undo_read_4c(bi);
if (pos->col < 0) {
pos->col = 0;
}
pos->col = MAX(pos->col, 0);
pos->coladd = undo_read_4c(bi);
if (pos->coladd < 0) {
pos->coladd = 0;
}
pos->coladd = MAX(pos->coladd, 0);
}
/// Serializes "info".
@@ -1209,14 +1203,12 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf,
// Strip any sticky and executable bits.
perm = perm & 0666;
int fd;
// If the undo file already exists, verify that it actually is an undo
// file, and delete it.
if (os_path_exists(file_name)) {
if (name == NULL || !forceit) {
// Check we can read it and it's an undo file.
fd = os_open(file_name, O_RDONLY, 0);
int fd = os_open(file_name, O_RDONLY, 0);
if (fd < 0) {
if (name != NULL || p_verbose > 0) {
if (name == NULL) {
@@ -1261,7 +1253,7 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf,
goto theend;
}
fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
int fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0) {
semsg(_(e_not_open), file_name);
goto theend;
@@ -2001,9 +1993,7 @@ void undo_time(int step, bool sec, bool file, bool absolute)
target = curbuf->b_u_seq_cur + step;
}
if (step < 0) {
if (target < 0) {
target = 0;
}
target = MAX(target, 0);
closest = -1;
} else {
if (dosec) {
@@ -2396,9 +2386,7 @@ static void u_undoredo(bool undo, bool do_buf_event)
}
// Set the '[ mark.
if (top + 1 < curbuf->b_op_start.lnum) {
curbuf->b_op_start.lnum = top + 1;
}
curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, top + 1);
// Set the '] mark.
if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) {
curbuf->b_op_end.lnum = top + 1;
@@ -2419,12 +2407,8 @@ static void u_undoredo(bool undo, bool do_buf_event)
}
// Ensure the '[ and '] marks are within bounds.
if (curbuf->b_op_start.lnum > curbuf->b_ml.ml_line_count) {
curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
}
if (curbuf->b_op_end.lnum > curbuf->b_ml.ml_line_count) {
curbuf->b_op_end.lnum = curbuf->b_ml.ml_line_count;
}
curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, curbuf->b_ml.ml_line_count);
curbuf->b_op_end.lnum = MIN(curbuf->b_op_end.lnum, curbuf->b_ml.ml_line_count);
// Adjust Extmarks
if (undo) {