(buf: &mut F, f: Fl)
| 288 | } |
| 289 | |
| 290 | fn format_float<F, Fl>(buf: &mut F, f: Fl) -> Nestable |
| 291 | where |
| 292 | F: FormatBuffer, |
| 293 | Fl: NumFloat + RyuFloat, |
| 294 | { |
| 295 | // Use ryu rather than the standard library. ryu uses scientific notation |
| 296 | // when possible, which better matches PostgreSQL. The standard library's |
| 297 | // `ToString` implementations print all available digits, which is rather |
| 298 | // verbose. |
| 299 | // |
| 300 | // Note that we have to fix up ryu's formatting in a few cases to match |
| 301 | // PostgreSQL. PostgreSQL spells out "Infinity" in full, never emits a |
| 302 | // trailing ".0", formats positive exponents as e.g. "1e+10" rather than |
| 303 | // "1e10", and emits a negative sign for negative zero. If we need to speed |
| 304 | // up float formatting, we can look into forking ryu and making these edits |
| 305 | // directly, but for now it doesn't seem worth it. |
| 306 | |
| 307 | match f.classify() { |
| 308 | FpCategory::Infinite if f.is_sign_negative() => buf.write_str("-Infinity"), |
| 309 | FpCategory::Infinite => buf.write_str("Infinity"), |
| 310 | FpCategory::Nan => buf.write_str("NaN"), |
| 311 | FpCategory::Zero if f.is_sign_negative() => buf.write_str("-0"), |
| 312 | _ => { |
| 313 | debug_assert!(f.is_finite()); |
| 314 | let mut ryu_buf = ryu::Buffer::new(); |
| 315 | let mut s = ryu_buf.format_finite(f); |
| 316 | if let Some(trimmed) = s.strip_suffix(".0") { |
| 317 | s = trimmed; |
| 318 | } |
| 319 | let mut chars = s.chars().peekable(); |
| 320 | while let Some(ch) = chars.next() { |
| 321 | buf.write_char(ch); |
| 322 | if ch == 'e' && chars.peek() != Some(&'-') { |
| 323 | buf.write_char('+'); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | Nestable::Yes |
| 330 | } |
| 331 | |
| 332 | /// Parses an `f32` from `s`. |
| 333 | pub fn parse_float32(s: &str) -> Result<f32, ParseError> { |
no test coverage detected