| 168 | |
| 169 | template <typename Result> |
| 170 | void prettify_string(const char *buffer, std::size_t length, int k, int min_exp, int max_exp, Result& result) |
| 171 | { |
| 172 | int nb_digits = (int)length; |
| 173 | int offset; |
| 174 | /* v = buffer * 10^k |
| 175 | kk is such that 10^(kk-1) <= v < 10^kk |
| 176 | this way kk gives the position of the decimal point. |
| 177 | */ |
| 178 | int kk = nb_digits + k; |
| 179 | |
| 180 | if (nb_digits <= kk && kk <= max_exp) |
| 181 | { |
| 182 | /* the first digits are already in. Add some 0s and call it a day. */ |
| 183 | /* the max_exp is a personal choice. Only 16 digits could possibly be relevant. |
| 184 | * Basically we want to print 12340000000 rather than 1234.0e7 or 1.234e10 */ |
| 185 | for (int i = 0; i < nb_digits; ++i) |
| 186 | { |
| 187 | result.push_back(buffer[i]); |
| 188 | } |
| 189 | for (int i = nb_digits; i < kk; ++i) |
| 190 | { |
| 191 | result.push_back('0'); |
| 192 | } |
| 193 | result.push_back('.'); |
| 194 | result.push_back('0'); |
| 195 | } |
| 196 | else if (0 < kk && kk <= max_exp) |
| 197 | { |
| 198 | /* comma number. Just insert a '.' at the correct location. */ |
| 199 | for (int i = 0; i < kk; ++i) |
| 200 | { |
| 201 | result.push_back(buffer[i]); |
| 202 | } |
| 203 | result.push_back('.'); |
| 204 | for (int i = kk; i < nb_digits; ++i) |
| 205 | { |
| 206 | result.push_back(buffer[i]); |
| 207 | } |
| 208 | } |
| 209 | else if (min_exp < kk && kk <= 0) |
| 210 | { |
| 211 | offset = 2 - kk; |
| 212 | |
| 213 | result.push_back('0'); |
| 214 | result.push_back('.'); |
| 215 | for (int i = 2; i < offset; ++i) |
| 216 | result.push_back('0'); |
| 217 | for (int i = 0; i < nb_digits; ++i) |
| 218 | { |
| 219 | result.push_back(buffer[i]); |
| 220 | } |
| 221 | } |
| 222 | else if (nb_digits == 1) |
| 223 | { |
| 224 | result.push_back(buffer[0]); |
| 225 | result.push_back('e'); |
| 226 | fill_exponent(kk - 1, result); |
| 227 | } |
no test coverage detected