@@ -1035,6 +1035,27 @@ diagnostic_report_diagnostic (diagnostic_context *context,
1035
1035
return true;
1036
1036
}
1037
1037
1038
+ /* Get the number of digits in the decimal representation of VALUE. */
1039
+
1040
+ int
1041
+ num_digits (int value )
1042
+ {
1043
+ /* Perhaps simpler to use log10 for this, but doing it this way avoids
1044
+ using floating point. */
1045
+ gcc_assert (value >= 0 );
1046
+
1047
+ if (value == 0 )
1048
+ return 1 ;
1049
+
1050
+ int digits = 0 ;
1051
+ while (value > 0 )
1052
+ {
1053
+ digits ++ ;
1054
+ value /= 10 ;
1055
+ }
1056
+ return digits ;
1057
+ }
1058
+
1038
1059
/* Given a partial pathname as input, return another pathname that
1039
1060
shares no directory elements with the pathname of __FILE__. This
1040
1061
is used by fancy_abort() to print `Internal compiler error in expr.c'
@@ -1785,6 +1806,29 @@ test_diagnostic_get_location_text ()
1785
1806
progname = old_progname ;
1786
1807
}
1787
1808
1809
+ /* Selftest for num_digits. */
1810
+
1811
+ static void
1812
+ test_num_digits ()
1813
+ {
1814
+ ASSERT_EQ (1 , num_digits (0 ));
1815
+ ASSERT_EQ (1 , num_digits (9 ));
1816
+ ASSERT_EQ (2 , num_digits (10 ));
1817
+ ASSERT_EQ (2 , num_digits (99 ));
1818
+ ASSERT_EQ (3 , num_digits (100 ));
1819
+ ASSERT_EQ (3 , num_digits (999 ));
1820
+ ASSERT_EQ (4 , num_digits (1000 ));
1821
+ ASSERT_EQ (4 , num_digits (9999 ));
1822
+ ASSERT_EQ (5 , num_digits (10000 ));
1823
+ ASSERT_EQ (5 , num_digits (99999 ));
1824
+ ASSERT_EQ (6 , num_digits (100000 ));
1825
+ ASSERT_EQ (6 , num_digits (999999 ));
1826
+ ASSERT_EQ (7 , num_digits (1000000 ));
1827
+ ASSERT_EQ (7 , num_digits (9999999 ));
1828
+ ASSERT_EQ (8 , num_digits (10000000 ));
1829
+ ASSERT_EQ (8 , num_digits (99999999 ));
1830
+ }
1831
+
1788
1832
/* Run all of the selftests within this file. */
1789
1833
1790
1834
void
@@ -1796,6 +1840,8 @@ diagnostic_c_tests ()
1796
1840
test_print_parseable_fixits_remove ();
1797
1841
test_print_parseable_fixits_replace ();
1798
1842
test_diagnostic_get_location_text ();
1843
+ test_num_digits ();
1844
+
1799
1845
}
1800
1846
1801
1847
} // namespace selftest
0 commit comments