Skip to content

Commit 981f76c

Browse files
authored
builtin, cgen: fix printing slice of fn call string (#19450)
1 parent b3ee304 commit 981f76c

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

vlib/builtin/array.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub fn (mut a array) delete_last() {
530530
// Alternative: Slices can also be made with [start..end] notation
531531
// Alternative: `.slice_ni()` will always return an array.
532532
fn (a array) slice(start int, _end int) array {
533-
mut end := _end
533+
mut end := if _end == 2147483647 { a.len } else { _end } // max_int
534534
$if !no_bounds_checking {
535535
if start > end {
536536
panic('array.slice: invalid slice index (${start} > ${end})')
@@ -565,7 +565,7 @@ fn (a array) slice(start int, _end int) array {
565565
// This function always return a valid array.
566566
fn (a array) slice_ni(_start int, _end int) array {
567567
// a.flags.clear(.noslices)
568-
mut end := _end
568+
mut end := if _end == 2147483647 { a.len } else { _end } // max_int
569569
mut start := _start
570570

571571
if start < 0 {

vlib/builtin/string.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,8 @@ fn (s string) substr2(start int, _end int, end_max bool) string {
10371037
// substr returns the string between index positions `start` and `end`.
10381038
// Example: assert 'ABCD'.substr(1,3) == 'BC'
10391039
[direct_array_access]
1040-
pub fn (s string) substr(start int, end int) string {
1040+
pub fn (s string) substr(start int, _end int) string {
1041+
end := if _end == 2147483647 { s.len } else { _end } // max_int
10411042
$if !no_bounds_checking {
10421043
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
10431044
panic('substr(${start}, ${end}) out of bounds (len=${s.len})')
@@ -1061,7 +1062,8 @@ pub fn (s string) substr(start int, end int) string {
10611062
// version of `substr()` that is used in `a[start..end] or {`
10621063
// return an error when the index is out of range
10631064
[direct_array_access]
1064-
pub fn (s string) substr_with_check(start int, end int) !string {
1065+
pub fn (s string) substr_with_check(start int, _end int) !string {
1066+
end := if _end == 2147483647 { s.len } else { _end } // max_int
10651067
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
10661068
return error('substr(${start}, ${end}) out of bounds (len=${s.len})')
10671069
}
@@ -1085,7 +1087,7 @@ pub fn (s string) substr_with_check(start int, end int) !string {
10851087
[direct_array_access]
10861088
pub fn (s string) substr_ni(_start int, _end int) string {
10871089
mut start := _start
1088-
mut end := _end
1090+
mut end := if _end == 2147483647 { s.len } else { _end } // max_int
10891091

10901092
// borders math
10911093
if start < 0 {

vlib/v/gen/c/index.v

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
6969
mut tmp_opt := ''
7070
mut cur_line := ''
7171
mut gen_or := node.or_expr.kind != .absent || node.is_option
72-
mut tmp_left := ''
7372

7473
if sym.kind == .string {
7574
if node.is_gated {
@@ -90,15 +89,6 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
9089
}
9190
g.expr(node.left)
9291
} else if sym.kind == .array {
93-
if !range.has_high {
94-
tmp_left = g.new_tmp_var()
95-
tmp_type := g.typ(node.left_type)
96-
g.insert_before_stmt('${util.tabs(g.indent)}${tmp_type} ${tmp_left};')
97-
// (tmp = expr, array_slice(...))
98-
g.write('(${tmp_left} = ')
99-
g.expr(node.left)
100-
g.write(', ')
101-
}
10292
if node.is_gated {
10393
g.write('array_slice_ni(')
10494
} else {
@@ -107,11 +97,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
10797
if node.left_type.is_ptr() {
10898
g.write('*')
10999
}
110-
if range.has_high {
111-
g.expr(node.left)
112-
} else {
113-
g.write(tmp_left)
114-
}
100+
g.expr(node.left)
115101
} else if sym.kind == .array_fixed {
116102
// Convert a fixed array to V array when doing `fixed_arr[start..end]`
117103
info := sym.info as ast.ArrayFixed
@@ -156,17 +142,8 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
156142
} else if sym.kind == .array_fixed {
157143
info := sym.info as ast.ArrayFixed
158144
g.write('${info.size}')
159-
} else if sym.kind == .array {
160-
if node.left_type.is_ptr() {
161-
g.write('${tmp_left}->')
162-
} else {
163-
g.write('${tmp_left}.')
164-
}
165-
g.write('len)')
166145
} else {
167-
g.write('(')
168-
g.expr(node.left)
169-
g.write(').len')
146+
g.write('2147483647') // max_int
170147
}
171148
g.write(')')
172149

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Get called
2+
ello
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Wrapper {
2+
element string
3+
}
4+
5+
fn (instance Wrapper) get() string {
6+
println("Get called")
7+
return instance.element
8+
}
9+
10+
fn main() {
11+
wrapper := Wrapper{
12+
element: "Hello"
13+
}
14+
println(wrapper.get()[1..])
15+
}

0 commit comments

Comments
 (0)