]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
expand: refactor to introduce ‘colno’
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Nov 2024 01:56:15 +0000 (17:56 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 Nov 2024 07:41:18 +0000 (23:41 -0800)
* 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.

src/expand-common.c
src/expand-common.h
src/unexpand.c

index ba14c6d81a7139901c67e194e33489a21b891ae2..7c0ea2ea7602a3bf70f1d6aa281388e42316b07f 100644 (file)
 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));
     }
index 9234691d62aaad8ca8e23fcd1a72480049f48f09..5b20d018a6663c1c40ea68c70b7d805b62ed955e 100644 (file)
@@ -15,6 +15,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
+/* 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));
 
index 846f8467e9461ab86997591ca7dc51415752b3b3..9e621929a2ab191be833b9c380a520d6072a8289 100644 (file)
@@ -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),