From f92c2921d252af8c787915550505f23579d5d41a Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Tue, 12 Mar 2019 22:38:42 -0700 Subject: [PATCH] Remove mini() and maxi() C++11 provides std::min/std::max which we're using all over, obviating the need for our own templates for this. util.h now only provides two things: get_time and wcsfilecmp. This commit removes everything that includes it which doesn't use either; most because they no longer need mini or maxi from it but some others were #including it unnecessarily. --- src/builtin_commandline.cpp | 3 +-- src/builtin_ulimit.cpp | 3 +-- src/env_universal_common.cpp | 4 ++-- src/fallback.cpp | 1 - src/input_common.cpp | 5 ++--- src/pager.cpp | 7 +++---- src/parse_util.cpp | 1 - src/proc.cpp | 1 - src/reader.cpp | 21 ++++++++++++--------- src/screen.cpp | 13 +++++++------ src/util.h | 12 ------------ 11 files changed, 28 insertions(+), 43 deletions(-) diff --git a/src/builtin_commandline.cpp b/src/builtin_commandline.cpp index de6cd8126..5f7c27a16 100644 --- a/src/builtin_commandline.cpp +++ b/src/builtin_commandline.cpp @@ -15,7 +15,6 @@ #include "proc.h" #include "reader.h" #include "tokenizer.h" -#include "util.h" #include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep @@ -398,7 +397,7 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) } current_buffer = reader_get_buffer(); - new_pos = maxi(0L, mini(new_pos, (long)wcslen(current_buffer))); + new_pos = std::max(0L, std::min(new_pos, (long)std::wcslen(current_buffer))); reader_set_buffer(current_buffer, (size_t)new_pos); } else { streams.out.append_format(L"%lu\n", (unsigned long)reader_get_cursor_pos()); diff --git a/src/builtin_ulimit.cpp b/src/builtin_ulimit.cpp index f7b9cf4c8..553c35514 100644 --- a/src/builtin_ulimit.cpp +++ b/src/builtin_ulimit.cpp @@ -9,7 +9,6 @@ #include "common.h" #include "fallback.h" // IWYU pragma: keep #include "io.h" -#include "util.h" #include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep @@ -81,7 +80,7 @@ static void print_all(int hard, io_streams_t &streams) { int w = 0; for (i = 0; resource_arr[i].desc; i++) { - w = maxi(w, fish_wcswidth(resource_arr[i].desc)); + w = std::max(w, fish_wcswidth(resource_arr[i].desc)); } for (i = 0; resource_arr[i].desc; i++) { diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index cde707860..7a033f03c 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1365,7 +1365,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t { // Now return the smaller of the two values. If we get ULONG_MAX, it means there's no more // need to poll; in that case return 0. - unsigned long result = mini(readback_delay, polling_delay); + unsigned long result = std::min(readback_delay, polling_delay); if (result == ULONG_MAX) { result = 0; } @@ -1378,7 +1378,7 @@ class universal_notifier_named_pipe_t : public universal_notifier_t { // Read back what we wrote. We do nothing with the value. while (this->readback_amount > 0) { char buff[64]; - size_t amt_to_read = mini(this->readback_amount, sizeof buff); + size_t amt_to_read = std::min(this->readback_amount, sizeof(buff)); ignore_result(read(this->pipe_fd, buff, amt_to_read)); this->readback_amount -= amt_to_read; } diff --git a/src/fallback.cpp b/src/fallback.cpp index 2b8dacb62..9f5542ba5 100644 --- a/src/fallback.cpp +++ b/src/fallback.cpp @@ -41,7 +41,6 @@ #include "common.h" // IWYU pragma: keep #include "fallback.h" // IWYU pragma: keep -#include "util.h" // IWYU pragma: keep #if defined(TPARM_SOLARIS_KLUDGE) #undef tparm diff --git a/src/input_common.cpp b/src/input_common.cpp index 19e4356a3..765a9d667 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -25,7 +25,6 @@ #include "fallback.h" // IWYU pragma: keep #include "input_common.h" #include "iothread.h" -#include "util.h" #include "wutil.h" /// Time in milliseconds to wait for another byte to be available for reading @@ -82,7 +81,7 @@ static wint_t readb() { FD_SET(0, &fdset); if (ioport > 0) { FD_SET(ioport, &fdset); - fd_max = maxi(fd_max, ioport); + fd_max = std::max(fd_max, ioport); } // Get our uvar notifier. @@ -92,7 +91,7 @@ static wint_t readb() { int notifier_fd = notifier.notification_fd(); if (notifier_fd > 0) { FD_SET(notifier_fd, &fdset); - fd_max = maxi(fd_max, notifier_fd); + fd_max = std::max(fd_max, notifier_fd); } // Get its suggested delay (possibly none). diff --git a/src/pager.cpp b/src/pager.cpp index 03273037d..8f6e864ca 100644 --- a/src/pager.cpp +++ b/src/pager.cpp @@ -18,7 +18,6 @@ #include "pager.h" #include "reader.h" #include "screen.h" -#include "util.h" #include "wutil.h" // IWYU pragma: keep typedef pager_t::comp_t comp_t; @@ -422,7 +421,7 @@ bool pager_t::completion_try_print(size_t cols, const wcstring &prefix, const co this->available_term_height - 1 - (search_field_shown ? 1 : 0); // we always subtract 1 to make room for a comment row if (!this->fully_disclosed) { - term_height = mini(term_height, (size_t)PAGER_UNDISCLOSED_MAX_ROWS); + term_height = std::min(term_height, (size_t)PAGER_UNDISCLOSED_MAX_ROWS); } size_t row_count = divide_round_up(lst.size(), cols); @@ -478,7 +477,7 @@ bool pager_t::completion_try_print(size_t cols, const wcstring &prefix, const co // suggested_start_row. assert(row_count > term_height); size_t last_starting_row = row_count - term_height; - start_row = mini(suggested_start_row, last_starting_row); + start_row = std::min(suggested_start_row, last_starting_row); stop_row = start_row + term_height; assert(start_row <= last_starting_row); } @@ -660,7 +659,7 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio // Cardinal directions. We have a completion index; we wish to compute its row and column. size_t current_row = this->get_selected_row(rendering); size_t current_col = this->get_selected_column(rendering); - size_t page_height = maxi(rendering.term_height - 1, (size_t)1); + size_t page_height = std::max(rendering.term_height - 1, 1UL); switch (direction) { case direction_page_north: { diff --git a/src/parse_util.cpp b/src/parse_util.cpp index bcca52cb4..5d8fe3cc8 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -22,7 +22,6 @@ #include "parser.h" #include "tnode.h" #include "tokenizer.h" -#include "util.h" #include "wildcard.h" #include "wutil.h" // IWYU pragma: keep diff --git a/src/proc.cpp b/src/proc.cpp index e2ededc77..7c5016a14 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -46,7 +46,6 @@ #include "reader.h" #include "sanity.h" #include "signal.h" -#include "util.h" #include "wutil.h" // IWYU pragma: keep /// Statuses of last job's processes to exit - ensure we start off with one entry of 0. diff --git a/src/reader.cpp b/src/reader.cpp index 0b8861633..8d2b0ec35 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -73,7 +73,6 @@ #include "signal.h" #include "tnode.h" #include "tokenizer.h" -#include "util.h" #include "wutil.h" // IWYU pragma: keep // Name of the variable that tells how long it took, in milliseconds, for the previous @@ -137,8 +136,9 @@ static inline unsigned read_generation_count() { void editable_line_t::insert_string(const wcstring &str, size_t start, size_t len) { // Clamp the range to something valid. size_t string_length = str.size(); - start = mini(start, string_length); //!OCLINT(parameter reassignment) - len = mini(len, string_length - start); //!OCLINT(parameter reassignment) + + start = std::min(start, string_length); //!OCLINT(parameter reassignment) + len = std::min(len, string_length - start); //!OCLINT(parameter reassignment) this->text.insert(this->position, str, start, len); this->position += len; } @@ -631,8 +631,8 @@ void reader_data_t::repaint() { // term size, minus the number of lines consumed by our string. (Note this doesn't yet consider // wrapping). int full_line_count = 1 + std::count(full_line.begin(), full_line.end(), '\n'); - pager.set_term_size(maxi(1, common_get_width()), - maxi(1, common_get_height() - full_line_count)); + pager.set_term_size(std::max(1, common_get_width()), + std::max(1, common_get_height() - full_line_count)); pager.update_rendering(¤t_page_rendering); bool focused_on_pager = active_edit_line() == &pager.search_field_line; @@ -665,7 +665,7 @@ void reader_data_t::kill(editable_line_t *el, size_t begin_idx, size_t length, i if (el->position > begin_idx) { // Move the buff position back by the number of characters we deleted, but don't go past // buff_pos. - size_t backtrack = mini(el->position - begin_idx, length); + size_t backtrack = std::min(el->position - begin_idx, length); update_buff_pos(el, el->position - backtrack); } @@ -816,10 +816,12 @@ bool reader_expand_abbreviation_in_command(const wcstring &cmdline, size_t curso bool reader_data_t::expand_abbreviation_as_necessary(size_t cursor_backtrack) { bool result = false; editable_line_t *el = active_edit_line(); + if (expand_abbreviations && el == &command_line) { // Try expanding abbreviations. wcstring new_cmdline; - size_t cursor_pos = el->position - mini(el->position, cursor_backtrack); + size_t cursor_pos = el->position - std::min(el->position, cursor_backtrack); + if (reader_expand_abbreviation_in_command(el->text, cursor_pos, vars(), &new_cmdline)) { // We expanded an abbreviation! The cursor moves by the difference in the command line // lengths. @@ -1585,7 +1587,8 @@ bool reader_data_t::handle_completions(const std::vector &comp, first = false; } else { // Determine the shared prefix length. - size_t idx, max = mini(common_prefix.size(), el.completion.size()); + size_t idx, max = std::min(common_prefix.size(), el.completion.size()); + for (idx = 0; idx < max; idx++) { wchar_t ac = common_prefix.at(idx), bc = el.completion.at(idx); bool matches = (ac == bc); @@ -2689,7 +2692,7 @@ maybe_t reader_data_t::readline(int nchars) { // If we landed on a newline, don't delete it. if (*begin == L'\n') begin++; assert(end >= begin); - size_t len = maxi(end - begin, 1); + size_t len = std::max(end - begin, 1); begin = end - len; kill(el, begin - buff, len, KILL_PREPEND, last_char != R_BACKWARD_KILL_LINE); break; diff --git a/src/screen.cpp b/src/screen.cpp index 30efe600f..ae11033ab 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -39,7 +39,6 @@ #include "output.h" #include "pager.h" #include "screen.h" -#include "util.h" /// The number of characters to indent new blocks. #define INDENT_STEP 4u @@ -86,7 +85,7 @@ static size_t next_tab_stop(size_t current_line_width) { } /// Like fish_wcwidth, but returns 0 for control characters instead of -1. -static int fish_wcwidth_min_0(wchar_t widechar) { return maxi(0, fish_wcwidth(widechar)); } +static int fish_wcwidth_min_0(wchar_t widechar) { return std::max(0, fish_wcwidth(widechar)); } /// Whether we permit soft wrapping. If so, in some cases we don't explicitly move to the second /// physical line on a wrapped logical line; instead we just output it. @@ -225,7 +224,8 @@ static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) if (!esc2[p]) continue; // Test both padded and unpadded version, just to be safe. Most versions of tparm don't // actually seem to do anything these days. - size_t esc_seq_len = maxi(try_sequence(tparm((char *)esc2[p]), code), try_sequence(esc2[p], code)); + size_t esc_seq_len = + std::max(try_sequence(tparm((char *)esc2[p]), code), try_sequence(esc2[p], code)); if (esc_seq_len) { *resulting_length = esc_seq_len; return true; @@ -615,7 +615,7 @@ static void s_update(screen_t *scr, const wcstring &left_prompt, const wcstring // Determine how many lines have stuff on them; we need to clear lines with stuff that we don't // want. - const size_t lines_with_stuff = maxi(actual_lines_before_reset, scr->actual.line_count()); + const size_t lines_with_stuff = std::max(actual_lines_before_reset, scr->actual.line_count()); if (left_prompt != scr->actual_left_prompt) { s_move(scr, 0, 0); @@ -657,7 +657,7 @@ static void s_update(screen_t *scr, const wcstring &left_prompt, const wcstring } } if (next_line_will_change) { - skip_remaining = mini(skip_remaining, (size_t)(scr->actual_width - 2)); + skip_remaining = std::min(skip_remaining, (size_t)(scr->actual_width - 2)); } } } @@ -1073,7 +1073,8 @@ void s_reset(screen_t *s, screen_reset_mode_t mode) { // underneath the cursor when resizing a window wider such that it reduces our desired line // count. if (!abandon_line) { - s->actual_lines_before_reset = maxi(s->actual_lines_before_reset, s->actual.line_count()); + s->actual_lines_before_reset = + std::max(s->actual_lines_before_reset, s->actual.line_count()); } if (repaint_prompt && !abandon_line) { diff --git a/src/util.h b/src/util.h index 9d2e4bbbb..f9f121849 100644 --- a/src/util.h +++ b/src/util.h @@ -2,18 +2,6 @@ #ifndef FISH_UTIL_H #define FISH_UTIL_H -/// Returns the larger of two ints. -template -inline T maxi(T a, T b) { - return a > b ? a : b; -} - -/// Returns the smaller of two ints. -template -inline T mini(T a, T b) { - return a < b ? a : b; -} - /// Compares two wide character strings with an (arguably) intuitive ordering. This function tries /// to order strings in a way which is intuitive to humans with regards to sorting strings /// containing numbers.