mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
string(bytes_array) cast
This commit is contained in:
parent
7f512eaf72
commit
c3c6648c57
@ -2665,13 +2665,22 @@ fn (p mut Parser) cast(typ string) string {
|
|||||||
p.expected_type = ''
|
p.expected_type = ''
|
||||||
// `string(buffer)` => `tos2(buffer)`
|
// `string(buffer)` => `tos2(buffer)`
|
||||||
// `string(buffer, len)` => `tos(buffer, len)`
|
// `string(buffer, len)` => `tos(buffer, len)`
|
||||||
if typ == 'string' && (expr_typ == 'byte*' || expr_typ == 'byteptr') {
|
// `string(bytes_array, len)` => `tos(bytes_array.data, len)`
|
||||||
|
is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr'
|
||||||
|
is_bytearr := expr_typ == 'array_byte'
|
||||||
|
if typ == 'string' && (is_byteptr || is_bytearr) {
|
||||||
if p.tok == .comma {
|
if p.tok == .comma {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
p.cgen.set_placeholder(pos, 'tos(')
|
p.cgen.set_placeholder(pos, 'tos(')
|
||||||
|
if is_bytearr {
|
||||||
|
p.gen('.data')
|
||||||
|
}
|
||||||
p.gen(', ')
|
p.gen(', ')
|
||||||
p.check_types(p.expression(), 'int')
|
p.check_types(p.expression(), 'int')
|
||||||
} else {
|
} else {
|
||||||
|
if is_bytearr {
|
||||||
|
p.gen('.data')
|
||||||
|
}
|
||||||
p.cgen.set_placeholder(pos, 'tos2(')
|
p.cgen.set_placeholder(pos, 'tos2(')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -328,4 +328,6 @@ fn test_bytes_to_string() {
|
|||||||
buf[4] = `o`
|
buf[4] = `o`
|
||||||
assert string(buf) == 'hello'
|
assert string(buf) == 'hello'
|
||||||
assert string(buf, 2) == 'he'
|
assert string(buf, 2) == 'he'
|
||||||
|
bytes := [`h`, `e`, `l`, `l`, `o`]
|
||||||
|
assert string(bytes) == 'hello'
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user