Skip to content

Commit

Permalink
builtin, cgen: fix printing slice of fn call string (#19450)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 27, 2023
1 parent b3ee304 commit 981f76c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions vlib/builtin/array.v
Expand Up @@ -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})')
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions vlib/builtin/string.v
Expand Up @@ -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})')
Expand All @@ -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})')
}
Expand All @@ -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 {
Expand Down
27 changes: 2 additions & 25 deletions vlib/v/gen/c/index.v
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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(')')

Expand Down
2 changes: 2 additions & 0 deletions vlib/v/slow_tests/inout/printing_slice_of_fn_call_string.out
@@ -0,0 +1,2 @@
Get called
ello
15 changes: 15 additions & 0 deletions vlib/v/slow_tests/inout/printing_slice_of_fn_call_string.vv
@@ -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..])
}

0 comments on commit 981f76c

Please sign in to comment.