all: remove redundant array.contains definitions (#7464)

pull/7467/head
yuyi 2020-12-22 15:32:32 +08:00 committed by GitHub
parent 939e10cb28
commit b47daad40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 164 deletions

View File

@ -34,9 +34,7 @@ pub fn (nn int) str1() string {
return tos(buf + max - len, len) return tos(buf + max - len, len)
} }
*/ */
// ----- value to string functions ----- // ----- value to string functions -----
/* /*
// old function for reference // old function for reference
pub fn ptr_str(ptr voidptr) string { pub fn ptr_str(ptr voidptr) string {
@ -45,14 +43,13 @@ pub fn ptr_str(ptr voidptr) string {
return tos(buf, vstrlen(buf)) return tos(buf, vstrlen(buf))
} }
*/ */
pub fn ptr_str(ptr voidptr) string { pub fn ptr_str(ptr voidptr) string {
buf1 := u64(ptr).hex() buf1 := u64(ptr).hex()
return buf1 return buf1
} }
const( const (
digit_pairs = "00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999" digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
) )
// This implementation is the quickest with gcc -O2 // This implementation is the quickest with gcc -O2
@ -64,13 +61,11 @@ pub fn (nn int) str_l(max int) string {
return '0' return '0'
} }
mut buf := malloc(max + 1) mut buf := malloc(max + 1)
mut is_neg := false mut is_neg := false
if n < 0 { if n < 0 {
n = -n n = -n
is_neg = true is_neg = true
} }
mut index := max mut index := max
unsafe { unsafe {
buf[index--] = `\0` buf[index--] = `\0`
@ -85,12 +80,10 @@ pub fn (nn int) str_l(max int) string {
} }
} }
index++ index++
// remove head zero // remove head zero
if d < 20 { if d < 20 {
index++ index++
} }
// Prepend - if it's negative // Prepend - if it's negative
if is_neg { if is_neg {
index-- index--
@ -98,12 +91,11 @@ pub fn (nn int) str_l(max int) string {
buf[index] = `-` buf[index] = `-`
} }
} }
unsafe { unsafe {
C.memmove(buf,buf+index, (max-index)+1 ) C.memmove(buf, buf + index, (max - index) + 1)
return tos(buf, (max-index)) return tos(buf, (max - index))
} }
//return tos(buf + index, (max-index)) // return tos(buf + index, (max-index))
} }
pub fn (n i8) str() string { pub fn (n i8) str() string {
@ -130,7 +122,6 @@ pub fn (nn u32) str() string {
} }
max := 12 max := 12
mut buf := malloc(max + 1) mut buf := malloc(max + 1)
mut index := max mut index := max
unsafe { unsafe {
buf[index--] = `\0` buf[index--] = `\0`
@ -145,17 +136,15 @@ pub fn (nn u32) str() string {
} }
} }
index++ index++
// remove head zero // remove head zero
if d < u32(20) { if d < u32(20) {
index++ index++
} }
unsafe { unsafe {
C.memmove(buf,buf+index, (max-index)+1 ) C.memmove(buf, buf + index, (max - index) + 1)
return tos(buf, (max-index)) return tos(buf, (max - index))
} }
//return tos(buf + index, (max-index)) // return tos(buf + index, (max-index))
} }
[inline] [inline]
@ -171,13 +160,11 @@ pub fn (nn i64) str() string {
} }
max := 20 max := 20
mut buf := vcalloc(max + 1) mut buf := vcalloc(max + 1)
mut is_neg := false mut is_neg := false
if n < 0 { if n < 0 {
n = -n n = -n
is_neg = true is_neg = true
} }
mut index := max mut index := max
unsafe { unsafe {
buf[index--] = `\0` buf[index--] = `\0`
@ -192,12 +179,10 @@ pub fn (nn i64) str() string {
} }
} }
index++ index++
// remove head zero // remove head zero
if d < i64(20) { if d < i64(20) {
index++ index++
} }
// Prepend - if it's negative // Prepend - if it's negative
if is_neg { if is_neg {
index-- index--
@ -205,12 +190,11 @@ pub fn (nn i64) str() string {
buf[index] = `-` buf[index] = `-`
} }
} }
unsafe { unsafe {
C.memmove(buf,buf+index, (max-index)+1 ) C.memmove(buf, buf + index, (max - index) + 1)
return tos(buf, (max-index)) return tos(buf, (max - index))
} }
//return tos(buf + index, (max-index)) // return tos(buf + index, (max-index))
} }
pub fn (nn u64) str() string { pub fn (nn u64) str() string {
@ -221,7 +205,6 @@ pub fn (nn u64) str() string {
} }
max := 20 max := 20
mut buf := vcalloc(max + 1) mut buf := vcalloc(max + 1)
mut index := max mut index := max
unsafe { unsafe {
buf[index--] = `\0` buf[index--] = `\0`
@ -236,17 +219,15 @@ pub fn (nn u64) str() string {
} }
} }
index++ index++
// remove head zero // remove head zero
if d < 20 { if d < 20 {
index++ index++
} }
unsafe { unsafe {
C.memmove(buf,buf+index, (max-index)+1 ) C.memmove(buf, buf + index, (max - index) + 1)
return tos(buf, (max-index)) return tos(buf, (max - index))
} }
//return tos(buf + index, (max-index)) // return tos(buf + index, (max-index))
} }
pub fn (b bool) str() string { pub fn (b bool) str() string {
@ -257,7 +238,6 @@ pub fn (b bool) str() string {
} }
// ----- value to hex string functions ----- // ----- value to hex string functions -----
/* /*
//old function for reference //old function for reference
pub fn (n int) hex1() string { pub fn (n int) hex1() string {
@ -267,14 +247,13 @@ pub fn (n int) hex1() string {
return tos(hex, count) return tos(hex, count)
} }
*/ */
[inline] [inline]
fn u64_to_hex(nn u64, len byte) string { fn u64_to_hex(nn u64, len byte) string {
mut n := nn mut n := nn
mut buf := [256]byte{} mut buf := [256]byte{}
buf[len] = `\0` buf[len] = `\0`
mut i := 0 mut i := 0
for i=len-1; i>=0; i-- { for i = len - 1; i >= 0; i-- {
d := byte(n & 0xF) d := byte(n & 0xF)
x := if d < 10 { d + `0` } else { d + 87 } x := if d < 10 { d + `0` } else { d + 87 }
buf[i] = x buf[i] = x
@ -292,7 +271,7 @@ fn u64_to_hex_no_leading_zeros(nn u64, len byte) string {
mut buf := [256]byte{} mut buf := [256]byte{}
buf[len] = `\0` buf[len] = `\0`
mut i := 0 mut i := 0
for i=len-1; i>=0; i-- { for i = len - 1; i >= 0; i-- {
d := byte(n & 0xF) d := byte(n & 0xF)
x := if d < 10 { d + `0` } else { d + 87 } x := if d < 10 { d + `0` } else { d + 87 }
buf[i] = x 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 (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) }
pub fn (b byte) str() string { pub fn (b byte) str() string {
// TODO // TODO
//return int(b).str_l(7) // return int(b).str_l(7)
mut str := string{ mut str := string{
str: malloc(2) str: malloc(2)
len: 1 len: 1
@ -393,7 +372,7 @@ pub fn (b byte) str() string {
str.str[0] = b str.str[0] = b
str.str[1] = `\0` str.str[1] = `\0`
} }
//println(str) // println(str)
return str return str
} }
@ -412,43 +391,3 @@ pub fn (b byte) str_escaped() string {
} }
return str 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
}

View File

@ -977,17 +977,6 @@ pub fn (s string) find_between(start string, end string) string {
return val.left(end_pos) 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 { pub fn (a []string) to_c() voidptr {
mut res := malloc(sizeof(byteptr) * a.len) mut res := malloc(sizeof(byteptr) * a.len)

View File

@ -53,70 +53,47 @@ pub fn new_request(method Method, url_ string, data string) ?Request {
return Request{ return Request{
method: method method: method
url: url url: url
data: data /* data: data
headers: { /*
headers: {
'Accept-Encoding': 'compress' '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 { pub fn get(url string) ?Response {
return fetch_with_method(.get, url, FetchConfig{}) return fetch_with_method(.get, url, FetchConfig{})
} }
pub fn post(url string, data string) ?Response { pub fn post(url string, data string) ?Response {
return fetch_with_method(.post, url, { return fetch_with_method(.post, url, data: data, headers: {
data: data
headers: {
'Content-Type': content_type_default 'Content-Type': content_type_default
} })
})
} }
pub fn post_json(url string, data string) ?Response { pub fn post_json(url string, data string) ?Response {
return fetch_with_method(.post, url, { return fetch_with_method(.post, url, data: data, headers: {
data: data
headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} })
})
} }
pub fn post_form(url string, data map[string]string) ?Response { pub fn post_form(url string, data map[string]string) ?Response {
return fetch_with_method(.post, url, { return fetch_with_method(.post, url, headers: {
headers: {
'Content-Type': 'application/x-www-form-urlencoded' '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 { pub fn put(url string, data string) ?Response {
return fetch_with_method(.put, url, { return fetch_with_method(.put, url, data: data, headers: {
data: data
headers: {
'Content-Type': content_type_default 'Content-Type': content_type_default
} })
})
} }
pub fn patch(url string, data string) ?Response { pub fn patch(url string, data string) ?Response {
return fetch_with_method(.patch, url, { return fetch_with_method(.patch, url, data: data, headers: {
data: data
headers: {
'Content-Type': content_type_default 'Content-Type': content_type_default
} })
})
} }
pub fn head(url string) ?Response { pub fn head(url string) ?Response {
@ -131,9 +108,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
if _url == '' { if _url == '' {
return error('http.fetch: empty url') return error('http.fetch: empty url')
} }
url := build_url_from_fetch(_url, config) or { url := build_url_from_fetch(_url, config) or { return error('http.fetch: invalid url $_url') }
return error('http.fetch: invalid url $_url')
}
data := config.data data := config.data
req := Request{ req := Request{
method: config.method method: config.method
@ -151,11 +126,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
} }
pub fn get_text(url string) string { pub fn get_text(url string) string {
resp := fetch(url, { resp := fetch(url, method: .get) or { return '' }
method: .get
}) or {
return ''
}
return resp.text 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 // do will send the HTTP request and returns `http.Response` as soon as the response is recevied
pub fn (req &Request) do() ?Response { pub fn (req &Request) do() ?Response {
mut url := urllib.parse(req.url) or { mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url $req.url') }
return error('http.Request.do: invalid url $req.url')
}
mut rurl := url mut rurl := url
mut resp := Response{} mut resp := Response{}
mut no_redirects := 0 mut no_redirects := 0
@ -276,7 +245,7 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Res
return res return res
} else if scheme == 'http' { } else if scheme == 'http' {
// println('http_do( $nport, $method, $host_name, $path )') // 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 res
} }
return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"') return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"')
@ -311,9 +280,7 @@ fn parse_response(resp string) Response {
break break
} }
i++ i++
pos := h.index(':') or { pos := h.index(':') or { continue }
continue
}
// if h.contains('Content-Type') { // if h.contains('Content-Type') {
// continue // continue
// } // }
@ -393,12 +360,12 @@ pub fn escape(s string) string {
} }
fn (req &Request) http_do(host string, method Method, path string) ?Response { 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) 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 // TODO this really needs to be exposed somehow
client.write(s.bytes())? client.write(s.bytes()) ?
mut bytes := io.read_all(reader: client)? mut bytes := io.read_all(reader: client) ?
client.close() client.close()
return parse_response(bytes.bytestr()) return parse_response(bytes.bytestr())
} }

View File

@ -93,15 +93,6 @@ pub fn (t Type) share() ShareType {
return sharetype_from_flags(t.has_flag(.shared_f), t.has_flag(.atomic_f)) 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` // return TypeSymbol idx for `t`
[inline] [inline]
pub fn (t Type) idx() int { pub fn (t Type) idx() int {

View File

@ -311,15 +311,6 @@ pub fn (t Kind) is_assign() bool {
return t in assign_tokens 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 { pub fn (t Kind) str() string {
return token_str[int(t)] return token_str[int(t)]
} }