mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler/vlib: replace substr/left/right with [start..end] everywhere
This commit is contained in:
committed by
Alexander Medvednikov
parent
ed55826686
commit
59378dce46
@@ -161,9 +161,9 @@ fn unescape(s_ string, mode EncodingMode) ?string {
|
||||
}
|
||||
n++
|
||||
if i+2 >= s.len || !ishex(s[i+1]) || !ishex(s[i+2]) {
|
||||
s = s.right(i)
|
||||
s = s[i..]
|
||||
if s.len > 3 {
|
||||
s = s.left(3)
|
||||
s = s[..3]
|
||||
}
|
||||
return error(error_msg(err_msg_escape, s))
|
||||
}
|
||||
@@ -173,8 +173,8 @@ fn unescape(s_ string, mode EncodingMode) ?string {
|
||||
// But https://tools.ietf.org/html/rfc6874#section-2
|
||||
// introduces %25 being allowed to escape a percent sign
|
||||
// in IPv6 scoped-address literals. Yay.
|
||||
if mode == .encode_host && unhex(s[i+1]) < 8 && s.substr(i, i+3) != '%25' {
|
||||
return error(error_msg(err_msg_escape, s.substr(i, i+3)))
|
||||
if mode == .encode_host && unhex(s[i+1]) < 8 && s[i..i+3] != '%25' {
|
||||
return error(error_msg(err_msg_escape, s[i..i+3]))
|
||||
}
|
||||
if mode == .encode_zone {
|
||||
// RFC 6874 says basically 'anything goes' for zone identifiers
|
||||
@@ -185,19 +185,18 @@ fn unescape(s_ string, mode EncodingMode) ?string {
|
||||
// to introduce bytes you couldn't just write directly.
|
||||
// But Windows puts spaces here! Yay.
|
||||
v := byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2]))
|
||||
if s.substr(i, i+3) != '%25' && v != ` ` && should_escape(v, .encode_host) {
|
||||
error(error_msg(err_msg_escape, s.substr(i, i+3)))
|
||||
if s[i..i+3] != '%25' && v != ` ` && should_escape(v, .encode_host) {
|
||||
error(error_msg(err_msg_escape, s[i..i+3]))
|
||||
}
|
||||
}
|
||||
i += 3
|
||||
}
|
||||
|
||||
`+`{
|
||||
has_plus = mode == .encode_query_component
|
||||
i++
|
||||
} else {
|
||||
if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) {
|
||||
error(error_msg('invalid character in host name', s.substr(i, i+1)))
|
||||
error(error_msg('invalid character in host name', s[i..i+1]))
|
||||
}
|
||||
i++
|
||||
}
|
||||
@@ -266,7 +265,7 @@ fn escape(s string, mode EncodingMode) string {
|
||||
|
||||
required := s.len + 2*hex_count
|
||||
if required <= buf.len {
|
||||
t = buf.left(required)
|
||||
t = buf[..required]
|
||||
} else {
|
||||
t = [byte(0)].repeat(required)
|
||||
}
|
||||
@@ -400,7 +399,7 @@ fn split_by_scheme(rawurl string) ?[]string {
|
||||
if i == 0 {
|
||||
return error(error_msg('missing protocol scheme', ''))
|
||||
}
|
||||
return [rawurl.left(i), rawurl.right(i+1)]
|
||||
return [rawurl[..i], rawurl[i+1..]]
|
||||
}
|
||||
else {
|
||||
// we have encountered an invalid character,
|
||||
@@ -427,9 +426,9 @@ fn split(s string, sep byte, cutc bool) (string, string) {
|
||||
return s, ''
|
||||
}
|
||||
if cutc {
|
||||
return s.left(i), s.right(i+1)
|
||||
return s[..i], s[i+1..]
|
||||
}
|
||||
return s.left(i), s.right(i)
|
||||
return s[..i], s[i..]
|
||||
}
|
||||
|
||||
// parse parses rawurl into a URL structure.
|
||||
@@ -492,9 +491,9 @@ fn parse_url(rawurl string, via_request bool) ?URL {
|
||||
url.scheme = url.scheme.to_lower()
|
||||
|
||||
// if rest.ends_with('?') && strings.count(rest, '?') == 1 {
|
||||
if rest.ends_with('?') && !rest.left(1).contains('?') {
|
||||
if rest.ends_with('?') && !rest[..1].contains('?') {
|
||||
url.force_query = true
|
||||
rest = rest.left(rest.len-1)
|
||||
rest = rest[..rest.len-1]
|
||||
} else {
|
||||
r, raw_query := split(rest, `?`, true)
|
||||
rest = r
|
||||
@@ -526,7 +525,7 @@ fn parse_url(rawurl string, via_request bool) ?URL {
|
||||
}
|
||||
|
||||
if ((url.scheme != '' || !via_request) && !rest.starts_with('///')) && rest.starts_with('//') {
|
||||
authority, r := split(rest.right(2), `/`, false)
|
||||
authority, r := split(rest[2..], `/`, false)
|
||||
rest = r
|
||||
a := parse_authority(authority) or {
|
||||
return error(err)
|
||||
@@ -559,7 +558,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||
}
|
||||
host = h
|
||||
} else {
|
||||
h := parse_host(authority.right(i+1)) or {
|
||||
h := parse_host(authority[i+1..]) or {
|
||||
return error(err)
|
||||
}
|
||||
host = h
|
||||
@@ -567,7 +566,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
|
||||
if i < 0 {
|
||||
return ParseAuthorityRes{host: host}
|
||||
}
|
||||
mut userinfo := authority.left(i)
|
||||
mut userinfo := authority[..i]
|
||||
if !valid_userinfo(userinfo) {
|
||||
return error(error_msg('invalid userinfo', ''))
|
||||
}
|
||||
@@ -605,7 +604,7 @@ fn parse_host(host string) ?string {
|
||||
if i < 0 {
|
||||
return error(error_msg('missing \']\' in host', ''))
|
||||
}
|
||||
mut colon_port := host.right(i+1)
|
||||
mut colon_port := host[i+1..]
|
||||
if !valid_optional_port(colon_port) {
|
||||
return error(error_msg('invalid port $colon_port after host ', ''))
|
||||
}
|
||||
@@ -616,22 +615,22 @@ fn parse_host(host string) ?string {
|
||||
// can only %-encode non-ASCII bytes.
|
||||
// We do impose some restrictions on the zone, to avoid stupidity
|
||||
// like newlines.
|
||||
zone := host.left(i).index('%25')
|
||||
zone := host[..i].index('%25')
|
||||
if zone >= 0 {
|
||||
host1 := unescape(host.left(zone), .encode_host) or {
|
||||
host1 := unescape(host[..zone], .encode_host) or {
|
||||
return err
|
||||
}
|
||||
host2 := unescape(host.substr(zone, i), .encode_zone) or {
|
||||
host2 := unescape(host[zone..i], .encode_zone) or {
|
||||
return err
|
||||
}
|
||||
host3 := unescape(host.right(i), .encode_host) or {
|
||||
host3 := unescape(host[i..], .encode_host) or {
|
||||
return err
|
||||
}
|
||||
return host1 + host2 + host3
|
||||
} else {
|
||||
i = host.last_index(':')
|
||||
if i != -1 {
|
||||
colon_port = host.right(i)
|
||||
colon_port = host[i..]
|
||||
if !valid_optional_port(colon_port) {
|
||||
return error(error_msg('invalid port $colon_port after host ', ''))
|
||||
}
|
||||
@@ -728,7 +727,7 @@ fn valid_optional_port(port string) bool {
|
||||
if port[0] != `:` {
|
||||
return false
|
||||
}
|
||||
for b in port.right(1) {
|
||||
for b in port[1..] {
|
||||
if b < `0` || b > `9` {
|
||||
return false
|
||||
}
|
||||
@@ -790,7 +789,7 @@ pub fn (u &URL) str() string {
|
||||
// preceded by a dot-segment (e.g., './this:that') to make a relative-
|
||||
// path reference.
|
||||
i := path.index_byte(`:`)
|
||||
if i > -1 && path.left(i).index_byte(`/`) == -1 {
|
||||
if i > -1 && path[..i].index_byte(`/`) == -1 {
|
||||
buf.write('./')
|
||||
}
|
||||
}
|
||||
@@ -845,8 +844,8 @@ fn parse_query_values(m mut Values, query string) ?bool {
|
||||
mut key := q
|
||||
mut i := key.index_any('&;')
|
||||
if i >= 0 {
|
||||
q = key.right(i+1)
|
||||
key = key.left(i)
|
||||
q = key[i+1..]
|
||||
key = key[..i]
|
||||
} else {
|
||||
q = ''
|
||||
}
|
||||
@@ -856,8 +855,8 @@ fn parse_query_values(m mut Values, query string) ?bool {
|
||||
mut value := ''
|
||||
i = key.index('=')
|
||||
if i >= 0 {
|
||||
value = key.right(i+1)
|
||||
key = key.left(i)
|
||||
value = key[i+1..]
|
||||
key = key[..i]
|
||||
}
|
||||
k := query_unescape(key) or {
|
||||
had_error = true
|
||||
@@ -913,7 +912,7 @@ fn resolve_path(base, ref string) string {
|
||||
full = base
|
||||
} else if ref[0] != `/` {
|
||||
i := base.last_index('/')
|
||||
full = base.left(i+1) + ref
|
||||
full = base[..i+1] + ref
|
||||
} else {
|
||||
full = ref
|
||||
}
|
||||
@@ -929,7 +928,7 @@ fn resolve_path(base, ref string) string {
|
||||
}
|
||||
'..' {
|
||||
if dst.len > 0 {
|
||||
dst = dst.left(dst.len-1)
|
||||
dst = dst[..dst.len-1]
|
||||
}
|
||||
} else {
|
||||
dst << elem
|
||||
@@ -1049,13 +1048,13 @@ fn split_host_port(hostport string) (string, string) {
|
||||
mut port := ''
|
||||
|
||||
colon := host.last_index_byte(`:`)
|
||||
if colon != -1 && valid_optional_port(host.right(colon)) {
|
||||
port = host.right(colon+1)
|
||||
host = host.left(colon)
|
||||
if colon != -1 && valid_optional_port(host[colon..]) {
|
||||
port = host[colon+1..]
|
||||
host = host[..colon]
|
||||
}
|
||||
|
||||
if host.starts_with('[') && host.ends_with(']') {
|
||||
host = host.substr(1, host.len-1)
|
||||
host = host[1..host.len-1]
|
||||
}
|
||||
|
||||
return host, port
|
||||
|
||||
Reference in New Issue
Block a user