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

all: replace []byte with []u8

This commit is contained in:
Alexander Medvednikov
2022-04-15 15:35:35 +03:00
parent 0527ac633e
commit fb192d949b
164 changed files with 533 additions and 533 deletions

View File

@@ -25,13 +25,13 @@ fn init_alphabets() map[string]Alphabet {
struct Alphabet {
mut:
decode []i8 = []i8{len: 128, init: -1}
encode []byte = []byte{len: 58}
encode []u8 = []u8{len: 58}
}
// str returns an Alphabet encode table byte array as a string
pub fn (alphabet Alphabet) str() string {
// i guess i had a brain fart here. Why would I actually use this code?!
// mut str := []byte{}
// mut str := []u8{}
// for entry in alphabet.encode {
// str << entry
// }

View File

@@ -15,7 +15,7 @@ pub fn encode_int_walpha(input int, alphabet Alphabet) ?string {
return error(@MOD + '.' + @FN + ': input must be greater than zero')
}
mut buffer := []byte{}
mut buffer := []u8{}
mut i := input
for i > 0 {
@@ -55,7 +55,7 @@ pub fn encode_walpha(input string, alphabet Alphabet) string {
// integer simplification of
// ceil(log(256)/log(58))
mut out := []byte{len: sz}
mut out := []u8{len: sz}
mut i := 0
mut high := 0
mut carry := u32(0)
@@ -131,7 +131,7 @@ pub fn decode_walpha(str string, alphabet Alphabet) ?string {
mut c := u64(0)
// the 32-bit algorithm stretches the result up to 2x
mut binu := []byte{len: 2 * ((b58sz * 406 / 555) + 1)}
mut binu := []u8{len: 2 * ((b58sz * 406 / 555) + 1)}
mut outi := []u32{len: (b58sz + 3) / 4}
for _, r in str {

View File

@@ -215,7 +215,7 @@ pub fn decode_str(data string) string {
}
}
// encode encodes the `[]byte` value passed in `data` to base64.
// encode encodes the `[]u8` value passed in `data` to base64.
// Please note: base64 encoding returns a `string` that is ~ 4/3 larger than the input.
// Please note: If you need to encode many strings repeatedly, take a look at `encode_in_buffer`.
// Example: assert base64.encode('V in base 64') == 'ViBpbiBiYXNlIDY0'

View File

@@ -17,7 +17,7 @@ const (
// url_decode returns a decoded URL `string` version of
// the a base64 url encoded `string` passed in `data`.
pub fn url_decode(data string) []byte {
pub fn url_decode(data string) []u8 {
mut result := data.replace_each(['-', '+', '_', '/'])
match result.len % 4 {
// Pad with trailing '='s
@@ -42,7 +42,7 @@ pub fn url_decode_str(data string) string {
// url_encode returns a base64 URL encoded `string` version
// of the value passed in `data`.
pub fn url_encode(data []byte) string {
pub fn url_encode(data []u8) string {
return encode(data).replace_each(['+', '-', '/', '_', '=', ''])
}

View File

@@ -4,7 +4,7 @@ fn test_long_encoding() {
repeats := 1000
input_size := 3000
s_original := []byte{len: input_size, init: `a`}
s_original := []u8{len: input_size, init: `a`}
s_encoded := base64.encode(s_original)
s_encoded_bytes := s_encoded.bytes()
s_decoded := base64.decode(s_encoded)
@@ -20,7 +20,7 @@ fn test_long_encoding() {
}
//
encoded_size := base64.encode_in_buffer(s_original, ebuffer)
mut encoded_in_buf := []byte{len: encoded_size}
mut encoded_in_buf := []u8{len: encoded_size}
unsafe { C.memcpy(encoded_in_buf.data, ebuffer, encoded_size) }
assert input_size * 4 / 3 == encoded_size
assert encoded_in_buf[0] == `Y`
@@ -37,7 +37,7 @@ fn test_long_encoding() {
decoded_size := base64.decode_in_buffer(s_encoded, dbuffer)
assert decoded_size == input_size
mut decoded_in_buf := []byte{len: decoded_size}
mut decoded_in_buf := []u8{len: decoded_size}
unsafe { C.memcpy(decoded_in_buf.data, dbuffer, decoded_size) }
assert decoded_in_buf == s_original

View File

@@ -137,9 +137,9 @@ fn test_decode_in_buffer_bytes() {
TestPair{'fooba', 'Zm9vYmE='},
TestPair{'foobar', 'Zm9vYmFy'},
]
mut src_dec_buf := []byte{len: 8}
mut src_enc_buf := []byte{len: 8}
mut out_buf := []byte{len: 8}
mut src_dec_buf := []u8{len: 8}
mut src_enc_buf := []u8{len: 8}
mut out_buf := []u8{len: 8}
for p in rfc4648_pairs {
src_dec_buf = p.decoded.bytes()

View File

@@ -5,26 +5,26 @@ module binary
// Little Endian
[inline]
pub fn little_endian_u16(b []byte) u16 {
pub fn little_endian_u16(b []u8) u16 {
_ = b[1] // bounds check
return u16(b[0]) | (u16(b[1]) << u16(8))
}
[inline]
pub fn little_endian_put_u16(mut b []byte, v u16) {
pub fn little_endian_put_u16(mut b []u8, v u16) {
_ = b[1] // bounds check
b[0] = u8(v)
b[1] = u8(v >> u16(8))
}
[inline]
pub fn little_endian_u32(b []byte) u32 {
pub fn little_endian_u32(b []u8) u32 {
_ = b[3] // bounds check
return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24))
}
[inline]
pub fn little_endian_put_u32(mut b []byte, v u32) {
pub fn little_endian_put_u32(mut b []u8, v u32) {
_ = b[3] // bounds check
b[0] = u8(v)
b[1] = u8(v >> u32(8))
@@ -33,13 +33,13 @@ pub fn little_endian_put_u32(mut b []byte, v u32) {
}
[inline]
pub fn little_endian_u64(b []byte) u64 {
pub fn little_endian_u64(b []u8) u64 {
_ = b[7] // bounds check
return u64(b[0]) | (u64(b[1]) << u64(8)) | (u64(b[2]) << u64(16)) | (u64(b[3]) << u64(24)) | (u64(b[4]) << u64(32)) | (u64(b[5]) << u64(40)) | (u64(b[6]) << u64(48)) | (u64(b[7]) << u64(56))
}
[inline]
pub fn little_endian_put_u64(mut b []byte, v u64) {
pub fn little_endian_put_u64(mut b []u8, v u64) {
_ = b[7] // bounds check
b[0] = u8(v)
b[1] = u8(v >> u64(8))
@@ -53,26 +53,26 @@ pub fn little_endian_put_u64(mut b []byte, v u64) {
// Big Endian
[inline]
pub fn big_endian_u16(b []byte) u16 {
pub fn big_endian_u16(b []u8) u16 {
_ = b[1] // bounds check
return u16(b[1]) | (u16(b[0]) << u16(8))
}
[inline]
pub fn big_endian_put_u16(mut b []byte, v u16) {
pub fn big_endian_put_u16(mut b []u8, v u16) {
_ = b[1] // bounds check
b[0] = u8(v >> u16(8))
b[1] = u8(v)
}
[inline]
pub fn big_endian_u32(b []byte) u32 {
pub fn big_endian_u32(b []u8) u32 {
_ = b[3] // bounds check
return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24))
}
[inline]
pub fn big_endian_put_u32(mut b []byte, v u32) {
pub fn big_endian_put_u32(mut b []u8, v u32) {
_ = b[3] // bounds check
b[0] = u8(v >> u32(24))
b[1] = u8(v >> u32(16))
@@ -81,13 +81,13 @@ pub fn big_endian_put_u32(mut b []byte, v u32) {
}
[inline]
pub fn big_endian_u64(b []byte) u64 {
pub fn big_endian_u64(b []u8) u64 {
_ = b[7] // bounds check
return u64(b[7]) | (u64(b[6]) << u64(8)) | (u64(b[5]) << u64(16)) | (u64(b[4]) << u64(24)) | (u64(b[3]) << u64(32)) | (u64(b[2]) << u64(40)) | (u64(b[1]) << u64(48)) | (u64(b[0]) << u64(56))
}
[inline]
pub fn big_endian_put_u64(mut b []byte, v u64) {
pub fn big_endian_put_u64(mut b []u8, v u64) {
_ = b[7] // bounds check
b[0] = u8(v >> u64(56))
b[1] = u8(v >> u64(48))

View File

@@ -5,7 +5,7 @@ import strings
// decode converts a hex string into an array of bytes. The expected
// input format is 2 ASCII characters for each output byte. If the provided
// string length is not a multiple of 2, an implicit `0` is prepended to it.
pub fn decode(s string) ?[]byte {
pub fn decode(s string) ?[]u8 {
mut hex_str := s
if hex_str.len >= 2 {
if s[0] == `0` && (s[1] == `x` || s[1] == `X`) {
@@ -13,7 +13,7 @@ pub fn decode(s string) ?[]byte {
}
}
if hex_str.len == 0 {
return []byte{}
return []u8{}
} else if hex_str.len == 1 {
return [char2nibble(hex_str[0]) ?]
} else if hex_str.len == 2 {
@@ -27,7 +27,7 @@ pub fn decode(s string) ?[]byte {
val = (val << 4) | char2nibble(hex_str[1]) ?
}
// set cap to hex_str.len/2 rounded up
mut bytes := []byte{len: 1, cap: (hex_str.len + 1) >> 1, init: val}
mut bytes := []u8{len: 1, cap: (hex_str.len + 1) >> 1, init: val}
// iterate over every 2 bytes
// the start index depends on if hex_str.len is odd
for i := 2 - (hex_str.len & 1); i < hex_str.len; i += 2 {
@@ -41,7 +41,7 @@ pub fn decode(s string) ?[]byte {
// encode converts an array of bytes into a string of ASCII hex bytes. The
// output will always be a string with length a multiple of 2.
[manualfree]
pub fn encode(bytes []byte) string {
pub fn encode(bytes []u8) string {
mut sb := strings.new_builder(bytes.len * 2)
for b in bytes {
sb.write_string(b.hex())