From ef8c1203b43ea5452936e32fbc6f043d7707315b Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Mon, 24 Feb 2020 17:55:16 +0100 Subject: [PATCH] cleanup: replace C for loops with range --- README.md | 2 +- doc/docs.md | 2 +- examples/cli.v | 2 +- examples/fibonacci.v | 2 +- .../modules/automaton/automaton.v | 4 +-- examples/hello_v_js.v | 2 +- examples/nbody.v | 10 +++--- examples/news_fetcher.v | 4 +-- examples/path_tracing.v | 4 +-- examples/spectral.v | 12 +++---- examples/tetris/tetris.v | 10 +++--- examples/vcasino/VCasino.v | 4 +-- vlib/bitfield/bitfield.v | 32 ++++++++--------- vlib/bitfield/bitfield_test.v | 36 +++++++++---------- vlib/builtin/array.v | 32 ++++++++--------- vlib/builtin/array_test.v | 6 ++-- vlib/builtin/bare/array_bare.v | 2 +- vlib/builtin/bare/string_bare.v | 4 +-- vlib/builtin/int.v | 4 +-- vlib/builtin/js/array.v | 2 +- vlib/builtin/js/int.v | 2 +- vlib/builtin/map_test.v | 2 +- vlib/builtin/sorted_map.v | 4 +-- vlib/builtin/string.v | 34 +++++++++--------- vlib/cli/command.v | 8 ++--- vlib/cli/command_test.v | 2 +- vlib/clipboard/clipboard_linux.v | 2 +- vlib/crypto/aes/block_generic.v | 6 ++-- vlib/crypto/cipher/xor_generic.v | 2 +- vlib/crypto/rc4/rc4.v | 4 +-- vlib/crypto/sha1/sha1block_generic.v | 2 +- vlib/crypto/sha256/sha256block_generic.v | 4 +-- vlib/crypto/sha512/sha512block_generic.v | 4 +-- vlib/encoding/base64/base64_memory_test.v | 4 +-- vlib/freetype/freetype.v | 10 +++--- vlib/glm/glm.v | 4 +-- vlib/hash/crc32/crc32.v | 6 ++-- vlib/hash/fnv1a/fnv1a.v | 8 ++--- vlib/math/stats/stats.v | 2 +- vlib/mysql/result.v | 4 +-- vlib/net/ftp/ftp.v | 2 +- vlib/net/socket.v | 2 +- vlib/net/urllib/urllib.v | 12 +++---- vlib/os/os_windows.v | 2 +- vlib/pg/pg.v | 6 ++-- vlib/sdl/examples/tvintris/tvintris.v | 14 ++++---- vlib/strings/similarity.v | 4 +-- vlib/time/time_test.v | 2 +- vlib/v/gen/cgen_test.v | 2 +- vlib/vweb/vweb.v | 2 +- 50 files changed, 168 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index e66f01bf84..1a74e07e7b 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ https://github.com/vlang/ui ```v fn main() { - for i := 0; i < 3; i++ { + for i in 0..3 { println('Hello from V.js') } } diff --git a/doc/docs.md b/doc/docs.md index 19679417a2..0c133b8282 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -641,7 +641,7 @@ so `register()` can change the user object. The same works with non-receiver arg ```v fn multiply_by_2(arr mut []int) { - for i := 0; i < arr.len; i++ { + for i in 0..arr.len { arr[i] *= 2 } } diff --git a/examples/cli.v b/examples/cli.v index 62f637a823..93a1c4ce4d 100644 --- a/examples/cli.v +++ b/examples/cli.v @@ -41,7 +41,7 @@ fn greet_func(cmd cli.Command) { language := cmd.flags.get_string('language') or { panic('failed to get \'language\' flag: $err') } times := cmd.flags.get_int('times') or { panic('failed to get \'times\' flag: $err') } - for i := 0; i < times; i++ { + for i in 0..times { match language { 'english' { println('Hello World') } 'german' { println('Hallo Welt') } diff --git a/examples/fibonacci.v b/examples/fibonacci.v index 5976fef4cf..8861080acc 100644 --- a/examples/fibonacci.v +++ b/examples/fibonacci.v @@ -25,7 +25,7 @@ fn main() { mut b := u64(0) mut c := u64(1) - for i := 0; i < stop; i++ { + for i in 0..stop { // Set a and b to the next term a = b b = c diff --git a/examples/game_of_life/modules/automaton/automaton.v b/examples/game_of_life/modules/automaton/automaton.v index b31fa33a76..d31a1381cb 100644 --- a/examples/game_of_life/modules/automaton/automaton.v +++ b/examples/game_of_life/modules/automaton/automaton.v @@ -48,8 +48,8 @@ fn new_automaton(f [][]int) Automaton { } field := &A2D{ maxx: maxx maxy: maxy data: &int( calloc( sizeof(int) * maxy * maxx ) ) } new_field := &A2D{ maxx: maxx maxy: maxy data: &int( calloc( sizeof(int) * maxy * maxx ) ) } - for y := 0; y < field.maxy; y++ { - for x := 0; x < field.maxx; x++ { + for y in 0..field.maxy { + for x in 0..field.maxx { field.set( x, y, f[y][x] ) } } diff --git a/examples/hello_v_js.v b/examples/hello_v_js.v index 16c9536ed2..41ab9c1bf7 100644 --- a/examples/hello_v_js.v +++ b/examples/hello_v_js.v @@ -1,5 +1,5 @@ fn main() { - for i := 0; i < 3; i++ { + for i in 0..3 { println('Hello from V.js') } } diff --git a/examples/nbody.v b/examples/nbody.v index 5fa23a457b..7ec43f1874 100644 --- a/examples/nbody.v +++ b/examples/nbody.v @@ -33,7 +33,7 @@ pub mut: } fn advance(sys mut System, dt f64) { - for i := 0; i < N - 1; i++ { + for i in 0..N - 1 { mut _vx := sys.v[i].x mut _vy := sys.v[i].y mut _vz := sys.v[i].z @@ -61,7 +61,7 @@ fn advance(sys mut System, dt f64) { sys.v[i].z = _vz } - for i := 0; i < N; i++ { + for i in 0..N { sys.s[i].x += dt * sys.v[i].x sys.s[i].y += dt * sys.v[i].y sys.s[i].z += dt * sys.v[i].z @@ -73,7 +73,7 @@ fn offsetmomentum(sys mut System) { mut py := f64(0) mut pz := f64(0) - for i := 0; i < N; i++ { + for i in 0..N { px += sys.v[i].x * sys.v[i].m py += sys.v[i].y * sys.v[i].m pz += sys.v[i].z * sys.v[i].m @@ -85,7 +85,7 @@ fn offsetmomentum(sys mut System) { fn energy(sys System) f64 { mut e := f64(0) - for i := 0; i < N; i++ { + for i in 0..N { e += 0.5 * sys.v[i].m * (sys.v[i].x * sys.v[i].x + sys.v[i].y * sys.v[i].y + sys.v[i].z * sys.v[i].z) for j := i + 1; j < N; j++ { dx := sys.s[i].x - sys.s[j].x @@ -124,7 +124,7 @@ sys := &System {arr_momentum(), arr_position()} offsetmomentum(mut sys) println('${energy(sys):.9f}') //-0.169075164 -for i := 0; i < 50000000; i++ { +for i in 0..50000000 { advance(mut sys, 0.01) } println('${energy(sys):.9f}') //-0.169059907 diff --git a/examples/news_fetcher.v b/examples/news_fetcher.v index e2fb26d43e..f15e87b68e 100644 --- a/examples/news_fetcher.v +++ b/examples/news_fetcher.v @@ -58,7 +58,7 @@ fn main() { if ids.len > 10 { // ids = ids[:10] mut tmp := [0].repeat(10) - for i := 0; i < 10; i++ { + for i in 0..10 { tmp[i] = ids[i] } ids = tmp @@ -69,7 +69,7 @@ fn main() { wg: sync.new_waitgroup() } fetcher.wg.add(ids.len) - for i := 0; i < nr_threads; i++ { + for i in 0..nr_threads { go fetcher.fetch() } fetcher.wg.wait() diff --git a/examples/path_tracing.v b/examples/path_tracing.v index 5e753efa78..1ad3044fe1 100644 --- a/examples/path_tracing.v +++ b/examples/path_tracing.v @@ -409,7 +409,7 @@ fn ray_trace(w int, h int, samps int, file_name string, scene_id int) Image { // OpenMP injection point! #pragma omp parallel for schedule(dynamic, 1) shared(c) for y:=0; y < h; y++ { eprint("\rRendering (${samps * 4} spp) ${(100.0 * f64(y)) / (f64(h) - 1.0):5.2f}%") - for x := 0; x < w; x++ { + for x in 0..w { i := (h - y - 1) * w + x mut ivec := &image.data[i] @@ -417,7 +417,7 @@ fn ray_trace(w int, h int, samps int, file_name string, scene_id int) Image { for sy := 0; sy < 2; sy ++ { for sx := 0; sx < 2; sx ++ { r = Vec{0,0,0} - for s := 0; s < samps; s++ { + for s in 0..samps { r1 := v_2 * rand_f64() dx := if r1 < v_1 { math.sqrt(r1) - v_1 } else { v_1 - math.sqrt(v_2 - r1) } diff --git a/examples/spectral.v b/examples/spectral.v index cd95f7d5de..4056fae01b 100644 --- a/examples/spectral.v +++ b/examples/spectral.v @@ -18,9 +18,9 @@ fn evala(i, j int) int { } fn times(v mut []f64, u []f64) { - for i := 0; i < v.len; i++ { + for i in 0..v.len { mut a := f64(0) - for j := 0; j < u.len; j++ { + for j in 0..u.len { a += u[j] / f64(evala(i, j)) } v[i] = a @@ -28,9 +28,9 @@ fn times(v mut []f64, u []f64) { } fn times_trans(v mut []f64, u []f64) { - for i := 0; i < v.len; i++ { + for i in 0..v.len { mut a := f64(0) - for j := 0; j < u.len; j++ { + for j in 0..u.len { a += u[j] / f64(evala(j, i)) } v[i] = a @@ -54,13 +54,13 @@ fn main() { } mut u := [f64(1.0)].repeat(n) mut v := [f64(1.0)].repeat(n) - for i := 0; i < 10; i++ { + for i in 0..10 { a_times_transp(mut v, u) a_times_transp(mut u, v) } mut vbv := f64(0) mut vv := f64(0) - for i := 0; i < n; i++ { + for i in 0..n { vbv += u[i] * v[i] vv += v[i] * v[i] } diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 0c19f532ac..3e05f7549e 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -175,7 +175,7 @@ fn (g mut Game) init_game() { g.generate_tetro() g.field = [] // TODO: g.field = [][]int // Generate the field, fill it with 0's, add -1's on each edge - for i := 0; i < FieldHeight + 2; i++ { + for i in 0..FieldHeight + 2 { mut row := [0].repeat(FieldWidth + 2) row[0] = - 1 row[FieldWidth + 1] = - 1 @@ -183,7 +183,7 @@ fn (g mut Game) init_game() { } mut first_row := g.field[0] mut last_row := g.field[FieldHeight + 1] - for j := 0; j < FieldWidth + 2; j++ { + for j in 0..FieldWidth + 2 { first_row[j] = - 1 last_row[j] = - 1 } @@ -238,7 +238,7 @@ fn (g mut Game) move_tetro() { fn (g mut Game) move_right(dx int) bool { // Reached left/right edge or another tetro? - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] y := tetro.y + g.pos_y x := tetro.x + g.pos_x + dx @@ -293,7 +293,7 @@ fn (g mut Game) get_tetro() { // TODO mut fn (g &Game) drop_tetro() { - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] x := tetro.x + g.pos_x y := tetro.y + g.pos_y @@ -305,7 +305,7 @@ fn (g &Game) drop_tetro() { } fn (g &Game) draw_tetro() { - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1) } diff --git a/examples/vcasino/VCasino.v b/examples/vcasino/VCasino.v index c5f5c9fac7..8b2d8b817d 100644 --- a/examples/vcasino/VCasino.v +++ b/examples/vcasino/VCasino.v @@ -21,7 +21,7 @@ fn display_help() { fn option_parser() bool { help := Options{'--help', '-h'} - for i := 0; i < os.args.len; i++ { + for i in 0..os.args.len { if os.args[i]== help.long_opt || os.args[i]== help.short_opt { display_help() return true @@ -31,7 +31,7 @@ fn option_parser() bool { } fn str_is_nbr(s string) bool { - for i := 0; i < s.len; i++ { + for i in 0..s.len { if !s[i].is_digit() { return false } diff --git a/vlib/bitfield/bitfield.v b/vlib/bitfield/bitfield.v index 4a2b57e7dd..2b5a154bd0 100644 --- a/vlib/bitfield/bitfield.v +++ b/vlib/bitfield/bitfield.v @@ -91,7 +91,7 @@ pub fn from_bytes(input []byte) BitField { pub fn from_string(input string) BitField { mut output := new(input.len) - for i := 0; i < input.len; i++ { + for i in 0..input.len { if input[i] != 48 { output.setbit(i) } @@ -104,7 +104,7 @@ pub fn from_string(input string) BitField { pub fn (input BitField) string() string { mut output := '' - for i := 0; i < input.size; i++ { + for i in 0..input.size { if input.getbit(i) == 1 { output = output + '1' } @@ -157,7 +157,7 @@ pub fn (instance mut BitField) clearbit(bitnr int) { // setall() sets all bits in the array to 1 pub fn (instance mut BitField) setall() { - for i := 0; i < bitnslots(instance.size); i++ { + for i in 0..bitnslots(instance.size) { instance.field[i] = u32(-1) } cleartail(mut instance) @@ -166,7 +166,7 @@ pub fn (instance mut BitField) setall() { // clearall() clears (sets to zero) all bits in the array pub fn (instance mut BitField) clearall() { - for i := 0; i < bitnslots(instance.size); i++ { + for i in 0..bitnslots(instance.size) { instance.field[i] = u32(0) } } @@ -251,7 +251,7 @@ pub fn join(input1 BitField, input2 BitField) BitField { output_size := input1.size + input2.size mut output := new(output_size) // copy the first input to output as is - for i := 0; i < bitnslots(input1.size); i++ { + for i in 0..bitnslots(input1.size) { output.field[i] = input1.field[i] } @@ -259,7 +259,7 @@ pub fn join(input1 BitField, input2 BitField) BitField { offset_bit := input1.size % SLOT_SIZE offset_slot := input1.size / SLOT_SIZE - for i := 0; i < bitnslots(input2.size); i++ { + for i in 0..bitnslots(input2.size) { output.field[i + offset_slot] |= u32(input2.field[i] << u32(offset_bit)) } @@ -278,12 +278,12 @@ pub fn join(input1 BitField, input2 BitField) BitField { * If offset_bit is zero, no additional copies needed. */ if (output_size - 1) % SLOT_SIZE < (input2.size - 1) % SLOT_SIZE { - for i := 0; i < bitnslots(input2.size); i++ { + for i in 0..bitnslots(input2.size) { output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(SLOT_SIZE - offset_bit)) } } else if (output_size - 1) % SLOT_SIZE > (input2.size - 1) % SLOT_SIZE { - for i := 0; i < bitnslots(input2.size) - 1; i++ { + for i in 0..bitnslots(input2.size) - 1 { output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(SLOT_SIZE - offset_bit)) } @@ -331,7 +331,7 @@ pub fn clone(input BitField) BitField { pub fn cmp(input1 BitField, input2 BitField) bool { if input1.size != input2.size {return false} - for i := 0; i < bitnslots(input1.size); i++ { + for i in 0..bitnslots(input1.size) { if input1.field[i] != input2.field[i] {return false} } return true @@ -344,14 +344,14 @@ pub fn (instance BitField) popcount() int { bitnslots := bitnslots(size) tail := size % SLOT_SIZE mut count := 0 - for i := 0; i < bitnslots - 1; i++ { - for j := 0; j < SLOT_SIZE; j++ { + for i in 0..bitnslots - 1 { + for j in 0..SLOT_SIZE { if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) { count++ } } } - for j := 0; j < tail; j++ { + for j in 0..tail { if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) { count++ } @@ -412,7 +412,7 @@ pub fn (input BitField) slice(_start int, _end int) BitField { if output_slots > 1 { if start_offset != 0 { - for i := 0; i < output_slots - 1; i++ { + for i in 0..output_slots - 1 { output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset)) output.field[i] = output.field[i] | @@ -421,7 +421,7 @@ pub fn (input BitField) slice(_start int, _end int) BitField { } } else { - for i := 0; i < output_slots - 1; i++ { + for i in 0..output_slots - 1 { output.field[i] = u32(input.field[start_slot + i]) } @@ -465,14 +465,14 @@ pub fn (instance BitField) reverse() BitField { bitnslots := bitnslots(size) mut output := new(size) for i:= 0; i < (bitnslots - 1); i++ { - for j := 0; j < SLOT_SIZE; j++ { + for j in 0..SLOT_SIZE { if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) { bitset(mut output, size - i * SLOT_SIZE - j - 1) } } } bits_in_last_input_slot := (size - 1) % SLOT_SIZE + 1 - for j := 0; j < bits_in_last_input_slot; j++ { + for j in 0..bits_in_last_input_slot { if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) { bitset(mut output, bits_in_last_input_slot - j - 1) } diff --git a/vlib/bitfield/bitfield_test.v b/vlib/bitfield/bitfield_test.v index 6977b1d38c..8d2279bf9d 100644 --- a/vlib/bitfield/bitfield_test.v +++ b/vlib/bitfield/bitfield_test.v @@ -49,7 +49,7 @@ fn test_clone_cmp() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) } @@ -63,7 +63,7 @@ fn test_slice_join() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) } @@ -87,7 +87,7 @@ fn test_popcount() { len := 80 mut count0 := 0 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) count0++ @@ -103,7 +103,7 @@ fn test_hamming() { mut count := 0 mut input1 := bitfield.new(len) mut input2 := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { match rand.next(4) { 0, 1 { input1.setbit(i) @@ -129,7 +129,7 @@ fn test_bf_from_bytes() { input := [byte(0xF0), byte(0x0F), byte(0xF0), byte(0xFF)] output := bitfield.from_bytes(input) mut result := 1 - for i := 0; i < input.len * 8; i++ { + for i in 0..input.len * 8 { if (input[i / 8] >> (i % 8)) & 1 != output.getbit(i) { result = 0 } @@ -141,7 +141,7 @@ fn test_bf_from_string() { rand.seed(time.now().unix) len := 80 mut input := '' - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input = input + '1' } @@ -151,7 +151,7 @@ fn test_bf_from_string() { } output := bitfield.from_string(input) mut result := 1 - for i := 0; i < len; i++ { + for i in 0..len { if input[i] != output.getbit(i) + 48 { result = 0 } @@ -163,13 +163,13 @@ fn test_bf_bf2str() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) } } mut check := '' - for i := 0; i < len; i++ { + for i in 0..len { if input.getbit(i) == 1 { check = check + '1' } @@ -179,7 +179,7 @@ fn test_bf_bf2str() { } output := input.string() mut result := 1 - for i := 0; i < len; i++ { + for i in 0..len { if check[i] != output[i] { result = 0 } @@ -193,7 +193,7 @@ fn test_bf_setall() { mut input := bitfield.new(len) input.setall() mut result := 1 - for i := 0; i < len; i++ { + for i in 0..len { if input.getbit(i) != 1 { result = 0 } @@ -205,14 +205,14 @@ fn test_bf_clearall() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) } } input.clearall() mut result := 1 - for i := 0; i < len; i++ { + for i in 0..len { if input.getbit(i) != 0 { result = 0 } @@ -224,7 +224,7 @@ fn test_bf_reverse() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - for i := 0; i < len; i++ { + for i in 0..len { if rand.next(2) == 1 { input.setbit(i) } @@ -232,7 +232,7 @@ fn test_bf_reverse() { check := bitfield.clone(input) output := input.reverse() mut result := 1 - for i := 0; i < len; i++ { + for i in 0..len { if output.getbit(i) != check.getbit(len - i - 1) { result = 0 } @@ -244,7 +244,7 @@ fn test_bf_resize() { rand.seed(time.now().unix) len := 80 mut input := bitfield.new(rand.next(len) + 1) - for i := 0; i < 100; i++ { + for i in 0..100 { input.resize(rand.next(len) + 1) input.setbit(input.getsize() - 1) } @@ -263,12 +263,12 @@ fn test_bf_pos() { len := 80 mut result := 1 for i := 1; i < len; i++ { // needle size - for j := 0; j < len - i; j++ { // needle position in the haystack + for j in 0..len - i { // needle position in the haystack // create the needle mut needle := bitfield.new(i) // fill the needle with random values - for k := 0; k < i; k++ { + for k in 0..i { if rand.next(2) == 1 { needle.setbit(k) } diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 5962596d64..a5e583e7e3 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -98,7 +98,7 @@ pub fn (a array) repeat(nr_repeats int) array { element_size: a.element_size data: calloc(size) } - for i := 0; i < nr_repeats; i++ { + for i in 0..nr_repeats { C.memcpy(arr.data + i * a.len * a.element_size, a.data, a.len * a.element_size) } return arr @@ -335,7 +335,7 @@ pub fn (a array) reverse() array { element_size: a.element_size data: calloc(a.cap * a.element_size) } - for i := 0; i < a.len; i++ { + for i in 0..a.len { C.memcpy(arr.data + i * arr.element_size, &a[a.len - 1 - i], arr.element_size) } return arr @@ -355,7 +355,7 @@ pub fn (a array) free() { pub fn (a []string) str() string { mut sb := strings.new_builder(a.len * 3) sb.write('[') - for i := 0; i < a.len; i++ { + for i in 0..a.len { val := a[i] sb.write('"') sb.write(val) @@ -373,7 +373,7 @@ pub fn (a []string) str() string { pub fn (a []bool) str() string { mut sb := strings.new_builder(a.len * 3) sb.write('[') - for i := 0; i < a.len; i++ { + for i in 0..a.len { val := a[i] if val { sb.write('true') @@ -394,7 +394,7 @@ pub fn (a []bool) str() string { pub fn (b []byte) hex() string { mut hex := malloc(b.len * 2 + 1) mut ptr := &hex[0] - for i := 0; i < b.len; i++ { + for i in 0..b.len { // QTODO ptr += C.sprintf(ptr, '%02x', b[i]) } @@ -435,7 +435,7 @@ pub fn (a mut []int) sort() { // []string.index returns the index of the first element equal to the given value, // or -1 if the value is not found in the array. pub fn (a []string) index(v string) int { - for i := 0; i < a.len; i++ { + for i in 0..a.len { if a[i] == v { return i } @@ -446,7 +446,7 @@ pub fn (a []string) index(v string) int { // []int.index returns the index of the first element equal to the given value, // or -1 if the value is not found in the array. pub fn (a []int) index(v int) int { - for i := 0; i < a.len; i++ { + for i in 0..a.len { if a[i] == v { return i } @@ -457,7 +457,7 @@ pub fn (a []int) index(v int) int { // []byte.index returns the index of the first element equal to the given value, // or -1 if the value is not found in the array. pub fn (a []byte) index(v byte) int { - for i := 0; i < a.len; i++ { + for i in 0..a.len { if a[i] == v { return i } @@ -469,7 +469,7 @@ pub fn (a []byte) index(v byte) int { // or -1 if the value is not found in the array. // TODO is `char` type yet in the language? pub fn (a []char) index(v char) int { - for i := 0; i < a.len; i++ { + for i in 0..a.len { if a[i] == v { return i } @@ -480,13 +480,11 @@ pub fn (a []char) index(v char) int { // []int.reduce executes a given reducer function on each element of the array, // resulting in a single output value. pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int { - mut _accum := 0 - /* - _accum = accum_start - for i := 0; i < a.len; i++ { - _accum = iter(_accum, a[i]) + mut _accum := accum_start + for i in a { + _accum = iter(_accum, i) } - */ + return _accum } @@ -497,7 +495,7 @@ fn array_eq(a1, a2 []T) bool { if a1.len != a2.len { return false } - for i := 0; i < a1.len; i++ { + for i in 0..a1.len { if a1[i] != a2[i] { return false } @@ -528,7 +526,7 @@ pub fn (a1 []string) eq(a2 []string) bool { if a1.len != a2.len { return false } - for i := 0; i < a1.len; i++ { + for i in 0..a1.len { if a1[i] != a2[i] { return false } diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 3195b1c62c..a851a7a9df 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -62,7 +62,7 @@ fn test_short() { fn test_large() { mut a := [0].repeat(0) - for i := 0; i < 10000; i++ { + for i in 0..10000 { a << i } assert a.len == 10000 @@ -311,7 +311,7 @@ fn test_clone() { fn test_doubling() { mut nums := [1, 2, 3, 4, 5] - for i := 0; i < nums.len; i++ { + for i in 0..nums.len { nums[i] *= 2 } assert nums.str() == '[2, 4, 6, 8, 10]' @@ -562,7 +562,7 @@ fn test_push_many_self() { actual_arr << actual_arr expected_arr := [1, 2, 3, 4, 1, 2, 3, 4] assert actual_arr.len == expected_arr.len - for i := 0; i < actual_arr.len; i++ { + for i in 0..actual_arr.len { assert actual_arr[i] == expected_arr[i] } } diff --git a/vlib/builtin/bare/array_bare.v b/vlib/builtin/bare/array_bare.v index e80ec8a53a..a2f8c0c181 100644 --- a/vlib/builtin/bare/array_bare.v +++ b/vlib/builtin/bare/array_bare.v @@ -47,7 +47,7 @@ pub fn (a array) repeat(nr_repeats int) array { element_size: a.element_size data: malloc(nr_repeats * a.len * a.element_size) } - for i := 0; i < nr_repeats; i++ { + for i in 0..nr_repeats { mem_copy(arr.data + i * a.len * a.element_size, a.data, a.len * a.element_size) } return arr diff --git a/vlib/builtin/bare/string_bare.v b/vlib/builtin/bare/string_bare.v index 1c6eb423d8..3b4d2e69e9 100644 --- a/vlib/builtin/bare/string_bare.v +++ b/vlib/builtin/bare/string_bare.v @@ -28,10 +28,10 @@ fn (s string) add(a string) string { len: new_len str: malloc(new_len + 1) } - for j := 0; j < s.len; j++ { + for j in 0..s.len { res[j] = s[j] } - for j := 0; j < a.len; j++ { + for j in 0..a.len { res[s.len + j] = a[j] } res[new_len] = `\0`// V strings are not null terminated, but just in case diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index ae20bb4985..f9adcf7827 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -186,7 +186,7 @@ pub fn (c rune) str() string { len: len str: malloc(len + 1) } - for i := 0; i < len; i++ { + for i in 0..len { str.str[i] = int(c)>>8 * (3 - i) & 0xff } str[len] = `\0` @@ -210,7 +210,7 @@ pub fn (c byte) is_capital() bool { pub fn (b []byte) clone() []byte { mut res := [byte(0)].repeat(b.len) //mut res := make([]byte, {repeat:b.len}) - for i := 0; i < b.len; i++ { + for i in 0..b.len { res[i] = b[i] } return res diff --git a/vlib/builtin/js/array.v b/vlib/builtin/js/array.v index 705f7103c0..53d0fc2a95 100644 --- a/vlib/builtin/js/array.v +++ b/vlib/builtin/js/array.v @@ -109,7 +109,7 @@ pub fn (a array) free() { pub fn (a []string) str() string { mut sb := strings.new_builder(a.len * 3) sb.write('[') - for i := 0; i < a.len; i++ { + for i in 0..a.len { val := a[i] sb.write('"') sb.write(val) diff --git a/vlib/builtin/js/int.v b/vlib/builtin/js/int.v index 1c1c7d4fec..f1affe00da 100644 --- a/vlib/builtin/js/int.v +++ b/vlib/builtin/js/int.v @@ -83,7 +83,7 @@ pub fn (c byte) is_capital() bool { pub fn (b []byte) clone() []byte { mut res := [byte(0)].repeat(b.len) - for i := 0; i < b.len; i++ { + for i in 0..b.len { res[i] = b[i] } return res diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index f232448ea9..c6003cfc70 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -74,7 +74,7 @@ fn test_large_map() { //ticks := time.ticks() mut nums := map[string]int N := 30 * 1000 - for i := 0; i < N; i++ { + for i in 0..N { key := i.str() nums[key] = i } diff --git a/vlib/builtin/sorted_map.v b/vlib/builtin/sorted_map.v index d2e9fad020..f1dd745ee4 100644 --- a/vlib/builtin/sorted_map.v +++ b/vlib/builtin/sorted_map.v @@ -309,7 +309,7 @@ fn (n mut mapnode) merge(idx int) { sibling := &mapnode(n.children[idx + 1]) child.keys[mid_index] = n.keys[idx] child.values[mid_index] = n.values[idx] - for i := 0; i < sibling.size; i++ { + for i in 0..sibling.size { child.keys[i + degree] = sibling.keys[i] child.values[i + degree] = sibling.values[i] } @@ -413,4 +413,4 @@ pub fn (m SortedMap) print() { // } // sb.writeln('}') // return sb.str() -// } \ No newline at end of file +// } diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index c84ba6ea16..b1e8232476 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -109,7 +109,7 @@ pub fn (a string) clone() string { len: a.len str: malloc(a.len + 1) } - for i := 0; i < a.len; i++ { + for i in 0..a.len { b[i] = a[i] } b[a.len] = `\0` @@ -168,7 +168,7 @@ pub fn (s string) replace(rep, with string) string { for i := 0; i < s.len; i++ { // Reached the location of rep, replace it with "with" if i == cur_idx { - for j := 0; j < with.len; j++ { + for j in 0..with.len { b[b_i] = with[j] b_i++ } @@ -266,7 +266,7 @@ val_idx:rep_i} if i == cur_idx.idx { rep := vals[cur_idx.val_idx] with := vals[cur_idx.val_idx + 1] - for j := 0; j < with.len; j++ { + for j in 0..with.len { b[b_i] = with[j] b_i++ } @@ -339,7 +339,7 @@ fn (s string) eq(a string) bool { if s.len != a.len { return false } - for i := 0; i < s.len; i++ { + for i in 0..s.len { if s[i] != a[i] { return false } @@ -354,7 +354,7 @@ fn (s string) ne(a string) bool { // s < a fn (s string) lt(a string) bool { - for i := 0; i < s.len; i++ { + for i in 0..s.len { if i >= a.len || s[i] > a[i] { return false } @@ -390,10 +390,10 @@ fn (s string) add(a string) string { len: new_len str: malloc(new_len + 1) } - for j := 0; j < s.len; j++ { + for j in 0..s.len { res[j] = s[j] } - for j := 0; j < a.len; j++ { + for j in 0..a.len { res[s.len + j] = a[j] } res[new_len] = `\0` // V strings are not null terminated, but just in case @@ -528,7 +528,7 @@ pub fn (s string) substr(start, end int) string { len: len str: malloc(len + 1) } - for i := 0; i < len; i++ { + for i in 0..len { res.str[i] = s.str[start + i] } res.str[len] = `\0` @@ -595,7 +595,7 @@ fn (s string) index_kmp(p string) int { prefix[i] = j } j = 0 - for i := 0; i < s.len; i++ { + for i in 0..s.len { for p[j] != s[i] && j > 0 { j = prefix[j - 1] } @@ -665,7 +665,7 @@ pub fn (s string) index_after(p string, start int) int { } pub fn (s string) index_byte(c byte) int { - for i := 0; i < s.len; i++ { + for i in 0..s.len { if s[i] == c { return i } @@ -714,7 +714,7 @@ pub fn (s string) starts_with(p string) bool { if p.len > s.len { return false } - for i := 0; i < p.len; i++ { + for i in 0..p.len { if s[i] != p[i] { return false } @@ -726,7 +726,7 @@ pub fn (s string) ends_with(p string) bool { if p.len > s.len { return false } - for i := 0; i < p.len; i++ { + for i in 0..p.len { if p[i] != s[s.len - p.len + i] { return false } @@ -737,7 +737,7 @@ pub fn (s string) ends_with(p string) bool { // TODO only works with ASCII pub fn (s string) to_lower() string { mut b := malloc(s.len + 1) - for i := 0; i < s.len; i++ { + for i in 0..s.len { b[i] = C.tolower(s.str[i]) } return tos(b, s.len) @@ -745,7 +745,7 @@ pub fn (s string) to_lower() string { pub fn (s string) to_upper() string { mut b := malloc(s.len + 1) - for i := 0; i < s.len; i++ { + for i in 0..s.len { b[i] = C.toupper(s.str[i]) } return tos(b, s.len) @@ -807,7 +807,7 @@ fn (ar []int) contains(val int) bool { /* pub fn (a []string) to_c() voidptr { mut res := malloc(sizeof(byteptr) * a.len) - for i := 0; i < a.len; i++ { + for i in 0..a.len { val := a[i] res[i] = val.str } @@ -1177,14 +1177,14 @@ pub fn (a []string) join(del string) string { mut idx := 0 // Go thru every string and copy its every char one by one for i, val in a { - for j := 0; j < val.len; j++ { + for j in 0..val.len { c := val[j] res.str[idx] = val.str[j] idx++ } // Add del if it's not last if i != a.len - 1 { - for k := 0; k < del.len; k++ { + for k in 0..del.len { res.str[idx] = del.str[k] idx++ } diff --git a/vlib/cli/command.v b/vlib/cli/command.v index fb8420105b..63fc36f3e4 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -43,7 +43,7 @@ pub fn (cmd mut Command) parse(args []string) { cmd.add_default_commands() cmd.args = args[1..] - for i := 0; i < cmd.commands.len; i++ { + for i in 0..cmd.commands.len { cmd.commands[i].parent = cmd } @@ -75,7 +75,7 @@ fn (cmd mut Command) parse_flags() { break } mut found := false - for i := 0; i < cmd.flags.len; i++ { + for i in 0..cmd.flags.len { mut flag := &cmd.flags[i] if flag.matches(cmd.args) { found = true @@ -102,9 +102,9 @@ fn (cmd &Command) parse_commands() { cmd.check_help_flag() cmd.check_version_flag() - for i := 0; i < cmd.args.len; i++ { + for i in 0..cmd.args.len { arg := cmd.args[i] - for j := 0; j < cmd.commands.len; j++ { + for j in 0..cmd.commands.len { mut command := cmd.commands[j] if command.name == arg { for flag in global_flags { diff --git a/vlib/cli/command_test.v b/vlib/cli/command_test.v index 66c4895a68..b0b7884f44 100644 --- a/vlib/cli/command_test.v +++ b/vlib/cli/command_test.v @@ -178,7 +178,7 @@ fn compare_arrays(array0 []string, array1 []string) bool { if array0.len != array1.len { return false } - for i := 0; i < array0.len; i++ { + for i in 0..array0.len { if array0[i] != array1[i] { return false } diff --git a/vlib/clipboard/clipboard_linux.v b/vlib/clipboard/clipboard_linux.v index d20a1416d3..decf50578e 100644 --- a/vlib/clipboard/clipboard_linux.v +++ b/vlib/clipboard/clipboard_linux.v @@ -361,7 +361,7 @@ fn (cb &Clipboard) pick_target(prop Property) Atom { //This is higher than the maximum priority. mut priority := math.max_i32 - for i := 0; i < prop.nitems; i++ { + for i in 0..prop.nitems { //See if this data type is allowed and of higher priority (closer to zero) //than the present one. diff --git a/vlib/crypto/aes/block_generic.v b/vlib/crypto/aes/block_generic.v index 4729400229..ca3bb39abe 100644 --- a/vlib/crypto/aes/block_generic.v +++ b/vlib/crypto/aes/block_generic.v @@ -63,7 +63,7 @@ fn encrypt_block_generic(xk []u32, dst, src []byte) { mut t1 := u32(0) mut t2 := u32(0) mut t3 := u32(0) - for r := 0; r < nr; r++ { + for r in 0..nr { t0 = xk[k+0] ^ te0[byte(s0>>24)] ^ te1[byte(s1>>16)] ^ te2[byte(s2>>8)] ^ u32(te3[byte(s3)]) t1 = xk[k+1] ^ te0[byte(s1>>24)] ^ te1[byte(s2>>16)] ^ te2[byte(s3>>8)] ^ u32(te3[byte(s0)]) t2 = xk[k+2] ^ te0[byte(s2>>24)] ^ te1[byte(s3>>16)] ^ te2[byte(s0>>8)] ^ u32(te3[byte(s1)]) @@ -115,7 +115,7 @@ fn decrypt_block_generic(xk []u32, dst, src []byte) { mut t1 := u32(0) mut t2 := u32(0) mut t3 := u32(0) - for r := 0; r < nr; r++ { + for r in 0..nr { t0 = xk[k+0] ^ td0[byte(s0>>24)] ^ td1[byte(s3>>16)] ^ td2[byte(s2>>8)] ^ u32(td3[byte(s1)]) t1 = xk[k+1] ^ td0[byte(s1>>24)] ^ td1[byte(s0>>16)] ^ td2[byte(s3>>8)] ^ u32(td3[byte(s2)]) t2 = xk[k+2] ^ td0[byte(s2>>24)] ^ td1[byte(s1>>16)] ^ td2[byte(s0>>8)] ^ u32(td3[byte(s3)]) @@ -189,7 +189,7 @@ fn expand_key_generic(key []byte, enc mut []u32, dec mut []u32) { n := enc.len for i = 0; i < n; i += 4 { ei := n - i - 4 - for j := 0; j < 4; j++ { + for j in 0..4 { mut x := enc[ei+j] if i > 0 && i+4 < n { x = td0[s_box0[x>>24]] ^ td1[s_box0[x>>16&0xff]] ^ td2[s_box0[x>>8&0xff]] ^ td3[s_box0[x&u32(0xff)]] diff --git a/vlib/crypto/cipher/xor_generic.v b/vlib/crypto/cipher/xor_generic.v index 5ab797a2b3..d93336b7b9 100644 --- a/vlib/crypto/cipher/xor_generic.v +++ b/vlib/crypto/cipher/xor_generic.v @@ -24,7 +24,7 @@ pub fn xor_bytes(dst mut []byte, a, b []byte) int { // n needs to be smaller or equal than the length of a and b. pub fn safe_xor_bytes(dst mut []byte, a, b []byte, n int) { - for i := 0; i < n; i++ { + for i in 0..n { dst[i] = a[i] ^ b[i] } } diff --git a/vlib/crypto/rc4/rc4.v b/vlib/crypto/rc4/rc4.v index 985ab48934..5ff4f90c53 100644 --- a/vlib/crypto/rc4/rc4.v +++ b/vlib/crypto/rc4/rc4.v @@ -32,11 +32,11 @@ pub fn new_cipher(key []byte) ?Cipher { mut c := Cipher{ s: [u32(0)].repeat(256) } - for i := 0; i < 256; i++ { + for i in 0..256 { c.s[i] = u32(i) } mut j := byte(0) - for i := 0; i < 256; i++ { + for i in 0..256 { j += byte(c.s[i]) + key[i%key.len] tmp := c.s[i] c.s[i] = c.s[j] diff --git a/vlib/crypto/sha1/sha1block_generic.v b/vlib/crypto/sha1/sha1block_generic.v index fad3eef5aa..02b7e06fdb 100644 --- a/vlib/crypto/sha1/sha1block_generic.v +++ b/vlib/crypto/sha1/sha1block_generic.v @@ -28,7 +28,7 @@ fn block_generic(dig mut Digest, p_ []byte) { for p.len >= chunk { // Can interlace the computation of w with the // rounds below if needed for speed. - for i := 0; i < 16; i++ { + for i in 0..16 { j := i * 4 w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3]) } diff --git a/vlib/crypto/sha256/sha256block_generic.v b/vlib/crypto/sha256/sha256block_generic.v index e6807d04be..12328b240c 100644 --- a/vlib/crypto/sha256/sha256block_generic.v +++ b/vlib/crypto/sha256/sha256block_generic.v @@ -97,7 +97,7 @@ fn block_generic(dig mut Digest, p_ []byte) { for p.len >= chunk { // Can interlace the computation of w with the // rounds below if needed for speed. - for i := 0; i < 16; i++ { + for i in 0..16 { j := i * 4 w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3]) } @@ -118,7 +118,7 @@ fn block_generic(dig mut Digest, p_ []byte) { mut g := h6 mut h := h7 - for i := 0; i < 64; i++ { + for i in 0..64 { t1 := h + ((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) + ((e & f) ^ (~e & g)) + u32(_k[i]) + w[i] t2 := ((bits.rotate_left_32(a, -2)) ^ (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c)) diff --git a/vlib/crypto/sha512/sha512block_generic.v b/vlib/crypto/sha512/sha512block_generic.v index 77fee2ba77..b28cc9e222 100644 --- a/vlib/crypto/sha512/sha512block_generic.v +++ b/vlib/crypto/sha512/sha512block_generic.v @@ -105,7 +105,7 @@ fn block_generic(dig mut Digest, p_ []byte) { mut h6 := dig.h[6] mut h7 := dig.h[7] for p.len >= Chunk { - for i := 0; i < 16; i++ { + for i in 0..16 { j := i * 8 w[i] = (u64(p[j])<<56) | (u64(p[j + 1])<<48) | (u64(p[j + 2])<<40) | (u64(p[j + 3])<<32) | (u64(p[j + 4])<<24) | (u64(p[j + 5])<<16) | (u64(p[j + 6])<<8) | u64(p[j + 7]) } @@ -124,7 +124,7 @@ fn block_generic(dig mut Digest, p_ []byte) { mut f := h5 mut g := h6 mut h := h7 - for i := 0; i < 80; i++ { + for i in 0..80 { t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i] t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c)) h = g diff --git a/vlib/encoding/base64/base64_memory_test.v b/vlib/encoding/base64/base64_memory_test.v index c33fba9b3d..161eee8c9d 100644 --- a/vlib/encoding/base64/base64_memory_test.v +++ b/vlib/encoding/base64/base64_memory_test.v @@ -14,14 +14,14 @@ fn test_long_encoding(){ mut s := 0 ebuffer := malloc( s_encoded.len ) - for i := 0; i < repeats; i++ { + for i in 0..repeats { resultsize := base64.encode_in_buffer(s_original, ebuffer) s += resultsize assert resultsize == s_encoded.len } dbuffer := malloc( s_decoded.len ) - for i := 0; i < repeats; i++ { + for i in 0..repeats { resultsize := base64.decode_in_buffer(s_encoded, dbuffer) s += resultsize assert resultsize == s_decoded.len diff --git a/vlib/freetype/freetype.v b/vlib/freetype/freetype.v index 2306a58bff..d4431006a8 100644 --- a/vlib/freetype/freetype.v +++ b/vlib/freetype/freetype.v @@ -226,7 +226,7 @@ pub fn new_context(cfg gg.Cfg) &FreeType { // Gen texture // Load first 128 characters of ASCII set mut chars := []Character - for c := 0; c < 128; c++ { + for c in 0..128 { ch := ft_load_char(face, i64(c)) // s := utf32_to_str(uint(0x043f)) // s := 'п' @@ -309,7 +309,7 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf gl.bind_vao(ctx.vao) // Iterate through all characters // utext := text.ustring() - for i := 0; i < utext.len; i++ { + for i in 0..utext.len { _rune := utext.at(i) // println('$i => $_rune') mut ch := Character{} @@ -325,7 +325,7 @@ fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCf } else if _rune.len > 1 { // TODO O(1) use map - for j := 0; j < ctx.utf_runes.len; j++ { + for j in 0..ctx.utf_runes.len { rune_j := ctx.utf_runes[j] if rune_j==_rune { ch = ctx.utf_chars[j] @@ -406,7 +406,7 @@ pub fn (ctx mut FreeType) text_size(s string) (int, int) { mut maxy := u32(0) mut _rune := '' mut ch := Character{} - for i := 0; i < utext.len; i++ { + for i in 0..utext.len { _rune = utext.at(i) ch = Character{} mut found := false @@ -421,7 +421,7 @@ pub fn (ctx mut FreeType) text_size(s string) (int, int) { } else if _rune.len > 1 { // TODO O(1) use map - for j := 0; j < ctx.utf_runes.len; j++ { + for j in 0..ctx.utf_runes.len { rune_j := ctx.utf_runes[j] if rune_j==_rune { ch = ctx.utf_chars[j] diff --git a/vlib/glm/glm.v b/vlib/glm/glm.v index fa21901113..ba6c18a6d8 100644 --- a/vlib/glm/glm.v +++ b/vlib/glm/glm.v @@ -59,11 +59,11 @@ pub fn (v Vec2) str() string { pub fn (m Mat4) str() string { mut s := '[ ' - for i := 0; i < 4; i++ { + for i in 0..4 { if i != 0 { s += ' ' } - for j := 0; j < 4; j++ { + for j in 0..4 { val := m.data[i * 4 + j] s += '${val:.2f} ' } diff --git a/vlib/hash/crc32/crc32.v b/vlib/hash/crc32/crc32.v index e75b8f7e37..7c116f2abe 100644 --- a/vlib/hash/crc32/crc32.v +++ b/vlib/hash/crc32/crc32.v @@ -24,9 +24,9 @@ mut: } fn(c mut Crc32) generate_table(poly int) { - for i := 0; i < 256; i++ { + for i in 0..256 { mut crc := u32(i) - for j := 0; j < 8; j++ { + for j in 0..8 { if crc & u32(1) == u32(1) { crc = (crc >> 1) ^ u32(poly) } else { @@ -39,7 +39,7 @@ fn(c mut Crc32) generate_table(poly int) { fn(c &Crc32) sum32(b []byte) u32 { mut crc := ~u32(0) - for i := 0; i < b.len; i++ { + for i in 0..b.len { crc = c.table[byte(crc)^b[i]] ^ u32(crc >> u32(8)) } return ~crc diff --git a/vlib/hash/fnv1a/fnv1a.v b/vlib/hash/fnv1a/fnv1a.v index 2ef6f423ca..24c35b2690 100644 --- a/vlib/hash/fnv1a/fnv1a.v +++ b/vlib/hash/fnv1a/fnv1a.v @@ -10,7 +10,7 @@ const ( [inline] pub fn sum32_string(data string) u32 { mut hash := fnv32_offset_basis - for i := 0; i < data.len; i++ { + for i in 0..data.len { hash = (hash ^ u32(data[i])) * fnv32_prime } return hash @@ -19,7 +19,7 @@ pub fn sum32_string(data string) u32 { [inline] pub fn sum32(data []byte) u32 { mut hash := fnv32_offset_basis - for i := 0; i < data.len; i++ { + for i in 0..data.len { hash = (hash ^ u32(data[i])) * fnv32_prime } return hash @@ -28,7 +28,7 @@ pub fn sum32(data []byte) u32 { [inline] pub fn sum64_string(data string) u64 { mut hash := fnv64_offset_basis - for i := 0; i < data.len; i++ { + for i in 0..data.len { hash = (hash ^ u64(data[i])) * fnv64_prime } return hash @@ -37,7 +37,7 @@ pub fn sum64_string(data string) u64 { [inline] pub fn sum64(data []byte) u64 { mut hash := fnv64_offset_basis - for i := 0; i < data.len; i++ { + for i in 0..data.len { hash = (hash ^ u64(data[i])) * fnv64_prime } return hash diff --git a/vlib/math/stats/stats.v b/vlib/math/stats/stats.v index bde6853e49..8affafd1fa 100644 --- a/vlib/math/stats/stats.v +++ b/vlib/math/stats/stats.v @@ -119,7 +119,7 @@ pub fn mode(arr []f64) f64 { freqs< freqs[max] { max = i } diff --git a/vlib/mysql/result.v b/vlib/mysql/result.v index 7ee4280c72..589205c08a 100644 --- a/vlib/mysql/result.v +++ b/vlib/mysql/result.v @@ -45,7 +45,7 @@ pub fn (r Result) rows() []Row { nr_cols := r.num_fields() for rr := r.fetch_row(); rr; rr = r.fetch_row() { mut row := Row{} - for i := 0; i < nr_cols; i++ { + for i in 0..nr_cols { if rr[i] == 0 { row.vals << '' } else { @@ -61,7 +61,7 @@ pub fn (r Result) fetch_fields() []Field { mut fields := []Field nr_cols := r.num_fields() orig_fields := mysql_fetch_fields(r.result) - for i := 0; i < nr_cols; i++ { + for i in 0..nr_cols { fields << Field{ name: string(orig_fields[i].name) org_name: string(orig_fields[i].org_name) diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index d6a1efa806..3c4b34cc47 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -47,7 +47,7 @@ fn (dtp DTP) read() []byte { buf, len := dtp.sock.recv(1024) if len == 0 { break } - for i := 0; i < len; i++ { + for i in 0..len { data << buf[i] } unsafe { free(buf) } diff --git a/vlib/net/socket.v b/vlib/net/socket.v index 20ac95316d..cf0ef5830d 100644 --- a/vlib/net/socket.v +++ b/vlib/net/socket.v @@ -329,7 +329,7 @@ pub fn (s Socket) read_line() string { } buf[n] = `\0` mut eol_idx := -1 - for i := 0; i < n; i++ { + for i in 0..n { if int(buf[i]) == `\n` { eol_idx = i // Ensure that tos_clone(buf) later, diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index e35e3cd453..81f8a9e12c 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -242,7 +242,7 @@ fn escape(s string, mode EncodingMode) string { mut space_count := 0 mut hex_count := 0 mut c := byte(0) - for i := 0; i < s.len; i++ { + for i in 0..s.len { c = s[i] if should_escape(c, mode) { if c == ` ` && mode == .encode_query_component { @@ -267,7 +267,7 @@ fn escape(s string, mode EncodingMode) string { } if hex_count == 0 { copy(t, s.bytes()) - for i := 0; i < s.len; i++ { + for i in 0..s.len { if s[i] == ` ` { t[i] = `+` } @@ -276,7 +276,7 @@ fn escape(s string, mode EncodingMode) string { } upperhex := '0123456789ABCDEF' mut j := 0 - for i := 0; i < s.len; i++ { + for i in 0..s.len { c1 := s[i] if c1 == ` ` && mode == .encode_query_component { t[j] = `+` @@ -382,7 +382,7 @@ fn (u &Userinfo) string() string { // (scheme must be [a-zA-Z][a-zA-Z0-9+-.]*) // If so, return [scheme, path]; else return ['', rawurl] fn split_by_scheme(rawurl string) ?[]string { - for i := 0; i < rawurl.len; i++ { + for i in 0..rawurl.len { c := rawurl[i] if (`a` <= c && c <= `z`) || (`A` <= c && c <= `Z`) { // do nothing @@ -692,7 +692,7 @@ fn (u &URL) escaped_path() string { // valid_encoded_path reports whether s is a valid encoded path. // It must not contain any bytes that require escaping during path encoding. fn valid_encoded_path(s string) bool { - for i := 0; i < s.len; i++ { + for i in 0..s.len { // RFC 3986, Appendix A. // pchar = unreserved / pct-encoded / sub-delims / ':' / '@'. // should_escape is not quite compliant with the RFC, @@ -1097,7 +1097,7 @@ pub fn valid_userinfo(s string) bool { // string_contains_ctl_byte reports whether s contains any ASCII control character. fn string_contains_ctl_byte(s string) bool { - for i := 0; i < s.len; i++ { + for i in 0..s.len { b := s[i] if b < ` ` || b == 0x7f { return true diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index e0efb66767..9a834e637b 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -82,7 +82,7 @@ mut: fn init_os_args_wide(argc int, argv &byteptr) []string { mut args := []string - for i := 0; i < argc; i++ { + for i in 0..argc { args << string_from_wide(&u16(argv[i])) } return args diff --git a/vlib/pg/pg.v b/vlib/pg/pg.v index af59aaa911..97b64fe272 100644 --- a/vlib/pg/pg.v +++ b/vlib/pg/pg.v @@ -50,9 +50,9 @@ fn res_to_rows(res voidptr) []pg.Row { nr_rows := C.PQntuples(res) nr_cols := C.PQnfields(res) mut rows := []pg.Row - for i := 0; i < nr_rows; i++ { + for i in 0..nr_rows { mut row := Row{} - for j := 0; j < nr_cols; j++ { + for j in 0..nr_cols { val := C.PQgetvalue(res, i, j) row.vals << string(val) } @@ -124,7 +124,7 @@ pub fn (db DB) exec_one(query string) ?pg.Row { // pub fn (db DB) exec_param_many(query string, params []string) []pg.Row { mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) ) - for i := 0; i < params.len; i++ { + for i in 0..params.len { param_vals[i] = params[i].str } res := C.PQexecParams(db.conn, query.str, params.len, 0, param_vals, 0, 0, 0) diff --git a/vlib/sdl/examples/tvintris/tvintris.v b/vlib/sdl/examples/tvintris/tvintris.v index 2ed96427a8..ec404d4dd9 100644 --- a/vlib/sdl/examples/tvintris/tvintris.v +++ b/vlib/sdl/examples/tvintris/tvintris.v @@ -257,11 +257,11 @@ fn (sdlc mut SdlContext) set_sdl_context(w int, h int, title string) { C.Mix_VolumeMusic(sdlc.actx.volume) } njoy := C.SDL_NumJoysticks() - for i := 0; i < njoy; i++ { + for i in 0..njoy { C.SDL_JoystickOpen(i) jn := tos_clone(sdl.joystick_name_for_index(i)) println('JOY NAME $jn') - for j := 0; j < NJOYMAX; j++ { + for j in 0..NJOYMAX { if sdlc.jnames[j] == jn { println('FOUND JOYSTICK $j $jn ID=$i') sdlc.jids[j] = i @@ -534,7 +534,7 @@ fn (g mut Game) init_game() { g.generate_tetro() g.field = [] // Generate the field, fill it with 0's, add -1's on each edge - for i := 0; i < FieldHeight + 2; i++ { + for i in 0..FieldHeight + 2 { mut row := [0].repeat(FieldWidth + 2) row[0] = - 1 row[FieldWidth + 1] = - 1 @@ -542,7 +542,7 @@ fn (g mut Game) init_game() { } mut first_row := g.field[0] mut last_row := g.field[FieldHeight + 1] - for j := 0; j < FieldWidth + 2; j++ { + for j in 0..FieldWidth + 2 { first_row[j] = - 1 last_row[j] = - 1 } @@ -626,7 +626,7 @@ fn (g mut Game) move_tetro() { fn (g mut Game) move_right(dx int) bool { // Reached left/right edge or another tetro? - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] y := tetro.y + g.pos_y x := tetro.x + g.pos_x + dx @@ -693,7 +693,7 @@ fn (g mut Game) get_tetro() { } fn (g &Game) drop_tetro() { - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] x := tetro.x + g.pos_x y := tetro.y + g.pos_y @@ -705,7 +705,7 @@ fn (g &Game) drop_tetro() { } fn (g &Game) draw_tetro() { - for i := 0; i < TetroSize; i++ { + for i in 0..TetroSize { tetro := g.tetro[i] g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1) } diff --git a/vlib/strings/similarity.v b/vlib/strings/similarity.v index 93f1d58369..76b2c08ec8 100644 --- a/vlib/strings/similarity.v +++ b/vlib/strings/similarity.v @@ -51,13 +51,13 @@ pub fn dice_coefficient(s1, s2 string) f32 { a := if s1.len > s2.len { s1 } else { s2 } b := if a == s1 { s2 } else { s1 } mut first_bigrams := map[string]int - for i := 0; i < a.len - 1; i++ { + for i in 0..a.len - 1 { bigram := a[i..i + 2] q := if bigram in first_bigrams { first_bigrams[bigram] + 1 } else { 1 } first_bigrams[bigram] = q } mut intersection_size := 0 - for i := 0; i < b.len - 1; i++ { + for i in 0..b.len - 1 { bigram := b[i..i + 2] count := if bigram in first_bigrams { first_bigrams[bigram] } else { 0 } if count > 0 { diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 39c4edae24..ca9b7e4153 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -106,7 +106,7 @@ fn test_smonth() { } fn test_day_of_week() { - for i := 0; i < 7; i++ { + for i in 0..7 { day_of_week := i + 1 // 2 Dec 2019 is Monday t := time.Time{ diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 0d526f00b5..65df7b3d2f 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -16,7 +16,7 @@ fn test_c_files() { vroot := filepath.dir(vexe) term_ok := term.ok_message('OK') term_fail := term.fail_message('FAIL') - for i in 1 .. nr_tests + 1 { + for i in 1..(nr_tests + 1) { path := '$vroot/vlib/v/gen/tests/${i}.vv' mut ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { panic(err) diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index be4f5680aa..f1e10de6f7 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -192,7 +192,7 @@ fn handle_conn(conn net.Socket, app mut T) { mut len := 0 mut body_len := 0 //for line in lines[1..] { - for j := 0; j < 100; j++ { + for j in 0..100 { //println(j) line := conn.read_line() sline := strip(line)