Skip to content

Commit

Permalink
replace ugly tos2(bytes) with string(bytes)
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jun 27, 2019
1 parent fda7cae commit 90c0791
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 39 deletions.
4 changes: 0 additions & 4 deletions builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion compiler/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 14 additions & 15 deletions compiler/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion examples/VCasino/VCasino.v
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ fn main() {
}
}
game_loop()
}
}
2 changes: 1 addition & 1 deletion examples/log.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ fn main(){
l.set_level(log.DEBUG)
l.debug('debug')
l.fatal('fatal')
}
}
4 changes: 2 additions & 2 deletions gl/gl.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''
}

Expand Down
5 changes: 1 addition & 4 deletions glfw/glfw.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions http/http_mac.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)))
}

// ////////////////
Expand Down
2 changes: 1 addition & 1 deletion http/http_win.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion log/log.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ pub fn (l Log) debug(s string){
f := termcolor.blue('D')
println('[$f]$s')
}
}
}
8 changes: 4 additions & 4 deletions os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -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(){}

Expand All @@ -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(' ')
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -473,4 +473,4 @@ fn on_segfault(f voidptr) {
# sa.sa_flags = SA_SIGINFO;
# sigaction(SIGSEGV, &sa, 0);
}
}
}

0 comments on commit 90c0791

Please sign in to comment.