1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

replace ugly tos2(bytes) with string(bytes)

This commit is contained in:
Alexander Medvednikov 2019-06-27 12:27:46 +02:00
parent fda7caef93
commit 90c0791345
11 changed files with 30 additions and 39 deletions

View File

@ -54,10 +54,6 @@ fn tos2(s byteptr) string {
return res
}
fn tos_no_len(s byteptr) string {
return tos2(s)
}
fn (a string) clone() string {
mut b := string {
len: a.len

View File

@ -160,7 +160,6 @@ fn (p mut Parser) fn_decl() {
}
if p.tok == PLUS || p.tok == MINUS || p.tok == MUL {
f.name = p.tok.str()
println('!!! $f.name')
p.next()
}
else {

View File

@ -1274,16 +1274,13 @@ fn (p mut Parser) name_expr() string {
if p.table.known_type(name) {
// float(5), byte(0), (*int)(ptr) etc
if p.peek() == LPAR || (deref && p.peek() == RPAR) {
// println('CASTT $name')
if deref {
// p.check(RPAR)
// p.next()
name += '*'
}
else if ptr {
name += '*'
}
p.gen('(/*casttt*/')
p.gen('(')
mut typ := p.cast(name)
p.gen(')')
for p.tok == DOT {
@ -2470,27 +2467,29 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
// `f32(3)`
// tok is `f32` or `)` if `(*int)(ptr)`
fn (p mut Parser) cast(typ string) string {
// typ := p.lit
if p.file_path.contains('test') {
println('CAST TYP=$typ tok=')
p.print_tok()
}
p.gen('($typ)(')
// p.fgen(typ)
p.next()
pos := p.cgen.add_placeholder()
if p.tok == RPAR {
// skip `)` if it's `(*int)(ptr)`, not `int(a)`
p.ptr_cast = true
p.next()
}
p.check(LPAR)
p.gen('/*77*/')
expr_typ := p.bool_expression()
p.check(RPAR)
p.gen(')')
if typ == 'string' && expr_typ == 'int' {
p.error('cannot convert `$expr_typ` to `$typ`')
// `string(buffer)` => `tos2(buffer)`
if typ == 'string' && (expr_typ == 'byte*' || expr_typ == 'byteptr') {
p.cgen.set_placeholder(pos, 'tos2(')
}
// `string(234)` => error
else if typ == 'string' && expr_typ == 'int' {
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
}
else {
p.cgen.set_placeholder(pos, '($typ)(')
// p.fgen(typ)
}
p.gen(')')
return typ
}

View File

@ -144,4 +144,4 @@ fn main() {
}
}
game_loop()
}
}

View File

@ -9,4 +9,4 @@ fn main(){
l.set_level(log.DEBUG)
l.debug('debug')
l.fatal('fatal')
}
}

View File

@ -86,14 +86,14 @@ pub fn shader_info_log(shader int) string {
# char infoLog[512];
# glGetShaderInfoLog(shader, 512, NULL, infoLog);
# printf("log=%s\n", infoLog);
# return tos_no_len(infoLog);
# return tos2(infoLog);
return ''
}
pub fn get_program_info_log(program int) string {
# char infoLog[512];
# glGetProgramInfoLog(program, 1024, NULL, infoLog);
# return tos_no_len(infoLog);
# return tos2(infoLog);
return ''
}

View File

@ -255,10 +255,7 @@ pub fn key_pressed(wnd voidptr, key int) bool {
// TODO not mut
pub fn (w mut Window) get_clipboard_text() string {
return tos2(C.glfwGetClipboardString(w.data))
// # char *c = glfwGetClipboardString(w->data);
// # return tos_no_len(c);
// return ''
return string(byteptr(C.glfwGetClipboardString(w.data)))
}
pub fn (w &Window) set_clipboard_text(s string) {

View File

@ -64,11 +64,11 @@ fn write_fn(contents byteptr, size, nmemb int, _mem *MemoryStruct) int {
}
contents += start + 1
// printf("GOOD CONTEnTS=%s\n", contents);
s := tos_no_len(contents)
s := string(contents)
// mem.ws_func('kek', 0)
# mem->ws_func(s, mem->user_ptr);
}
mut c := tos_no_len(contents)
mut c := string(contents)
c = c.trim_space()
// Need to clone because libcurl reuses this memory
mem.strings << c.clone()
@ -146,7 +146,7 @@ fn (req &Request) do() Response {
err := C.curl_easy_strerror(res)
println('curl_easy_perform() failed: $err')
}
body := chunk.strings.join('')// tos_no_len(chunk.memory)
body := chunk.strings.join('')// string(chunk.memory)
// chunk.strings.free()
// resp.headers = hchunk.strings
if hchunk.strings.len == 0 {
@ -192,11 +192,11 @@ fn (req &Request) do() Response {
}
fn unescape(s string) string {
return tos2(C.curl_unescape(s.cstr(), s.len))
return string(byteptr(C.curl_unescape(s.cstr(), s.len)))
}
fn escape(s string) string {
return tos2(C.curl_escape(s.cstr(), s.len))
return string(byteptr(C.curl_escape(s.cstr(), s.len)))
}
// ////////////////

View File

@ -156,7 +156,7 @@ fn (req &Request) do() Response {
println('ireadfile()')
buf[nr_read] = 0
C.printf('buf="%s"\n', buf)
s += tos2(buf)// TODO perf
s += string(buf)// TODO perf
nr_read = 0
}
C.InternetCloseHandle(request)

View File

@ -50,4 +50,4 @@ pub fn (l Log) debug(s string){
f := termcolor.blue('D')
println('[$f]$s')
}
}
}

View File

@ -27,8 +27,8 @@ import const (
)
fn C.getline(voidptr, voidptr, voidptr) int
fn C.ftell(fp voidptr) int
fn C.getenv(byteptr) byteptr
fn todo_remove(){}
@ -45,7 +45,7 @@ fn init_os_args(argc int, _argv *byteptr) []string {
}
fn parse_windows_cmd_line(cmd byteptr) []string {
s := tos2(cmd)
s := string(cmd)
return s.split(' ')
}
@ -313,7 +313,7 @@ pub fn getenv(key string) string {
if isnil(s) {
return ''
}
return tos2(s)
return string(s)
}
// `file_exists` returns true if `path` exists.
@ -473,4 +473,4 @@ fn on_segfault(f voidptr) {
# sa.sa_flags = SA_SIGINFO;
# sigaction(SIGSEGV, &sa, 0);
}
}
}