Skip to content

Commit

Permalink
all: new string interpolation in pure V (#10181)
Browse files Browse the repository at this point in the history
  • Loading branch information
penguindark committed May 24, 2021
1 parent 603e577 commit d8d05e0
Show file tree
Hide file tree
Showing 28 changed files with 2,623 additions and 639 deletions.
1 change: 1 addition & 0 deletions cmd/tools/vtest-cleancode.v
Expand Up @@ -24,6 +24,7 @@ const (
'examples/sokol/03_march_tracing_glsl/rt_glsl.v',
'examples/sokol/04_multi_shader_glsl/rt_glsl.v',
'examples/sokol/05_instancing_glsl/rt_glsl.v',
'examples/sokol/06_obj_viewer/show_obj.v',
'vlib/gg/m4/graphic.v',
'vlib/gg/m4/m4_test.v',
'vlib/gg/m4/matrix.v',
Expand Down
2 changes: 1 addition & 1 deletion examples/sokol/06_obj_viewer/show_obj.v
Expand Up @@ -336,4 +336,4 @@ fn main() {

app.ticks = time.ticks()
app.gg.run()
}
}
50 changes: 50 additions & 0 deletions vlib/builtin/float.v
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down

0 comments on commit d8d05e0

Please sign in to comment.