1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![deny(missing_docs)]

//! Wrapper for using libc's `printf("%g")` format for your floating point output

use libc::c_char;
use std::fmt;
use std::io::Write;

/// A wrapper around floats providing an implementation of `Display` which uses
/// the underlying `libc`'s `printf()` with format `"%g"`, for when you need to
/// match exactly what C a program would output.
///
/// `Float` should be a floating point type, i.e. `f32` or `f64`.
///
/// Available formatting options:
/// ```
/// use gpoint::GPoint;
///
/// assert!(format!("{}",    GPoint(42f32))  == "42");
/// assert!(format!("{}",    GPoint(42f64))  == "42");
/// assert!(format!("{:.3}", GPoint(1.2345)) == "1.23");
/// assert!(format!("{:4}",  GPoint(42.))    == "  42");
/// assert!(format!("{:-4}", GPoint(42.))    == "42  ");
/// assert!(format!("{:04}", GPoint(42.))    == "0042");
/// assert!(format!("{:+}",  GPoint(42.))    == "+42");
/// assert!(format!("{:#4}", GPoint(42.))    == "42.0000");
/// ```
#[derive(Debug, Default, Clone, Copy)]
#[repr(transparent)]
pub struct GPoint<Float>(
    /// Your floating point number you want to `Display`
    pub Float,
);

impl std::fmt::Display for GPoint<f64> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_g(f, self.0)
    }
}

impl std::fmt::Display for GPoint<f32> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_g(f, self.0 as f64)
    }
}

const FORMAT_SIZE: usize = 20;
const NUMSTR_SIZE: usize = 200;

fn fmt_g(formatter: &mut fmt::Formatter<'_>, value: f64) -> fmt::Result {
    let mut format = [0u8; FORMAT_SIZE];
    let numstr = [0u8; NUMSTR_SIZE];
    let mut fmtbuf = std::io::Cursor::new(&mut format[..FORMAT_SIZE - 1]); // keep final 0

    let zero_pad = if formatter.sign_aware_zero_pad() {
        "0"
    } else {
        ""
    };
    let sign_pad = if formatter.sign_minus() {
        "-"
    } else if formatter.sign_plus() {
        "+"
    } else {
        ""
    };
    let alternate = if formatter.alternate() { "#" } else { "" };
    match (formatter.width(), formatter.precision()) {
        (None, None) => write!(fmtbuf, "%{}{}g", alternate, sign_pad),
        (Some(w), None) => write!(fmtbuf, "%{}{}{}{}g", alternate, sign_pad, zero_pad, w),
        (None, Some(p)) => write!(fmtbuf, "%{}.{}g", alternate, p),
        (Some(w), Some(p)) => write!(fmtbuf, "%{}{}{}{}.{}g", alternate, sign_pad, zero_pad, w, p),
    }
    .map_err(|_| fmt::Error)?;
    let nbchars = unsafe {
        libc::snprintf(
            numstr.as_ptr() as *mut c_char,
            NUMSTR_SIZE,
            format.as_ptr() as *const c_char,
            value,
        )
    };
    // check if we (virtually) overflowed our buffer
    if nbchars < 0 || nbchars >= NUMSTR_SIZE as i32 {
        return Err(fmt::Error);
    }
    let numstr = &numstr[..nbchars as usize];

    formatter.write_str(unsafe { std::str::from_utf8_unchecked(numstr) })
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn simple() {
        for (num, res) in [
            (42., "42"),
            (f64::NAN, "nan"),
            (-f64::INFINITY, "-inf"),
            (f64::INFINITY, "inf"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{}", num), res);
        }
    }
    #[test]
    fn pad() {
        for (num, res) in [
            (42., "      42"),
            (f64::NAN, "     nan"),
            (-f64::INFINITY, "    -inf"),
            (f64::INFINITY, "     inf"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:8}", num), res);
        }
    }
    #[test]
    fn zero_pad() {
        for (num, res) in [
            (42., "00000042"),
            (-1.01, "-0001.01"),
            (f64::NAN, "     nan"),
            (-f64::INFINITY, "    -inf"),
            (f64::INFINITY, "     inf"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:08}", num), res);
        }
    }
    #[test]
    fn minus_pad() {
        for (num, res) in [
            (42., "42      "),
            (-1.01, "-1.01   "),
            (f64::NAN, "nan     "),
            (-f64::INFINITY, "-inf    "),
            (f64::INFINITY, "inf     "),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:-8}", num), res);
        }
    }
    #[test]
    fn plus() {
        for (num, res) in [
            (42., "+42"),
            (-1.01, "-1.01"),
            (f64::NAN, "+nan"),
            (-f64::INFINITY, "-inf"),
            (f64::INFINITY, "+inf"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:+}", num), res);
        }
    }
    #[test]
    fn plus_pad() {
        for (num, res) in [
            (42., "     +42"),
            (-1.01, "   -1.01"),
            (f64::NAN, "    +nan"),
            (-f64::INFINITY, "    -inf"),
            (f64::INFINITY, "    +inf"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:+8}", num), res);
        }
    }
    #[test]
    fn prec() {
        for (num, res) in [
            (42., "42"),
            (-1.012345678901, "-1.01"),
            (-42.8952, "-42.9"),
            (4321., "4.32e+03"),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:.3}", num), res);
        }
    }
    #[test]
    fn alt() {
        for (num, res) in [
            (42., "42.0000"),
            (-1.012345678901, "-1.01235"),
            (432100., "432100."),
        ] {
            let num = GPoint(num);
            assert_eq!(&format!("{:#}", num), res);
        }
    }
    #[test]
    fn in_context() {
        assert_eq!(&format!("answer={}!", GPoint(42.)), "answer=42!");
    }
}