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

vlib,cgen: cleanup array inits using `.repeat() instead of new init syntax

This commit is contained in:
Emily Hudson
2020-06-27 20:46:04 +01:00
committed by GitHub
parent 2669610be9
commit c84bafbdae
31 changed files with 52 additions and 53 deletions

View File

@ -30,7 +30,7 @@ fn new_aes_cbc(b AesCipher, iv []byte) AesCbc {
b: b,
block_size: b.block_size(),
iv: iv.clone(),
tmp: [byte(0)].repeat(b.block_size()),
tmp: []byte{len:(b.block_size()),}
}
}

View File

@ -9,8 +9,8 @@ module aes
fn new_cipher_generic(key []byte) AesCipher {
n := key.len + 28
mut c := AesCipher{
enc: [u32(0)].repeat(n)
dec: [u32(0)].repeat(n)
enc: []u32{len:(n)}
dec: []u32{len:(n)}
}
expand_key_generic(key, mut c.enc, mut c.dec)
return c

View File

@ -38,8 +38,8 @@ mut:
}
fn (mut d Digest) reset() {
d.s = [u32(0)].repeat(4)
d.x = [byte(0)].repeat(block_size)
d.s = []u32{len:(4)}
d.x = []byte{len:(block_size)}
d.s[0] = u32(init0)
d.s[1] = u32(init1)
d.s[2] = u32(init2)
@ -105,7 +105,7 @@ pub fn (mut d Digest) checksum() []byte {
//
// 1 byte end marker :: 0-63 padding bytes :: 8 byte length
// tmp := [1 + 63 + 8]byte{0x80}
mut tmp := [byte(0)].repeat(1 + 63 + 8)
mut tmp := []byte{len:(1 + 63 + 8)}
tmp[0] = 0x80
pad := ((55 - d.len) % 64) // calculate number of padding bytes
binary.little_endian_put_u64(mut tmp[1+pad..], d.len<<3) // append length in bits
@ -117,7 +117,7 @@ pub fn (mut d Digest) checksum() []byte {
panic('d.nx != 0')
}
digest := [byte(0)].repeat(size)
digest := []byte{len:(size)}
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest[4..], d.s[1])

View File

@ -38,7 +38,7 @@ pub fn int_u64(max u64) ?u64 {
fn bytes_to_u64(b []byte) []u64 {
ws := 64/8
mut z := [u64(0)].repeat((b.len + ws - 1) / ws)
mut z := []u64{len:((b.len + ws - 1) / ws)}
mut i := b.len
for k := 0; i >= ws; k++ {
z[k] = binary.big_endian_u64(b[i-ws..i])

View File

@ -30,7 +30,7 @@ pub fn new_cipher(key []byte) ?Cipher {
return error('crypto.rc4: invalid key size ' + key.len.str())
}
mut c := Cipher{
s: [u32(0)].repeat(256)
s: []u32{len:(256)}
}
for i in 0..256 {
c.s[i] = u32(i)

View File

@ -40,8 +40,8 @@ mut:
}
fn (mut d Digest) reset() {
d.x = [byte(0)].repeat(chunk)
d.h = [u32(0)].repeat(5)
d.x = []byte{len:(chunk)}
d.h = []u32{len:(5)}
d.h[0] = u32(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
@ -105,7 +105,7 @@ pub fn (d &Digest) sum(b_in []byte) []byte {
fn (mut d Digest) checksum() []byte {
mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
mut tmp := [byte(0)].repeat(64)
mut tmp := []byte{len:(64)}
tmp[0] = 0x80
@ -120,7 +120,7 @@ fn (mut d Digest) checksum() []byte {
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp[..8])
mut digest := [byte(0)].repeat(size)
mut digest := []byte{len:(size)}
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest[4..], d.h[1])

View File

@ -19,7 +19,7 @@ const (
fn block_generic(mut dig Digest, p_ []byte) {
mut p := p_
mut w := [u32(0)].repeat(16)
mut w := []u32{len:(16)}
mut h0 := dig.h[0]
mut h1 := dig.h[1]
mut h2 := dig.h[2]

View File

@ -52,8 +52,8 @@ mut:
}
fn (mut d Digest) reset() {
d.h = [u32(0)].repeat(8)
d.x = [byte(0)].repeat(chunk)
d.h = []u32{len:(8)}
d.x = []byte{len:(chunk)}
if !d.is224 {
d.h[0] = u32(init0)
d.h[1] = u32(init1)
@ -144,7 +144,7 @@ fn (d &Digest) sum(b_in []byte) []byte {
fn (mut d Digest) checksum() []byte {
mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
mut tmp := [byte(0)].repeat(64)
mut tmp := []byte{len:(64)}
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp[..56-int(len)%64])
@ -161,7 +161,7 @@ fn (mut d Digest) checksum() []byte {
panic('d.nx != 0')
}
digest := [byte(0)].repeat(size)
digest := []byte{len:(size)}
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest[4..], d.h[1])
@ -194,7 +194,7 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data)
sum := d.checksum()
sum224 := [byte(0)].repeat(size224)
sum224 := []byte{len:(size224)}
copy(sum224, sum[..size224])
return sum224
}

View File

@ -83,7 +83,7 @@ const (
fn block_generic(mut dig Digest, p_ []byte) {
mut p := p_
mut w := [u32(0)].repeat(64)
mut w := []u32{len:(64)}
mut h0 := dig.h[0]
mut h1 := dig.h[1]

View File

@ -70,8 +70,8 @@ mut:
}
fn (mut d Digest) reset() {
d.h = [u64(0)].repeat(8)
d.x = [byte(0)].repeat(chunk)
d.h = []u64{len:(8)}
d.x = []byte{len:(chunk)}
match d.function {
.sha384 {
d.h[0] = init0_384
@ -212,7 +212,7 @@ fn (d &Digest) sum(b_in []byte) []byte {
fn (mut d Digest) checksum() []byte {
// Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
mut len := d.len
mut tmp := [byte(0)].repeat(128)
mut tmp := []byte{len:(128)}
tmp[0] = 0x80
if int(len) % 128 < 112 {
d.write(tmp[..112 - int(len) % 128])
@ -228,7 +228,7 @@ fn (mut d Digest) checksum() []byte {
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := [byte(0)].repeat(size)
mut digest := []byte{len:(size)}
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest[8..], d.h[1])
binary.big_endian_put_u64(mut digest[16..], d.h[2])
@ -254,7 +254,7 @@ pub fn sum384(data []byte) []byte {
mut d := new_digest(.sha384)
d.write(data)
sum := d.checksum()
sum384 := [byte(0)].repeat(size384)
sum384 := []byte{len:(size384)}
copy(sum384, sum[..size384])
return sum384
}
@ -264,7 +264,7 @@ pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224)
d.write(data)
sum := d.checksum()
sum224 := [byte(0)].repeat(size224)
sum224 := []byte{len:(size224)}
copy(sum224, sum[..size224])
return sum224
}
@ -274,7 +274,7 @@ pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256)
d.write(data)
sum := d.checksum()
sum256 := [byte(0)].repeat(size256)
sum256 := []byte{len:(size256)}
copy(sum256, sum[..size256])
return sum256
}

View File

@ -95,7 +95,7 @@ const (
fn block_generic(mut dig Digest, p_ []byte) {
mut p := p_
mut w := [u64(0)].repeat(80)
mut w := []u64{len:(80)}
mut h0 := dig.h[0]
mut h1 := dig.h[1]
mut h2 := dig.h[2]