unit: fix width formatting and improve test coverage
MODIFY
unit/dimless.go
unit/dimless.go
@@ -61,6 +61,5 @@ func (d Dimless) Format(fs fmt.State, c rune) {
}
default:
fmt.Fprintf(fs, "%%!%c(%T=%g)", c, d, float64(d))
- return
}
}

MODIFY
unit/generate_unit.go
unit/generate_unit.go
@@ -273,17 +273,18 @@ func ({{.Receiver}} {{.Name}}) Format(fs fmt.State, c rune) {
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
+ const unit = " {{.PrintString}}"
switch {
case pOk && wOk:
- fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64({{.Receiver}}))
+ fmt.Fprintf(fs, "%*.*"+string(c), pos(w-len(unit)), p, float64({{.Receiver}}))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64({{.Receiver}}))
case wOk:
- fmt.Fprintf(fs, "%*"+string(c), w, float64({{.Receiver}}))
+ fmt.Fprintf(fs, "%*"+string(c), pos(w-len(unit)), float64({{.Receiver}}))
default:
fmt.Fprintf(fs, "%"+string(c), float64({{.Receiver}}))
}
- fmt.Fprint(fs, " {{.PrintString}}")
+ fmt.Fprint(fs, unit)
default:
fmt.Fprintf(fs, "%%!%c(%T=%g {{.PrintString}})", c, {{.Receiver}}, float64({{.Receiver}}))
}

MODIFY
unit/length.go
unit/length.go
@@ -73,17 +73,18 @@ func (l Length) Format(fs fmt.State, c rune) {
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
+ const unit = " m"
switch {
case pOk && wOk:
- fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(l))
+ fmt.Fprintf(fs, "%*.*"+string(c), pos(w-len(unit)), p, float64(l))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64(l))
case wOk:
- fmt.Fprintf(fs, "%*"+string(c), w, float64(l))
+ fmt.Fprintf(fs, "%*"+string(c), pos(w-len(unit)), float64(l))
default:
fmt.Fprintf(fs, "%"+string(c), float64(l))
}
- fmt.Fprint(fs, " m")
+ fmt.Fprint(fs, unit)
default:
fmt.Fprintf(fs, "%%!%c(%T=%g m)", c, l, float64(l))
}

MODIFY
unit/mass.go
unit/mass.go
@@ -73,17 +73,18 @@ func (m Mass) Format(fs fmt.State, c rune) {
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
+ const unit = " kg"
switch {
case pOk && wOk:
- fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(m))
+ fmt.Fprintf(fs, "%*.*"+string(c), pos(w-len(unit)), p, float64(m))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64(m))
case wOk:
- fmt.Fprintf(fs, "%*"+string(c), w, float64(m))
+ fmt.Fprintf(fs, "%*"+string(c), pos(w-len(unit)), float64(m))
default:
fmt.Fprintf(fs, "%"+string(c), float64(m))
}
- fmt.Fprint(fs, " kg")
+ fmt.Fprint(fs, unit)
default:
fmt.Fprintf(fs, "%%!%c(%T=%g kg)", c, m, float64(m))
}

MODIFY
unit/time.go
unit/time.go
@@ -76,17 +76,18 @@ func (t Time) Format(fs fmt.State, c rune) {
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
+ const unit = " s"
switch {
case pOk && wOk:
- fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(t))
+ fmt.Fprintf(fs, "%*.*"+string(c), pos(w-len(unit)), p, float64(t))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64(t))
case wOk:
- fmt.Fprintf(fs, "%*"+string(c), w, float64(t))
+ fmt.Fprintf(fs, "%*"+string(c), pos(w-len(unit)), float64(t))
default:
fmt.Fprintf(fs, "%"+string(c), float64(t))
}
- fmt.Fprint(fs, " s")
+ fmt.Fprint(fs, unit)
default:
fmt.Fprintf(fs, "%%!%c(%T=%g s)", c, t, float64(t))
}

MODIFY
unit/unit_test.go
unit/unit_test.go
@@ -17,22 +17,44 @@ var formatTests = []struct {
}{
{New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%f", "9.810000 kg s^-2"},
{New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%1.f", "10 kg s^-2"},
+ {New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%10.f", "10 kg s^-2"},
{New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%.1f", "9.8 kg s^-2"},
+ {New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%20f", " 9.810000 kg s^-2"},
+ {New(9.81, Dimensions{MassDim: 1, TimeDim: -2}), "%1f", "9.810000 kg s^-2"},
{New(9.81, Dimensions{MassDim: 1, TimeDim: -2, LengthDim: 0}), "%f", "9.810000 kg s^-2"},
{New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%e", "6.626070e-34 kg^2 s^-1"},
+ {New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%20.3e", " 6.626e-34 kg^2 s^-1"},
{New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%.3e", "6.626e-34 kg^2 s^-1"},
+ {New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%25e", " 6.626070e-34 kg^2 s^-1"},
+ {New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%1e", "6.626070e-34 kg^2 s^-1"},
{New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%v", "6.62606957e-34 kg^2 s^-1"},
{New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1}), "%s", "%!s(*Unit=6.62606957e-34 kg^2 s^-1)"},
{Dimless(math.E), "%v", "2.718281828459045"},
+ {Dimless(math.E), "%.2v", "2.7"},
+ {Dimless(math.E), "%6.2v", " 2.7"},
+ {Dimless(math.E), "%20v", " 2.718281828459045"},
+ {Dimless(math.E), "%1v", "2.718281828459045"},
{Dimless(math.E), "%#v", "unit.Dimless(2.718281828459045)"},
{Dimless(math.E), "%s", "%!s(unit.Dimless=2.718281828459045)"},
{Mass(1), "%v", "1 kg"},
+ {Mass(0.45), "%.1v", "0.5 kg"},
+ {Mass(0.45), "%10.1v", " 0.5 kg"},
+ {Mass(0.45), "%10v", " 0.45 kg"},
+ {Mass(0.45), "%1v", "0.45 kg"},
{Mass(1), "%#v", "unit.Mass(1)"},
{Mass(1), "%s", "%!s(unit.Mass=1 kg)"},
{Length(1.61619926e-35), "%v", "1.61619926e-35 m"},
+ {Length(1.61619926e-35), "%.2v", "1.6e-35 m"},
+ {Length(1.61619926e-35), "%10.2v", " 1.6e-35 m"},
+ {Length(1.61619926e-35), "%20v", " 1.61619926e-35 m"},
+ {Length(1.61619926e-35), "%1v", "1.61619926e-35 m"},
{Length(1.61619926e-35), "%#v", "unit.Length(1.61619926e-35)"},
{Length(1.61619926e-35), "%s", "%!s(unit.Length=1.61619926e-35 m)"},
{Time(15.2), "%v", "15.2 s"},
+ {Time(15.2), "%.2v", "15 s"},
+ {Time(15.2), "%6.2v", " 15 s"},
+ {Time(15.2), "%10v", " 15.2 s"},
+ {Time(15.2), "%1v", "15.2 s"},
{Time(15.2), "%#v", "unit.Time(15.2)"},
{Time(15.2), "%s", "%!s(unit.Time=15.2 s)"},
}

MODIFY
unit/unittype.go
unit/unittype.go
@@ -321,22 +321,28 @@ func (u *Unit) Format(fs fmt.State, c rune) {
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
+ if u.formatted == "" && len(u.dimensions) > 0 {
+ u.formatted = u.dimensions.String()
+ }
switch {
case pOk && wOk:
- fmt.Fprintf(fs, "%*.*"+string(c), w, p, u.value)
+ fmt.Fprintf(fs, "%*.*"+string(c), pos(w-len(u.formatted)-1), p, u.value)
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, u.value)
case wOk:
- fmt.Fprintf(fs, "%*"+string(c), w, u.value)
+ fmt.Fprintf(fs, "%*"+string(c), pos(w-len(u.formatted)-1), u.value)
default:
fmt.Fprintf(fs, "%"+string(c), u.value)
}
+ fmt.Fprintf(fs, " %s", u.formatted)
default:
fmt.Fprintf(fs, "%%!%c(*Unit=%g)", c, u)
- return
}
- if u.formatted == "" && len(u.dimensions) > 0 {
- u.formatted = u.dimensions.String()
}
- fmt.Fprintf(fs, " %s", u.formatted)
+
+func pos(a int) int {
+ if a < 0 {
+ return 0
+ }
+ return a
}

unit: fix width formatting and improve test coverage