4444#define YY_DOUBLE_SUPPORTED 0
4545#endif
4646
47-
4847template <arithmetic_float T>
4948struct BenchArgs {
5049 using Type = T;
@@ -63,60 +62,61 @@ struct BenchArgs {
6362
6463namespace BenchmarkShortest {
6564
66-
6765/* *
6866 * We have that std::to_chars does not produce the shortest
6967 * representation for numbers in scientific notation, so we
7068 * optimize the string representation to be shorter.
7169 */
7270inline std::string optimize_number_string (const std::string &input) {
7371 // Check if input contains 'E' or 'e' for scientific notation
74- auto e_pos = input.find_first_of (" Ee" );
75- if ( e_pos != std::string::npos) {
72+ if ( const auto e_pos = input.find_first_of (" Ee" );
73+ e_pos != std::string::npos) {
7674 // Handle scientific notation
77- std::string mantissa = input.substr (0 , e_pos);
75+ const std::string mantissa = input.substr (0 , e_pos);
7876 std::string exponent = input.substr (e_pos + 1 );
7977
8078 // Remove leading zeros in exponent, preserving sign
81- bool negative = exponent[0 ] == ' -' ;
82- exponent.erase (0 , negative ? 1 : 0 );
79+ const bool negative = exponent[0 ] == ' -' ;
80+ const bool positive = exponent[0 ] == ' +' ;
81+ exponent.erase (0 , (negative || positive) ? 1 : 0 );
8382 exponent.erase (0 , exponent.find_first_not_of (' 0' ));
8483 if (exponent.empty ())
8584 exponent = " 0" ;
8685 if (negative && exponent != " 0" )
8786 exponent = " -" + exponent;
8887
8988 // Reconstruct the number
90- return mantissa + " E " + exponent;
89+ return mantissa + " e " + exponent;
9190 }
9291
9392 // Handle non-scientific notation
9493 if (input == " 0" || input == " -0" )
9594 return input;
9695
9796 // Determine sign
98- bool is_negative = input[0 ] == ' -' ;
99- std::string num = is_negative ? input.substr (1 ) : input;
97+ const bool is_negative = input[0 ] == ' -' ;
10098
10199 // Find first and last significant digits
102- std::string digits = num ;
103- size_t decimal_pos = digits.find (' .' );
104- if ( decimal_pos != std::string::npos) {
105- digits.erase (decimal_pos, 1 ); // Remove decimal point
100+ std::string digits = is_negative ? input. substr ( 1 ) : input ;
101+ if ( const size_t decimal_pos = digits.find (' .' );
102+ decimal_pos != std::string::npos) {
103+ digits.erase (decimal_pos, 1 ); // Remove decimal point
106104 }
107- size_t first_non_zero = digits.find_first_not_of (' 0' );
108- size_t last_non_zero = digits.find_last_not_of (' 0' );
105+ const size_t first_non_zero = digits.find_first_not_of (' 0' );
106+ const size_t last_non_zero = digits.find_last_not_of (' 0' );
109107 digits = digits.substr (first_non_zero, last_non_zero - first_non_zero + 1 );
108+
110109 // Count significant digits
111- size_t num_digits = digits.length ();
110+ const size_t num_digits = digits.length ();
112111 if (num_digits == 0 )
113112 return input;
113+
114114 // Calculate exponent
115- size_t input_decimal_pos = input.find (' .' );
116- size_t input_first_non_zero = input.find_first_not_of (' 0' );
117- size_t input_last_non_zero = input.find_last_not_of (' 0' );
115+ const size_t input_decimal_pos = input.find (' .' );
116+ const size_t input_first_non_zero = input.find_first_not_of (' 0' );
117+ const size_t input_last_non_zero = input.find_last_not_of (' 0' );
118118
119- int exponent = 0 ;
119+ int exponent;
120120 if (input_decimal_pos == std::string::npos) {
121121 // we have 123232900000
122122 exponent = (input_last_non_zero - input_first_non_zero);
@@ -126,19 +126,21 @@ inline std::string optimize_number_string(const std::string &input) {
126126 } else {
127127 // Number like 0.000123
128128 exponent =
129- -static_cast <int >(input.find_first_not_of (' 0' , input_decimal_pos + 1 ) -
130- input_decimal_pos);
129+ -static_cast <int >(input.find_first_not_of (' 0' , input_decimal_pos + 1 )
130+ - input_decimal_pos);
131131 }
132132 // Calculate scientific notation length
133- size_t mantissa_len =
134- num_digits + (num_digits > 1 ? 1 : 0 ); // Digits + optional decimal
135- size_t exponent_len = (exponent == 0 ) ? 1
136- : (exponent < 0 ? 1 : 0 ) +
137- (std::abs (exponent) < 10 ? 1
138- : std::abs (exponent) < 100 ? 2
139- : 3 );
140- size_t sci_len = mantissa_len + 1 + exponent_len +
141- (is_negative ? 1 : 0 ); // Mantissa + E + exponent + sign
133+ const size_t mantissa_len =
134+ num_digits + (num_digits > 1 ? 1 : 0 ); // Digits + optional decimal
135+ const size_t exponent_len = (exponent == 0 )
136+ ? 1
137+ : (exponent < 0 ? 1 : 0 )
138+ + (std::abs (exponent) < 10 ? 1
139+ : std::abs (exponent) < 100 ? 2
140+ : 3 );
141+ const size_t sci_len =
142+ mantissa_len + 1 + exponent_len
143+ + (is_negative ? 1 : 0 ); // Mantissa + E + exponent + sign
142144
143145 // Compare lengths
144146 if (sci_len >= input.length ())
@@ -552,7 +554,6 @@ int std_to_chars(T d, std::span<char>& buffer) {
552554#endif
553555}
554556
555-
556557} // namespace BenchmarksShortest
557558
558559template <typename T>
0 commit comments