mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: byte => u8
This commit is contained in:
@ -24,32 +24,32 @@ fn test_integer_from_u64() {
|
||||
|
||||
fn test_integer_from_bytes() {
|
||||
assert big.integer_from_bytes([]).hex() == '0'
|
||||
assert big.integer_from_bytes([byte(0)]).hex() == '0'
|
||||
assert big.integer_from_bytes([byte(0x13), 0x37]).hex() == '1337'
|
||||
assert big.integer_from_bytes([byte(0x13), 0x37, 0xca]).hex() == '1337ca'
|
||||
assert big.integer_from_bytes([byte(0x13), 0x37, 0xca, 0xfe]).hex() == '1337cafe'
|
||||
assert big.integer_from_bytes([byte(0x13), 0x37, 0xca, 0xfe, 0xba]).hex() == '1337cafeba'
|
||||
assert big.integer_from_bytes([byte(0x13), 0x37, 0xca, 0xfe, 0xba, 0xbe]).hex() == '1337cafebabe'
|
||||
assert big.integer_from_bytes([u8(0)]).hex() == '0'
|
||||
assert big.integer_from_bytes([u8(0x13), 0x37]).hex() == '1337'
|
||||
assert big.integer_from_bytes([u8(0x13), 0x37, 0xca]).hex() == '1337ca'
|
||||
assert big.integer_from_bytes([u8(0x13), 0x37, 0xca, 0xfe]).hex() == '1337cafe'
|
||||
assert big.integer_from_bytes([u8(0x13), 0x37, 0xca, 0xfe, 0xba]).hex() == '1337cafeba'
|
||||
assert big.integer_from_bytes([u8(0x13), 0x37, 0xca, 0xfe, 0xba, 0xbe]).hex() == '1337cafebabe'
|
||||
|
||||
mut bytes := []byte{cap: 1024}
|
||||
mut expected := ''
|
||||
for i := 0; i < bytes.cap; i++ {
|
||||
bytes << byte(i)
|
||||
expected = expected + byte(i).hex()
|
||||
bytes << u8(i)
|
||||
expected = expected + u8(i).hex()
|
||||
}
|
||||
assert big.integer_from_bytes(bytes).hex() == expected.trim_left('0')
|
||||
}
|
||||
|
||||
fn test_bytes() {
|
||||
result1, sign1 := big.integer_from_u64(0x1337cafebabe).bytes()
|
||||
assert result1 == [byte(0x13), 0x37, 0xca, 0xfe, 0xba, 0xbe]
|
||||
assert result1 == [u8(0x13), 0x37, 0xca, 0xfe, 0xba, 0xbe]
|
||||
assert sign1 == 1
|
||||
|
||||
mut bytes := []byte{cap: 1024}
|
||||
mut expected := ''
|
||||
for i := 0; i < bytes.cap; i++ {
|
||||
bytes << byte(i | 1)
|
||||
expected = expected + byte(i).hex()
|
||||
bytes << u8(i | 1)
|
||||
expected = expected + u8(i).hex()
|
||||
}
|
||||
result2, sign2 := big.integer_from_bytes(bytes).bytes()
|
||||
assert result2 == bytes
|
||||
@ -334,8 +334,8 @@ fn test_lshift() {
|
||||
assert big.integer_from_int(45).lshift(4) == big.integer_from_int(45 * 16)
|
||||
assert big.integer_from_int(45).lshift(5) == big.integer_from_int(45 * 32)
|
||||
assert big.integer_from_u64(0xabcedabcde).lshift(20) == big.integer_from_u64(0xabcedabcde00000)
|
||||
assert big.integer_from_bytes([byte(1), 1, 1]).lshift(56) == big.integer_from_bytes([
|
||||
byte(1),
|
||||
assert big.integer_from_bytes([u8(1), 1, 1]).lshift(56) == big.integer_from_bytes([
|
||||
u8(1),
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
@ -352,7 +352,7 @@ fn test_rshift() {
|
||||
assert big.integer_from_int(45).rshift(3) == big.integer_from_int(5)
|
||||
assert big.integer_from_int(0x13374956).rshift(16) == big.integer_from_int(0x1337)
|
||||
assert big.integer_from_bytes([
|
||||
byte(1),
|
||||
u8(1),
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
@ -362,7 +362,7 @@ fn test_rshift() {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
]).rshift(56) == big.integer_from_bytes([byte(1), 1, 1])
|
||||
]).rshift(56) == big.integer_from_bytes([u8(1), 1, 1])
|
||||
}
|
||||
|
||||
fn test_isqrt() {
|
||||
|
@ -787,7 +787,7 @@ pub fn (a Integer) bytes() ([]byte, int) {
|
||||
mut offset := 24
|
||||
mut non_zero_found := false
|
||||
for index := a.digits.len - 1; index >= 0; {
|
||||
value := byte((a.digits[index] & mask) >> offset)
|
||||
value := u8((a.digits[index] & mask) >> offset)
|
||||
non_zero_found = non_zero_found || value != 0
|
||||
if non_zero_found {
|
||||
result << value
|
||||
|
@ -149,7 +149,7 @@ fn pow2(k int) Integer {
|
||||
}
|
||||
}
|
||||
|
||||
// optimized left shift of full byte(s) in place. byte_nb must be positive
|
||||
// optimized left shift of full u8(s) in place. byte_nb must be positive
|
||||
fn lshift_byte_in_place(mut a []u32, byte_nb int) {
|
||||
a_len := a.len
|
||||
// control or allocate capacity
|
||||
|
@ -6,10 +6,10 @@ module bits
|
||||
const (
|
||||
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
|
||||
de_bruijn32 = u32(0x077CB531)
|
||||
de_bruijn32tab = [byte(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13,
|
||||
de_bruijn32tab = [u8(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13,
|
||||
23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
|
||||
de_bruijn64 = u64(0x03f79d71b4ca8b09)
|
||||
de_bruijn64tab = [byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 62, 47,
|
||||
de_bruijn64tab = [u8(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 62, 47,
|
||||
59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 63, 55, 48, 27, 60, 41, 37, 16,
|
||||
46, 35, 44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7,
|
||||
6]
|
||||
@ -147,7 +147,7 @@ pub fn ones_count_64(x u64) int {
|
||||
// This function's execution time does not depend on the inputs.
|
||||
[inline]
|
||||
pub fn rotate_left_8(x byte, k int) byte {
|
||||
n := byte(8)
|
||||
n := u8(8)
|
||||
s := u8(k) & (n - u8(1))
|
||||
return (x << s) | (x >> (n - s))
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
module bits
|
||||
|
||||
const (
|
||||
ntz_8_tab = [byte(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00,
|
||||
ntz_8_tab = [u8(0x08), 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00,
|
||||
0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01,
|
||||
0x00, 0x02, 0x00, 0x01, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00,
|
||||
0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03,
|
||||
@ -22,7 +22,7 @@ const (
|
||||
0x01, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02,
|
||||
0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00,
|
||||
0x02, 0x00, 0x01, 0x00]
|
||||
pop_8_tab = [byte(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03,
|
||||
pop_8_tab = [u8(0x00), 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03,
|
||||
0x02, 0x03, 0x03, 0x04, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03,
|
||||
0x04, 0x03, 0x04, 0x04, 0x05, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03,
|
||||
0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03,
|
||||
@ -40,7 +40,7 @@ const (
|
||||
0x06, 0x07, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05,
|
||||
0x06, 0x06, 0x07, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07,
|
||||
0x06, 0x07, 0x07, 0x08]
|
||||
rev_8_tab = [byte(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0,
|
||||
rev_8_tab = [u8(0x00), 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0,
|
||||
0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58,
|
||||
0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94,
|
||||
0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c,
|
||||
@ -58,7 +58,7 @@ const (
|
||||
0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37,
|
||||
0xb7, 0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf,
|
||||
0x3f, 0xbf, 0x7f, 0xff]
|
||||
len_8_tab = [byte(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04,
|
||||
len_8_tab = [u8(0x00), 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04,
|
||||
0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||
|
@ -15,7 +15,7 @@ fn test_bits() {
|
||||
i = 1
|
||||
for x in 0 .. 8 {
|
||||
// C.printf("x:%02x lz: %d cmp: %d\n", i << x, leading_zeros_8(i << x), 7-x)
|
||||
assert leading_zeros_8(byte(i << x)) == 7 - x
|
||||
assert leading_zeros_8(u8(i << x)) == 7 - x
|
||||
}
|
||||
|
||||
// 16 bit
|
||||
@ -46,8 +46,8 @@ fn test_bits() {
|
||||
// 8 bit
|
||||
i = 0
|
||||
for x in 0 .. 9 {
|
||||
// C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), ones_count_8(byte(i)), x)
|
||||
assert ones_count_8(byte(i)) == x
|
||||
// C.printf("x:%02x lz: %llu cmp: %d\n", u8(i), ones_count_8(u8(i)), x)
|
||||
assert ones_count_8(u8(i)) == x
|
||||
i = (i << 1) + 1
|
||||
}
|
||||
|
||||
@ -90,16 +90,16 @@ fn test_bits() {
|
||||
// 8 bit
|
||||
i = 0
|
||||
for _ in 0 .. 9 {
|
||||
mut rv := byte(0)
|
||||
mut rv := u8(0)
|
||||
mut bc := 0
|
||||
mut n := i
|
||||
for bc < 8 {
|
||||
rv = (rv << 1) | (byte(n) & 0x01)
|
||||
rv = (rv << 1) | (u8(n) & 0x01)
|
||||
bc++
|
||||
n = n >> 1
|
||||
}
|
||||
// C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), reverse_8(byte(i)), rv)
|
||||
assert reverse_8(byte(i)) == rv
|
||||
// C.printf("x:%02x lz: %llu cmp: %d\n", u8(i), reverse_8(u8(i)), rv)
|
||||
assert reverse_8(u8(i)) == rv
|
||||
i = (i << 1) + 1
|
||||
}
|
||||
|
||||
|
@ -391,7 +391,7 @@ pub fn (u_ Uint128) str() string {
|
||||
mut n := int(0)
|
||||
for ; r != 0; r /= 10 {
|
||||
n++
|
||||
buf[i - n] += byte(r % 10)
|
||||
buf[i - n] += u8(r % 10)
|
||||
}
|
||||
if q.is_zero() {
|
||||
return buf[i - n..].bytestr()
|
||||
|
@ -329,7 +329,7 @@ pub fn (u_ Uint256) str() string {
|
||||
mut n := 0
|
||||
for ; r != 0; r /= 10 {
|
||||
n++
|
||||
buf[i - n] += byte(r % 10)
|
||||
buf[i - n] += u8(r % 10)
|
||||
}
|
||||
if q.is_zero() {
|
||||
return buf[i - n..].bytestr()
|
||||
|
Reference in New Issue
Block a user