From 7e4799334fc343fa4c80b77b0435a0ee5b5a1aba Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 30 Nov 2019 12:37:34 +0300 Subject: [PATCH] array: make left/right/slice private --- examples/tetris/tetris.v | 4 ++-- vlib/builtin/array.v | 8 ++++---- vlib/cli/command.v | 6 +++--- vlib/compiler/parser.v | 2 +- vlib/os/os.v | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 01d9f84a1b..edbdd5b056 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -289,7 +289,7 @@ fn (g mut Game) generate_tetro() { // Get the right tetro from cache fn (g mut Game) get_tetro() { idx := g.tetro_idx * TetroSize * TetroSize + g.rotation_idx * TetroSize - g.tetro = g.tetros_cache.slice(idx, idx + TetroSize) + g.tetro = g.tetros_cache[idx..idx+TetroSize] } // TODO mut @@ -403,7 +403,7 @@ fn key_down(wnd voidptr, key, code, action, mods int) { } } } - + if game.state != .running { return } diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index a0b03382b3..85671c138f 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -173,7 +173,7 @@ pub fn (a array) last() voidptr { // array.left returns a new array using the same buffer as the given array // with the first `n` elements of the given array. -pub fn (a array) left(n int) array { +fn (a array) left(n int) array { if n < 0 { panic('array.left: index is negative (n == $n)') } @@ -187,7 +187,7 @@ pub fn (a array) left(n int) array { // but starting with the element of the given array beyond the index `n`. // If `n` is bigger or equal to the length of the given array, // returns an empty array of the same type as the given array. -pub fn (a array) right(n int) array { +fn (a array) right(n int) array { if n < 0 { panic('array.right: index is negative (n == $n)') } @@ -207,7 +207,7 @@ fn (a array) slice2(start, _end int, end_max bool) array { // but starting from the `start` element and ending with the element before // the `end` element of the original array with the length and capacity // set to the number of the elements in the slice. -pub fn (a array) slice(start, _end int) array { +fn (a array) slice(start, _end int) array { mut end := _end if start > end { panic('array.slice: invalid slice index ($start > $end)') @@ -315,7 +315,7 @@ pub fn (a []bool) str() string { sb.write('true') } else { sb.write('false') - } + } if i < a.len - 1 { sb.write(', ') } diff --git a/vlib/cli/command.v b/vlib/cli/command.v index d91d6a7e25..7e6e197f67 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -42,7 +42,7 @@ pub fn (cmd mut Command) parse(args []string) { cmd.add_default_flags() cmd.add_default_commands() - cmd.args = args.right(1) + cmd.args = args[1..] for i := 0; i < cmd.commands.len; i++ { cmd.commands[i].parent = cmd } @@ -110,7 +110,7 @@ fn (cmd mut Command) parse_commands() { for flag in global_flags { command.add_flag(flag) } - command.parse(cmd.args.right(i)) + command.parse(cmd.args[i..]) return } } @@ -187,4 +187,4 @@ fn (cmds []Command) get(name string) ?Command { } } return error('command \'${name}\' not found.') -} +} diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 57c30b91e0..7e3965c3e6 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -547,7 +547,7 @@ fn (p mut Parser) parse(pass Pass) { } p.genln('') end := p.cgen.lines.len - lines := p.cgen.lines.slice(start, end) + lines := p.cgen.lines[start..end] //mut line := p.cgen.fn_main + lines.join('\n') //line = line.trim_space() p.cgen.fn_main = p.cgen.fn_main + lines.join('\n') diff --git a/vlib/os/os.v b/vlib/os/os.v index d1fdfb6e1a..7ec73fb0ba 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -80,7 +80,7 @@ pub fn (f File) read_bytes_at(size, pos int) []byte { C.fseek(f.cfile, pos, C.SEEK_SET) nreadbytes := C.fread(arr.data, 1, size, f.cfile) C.fseek(f.cfile, 0, C.SEEK_SET) - return arr.slice(0, nreadbytes) + return arr[0..nreadbytes] } pub fn read_bytes(path string) ?[]byte { @@ -95,7 +95,7 @@ pub fn read_bytes(path string) ?[]byte { mut res := [`0`].repeat(fsize) nreadbytes := C.fread(res.data, fsize, 1, fp) C.fclose(fp) - return res.slice(0, nreadbytes ) + return res[0..nreadbytes] } // read_file reads the file in `path` and returns the contents.