From ae6a25f44e2f7a852aa3008e3fd58b005c40751e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 15 Apr 2022 16:24:02 +0300 Subject: [PATCH] websocket, utf: u8 fixes --- vlib/builtin/utf8.v | 8 ++++---- vlib/net/websocket/message.v | 24 ++++++++++++------------ vlib/v/tests/alias_basic_types_test.v | 2 +- vlib/x/json2/encoder.v | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index 112493b4c9..dfd3269264 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -3,7 +3,7 @@ // that can be found in the LICENSE file. module builtin -pub fn utf8_char_len(b byte) int { +pub fn utf8_char_len(b u8) int { return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1 } @@ -22,7 +22,7 @@ pub fn utf32_to_str(code u32) string { } [manualfree; unsafe] -pub fn utf32_to_str_no_malloc(code u32, buf &byte) string { +pub fn utf32_to_str_no_malloc(code u32, buf &u8) string { unsafe { len := utf32_decode_to_buffer(code, buf) if len == 0 { @@ -34,7 +34,7 @@ pub fn utf32_to_str_no_malloc(code u32, buf &byte) string { } [manualfree; unsafe] -pub fn utf32_decode_to_buffer(code u32, buf &byte) int { +pub fn utf32_decode_to_buffer(code u32, buf &u8) int { unsafe { icode := int(code) // Prevents doing casts everywhere mut buffer := &u8(buf) @@ -112,7 +112,7 @@ pub fn (_bytes []u8) utf8_to_utf32() ?rune { } // Calculate length to read from the first byte -fn utf8_len(c byte) int { +fn utf8_len(c u8) int { mut b := 0 mut x := c if (x & 240) != 0 { diff --git a/vlib/net/websocket/message.v b/vlib/net/websocket/message.v index 237a1784e2..d2c7e49e60 100644 --- a/vlib/net/websocket/message.v +++ b/vlib/net/websocket/message.v @@ -11,7 +11,7 @@ const ( // Fragment represents a websocket data fragment struct Fragment { - data []u8 // included data payload data in a fragment + data []u8 // included data payload data in a fragment opcode OPCode // interpretation of the payload data } @@ -22,14 +22,14 @@ mut: header_len int = 2 // size of total frame frame_size int = 2 - fin bool // true if final fragment of message - rsv1 bool // reserved for future use in websocket RFC - rsv2 bool // reserved for future use in websocket RFC - rsv3 bool // reserved for future use in websocket RFC - opcode OPCode // interpretation of the payload data - has_mask bool // true if the payload data is masked - payload_len int // payload length - masking_key [4]u8 // all frames from client to server is masked with this key + fin bool // true if final fragment of message + rsv1 bool // reserved for future use in websocket RFC + rsv2 bool // reserved for future use in websocket RFC + rsv3 bool // reserved for future use in websocket RFC + opcode OPCode // interpretation of the payload data + has_mask bool // true if the payload data is masked + payload_len int // payload length + masking_key [4]u8 // all frames from client to server is masked with this key } const ( @@ -86,7 +86,7 @@ fn (mut ws Client) read_payload(frame &Frame) ?[]u8 { return []u8{} } mut buffer := []u8{cap: frame.payload_len} - mut read_buf := [1]byte{} + mut read_buf := [1]u8{} mut bytes_read := 0 for bytes_read < frame.payload_len { len := ws.socket_read_ptr(&read_buf[0], 1) ? @@ -209,10 +209,10 @@ fn (ws Client) opcode_from_fragments() OPCode { // parse_frame_header parses next message by decoding the incoming frames pub fn (mut ws Client) parse_frame_header() ?Frame { - mut buffer := [256]byte{} + mut buffer := [256]u8{} mut bytes_read := 0 mut frame := Frame{} - mut rbuff := [1]byte{} + mut rbuff := [1]u8{} mut mask_end_byte := 0 for ws.state == .open { read_bytes := ws.socket_read_ptr(&rbuff[0], 1) ? diff --git a/vlib/v/tests/alias_basic_types_test.v b/vlib/v/tests/alias_basic_types_test.v index 71efa4a6f9..2b6cd5224a 100644 --- a/vlib/v/tests/alias_basic_types_test.v +++ b/vlib/v/tests/alias_basic_types_test.v @@ -1,4 +1,4 @@ -type MyByte = byte +type MyByte = u8 type MyInt = int diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index a1684822c6..6f573503c3 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -9,7 +9,7 @@ import strings // Encoder encodes the an `Any` type into JSON representation. // It provides parameters in order to change the end result. pub struct Encoder { - newline byte + newline u8 newline_spaces_count int escape_unicode bool = true }