builtin: fix interpolation bug for `${x:g/G}`, where `x` is a zero or an infinity (#10271)

pull/10281/head
penguindark 2021-05-30 18:30:18 +02:00 committed by GitHub
parent aa861d1cac
commit 1343787009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import strconv
fn test_common_atoi() {
// test common cases
assert '70zzz'.int() == 70
@ -182,4 +184,38 @@ fn test_signed_cast() {
s := n.str() + 'z'
assert s.i16() == n
}
// test g format
{
mut u := strconv.Float64u{
u: strconv.double_plus_zero
}
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
u.u = strconv.double_minus_zero
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
u.u = strconv.double_plus_infinity
assert '${u.f:g}' == '+inf'
assert '${u.f:G}' == '+INF'
u.u = strconv.double_minus_infinity
assert '${u.f:g}' == '-inf'
assert '${u.f:G}' == '-INF'
}
{
mut u := strconv.Float32u{
u: strconv.single_plus_zero
}
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
u.u = strconv.single_minus_zero
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
u.u = strconv.single_plus_infinity
assert '${u.f:g}' == '+inf'
assert '${u.f:G}' == '+INF'
u.u = strconv.single_minus_infinity
assert '${u.f:g}' == '-inf'
assert '${u.f:G}' == '-INF'
}
}

View File

@ -450,6 +450,37 @@ fn (data StrIntpData) get_fmt_format(mut sb strings.Builder) {
sb.write_string(f)
f.free()
} else {
// Manage +/-0
if data.d.d_f32 == strconv.single_plus_zero {
tmp_str := '0'
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
return
}
if data.d.d_f32 == strconv.single_minus_zero {
tmp_str := '-0'
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
return
}
// Manage +/-INF
if data.d.d_f32 == strconv.single_plus_infinity {
mut tmp_str := '+inf'
if upper_case {
tmp_str = '+INF'
}
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
}
if data.d.d_f32 == strconv.single_minus_infinity {
mut tmp_str := '-inf'
if upper_case {
tmp_str = '-INF'
}
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
}
if data.d.d_f32 < 0 {
bf.positive = false
}
@ -487,6 +518,37 @@ fn (data StrIntpData) get_fmt_format(mut sb strings.Builder) {
sb.write_string(f)
f.free()
} else {
// Manage +/-0
if data.d.d_f64 == strconv.double_plus_zero {
tmp_str := '0'
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
return
}
if data.d.d_f64 == strconv.double_minus_zero {
tmp_str := '-0'
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
return
}
// Manage +/-INF
if data.d.d_f64 == strconv.double_plus_infinity {
mut tmp_str := '+inf'
if upper_case {
tmp_str = '+INF'
}
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
}
if data.d.d_f64 == strconv.double_minus_infinity {
mut tmp_str := '-inf'
if upper_case {
tmp_str = '-INF'
}
strconv.format_str_sb(tmp_str, bf, mut sb)
tmp_str.free()
}
if data.d.d_f64 < 0 {
bf.positive = false
}