mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: remove redundant array.contains definitions (#7464)
This commit is contained in:
parent
939e10cb28
commit
b47daad40d
@ -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,7 +91,6 @@ 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))
|
||||||
@ -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,12 +136,10 @@ 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))
|
||||||
@ -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,7 +190,6 @@ 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))
|
||||||
@ -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,12 +219,10 @@ 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))
|
||||||
@ -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,7 +247,6 @@ 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
|
||||||
@ -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
|
|
||||||
}
|
|
||||||
|
@ -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)
|
||||||
|
@ -53,7 +53,8 @@ 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'
|
||||||
}
|
}
|
||||||
@ -61,61 +62,37 @@ pub fn new_request(method Method, url_ string, data string) ?Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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
|
||||||
@ -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
|
||||||
// }
|
// }
|
||||||
|
@ -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 {
|
||||||
|
@ -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)]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user