From c8416f9a54b77c2b2098aada823741438bd23cd4 Mon Sep 17 00:00:00 2001 From: zakuro Date: Sun, 21 Mar 2021 01:25:51 +0900 Subject: [PATCH] all: deprecate write_str and replace it with write_string (#9369) --- examples/net_udp_server_and_client.v | 2 +- examples/websocket/client-server/client.v | 4 +- examples/websocket/ping.v | 2 +- vlib/net/tcp_simple_client_server_test.v | 12 +++--- vlib/net/tcp_test.v | 2 +- vlib/net/udp.v | 5 +++ vlib/net/udp_test.v | 2 +- vlib/net/unix/stream_nix.v | 6 +++ vlib/net/unix/unix_test.v | 2 +- vlib/os/os_test.v | 4 +- vlib/picohttpparser/response.v | 50 +++++++++++++---------- vlib/x/websocket/websocket_client.v | 8 +++- vlib/x/websocket/websocket_test.v | 2 +- 13 files changed, 62 insertions(+), 39 deletions(-) diff --git a/examples/net_udp_server_and_client.v b/examples/net_udp_server_and_client.v index 77abffd5bc..6051c37a20 100644 --- a/examples/net_udp_server_and_client.v +++ b/examples/net_udp_server_and_client.v @@ -36,7 +36,7 @@ fn main() { } else {} } - c.write_str(line) ? + c.write_string(line) ? read, _ := c.read(mut buf) ? println('server : ' + buf[0..read].bytestr()) } diff --git a/examples/websocket/client-server/client.v b/examples/websocket/client-server/client.v index 14f2bace63..c6d475906c 100644 --- a/examples/websocket/client-server/client.v +++ b/examples/websocket/client-server/client.v @@ -4,7 +4,7 @@ import os import x.websocket import term -// This client should be compiled an run in different konsoles +// This client should be compiled an run in different konsoles // it connects to the server who will broadcast your messages // to all other connected clients fn main() { @@ -16,7 +16,7 @@ fn main() { if line == '' { break } - ws.write_str(line) ? + ws.write_string(line) ? } ws.close(1000, 'normal') or { println(term.red('panicing $err')) } unsafe { diff --git a/examples/websocket/ping.v b/examples/websocket/ping.v index a68fa73c51..a79ec827a4 100644 --- a/examples/websocket/ping.v +++ b/examples/websocket/ping.v @@ -75,7 +75,7 @@ fn write_echo(mut ws websocket.Client) ? { message := 'echo this' for i := 0; i <= 10; i++ { // Server will send pings every 30 seconds - ws.write_str(message) or { println('panicing writing $err') } + ws.write_string(message) or { println('panicing writing $err') } time.sleep(100 * time.millisecond) } ws.close(1000, 'normal') or { println('panicing $err') } diff --git a/vlib/net/tcp_simple_client_server_test.v b/vlib/net/tcp_simple_client_server_test.v index dffe45edf3..321ed04579 100644 --- a/vlib/net/tcp_simple_client_server_test.v +++ b/vlib/net/tcp_simple_client_server_test.v @@ -30,7 +30,7 @@ fn test_socket() { cleanup(mut server, mut client, mut socket) } message := 'Hello World' - socket.write_str(message) or { + socket.write_string(message) or { assert false return } @@ -62,7 +62,7 @@ fn test_socket_write_and_read() { cleanup(mut server, mut client, mut socket) } message1 := 'a message 1' - socket.write_str(message1) or { assert false } + socket.write_string(message1) or { assert false } mut rbuf := []byte{len: message1.len} client.read(mut rbuf) or { assert false @@ -82,7 +82,7 @@ fn test_socket_read_line() { } message1, message2 := 'message1', 'message2' message := '$message1\n$message2\n' - socket.write_str(message) or { assert false } + socket.write_string(message) or { assert false } assert true // line1 := reader.read_line() or { @@ -114,7 +114,7 @@ fn test_socket_write_fail_without_panic() { } // TODO: fix segfaulting on Solaris for i := 0; i < 3; i++ { - socket.write_str(message2) or { + socket.write_string(message2) or { println('write to a socket without a recipient should produce an option fail: $err | $message2') assert true } @@ -130,11 +130,11 @@ fn test_socket_read_line_long_line_without_eol() { cleanup(mut server, mut client, mut socket) } message := strings.repeat_string('123', 400) - socket.write_str(message) or { + socket.write_string(message) or { assert false return } - socket.write_str('\n') or { + socket.write_string('\n') or { assert false return } diff --git a/vlib/net/tcp_test.v b/vlib/net/tcp_test.v index 557300c73d..136e5e2074 100644 --- a/vlib/net/tcp_test.v +++ b/vlib/net/tcp_test.v @@ -32,7 +32,7 @@ fn echo() ? { c.close() or { } } data := 'Hello from vlib/net!' - c.write_str(data) ? + c.write_string(data) ? mut buf := []byte{len: 4096} read := c.read(mut buf) ? assert read == data.len diff --git a/vlib/net/udp.v b/vlib/net/udp.v index be6ba90724..f0e81734b2 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -54,7 +54,12 @@ pub fn (mut c UdpConn) write(buf []byte) ?int { return c.write_ptr(buf.data, buf.len) } +[deprecated: 'use UdpConn.write_string() instead'] pub fn (mut c UdpConn) write_str(s string) ?int { + return c.write_string(s) +} + +pub fn (mut c UdpConn) write_string(s string) ?int { return c.write_ptr(s.str, s.len) } diff --git a/vlib/net/udp_test.v b/vlib/net/udp_test.v index 100cf3d6c7..71fe36370a 100644 --- a/vlib/net/udp_test.v +++ b/vlib/net/udp_test.v @@ -19,7 +19,7 @@ fn echo() ? { } data := 'Hello from vlib/net!' - c.write_str(data) ? + c.write_string(data) ? mut buf := []byte{len: 100, init: 0} read, addr := c.read(mut buf) ? diff --git a/vlib/net/unix/stream_nix.v b/vlib/net/unix/stream_nix.v index 44508b4adc..9d1dcd290e 100644 --- a/vlib/net/unix/stream_nix.v +++ b/vlib/net/unix/stream_nix.v @@ -204,7 +204,13 @@ pub fn (mut c StreamConn) write(bytes []byte) ?int { } // write_str blocks and attempts to write all data +[deprecated: 'use StreamConn.write_string() instead'] pub fn (mut c StreamConn) write_str(s string) ?int { + return c.write_string(s) +} + +// write_string blocks and attempts to write all data +pub fn (mut c StreamConn) write_string(s string) ?int { return c.write_ptr(s.str, s.len) } diff --git a/vlib/net/unix/unix_test.v b/vlib/net/unix/unix_test.v index 2b5ffce6c6..c26cfae49a 100644 --- a/vlib/net/unix/unix_test.v +++ b/vlib/net/unix/unix_test.v @@ -39,7 +39,7 @@ fn echo() ? { c.close() or { } } data := 'Hello from vlib/net!' - c.write_str(data) ? + c.write_string(data) ? mut buf := []byte{len: 4096} read := c.read(mut buf) ? assert read == data.len diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 107ea5b528..2bdbed2eac 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -38,7 +38,7 @@ fn test_open_file() { os.File{} } mut file := os.open_file(filename, 'w+', 0o666) or { panic(err) } - file.write_str(hello) or { panic(err) } + file.write_string(hello) or { panic(err) } file.close() assert hello.len == os.file_size(filename) read_hello := os.read_file(filename) or { panic('error reading file $filename') } @@ -87,7 +87,7 @@ fn test_create_file() { filename := './test1.txt' hello := 'hello world!' mut f := os.create(filename) or { panic(err) } - f.write_str(hello) or { panic(err) } + f.write_string(hello) or { panic(err) } f.close() assert hello.len == os.file_size(filename) os.rm(filename) or { panic(err) } diff --git a/vlib/picohttpparser/response.v b/vlib/picohttpparser/response.v index 4b6a0a9354..8a29cdad88 100644 --- a/vlib/picohttpparser/response.v +++ b/vlib/picohttpparser/response.v @@ -10,7 +10,13 @@ pub mut: } [inline] -fn (mut r Response) write_str(s string) { +[deprecated: 'use Response.write_string() instead'] +pub fn (mut r Response) write_str(s string) { + r.write_string(s) +} + +[inline] +fn (mut r Response) write_string(s string) { unsafe { C.memcpy(r.buf, s.str, s.len) r.buf += s.len @@ -19,89 +25,89 @@ fn (mut r Response) write_str(s string) { [inline] pub fn (mut r Response) http_ok() &Response { - r.write_str('HTTP/1.1 200 OK\r\n') + r.write_string('HTTP/1.1 200 OK\r\n') return r } [inline] pub fn (mut r Response) header(k string, v string) &Response { - r.write_str(k) - r.write_str(': ') - r.write_str(v) - r.write_str('\r\n') + r.write_string(k) + r.write_string(': ') + r.write_string(v) + r.write_string('\r\n') return r } [inline] pub fn (mut r Response) header_date() &Response { - r.write_str('Date: ') + r.write_string('Date: ') unsafe { r.buf += cpy(r.buf, r.date, 29) } - r.write_str('\r\n') + r.write_string('\r\n') return r } [inline] pub fn (mut r Response) header_server() &Response { - r.write_str('Server: V\r\n') + r.write_string('Server: V\r\n') return r } [inline] pub fn (mut r Response) content_type(s string) &Response { - r.write_str('Content-Type: ') - r.write_str(s) - r.write_str('\r\n') + r.write_string('Content-Type: ') + r.write_string(s) + r.write_string('\r\n') return r } [inline] pub fn (mut r Response) html() &Response { - r.write_str('Content-Type: text/html\r\n') + r.write_string('Content-Type: text/html\r\n') return r } [inline] pub fn (mut r Response) plain() &Response { - r.write_str('Content-Type: text/plain\r\n') + r.write_string('Content-Type: text/plain\r\n') return r } [inline] pub fn (mut r Response) json() &Response { - r.write_str('Content-Type: application/json\r\n') + r.write_string('Content-Type: application/json\r\n') return r } [inline] pub fn (mut r Response) body(body string) { - r.write_str('Content-Length: ') + r.write_string('Content-Length: ') unsafe { r.buf += C.u64toa(r.buf, body.len) } - r.write_str('\r\n\r\n') - r.write_str(body) + r.write_string('\r\n\r\n') + r.write_string(body) } [inline] pub fn (mut r Response) http_404() { - r.write_str('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n') + r.write_string('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n') } [inline] pub fn (mut r Response) http_405() { - r.write_str('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n') + r.write_string('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n') } [inline] pub fn (mut r Response) http_500() { - r.write_str('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n') + r.write_string('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n') } [inline] pub fn (mut r Response) raw(response string) { - r.write_str(response) + r.write_string(response) } [inline] diff --git a/vlib/x/websocket/websocket_client.v b/vlib/x/websocket/websocket_client.v index bfa5b52416..b468fcc8d9 100644 --- a/vlib/x/websocket/websocket_client.v +++ b/vlib/x/websocket/websocket_client.v @@ -307,8 +307,14 @@ pub fn (mut ws Client) write(bytes []byte, code OPCode) ?int { return ws.write_ptr(byteptr(bytes.data), bytes.len, code) } -// write_str, writes a string with a websocket texttype to socket +// write_string, writes a string with a websocket texttype to socket +[deprecated: 'use Client.write_string() instead'] pub fn (mut ws Client) write_str(str string) ?int { + return ws.write_string(str) +} + +// write_str, writes a string with a websocket texttype to socket +pub fn (mut ws Client) write_string(str string) ?int { return ws.write_ptr(str.str, str.len, .text_frame) } diff --git a/vlib/x/websocket/websocket_test.v b/vlib/x/websocket/websocket_test.v index f37cc79834..6567efc80c 100644 --- a/vlib/x/websocket/websocket_test.v +++ b/vlib/x/websocket/websocket_test.v @@ -40,7 +40,7 @@ fn start_server(listen_port int) ? { }) ? s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? { match msg.opcode { - .pong { ws.write_str('pong') or { panic(err) } } + .pong { ws.write_string('pong') or { panic(err) } } else { ws.write(msg.payload, msg.opcode) or { panic(err) } } } })