all: deprecate write_str and replace it with write_string (#9369)

pull/9393/head
zakuro 2021-03-21 01:25:51 +09:00 committed by GitHub
parent b7a0c44f39
commit c8416f9a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 62 additions and 39 deletions

View File

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

View File

@ -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 {

View File

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

View File

@ -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
}

View File

@ -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

View File

@ -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)
}

View File

@ -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) ?

View File

@ -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)
}

View File

@ -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

View File

@ -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) }

View File

@ -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]

View File

@ -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)
}

View File

@ -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) } }
}
})