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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 52 additions and 53 deletions

View File

@ -68,7 +68,7 @@ pub fn new(size int) BitField {
output := BitField{
size: size
//field: *u32(calloc(bitnslots(size) * slot_size / 8))
field: [u32(0)].repeat(bitnslots(size))
field: []u32{len:bitnslots(size)}
}
return output
}
@ -394,7 +394,7 @@ pub fn (mut instance BitField) resize(new_size int) {
new_bitnslots := bitnslots(new_size)
old_size := instance.size
old_bitnslots := bitnslots(old_size)
mut field := [u32(0)].repeat(new_bitnslots)
mut field := []u32{len:new_bitnslots}
for i := 0; i < old_bitnslots && i < new_bitnslots; i++ {
field[i] = instance.field[i]
}

View File

@ -1,7 +1,6 @@
import bitfield
import rand
import time
fn test_bf_new_size() {
instance := bitfield.new(75)

View File

@ -7,7 +7,7 @@ pub fn run (op fn(), label string, code Wi_si_code, status int) int {
sys_exit(0)
}
siginfo := [0].repeat(int(Sig_index.si_size))
siginfo := []int{len:int(Sig_index.si_size)}
e := sys_waitid(.p_pid, child, intptr(&siginfo[0]), .wexited, 0)

View File

@ -40,7 +40,7 @@ fn check_read_write_pipe() {
// sys_read
// sys_close
//
buffer0 := [byte(0)].repeat(128)
buffer0 := []byte{len:(128)}
buffer := byteptr(buffer0.data)
fd := [-1, -1]
@ -87,7 +87,7 @@ fn check_read_file() {
sys_close
sys_open
*/
buffer0 := [byte(0)].repeat(128)
buffer0 := []byte{len:(128)}
buffer := byteptr(buffer0.data)
test_file := "sample_text1.txt"

View File

@ -8,7 +8,7 @@ fn check_string_eq () {
}
fn check_i64_tos() {
buffer0 := [byte(0)].repeat(128)
buffer0 := []byte{len:(128)}
buffer := byteptr(buffer0.data)
s0 := i64_tos(buffer, 70, 140, 10)

View File

@ -399,7 +399,7 @@ pub fn (c byte) is_capital() bool {
}
pub fn (b []byte) clone() []byte {
mut res := [byte(0)].repeat(b.len)
mut res := []byte{len: b.len}
//mut res := make([]byte, {repeat:b.len})
for i in 0..b.len {
res[i] = b[i]

View File

@ -379,7 +379,7 @@ fn (n &mapnode) subkeys(mut keys []string, at int) int {
}
pub fn (m &SortedMap) keys() []string {
mut keys := [''].repeat(m.len)
mut keys := []string{len:m.len}
if isnil(m.root) || m.root.len == 0 {
return keys
}

View File

@ -595,7 +595,7 @@ fn (s string) index_kmp(p string) int {
if p.len > s.len {
return -1
}
mut prefix := [0].repeat(p.len)
mut prefix := []int{len:p.len}
mut j := 0
for i := 1; i < p.len; i++ {
for p.str[j] != p.str[i] && j > 0 {
@ -1326,7 +1326,7 @@ pub fn (s string) bytes() []byte {
if s.len == 0 {
return []
}
mut buf := [byte(0)].repeat(s.len)
mut buf := []byte{ len:s.len }
C.memcpy(buf.data, s.str, s.len)
return buf
}

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]

View File

@ -256,14 +256,14 @@ fn escape(s string, mode EncodingMode) string {
if space_count == 0 && hex_count == 0 {
return s
}
buf := [byte(0)].repeat(64)
buf := []byte{len:(64)}
mut t := []byte{}
required := s.len + 2 * hex_count
if required <= buf.len {
t = buf[..required]
}
else {
t = [byte(0)].repeat(required)
t = []byte{len:(required)}
}
if hex_count == 0 {
copy(t, s.bytes())

View File

@ -26,7 +26,7 @@ fn mt19937_basic_test() {
fn gen_randoms(seed_data []u32, bound int) []u64 {
bound_u64 := u64(bound)
mut randoms := [u64(0)].repeat(20)
mut randoms := []u64{len:(20)}
mut rnd := mt19937.MT19937RNG{}
rnd.seed(seed_data)
for i in 0 .. 20 {

View File

@ -16,7 +16,7 @@ const (
fn gen_randoms(seed_data []u32, bound int) []u64 {
bound_u64 := u64(bound)
mut randoms := [u64(0)].repeat(20)
mut randoms := []u64{len:(20)}
mut rnd := musl.MuslRNG{}
rnd.seed(seed_data)
for i in 0 .. 20 {

View File

@ -16,7 +16,7 @@ const (
fn gen_randoms(seed_data []u32, bound int) []u64 {
bound_u64 := u64(bound)
mut randoms := [u64(0)].repeat(20)
mut randoms := []u64{len:(20)}
mut rnd := splitmix64.SplitMix64RNG{}
rnd.seed(seed_data)
for i in 0 .. 20 {

View File

@ -16,7 +16,7 @@ const (
fn gen_randoms(seed_data []u32, bound int) []u64 {
bound_u64 := u64(bound)
mut randoms := [u64(0)].repeat(20)
mut randoms := []u64{len:(20)}
mut rnd := wyrand.WyRandRNG{}
rnd.seed(seed_data)
for i in 0 .. 20 {

View File

@ -479,7 +479,7 @@ enum CharClass_parse_state {
}
fn (re RE) get_char_class(pc int) string {
buf := [byte(0)].repeat(re.cc.len)
buf := []byte{len:(re.cc.len)}
mut buf_ptr := &byte(&buf)
mut cc_i := re.prog[pc].cc_index

View File

@ -77,7 +77,7 @@ fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string {
fw_zeros = pad_digit -out_len
}
mut buf := [byte(0)].repeat(out_len + 5 + 1 +1) // sign + mant_len + . + e + e_sign + exp_len(2) + \0
mut buf := []byte{len:int(out_len + 5 + 1 +1)} // sign + mant_len + . + e + e_sign + exp_len(2) + \0}
mut i := 0
if neg {

View File

@ -90,7 +90,7 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
fw_zeros = pad_digit - out_len
}
mut buf := [byte(0)].repeat(out_len + 6 + 1 +1 + fw_zeros) // sign + mant_len + . + e + e_sign + exp_len(2) + \0
mut buf := []byte{len:(out_len + 6 + 1 +1 + fw_zeros)} // sign + mant_len + . + e + e_sign + exp_len(2) + \0}
mut i := 0
if neg {

View File

@ -114,8 +114,8 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {
pool.results = []
pool.thread_contexts = []
pool.items << items
pool.results = [voidptr(0)].repeat(pool.items.len)
pool.thread_contexts << [voidptr(0)].repeat(pool.items.len)
pool.results = []voidptr{len:(pool.items.len)}
pool.thread_contexts << []voidptr{len:(pool.items.len)}
pool.waitgroup.add(njobs)
for i := 0; i < njobs; i++ {
if njobs > 1 {

View File

@ -841,9 +841,9 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.writeln(', $key, &($val_styp[]){ $zero }));')
}
g.stmts(it.stmts)
g.writeln('/* for in map cleanup*/ string_free(&$key);')
g.writeln('}')
g.writeln('/*for in map cleanup*/')
g.writeln('for (int $idx = 0; $idx < ${keys_tmp}.len; $idx++) { string_free(&(($key_styp*)${keys_tmp}.data)[$idx]); }')
g.writeln('array_free(&$keys_tmp);')
} else if it.cond_type.has_flag(.variadic) {
g.writeln('// FOR IN cond_type/variadic')

View File

@ -25,7 +25,7 @@ fn get_user_opt() ?User {
fn (u &User) foo() {
age := u.age
zzz := [''].repeat(u.age)
zzz := []string{len:(u.age)}
a := 10
if a in [10, 20, 30] {
b := 10

View File

@ -148,7 +148,7 @@ fn build_keys() map[string]Kind {
// TODO remove once we have `enum Kind { name('name') if('if') ... }`
fn build_token_str() []string {
mut s := [''].repeat(nr_tokens)
mut s := []string{len:(nr_tokens)}
s[Kind.unknown] = 'unknown'
s[Kind.eof] = 'eof'
s[Kind.name] = 'name'