mirror of
https://github.com/vlang/v.git
synced 2025-09-08 06:41:58 -04:00
builtin, cgen: fix printing slice of fn call string (#19450)
This commit is contained in:
parent
b3ee304381
commit
981f76cd04
@ -530,7 +530,7 @@ pub fn (mut a array) delete_last() {
|
||||
// Alternative: Slices can also be made with [start..end] notation
|
||||
// Alternative: `.slice_ni()` will always return an array.
|
||||
fn (a array) slice(start int, _end int) array {
|
||||
mut end := _end
|
||||
mut end := if _end == 2147483647 { a.len } else { _end } // max_int
|
||||
$if !no_bounds_checking {
|
||||
if start > end {
|
||||
panic('array.slice: invalid slice index (${start} > ${end})')
|
||||
@ -565,7 +565,7 @@ fn (a array) slice(start int, _end int) array {
|
||||
// This function always return a valid array.
|
||||
fn (a array) slice_ni(_start int, _end int) array {
|
||||
// a.flags.clear(.noslices)
|
||||
mut end := _end
|
||||
mut end := if _end == 2147483647 { a.len } else { _end } // max_int
|
||||
mut start := _start
|
||||
|
||||
if start < 0 {
|
||||
|
@ -1037,7 +1037,8 @@ fn (s string) substr2(start int, _end int, end_max bool) string {
|
||||
// substr returns the string between index positions `start` and `end`.
|
||||
// Example: assert 'ABCD'.substr(1,3) == 'BC'
|
||||
[direct_array_access]
|
||||
pub fn (s string) substr(start int, end int) string {
|
||||
pub fn (s string) substr(start int, _end int) string {
|
||||
end := if _end == 2147483647 { s.len } else { _end } // max_int
|
||||
$if !no_bounds_checking {
|
||||
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
|
||||
panic('substr(${start}, ${end}) out of bounds (len=${s.len})')
|
||||
@ -1061,7 +1062,8 @@ pub fn (s string) substr(start int, end int) string {
|
||||
// version of `substr()` that is used in `a[start..end] or {`
|
||||
// return an error when the index is out of range
|
||||
[direct_array_access]
|
||||
pub fn (s string) substr_with_check(start int, end int) !string {
|
||||
pub fn (s string) substr_with_check(start int, _end int) !string {
|
||||
end := if _end == 2147483647 { s.len } else { _end } // max_int
|
||||
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
|
||||
return error('substr(${start}, ${end}) out of bounds (len=${s.len})')
|
||||
}
|
||||
@ -1085,7 +1087,7 @@ pub fn (s string) substr_with_check(start int, end int) !string {
|
||||
[direct_array_access]
|
||||
pub fn (s string) substr_ni(_start int, _end int) string {
|
||||
mut start := _start
|
||||
mut end := _end
|
||||
mut end := if _end == 2147483647 { s.len } else { _end } // max_int
|
||||
|
||||
// borders math
|
||||
if start < 0 {
|
||||
|
@ -69,7 +69,6 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
|
||||
mut tmp_opt := ''
|
||||
mut cur_line := ''
|
||||
mut gen_or := node.or_expr.kind != .absent || node.is_option
|
||||
mut tmp_left := ''
|
||||
|
||||
if sym.kind == .string {
|
||||
if node.is_gated {
|
||||
@ -90,15 +89,6 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
|
||||
}
|
||||
g.expr(node.left)
|
||||
} else if sym.kind == .array {
|
||||
if !range.has_high {
|
||||
tmp_left = g.new_tmp_var()
|
||||
tmp_type := g.typ(node.left_type)
|
||||
g.insert_before_stmt('${util.tabs(g.indent)}${tmp_type} ${tmp_left};')
|
||||
// (tmp = expr, array_slice(...))
|
||||
g.write('(${tmp_left} = ')
|
||||
g.expr(node.left)
|
||||
g.write(', ')
|
||||
}
|
||||
if node.is_gated {
|
||||
g.write('array_slice_ni(')
|
||||
} else {
|
||||
@ -107,11 +97,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
|
||||
if node.left_type.is_ptr() {
|
||||
g.write('*')
|
||||
}
|
||||
if range.has_high {
|
||||
g.expr(node.left)
|
||||
} else {
|
||||
g.write(tmp_left)
|
||||
}
|
||||
g.expr(node.left)
|
||||
} else if sym.kind == .array_fixed {
|
||||
// Convert a fixed array to V array when doing `fixed_arr[start..end]`
|
||||
info := sym.info as ast.ArrayFixed
|
||||
@ -156,17 +142,8 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
|
||||
} else if sym.kind == .array_fixed {
|
||||
info := sym.info as ast.ArrayFixed
|
||||
g.write('${info.size}')
|
||||
} else if sym.kind == .array {
|
||||
if node.left_type.is_ptr() {
|
||||
g.write('${tmp_left}->')
|
||||
} else {
|
||||
g.write('${tmp_left}.')
|
||||
}
|
||||
g.write('len)')
|
||||
} else {
|
||||
g.write('(')
|
||||
g.expr(node.left)
|
||||
g.write(').len')
|
||||
g.write('2147483647') // max_int
|
||||
}
|
||||
g.write(')')
|
||||
|
||||
|
@ -0,0 +1,2 @@
|
||||
Get called
|
||||
ello
|
15
vlib/v/slow_tests/inout/printing_slice_of_fn_call_string.vv
Normal file
15
vlib/v/slow_tests/inout/printing_slice_of_fn_call_string.vv
Normal file
@ -0,0 +1,15 @@
|
||||
struct Wrapper {
|
||||
element string
|
||||
}
|
||||
|
||||
fn (instance Wrapper) get() string {
|
||||
println("Get called")
|
||||
return instance.element
|
||||
}
|
||||
|
||||
fn main() {
|
||||
wrapper := Wrapper{
|
||||
element: "Hello"
|
||||
}
|
||||
println(wrapper.get()[1..])
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user