mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
raw strings with r'raw'
This commit is contained in:
parent
ebc50432c7
commit
6a0599b5f4
@ -1670,6 +1670,11 @@ fn (p mut Parser) name_expr() string {
|
|||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
mut name := p.lit
|
mut name := p.lit
|
||||||
|
// Raw string (`s := r'hello \n ')
|
||||||
|
if name == 'r' && p.peek() == .str {
|
||||||
|
p.string_expr()
|
||||||
|
return 'string'
|
||||||
|
}
|
||||||
p.fgen(name)
|
p.fgen(name)
|
||||||
// known_type := p.table.known_type(name)
|
// known_type := p.table.known_type(name)
|
||||||
orig_name := name
|
orig_name := name
|
||||||
@ -2186,14 +2191,14 @@ enum IndexType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_index_type(typ string) IndexType {
|
fn get_index_type(typ string) IndexType {
|
||||||
if typ.starts_with('map_') { return IndexType.map }
|
if typ.starts_with('map_') { return .map }
|
||||||
if typ == 'string' { return IndexType.str }
|
if typ == 'string' { return .str }
|
||||||
if typ.starts_with('array_') || typ == 'array' { return IndexType.array }
|
if typ.starts_with('array_') || typ == 'array' { return .array }
|
||||||
if typ == 'byte*' || typ == 'byteptr' || typ.contains('*') {
|
if typ == 'byte*' || typ == 'byteptr' || typ.contains('*') {
|
||||||
return IndexType.ptr
|
return .ptr
|
||||||
}
|
}
|
||||||
if typ[0] == `[` { return IndexType.fixed_array }
|
if typ[0] == `[` { return .fixed_array }
|
||||||
return IndexType.noindex
|
return .noindex
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||||
@ -2762,11 +2767,15 @@ fn format_str(_str string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) string_expr() {
|
fn (p mut Parser) string_expr() {
|
||||||
|
is_raw := p.tok == .name && p.lit == 'r'
|
||||||
|
if is_raw {
|
||||||
|
p.next()
|
||||||
|
}
|
||||||
str := p.lit
|
str := p.lit
|
||||||
// No ${}, just return a simple string
|
// No ${}, just return a simple string
|
||||||
if p.peek() != .dollar {
|
if p.peek() != .dollar || is_raw {
|
||||||
p.fgen('\'$str\'')
|
p.fgen("'$str'")
|
||||||
f := format_str(str)
|
f := if is_raw { str.replace('\\', '\\\\') } else { format_str(str) }
|
||||||
// `C.puts('hi')` => `puts("hi");`
|
// `C.puts('hi')` => `puts("hi");`
|
||||||
/*
|
/*
|
||||||
Calling a C function sometimes requires a call to a string method
|
Calling a C function sometimes requires a call to a string method
|
||||||
@ -2776,7 +2785,7 @@ fn (p mut Parser) string_expr() {
|
|||||||
p.gen('"$f"')
|
p.gen('"$f"')
|
||||||
}
|
}
|
||||||
else if p.is_sql {
|
else if p.is_sql {
|
||||||
p.gen('\'$str\'')
|
p.gen("'$str'")
|
||||||
}
|
}
|
||||||
else if p.is_js {
|
else if p.is_js {
|
||||||
p.gen('"$f"')
|
p.gen('"$f"')
|
||||||
@ -2790,7 +2799,6 @@ fn (p mut Parser) string_expr() {
|
|||||||
$if js {
|
$if js {
|
||||||
p.error('js backend does not support string formatting yet')
|
p.error('js backend does not support string formatting yet')
|
||||||
}
|
}
|
||||||
// tmp := p.get_tmp()
|
|
||||||
p.is_alloc = true // $ interpolation means there's allocation
|
p.is_alloc = true // $ interpolation means there's allocation
|
||||||
mut args := '"'
|
mut args := '"'
|
||||||
mut format := '"'
|
mut format := '"'
|
||||||
|
@ -284,6 +284,6 @@ fn test_multi() {
|
|||||||
assert a[0][2] == 3
|
assert a[0][2] == 3
|
||||||
assert a[1][2] == 6
|
assert a[1][2] == 6
|
||||||
// TODO
|
// TODO
|
||||||
//b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
|
b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
|
||||||
//assert b[0][0][0] == 1
|
assert b[0][0][0] == 1
|
||||||
}
|
}
|
||||||
|
@ -464,3 +464,10 @@ fn test_repeat() {
|
|||||||
s := 'V! '
|
s := 'V! '
|
||||||
assert s.repeat(5) == 'V! V! V! V! V! '
|
assert s.repeat(5) == 'V! V! V! V! V! '
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_raw() {
|
||||||
|
raw := r'raw\nstring'
|
||||||
|
lines := raw.split('\n')
|
||||||
|
assert lines.len == 1
|
||||||
|
println('raw string: "$raw"')
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user