From b47daad40dfa5e6660c3aaf2d545ad9fec20830b Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 22 Dec 2020 15:32:32 +0800 Subject: [PATCH] all: remove redundant array.contains definitions (#7464) --- vlib/builtin/int.v | 97 ++++++++----------------------------------- vlib/builtin/string.v | 11 ----- vlib/net/http/http.v | 79 ++++++++++------------------------- vlib/v/table/types.v | 9 ---- vlib/v/token/token.v | 9 ---- 5 files changed, 41 insertions(+), 164 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 03af49c1f0..7de7c7a340 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -34,9 +34,7 @@ pub fn (nn int) str1() string { return tos(buf + max - len, len) } */ - // ----- value to string functions ----- - /* // old function for reference pub fn ptr_str(ptr voidptr) string { @@ -45,14 +43,13 @@ pub fn ptr_str(ptr voidptr) string { return tos(buf, vstrlen(buf)) } */ - pub fn ptr_str(ptr voidptr) string { buf1 := u64(ptr).hex() return buf1 } -const( - digit_pairs = "00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999" +const ( + digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999' ) // This implementation is the quickest with gcc -O2 @@ -64,13 +61,11 @@ pub fn (nn int) str_l(max int) string { return '0' } mut buf := malloc(max + 1) - mut is_neg := false if n < 0 { n = -n is_neg = true } - mut index := max unsafe { buf[index--] = `\0` @@ -85,12 +80,10 @@ pub fn (nn int) str_l(max int) string { } } index++ - // remove head zero if d < 20 { index++ } - // Prepend - if it's negative if is_neg { index-- @@ -98,12 +91,11 @@ pub fn (nn int) str_l(max int) string { buf[index] = `-` } } - unsafe { - C.memmove(buf,buf+index, (max-index)+1 ) - return tos(buf, (max-index)) + C.memmove(buf, buf + index, (max - index) + 1) + return tos(buf, (max - index)) } - //return tos(buf + index, (max-index)) + // return tos(buf + index, (max-index)) } pub fn (n i8) str() string { @@ -130,7 +122,6 @@ pub fn (nn u32) str() string { } max := 12 mut buf := malloc(max + 1) - mut index := max unsafe { buf[index--] = `\0` @@ -145,17 +136,15 @@ pub fn (nn u32) str() string { } } index++ - // remove head zero if d < u32(20) { index++ } - unsafe { - C.memmove(buf,buf+index, (max-index)+1 ) - return tos(buf, (max-index)) + C.memmove(buf, buf + index, (max - index) + 1) + return tos(buf, (max - index)) } - //return tos(buf + index, (max-index)) + // return tos(buf + index, (max-index)) } [inline] @@ -171,13 +160,11 @@ pub fn (nn i64) str() string { } max := 20 mut buf := vcalloc(max + 1) - mut is_neg := false if n < 0 { n = -n is_neg = true } - mut index := max unsafe { buf[index--] = `\0` @@ -192,12 +179,10 @@ pub fn (nn i64) str() string { } } index++ - // remove head zero if d < i64(20) { index++ } - // Prepend - if it's negative if is_neg { index-- @@ -205,12 +190,11 @@ pub fn (nn i64) str() string { buf[index] = `-` } } - unsafe { - C.memmove(buf,buf+index, (max-index)+1 ) - return tos(buf, (max-index)) + C.memmove(buf, buf + index, (max - index) + 1) + return tos(buf, (max - index)) } - //return tos(buf + index, (max-index)) + // return tos(buf + index, (max-index)) } pub fn (nn u64) str() string { @@ -221,7 +205,6 @@ pub fn (nn u64) str() string { } max := 20 mut buf := vcalloc(max + 1) - mut index := max unsafe { buf[index--] = `\0` @@ -236,17 +219,15 @@ pub fn (nn u64) str() string { } } index++ - // remove head zero if d < 20 { index++ } - unsafe { - C.memmove(buf,buf+index, (max-index)+1 ) - return tos(buf, (max-index)) + C.memmove(buf, buf + index, (max - index) + 1) + return tos(buf, (max - index)) } - //return tos(buf + index, (max-index)) + // return tos(buf + index, (max-index)) } pub fn (b bool) str() string { @@ -257,7 +238,6 @@ pub fn (b bool) str() string { } // ----- value to hex string functions ----- - /* //old function for reference pub fn (n int) hex1() string { @@ -267,14 +247,13 @@ pub fn (n int) hex1() string { return tos(hex, count) } */ - [inline] fn u64_to_hex(nn u64, len byte) string { mut n := nn mut buf := [256]byte{} buf[len] = `\0` mut i := 0 - for i=len-1; i>=0; i-- { + for i = len - 1; i >= 0; i-- { d := byte(n & 0xF) x := if d < 10 { d + `0` } else { d + 87 } buf[i] = x @@ -292,7 +271,7 @@ fn u64_to_hex_no_leading_zeros(nn u64, len byte) string { mut buf := [256]byte{} buf[len] = `\0` mut i := 0 - for i=len-1; i>=0; i-- { + for i = len - 1; i >= 0; i-- { d := byte(n & 0xF) x := if d < 10 { d + `0` } else { d + 87 } buf[i] = x @@ -384,7 +363,7 @@ pub fn (nn u64) hex_full() string { // pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) } pub fn (b byte) str() string { // TODO - //return int(b).str_l(7) + // return int(b).str_l(7) mut str := string{ str: malloc(2) len: 1 @@ -393,7 +372,7 @@ pub fn (b byte) str() string { str.str[0] = b str.str[1] = `\0` } - //println(str) + // println(str) return str } @@ -412,43 +391,3 @@ pub fn (b byte) str_escaped() string { } return str } - -// TODO generic -pub fn (a []byte) contains(val byte) bool { - for aa in a { - if aa == val { - return true - } - } - return false -} - -// TODO generic -pub fn (a []u16) contains(val u16) bool { - for aa in a { - if aa == val { - return true - } - } - return false -} - -// TODO generic -fn (ar []int) contains(val int) bool { - for s in ar { - if s == val { - return true - } - } - return false -} - -// TODO generic -pub fn (a []u64) contains(val u64) bool { - for aa in a { - if aa == val { - return true - } - } - return false -} diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 4a930a320e..bcf5c1a78d 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -977,17 +977,6 @@ pub fn (s string) find_between(start string, end string) string { return val.left(end_pos) } -// contains returns `true` if `val` string is found in the array. -// TODO generic -fn (ar []string) contains(val string) bool { - for s in ar { - if s == val { - return true - } - } - return false -} - /* pub fn (a []string) to_c() voidptr { mut res := malloc(sizeof(byteptr) * a.len) diff --git a/vlib/net/http/http.v b/vlib/net/http/http.v index 0b118975c6..beae965972 100644 --- a/vlib/net/http/http.v +++ b/vlib/net/http/http.v @@ -53,70 +53,47 @@ pub fn new_request(method Method, url_ string, data string) ?Request { return Request{ method: method url: url - data: data /* - headers: { + data: data + /* + headers: { 'Accept-Encoding': 'compress' } - */ + */ } } -fn (methods []Method) contains(m Method) bool { - for method in methods { - if method == m { - return true - } - } - return false -} - pub fn get(url string) ?Response { return fetch_with_method(.get, url, FetchConfig{}) } pub fn post(url string, data string) ?Response { - return fetch_with_method(.post, url, { - data: data - headers: { + return fetch_with_method(.post, url, data: data, headers: { 'Content-Type': content_type_default - } - }) + }) } pub fn post_json(url string, data string) ?Response { - return fetch_with_method(.post, url, { - data: data - headers: { + return fetch_with_method(.post, url, data: data, headers: { 'Content-Type': 'application/json' - } - }) + }) } pub fn post_form(url string, data map[string]string) ?Response { - return fetch_with_method(.post, url, { - headers: { + return fetch_with_method(.post, url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' - } - data: url_encode_form_data(data) - }) + }, data: url_encode_form_data(data)) } pub fn put(url string, data string) ?Response { - return fetch_with_method(.put, url, { - data: data - headers: { + return fetch_with_method(.put, url, data: data, headers: { 'Content-Type': content_type_default - } - }) + }) } pub fn patch(url string, data string) ?Response { - return fetch_with_method(.patch, url, { - data: data - headers: { + return fetch_with_method(.patch, url, data: data, headers: { 'Content-Type': content_type_default - } - }) + }) } pub fn head(url string) ?Response { @@ -131,9 +108,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response { if _url == '' { return error('http.fetch: empty url') } - url := build_url_from_fetch(_url, config) or { - return error('http.fetch: invalid url $_url') - } + url := build_url_from_fetch(_url, config) or { return error('http.fetch: invalid url $_url') } data := config.data req := Request{ method: config.method @@ -151,11 +126,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response { } pub fn get_text(url string) string { - resp := fetch(url, { - method: .get - }) or { - return '' - } + resp := fetch(url, method: .get) or { return '' } return resp.text } @@ -223,9 +194,7 @@ pub fn parse_headers(lines []string) map[string]string { // do will send the HTTP request and returns `http.Response` as soon as the response is recevied pub fn (req &Request) do() ?Response { - mut url := urllib.parse(req.url) or { - return error('http.Request.do: invalid url $req.url') - } + mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url $req.url') } mut rurl := url mut resp := Response{} mut no_redirects := 0 @@ -276,7 +245,7 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Res return res } else if scheme == 'http' { // println('http_do( $nport, $method, $host_name, $path )') - res := req.http_do('$host_name:$nport', method, path)? + res := req.http_do('$host_name:$nport', method, path) ? return res } return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"') @@ -311,9 +280,7 @@ fn parse_response(resp string) Response { break } i++ - pos := h.index(':') or { - continue - } + pos := h.index(':') or { continue } // if h.contains('Content-Type') { // continue // } @@ -393,12 +360,12 @@ pub fn escape(s string) string { } fn (req &Request) http_do(host string, method Method, path string) ?Response { - host_name, _ := net.split_address(host)? + host_name, _ := net.split_address(host) ? s := req.build_request_headers(method, host_name, path) - mut client := net.dial_tcp(host)? + mut client := net.dial_tcp(host) ? // TODO this really needs to be exposed somehow - client.write(s.bytes())? - mut bytes := io.read_all(reader: client)? + client.write(s.bytes()) ? + mut bytes := io.read_all(reader: client) ? client.close() return parse_response(bytes.bytestr()) } diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 44e9861317..aec412fe14 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -93,15 +93,6 @@ pub fn (t Type) share() ShareType { return sharetype_from_flags(t.has_flag(.shared_f), t.has_flag(.atomic_f)) } -pub fn (types []Type) contains(typ Type) bool { - for t in types { - if int(typ) == int(t) { - return true - } - } - return false -} - // return TypeSymbol idx for `t` [inline] pub fn (t Type) idx() int { diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 319f1ecf9b..0646ce1e38 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -311,15 +311,6 @@ pub fn (t Kind) is_assign() bool { return t in assign_tokens } -fn (t []Kind) contains(val Kind) bool { - for tt in t { - if tt == val { - return true - } - } - return false -} - pub fn (t Kind) str() string { return token_str[int(t)] }