Skip to content

Commit d8d05e0

Browse files
authored
all: new string interpolation in pure V (#10181)
1 parent 603e577 commit d8d05e0

28 files changed

+2623
-639
lines changed

cmd/tools/vtest-cleancode.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
'examples/sokol/03_march_tracing_glsl/rt_glsl.v',
2525
'examples/sokol/04_multi_shader_glsl/rt_glsl.v',
2626
'examples/sokol/05_instancing_glsl/rt_glsl.v',
27+
'examples/sokol/06_obj_viewer/show_obj.v',
2728
'vlib/gg/m4/graphic.v',
2829
'vlib/gg/m4/m4_test.v',
2930
'vlib/gg/m4/matrix.v',

examples/sokol/06_obj_viewer/show_obj.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,4 @@ fn main() {
336336

337337
app.ticks = time.ticks()
338338
app.gg.run()
339-
}
339+
}

vlib/builtin/float.v

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import strconv
1212
// str return a `f64` as `string` in suitable notation.
1313
[inline]
1414
pub fn (x f64) str() string {
15+
unsafe {
16+
f := strconv.Float64u{
17+
f: x
18+
}
19+
if f.u == strconv.double_minus_zero {
20+
return '-0'
21+
}
22+
if f.u == strconv.double_plus_zero {
23+
return '0'
24+
}
25+
}
1526
abs_x := f64_abs(x)
1627
if abs_x >= 0.0001 && abs_x < 1.0e6 {
1728
return strconv.f64_to_str_l(x)
@@ -20,6 +31,20 @@ pub fn (x f64) str() string {
2031
}
2132
}
2233

34+
// strg return a `f64` as `string` in "g" printf format
35+
[inline]
36+
pub fn (x f64) strg() string {
37+
if x == 0 {
38+
return '0'
39+
}
40+
abs_x := f64_abs(x)
41+
if abs_x >= 0.0001 && abs_x < 1.0e6 {
42+
return strconv.f64_to_str_l_no_dot(x)
43+
} else {
44+
return strconv.ftoa_64(x)
45+
}
46+
}
47+
2348
// str returns the value of the `float_literal` as a `string`.
2449
[inline]
2550
pub fn (d float_literal) str() string {
@@ -53,6 +78,17 @@ pub fn (x f64) strlong() string {
5378
// str returns a `f32` as `string` in suitable notation.
5479
[inline]
5580
pub fn (x f32) str() string {
81+
unsafe {
82+
f := strconv.Float32u{
83+
f: x
84+
}
85+
if f.u == strconv.single_minus_zero {
86+
return '-0'
87+
}
88+
if f.u == strconv.single_plus_zero {
89+
return '0'
90+
}
91+
}
5692
abs_x := f32_abs(x)
5793
if abs_x >= 0.0001 && abs_x < 1.0e6 {
5894
return strconv.f32_to_str_l(x)
@@ -61,6 +97,20 @@ pub fn (x f32) str() string {
6197
}
6298
}
6399

100+
// strg return a `f32` as `string` in "g" printf format
101+
[inline]
102+
pub fn (x f32) strg() string {
103+
if x == 0 {
104+
return '0'
105+
}
106+
abs_x := f32_abs(x)
107+
if abs_x >= 0.0001 && abs_x < 1.0e6 {
108+
return strconv.f32_to_str_l_no_dot(x)
109+
} else {
110+
return strconv.ftoa_32(x)
111+
}
112+
}
113+
64114
// strsci returns the `f32` as a `string` in scientific notation with `digit_num` deciamals displayed, max 8 digits.
65115
// Example: assert f32(1.234).strsci(3) == '1.234e+00'
66116
[inline]

0 commit comments

Comments
 (0)