1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: new string interpolation in pure V (#10181)

This commit is contained in:
penguindark
2021-05-24 04:20:45 +02:00
committed by GitHub
parent 603e57745f
commit d8d05e0106
28 changed files with 2624 additions and 640 deletions

View File

@@ -12,6 +12,17 @@ import strconv
// str return a `f64` as `string` in suitable notation.
[inline]
pub fn (x f64) str() string {
unsafe {
f := strconv.Float64u{
f: x
}
if f.u == strconv.double_minus_zero {
return '-0'
}
if f.u == strconv.double_plus_zero {
return '0'
}
}
abs_x := f64_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f64_to_str_l(x)
@@ -20,6 +31,20 @@ pub fn (x f64) str() string {
}
}
// strg return a `f64` as `string` in "g" printf format
[inline]
pub fn (x f64) strg() string {
if x == 0 {
return '0'
}
abs_x := f64_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f64_to_str_l_no_dot(x)
} else {
return strconv.ftoa_64(x)
}
}
// str returns the value of the `float_literal` as a `string`.
[inline]
pub fn (d float_literal) str() string {
@@ -53,6 +78,17 @@ pub fn (x f64) strlong() string {
// str returns a `f32` as `string` in suitable notation.
[inline]
pub fn (x f32) str() string {
unsafe {
f := strconv.Float32u{
f: x
}
if f.u == strconv.single_minus_zero {
return '-0'
}
if f.u == strconv.single_plus_zero {
return '0'
}
}
abs_x := f32_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f32_to_str_l(x)
@@ -61,6 +97,20 @@ pub fn (x f32) str() string {
}
}
// strg return a `f32` as `string` in "g" printf format
[inline]
pub fn (x f32) strg() string {
if x == 0 {
return '0'
}
abs_x := f32_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f32_to_str_l_no_dot(x)
} else {
return strconv.ftoa_32(x)
}
}
// strsci returns the `f32` as a `string` in scientific notation with `digit_num` deciamals displayed, max 8 digits.
// Example: assert f32(1.234).strsci(3) == '1.234e+00'
[inline]