From: Paul Eggert Date: Thu, 7 Nov 2024 01:56:15 +0000 (-0800) Subject: expand: refactor to introduce ‘colno’ X-Git-Tag: v9.6~110 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4edd48a2a1bbe4fd09c1fbc0330d00861ac93d5;p=thirdparty%2Fcoreutils.git expand: refactor to introduce ‘colno’ * src/expand-common.h (colno): New typedef. All uses of uintmax_t for column numbers replaced by colno. * src/expand-common.c (add_tab_stop): Use xpalloc instead of X2NREALLOC, and use ckd_add to check for overflow. --- diff --git a/src/expand-common.c b/src/expand-common.c index ba14c6d81a..7c0ea2ea76 100644 --- a/src/expand-common.c +++ b/src/expand-common.c @@ -30,13 +30,13 @@ bool convert_entire_line = false; /* If nonzero, the size of all tab stops. If zero, use 'tab_list' instead. */ -static uintmax_t tab_size = 0; +static colno tab_size = 0; /* If nonzero, the size of all tab stops after the last specified. */ -static uintmax_t extend_size = 0; +static colno extend_size = 0; /* If nonzero, an increment for additional tab stops after the last specified.*/ -static uintmax_t increment_size = 0; +static colno increment_size = 0; /* The maximum distance between tab stops. */ idx_t max_column_width; @@ -44,7 +44,7 @@ idx_t max_column_width; /* Array of the explicit column numbers of the tab stops; after 'tab_list' is exhausted, each additional tab is replaced by a space. The first column is column 0. */ -static uintmax_t *tab_list = nullptr; +static colno *tab_list = nullptr; /* The number of allocated entries in 'tab_list'. */ static idx_t n_tabs_allocated = 0; @@ -72,10 +72,10 @@ int exit_status = EXIT_SUCCESS; /* Add tab stop TABVAL to the end of 'tab_list'. */ extern void -add_tab_stop (uintmax_t tabval) +add_tab_stop (colno tabval) { - uintmax_t prev_column = first_free_tab ? tab_list[first_free_tab - 1] : 0; - uintmax_t column_width = prev_column <= tabval ? tabval - prev_column : 0; + colno prev_column = first_free_tab ? tab_list[first_free_tab - 1] : 0; + colno column_width = prev_column <= tabval ? tabval - prev_column : 0; if (first_free_tab == n_tabs_allocated) tab_list = xpalloc (tab_list, &n_tabs_allocated, 1, -1, sizeof *tab_list); @@ -89,7 +89,7 @@ add_tab_stop (uintmax_t tabval) } static bool -set_extend_size (uintmax_t tabval) +set_extend_size (colno tabval) { bool ok = true; @@ -106,7 +106,7 @@ set_extend_size (uintmax_t tabval) } static bool -set_increment_size (uintmax_t tabval) +set_increment_size (colno tabval) { bool ok = true; @@ -128,7 +128,7 @@ extern void parse_tab_stops (char const *stops) { bool have_tabval = false; - uintmax_t tabval = 0; + colno tabval = 0; bool extend_tabval = false; bool increment_tabval = false; char const *num_start = nullptr; @@ -230,9 +230,9 @@ parse_tab_stops (char const *stops) contains only nonzero, ascending values. */ static void -validate_tab_stops (uintmax_t const *tabs, idx_t entries) +validate_tab_stops (colno const *tabs, idx_t entries) { - uintmax_t prev_tab = 0; + colno prev_tab = 0; for (idx_t i = 0; i < entries; i++) { @@ -271,8 +271,8 @@ finalize_tab_stops (void) } -extern uintmax_t -get_next_tab_column (const uintmax_t column, idx_t *tab_index, +extern colno +get_next_tab_column (const colno column, idx_t *tab_index, bool *last_tab) { *last_tab = false; @@ -285,7 +285,7 @@ get_next_tab_column (const uintmax_t column, idx_t *tab_index, the current input column. */ for ( ; *tab_index < first_free_tab ; (*tab_index)++ ) { - uintmax_t tab = tab_list[*tab_index]; + colno tab = tab_list[*tab_index]; if (column < tab) return tab; } @@ -297,7 +297,7 @@ get_next_tab_column (const uintmax_t column, idx_t *tab_index, /* incremental last tab - add increment_size to the previous tab stop */ if (increment_size) { - uintmax_t end_tab = tab_list[first_free_tab - 1]; + colno end_tab = tab_list[first_free_tab - 1]; return column + (increment_size - ((column - end_tab) % increment_size)); } diff --git a/src/expand-common.h b/src/expand-common.h index 9234691d62..5b20d018a6 100644 --- a/src/expand-common.h +++ b/src/expand-common.h @@ -15,6 +15,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +/* Column numbers are nonnegative, with the leftmost column being zero. */ +typedef uintmax_t colno; + /* If true, convert blanks even after nonblank characters have been read on the line. */ extern bool convert_entire_line; @@ -27,7 +30,7 @@ extern int exit_status; /* Add tab stop TABVAL to the end of 'tab_list'. */ extern void -add_tab_stop (uintmax_t tabval); +add_tab_stop (colno tabval); /* Add the comma or blank separated list of tab stops STOPS to the list of tab stops. */ @@ -35,8 +38,8 @@ extern void parse_tab_stops (char const *stops) _GL_ATTRIBUTE_NONNULL (); /* TODO: Document */ -extern uintmax_t -get_next_tab_column (const uintmax_t column, idx_t *tab_index, +extern colno +get_next_tab_column (const colno column, idx_t *tab_index, bool *last_tab) _GL_ATTRIBUTE_NONNULL ((3)); diff --git a/src/unexpand.c b/src/unexpand.c index 846f8467e9..9e621929a2 100644 --- a/src/unexpand.c +++ b/src/unexpand.c @@ -132,10 +132,10 @@ unexpand (void) is true: */ /* Column of next input character. */ - uintmax_t column = 0; + colno column = 0; /* Column the next input tab stop is on. */ - uintmax_t next_tab_column = 0; + colno next_tab_column = 0; /* Index in TAB_LIST of next tab stop to examine. */ idx_t tab_index = 0; @@ -255,7 +255,7 @@ int main (int argc, char **argv) { bool have_tabval = false; - uintmax_t tabval IF_LINT ( = 0); + colno tabval IF_LINT ( = 0); int c; /* If true, cancel the effect of any -a (explicit or implicit in -t),