update []

pull/2760/head
Alexander Medvednikov 2019-11-14 09:53:05 +03:00
parent 4a833d8151
commit 96b530cf85
7 changed files with 41 additions and 40 deletions

View File

@ -65,12 +65,12 @@ struct K {
} }
fn test_empty() { fn test_empty() {
mut chunks := []Chunk mut chunks := []
a := Chunk{} a := Chunk{}
assert chunks.len == 0 assert chunks.len == 0
chunks << a chunks << a
assert chunks.len == 1 assert chunks.len == 1
chunks = []Chunk chunks = []
assert chunks.len == 0 assert chunks.len == 0
chunks << a chunks << a
assert chunks.len == 1 assert chunks.len == 1

View File

@ -2428,13 +2428,14 @@ fn (p mut Parser) array_init() string {
if p.tok != .name && i == 0 && !exp_array { if p.tok != .name && i == 0 && !exp_array {
p.error('specify array type: `[]typ` instead of `[]`') p.error('specify array type: `[]typ` instead of `[]`')
} }
if p.tok == .name && i == 0 { if p.tok == .name && i == 0 &&
p.tokens[p.token_idx-2].line_nr == p.tokens[p.token_idx-1].line_nr { // TODO
// vals.len == 0 { // vals.len == 0 {
if exp_array { if exp_array {
p.error('use `foo = []` instead of `foo = []Type`') p.error('use `foo = []` instead of `foo = []Type`')
} }
typ = p.get_type() typ = p.get_type()
} else if exp_array { } else if exp_array && i == 0 {
// allow `known_array = []` // allow `known_array = []`
typ = p.expected_type[6..] typ = p.expected_type[6..]
} }

View File

@ -67,7 +67,7 @@ pub fn (d mut Digest) write(p_ []byte) int {
d.nx = 0 d.nx = 0
} }
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }
@ -76,7 +76,7 @@ pub fn (d mut Digest) write(p_ []byte) int {
n := p.len &~ (block_size - 1) n := p.len &~ (block_size - 1)
block(mut d, p[..n]) block(mut d, p[..n])
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }

View File

@ -8,7 +8,7 @@
// applications. // applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/sha1 // Based off: https://github.com/golang/go/blob/master/src/crypto/sha1
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01 // Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
module sha1 module sha1
@ -71,7 +71,7 @@ pub fn (d mut Digest) write(p_ []byte) int {
d.nx = 0 d.nx = 0
} }
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }
@ -80,7 +80,7 @@ pub fn (d mut Digest) write(p_ []byte) int {
n := p.len &~ (chunk - 1) n := p.len &~ (chunk - 1)
block(d, p[..n]) block(d, p[..n])
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }

View File

@ -163,7 +163,7 @@ fn (d mut Digest) write(p_ []byte) int {
d.nx = 0 d.nx = 0
} }
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }
@ -172,7 +172,7 @@ fn (d mut Digest) write(p_ []byte) int {
n := p.len &~ (Chunk - 1) n := p.len &~ (Chunk - 1)
block(mut d, p[..n]) block(mut d, p[..n])
if n >= p.len { if n >= p.len {
p = []byte p = []
} else { } else {
p = p[n..] p = p[n..]
} }

View File

@ -2,8 +2,8 @@
import flag import flag
fn test_if_flag_not_given_return_default_values() { fn test_if_flag_not_given_return_default_values() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([])
assert false == fp.bool('a_bool', false, '') assert false == fp.bool('a_bool', false, '')
&& 42 == fp.int('an_int', 42, '') && 42 == fp.int('an_int', 42, '')
&& 1.0 == fp.float('a_float', 1.0, '') && 1.0 == fp.float('a_float', 1.0, '')
@ -12,7 +12,7 @@ fn test_if_flag_not_given_return_default_values() {
fn test_could_define_application_name_and_version() { fn test_could_define_application_name_and_version() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([])
fp.application('test app') fp.application('test app')
fp.version('0.0.42') fp.version('0.0.42')
fp.description('some text') fp.description('some text')
@ -30,12 +30,12 @@ fn test_bool_flags_do_not_need_an_value() {
fn test_flags_could_be_defined_with_eq() { fn test_flags_could_be_defined_with_eq() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int=42', '--an_int=42',
'--a_float=2.0', '--a_float=2.0',
'--bool_without', '--bool_without',
'--a_string=stuff', '--a_string=stuff',
'--a_bool=true']) '--a_bool=true'])
assert 42 == fp.int('an_int', 666, '') assert 42 == fp.int('an_int', 666, '')
&& true == fp.bool('a_bool', false, '') && true == fp.bool('a_bool', false, '')
&& true == fp.bool('bool_without', false, '') && true == fp.bool('bool_without', false, '')
@ -45,12 +45,12 @@ fn test_flags_could_be_defined_with_eq() {
fn test_values_could_be_defined_without_eq() { fn test_values_could_be_defined_without_eq() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int', '42', '--an_int', '42',
'--a_float', '2.0', '--a_float', '2.0',
'--bool_without', '--bool_without',
'--a_string', 'stuff', '--a_string', 'stuff',
'--a_bool', 'true']) '--a_bool', 'true'])
assert 42 == fp.int('an_int', 666, '') assert 42 == fp.int('an_int', 666, '')
&& true == fp.bool('a_bool', false, '') && true == fp.bool('a_bool', false, '')
&& true == fp.bool('bool_without', false, '') && true == fp.bool('bool_without', false, '')
@ -60,12 +60,12 @@ fn test_values_could_be_defined_without_eq() {
fn test_values_could_be_defined_mixed() { fn test_values_could_be_defined_mixed() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int', '42', '--an_int', '42',
'--a_float=2.0', '--a_float=2.0',
'--bool_without', '--bool_without',
'--a_string', 'stuff', '--a_string', 'stuff',
'--a_bool=true']) '--a_bool=true'])
assert 42 == fp.int('an_int', 666, '') assert 42 == fp.int('an_int', 666, '')
&& true == fp.bool('a_bool', false, '') && true == fp.bool('a_bool', false, '')
&& true == fp.bool('bool_without', false, '') && true == fp.bool('bool_without', false, '')
@ -75,10 +75,10 @@ fn test_values_could_be_defined_mixed() {
fn test_beaware_for_argument_names_with_same_prefix() { fn test_beaware_for_argument_names_with_same_prefix() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--short', '5', '--short', '5',
'--shorter=7' '--shorter=7'
]) ])
assert 5 == fp.int('short', 666, '') assert 5 == fp.int('short', 666, '')
&& 7 == fp.int('shorter', 666, '') && 7 == fp.int('shorter', 666, '')
} }
@ -86,18 +86,18 @@ fn test_beaware_for_argument_names_with_same_prefix() {
fn test_beaware_for_argument_names_with_same_prefix_inverse() { fn test_beaware_for_argument_names_with_same_prefix_inverse() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--shorter=7', '--shorter=7',
'--short', '5', '--short', '5',
]) ])
assert 5 == fp.int('short', 666, '') assert 5 == fp.int('short', 666, '')
&& 7 == fp.int('shorter', 666, '') && 7 == fp.int('shorter', 666, '')
} }
fn test_allow_to_skip_executable_path() { fn test_allow_to_skip_executable_path() {
mut fp := flag.new_flag_parser(['./path/to/execuable']) mut fp := flag.new_flag_parser(['./path/to/execuable'])
fp.skip_executable() fp.skip_executable()
args := fp.finalize() or { args := fp.finalize() or {
assert false assert false
return return
@ -108,7 +108,7 @@ fn test_allow_to_skip_executable_path() {
fn test_none_flag_arguments_are_allowed() { fn test_none_flag_arguments_are_allowed() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'file1', '--an_int=2', 'file2', 'file3', '--bool_without', 'file4', '--outfile', 'outfile']) 'file1', '--an_int=2', 'file2', 'file3', '--bool_without', 'file4', '--outfile', 'outfile'])
assert 2 == fp.int('an_int', 666, '') assert 2 == fp.int('an_int', 666, '')
&& 'outfile' == fp.string('outfile', 'bad', '') && 'outfile' == fp.string('outfile', 'bad', '')
&& true == fp.bool('bool_without', false, '') && true == fp.bool('bool_without', false, '')
@ -124,7 +124,7 @@ fn test_finalize_returns_none_flag_arguments_ordered() {
} }
expected := ['d', 'b', 'x', 'a'] expected := ['d', 'b', 'x', 'a']
mut all_as_expected := true mut all_as_expected := true
for i, v in finalized { for i, v in finalized {
all_as_expected = all_as_expected && v == expected[i] all_as_expected = all_as_expected && v == expected[i]
} }
@ -133,7 +133,7 @@ fn test_finalize_returns_none_flag_arguments_ordered() {
fn test_finalize_returns_error_for_unknown_flags() { fn test_finalize_returns_error_for_unknown_flags() {
mut fp := flag.new_flag_parser(['--known', '--unknown']) mut fp := flag.new_flag_parser(['--known', '--unknown'])
fp.bool('known', false, '') fp.bool('known', false, '')
finalized := fp.finalize() or { finalized := fp.finalize() or {
@ -155,10 +155,10 @@ fn test_allow_to_build_usage_message() {
fp.bool('bool_without_but_really_big', false, 'this should appear on the next line') fp.bool('bool_without_but_really_big', false, 'this should appear on the next line')
fp.float('a_float', 1.0, 'some float as well') fp.float('a_float', 1.0, 'some float as well')
fp.string('a_string', 'not_stuff', 'your credit card number') fp.string('a_string', 'not_stuff', 'your credit card number')
usage := fp.usage() usage := fp.usage()
mut all_strings_found := true mut all_strings_found := true
for s in ['flag_tool', 'v0.0.0', for s in ['flag_tool', 'v0.0.0',
'an_int <int>', 'a_bool', 'bool_without', 'a_float <float>', 'a_string <string>:not_stuff', 'an_int <int>', 'a_bool', 'bool_without', 'a_float <float>', 'a_string <string>:not_stuff',
'some int to define', 'some int to define',
'some bool to define', 'some bool to define',
@ -182,7 +182,7 @@ fn test_if_no_description_given_usage_message_does_not_contain_descpription() {
fp.version('v0.0.0') fp.version('v0.0.0')
fp.bool('a_bool', false, '') fp.bool('a_bool', false, '')
assert !fp.usage().contains('Description:') assert !fp.usage().contains('Description:')
} }
@ -190,7 +190,7 @@ fn test_if_no_options_given_usage_message_does_not_contain_options() {
mut fp := flag.new_flag_parser([]string) mut fp := flag.new_flag_parser([]string)
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
assert !fp.usage().contains('Options:') assert !fp.usage().contains('Options:')
} }
@ -211,7 +211,7 @@ fn test_error_for_to_few_free_args() {
assert err.starts_with('Expected at least 5 arguments') assert err.starts_with('Expected at least 5 arguments')
return return
} }
assert args.len < 0 // expect an error and need to use args assert args.len < 0 // expect an error and need to use args
} }
fn test_error_for_to_much_free_args() { fn test_error_for_to_much_free_args() {
@ -221,7 +221,7 @@ fn test_error_for_to_much_free_args() {
assert err.starts_with('Expected at most 2 arguments') assert err.starts_with('Expected at most 2 arguments')
return return
} }
assert args.len < 0 // expect an error and need to use args assert args.len < 0 // expect an error and need to use args
} }
fn test_could_expect_no_free_args() { fn test_could_expect_no_free_args() {
@ -231,7 +231,7 @@ fn test_could_expect_no_free_args() {
assert err.starts_with('Expected no arguments') assert err.starts_with('Expected no arguments')
return return
} }
assert args.len < 0 // expect an error and need to use args assert args.len < 0 // expect an error and need to use args
} }
fn test_allow_abreviations() { fn test_allow_abreviations() {

View File

@ -18,7 +18,7 @@ mut:
} }
// new_values returns a new Values struct for creating // new_values returns a new Values struct for creating
// urlencoded query string parameters. it can also be to // urlencoded query string parameters. it can also be to
// post form data with application/x-www-form-urlencoded. // post form data with application/x-www-form-urlencoded.
// values.encode() will return the encoded data // values.encode() will return the encoded data
pub fn new_values() Values { pub fn new_values() Values {
@ -53,11 +53,11 @@ pub fn (v &Values) get(key string) string {
// a empty []string. // a empty []string.
pub fn (v &Values) get_all(key string) []string { pub fn (v &Values) get_all(key string) []string {
if v.data.size == 0 { if v.data.size == 0 {
return []string return []
} }
vs := v.data[key] vs := v.data[key]
if vs.data.len == 0 { if vs.data.len == 0 {
return []string return []
} }
return vs.data return vs.data
} }
@ -76,7 +76,7 @@ pub fn (v mut Values) set(key, value string) {
pub fn (v mut Values) add(key, value string) { pub fn (v mut Values) add(key, value string) {
mut a := v.data[key] mut a := v.data[key]
if a.data.len == 0 { if a.data.len == 0 {
a.data = []string a.data = []
} }
a.data << value a.data << value
v.data[key] = a v.data[key] = a