1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: change optional to result of io (#16075)

This commit is contained in:
yuyi
2022-10-16 14:28:57 +08:00
committed by GitHub
parent 6e46933c55
commit f6844e9766
187 changed files with 1885 additions and 1874 deletions

View File

@ -127,7 +127,7 @@ fn should_escape(c u8, mode EncodingMode) bool {
// hex-decoded byte 0xAB.
// It returns an error if any % is not followed by two hexadecimal
// digits.
pub fn query_unescape(s string) ?string {
pub fn query_unescape(s string) !string {
return unescape(s, .encode_query_component)
}
@ -138,13 +138,13 @@ pub fn query_unescape(s string) ?string {
//
// path_unescape is identical to query_unescape except that it does not
// unescape '+' to ' ' (space).
pub fn path_unescape(s string) ?string {
pub fn path_unescape(s string) !string {
return unescape(s, .encode_path_segment)
}
// unescape unescapes a string; the mode specifies
// which section of the URL string is being unescaped.
fn unescape(s_ string, mode EncodingMode) ?string {
fn unescape(s_ string, mode EncodingMode) !string {
mut s := s_
// Count %, check that they're well-formed.
mut n := 0
@ -387,7 +387,7 @@ fn (u &Userinfo) str() string {
// Maybe rawurl is of the form scheme:path.
// (scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// If so, return [scheme, path]; else return ['', rawurl]
fn split_by_scheme(rawurl string) ?[]string {
fn split_by_scheme(rawurl string) ![]string {
for i in 0 .. rawurl.len {
c := rawurl[i]
if (`a` <= c && c <= `z`) || (`A` <= c && c <= `Z`) {
@ -410,7 +410,7 @@ fn split_by_scheme(rawurl string) ?[]string {
return ['', rawurl]
}
fn get_scheme(rawurl string) ?string {
fn get_scheme(rawurl string) !string {
split := split_by_scheme(rawurl) or { return err.msg() }
return split[0]
}
@ -435,7 +435,7 @@ fn split(s string, sep u8, cutc bool) (string, string) {
// (starting with a scheme). Trying to parse a hostname and path
// without a scheme is invalid but may not necessarily return an
// error, due to parsing ambiguities.
pub fn parse(rawurl string) ?URL {
pub fn parse(rawurl string) !URL {
// Cut off #frag
u, frag := split(rawurl, `#`, true)
mut url := parse_url(u, false) or { return error(error_msg(urllib.err_msg_parse, u)) }
@ -453,7 +453,7 @@ pub fn parse(rawurl string) ?URL {
// only as an absolute URI or an absolute path.
// The string rawurl is assumed not to have a #fragment suffix.
// (Web browsers strip #fragment before sending the URL to a web server.)
fn parse_request_uri(rawurl string) ?URL {
fn parse_request_uri(rawurl string) !URL {
return parse_url(rawurl, true)
}
@ -462,7 +462,7 @@ fn parse_request_uri(rawurl string) ?URL {
// in which case only absolute URLs or path-absolute relative URLs are allowed.
// If via_request is false, all forms of relative URLs are allowed.
[manualfree]
fn parse_url(rawurl string, via_request bool) ?URL {
fn parse_url(rawurl string, via_request bool) !URL {
if string_contains_ctl_u8(rawurl) {
return error(error_msg('parse_url: invalid control character in URL', rawurl))
}
@ -478,7 +478,7 @@ fn parse_url(rawurl string, via_request bool) ?URL {
}
// Split off possible leading 'http:', 'mailto:', etc.
// Cannot contain escaped characters.
p := split_by_scheme(rawurl)?
p := split_by_scheme(rawurl)!
url.scheme = p[0]
mut rest := p[1]
url.scheme = url.scheme.to_lower()
@ -517,7 +517,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[2..], `/`, false)
rest = r
a := parse_authority(authority)?
a := parse_authority(authority)!
url.user = a.user
url.host = a.host
}
@ -525,7 +525,7 @@ fn parse_url(rawurl string, via_request bool) ?URL {
// raw_path is a hint of the encoding of path. We don't want to set it if
// the default escaping of path is equivalent, to help make sure that people
// don't rely on it in general.
url.set_path(rest)?
url.set_path(rest)!
return url
}
@ -534,15 +534,15 @@ struct ParseAuthorityRes {
host string
}
fn parse_authority(authority string) ?ParseAuthorityRes {
fn parse_authority(authority string) !ParseAuthorityRes {
i := authority.last_index('@') or { -1 }
mut host := ''
mut zuser := user('')
if i < 0 {
h := parse_host(authority)?
h := parse_host(authority)!
host = h
} else {
h := parse_host(authority[i + 1..])?
h := parse_host(authority[i + 1..])!
host = h
}
if i < 0 {
@ -556,14 +556,14 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
return error(error_msg('parse_authority: invalid userinfo', ''))
}
if !userinfo.contains(':') {
u := unescape(userinfo, .encode_user_password)?
u := unescape(userinfo, .encode_user_password)!
userinfo = u
zuser = user(userinfo)
} else {
mut username, mut password := split(userinfo, `:`, true)
u := unescape(username, .encode_user_password)?
u := unescape(username, .encode_user_password)!
username = u
p := unescape(password, .encode_user_password)?
p := unescape(password, .encode_user_password)!
password = p
zuser = user_password(username, password)
}
@ -575,7 +575,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes {
// parse_host parses host as an authority without user
// information. That is, as host[:port].
fn parse_host(host string) ?string {
fn parse_host(host string) !string {
if host.starts_with('[') {
// parse an IP-Literal in RFC 3986 and RFC 6874.
// E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'.
@ -620,8 +620,8 @@ fn parse_host(host string) ?string {
// - set_path('/foo%2fbar') will set path='/foo/bar' and raw_path='/foo%2fbar'
// set_path will return an error only if the provided path contains an invalid
// escaping.
pub fn (mut u URL) set_path(p string) ?bool {
u.path = unescape(p, .encode_path)?
pub fn (mut u URL) set_path(p string) !bool {
u.path = unescape(p, .encode_path)!
u.raw_path = if p == escape(u.path, .encode_path) { '' } else { p }
return true
}
@ -781,9 +781,9 @@ pub fn (u URL) str() string {
// Query is expected to be a list of key=value settings separated by
// ampersands or semicolons. A setting without an equals sign is
// interpreted as a key set to an empty value.
pub fn parse_query(query string) ?Values {
pub fn parse_query(query string) !Values {
mut m := new_values()
parse_query_values(mut m, query)?
parse_query_values(mut m, query)!
return m
}
@ -795,7 +795,7 @@ fn parse_query_silent(query string) Values {
return m
}
fn parse_query_values(mut m Values, query string) ?bool {
fn parse_query_values(mut m Values, query string) !bool {
mut had_error := false
mut q := query
for q != '' {
@ -914,8 +914,8 @@ pub fn (u &URL) is_abs() bool {
// parse parses a URL in the context of the receiver. The provided URL
// may be relative or absolute. parse returns nil, err on parse
// failure, otherwise its return value is the same as resolve_reference.
pub fn (u &URL) parse(ref string) ?URL {
refurl := parse(ref)?
pub fn (u &URL) parse(ref string) !URL {
refurl := parse(ref)!
return u.resolve_reference(refurl)
}
@ -925,7 +925,7 @@ pub fn (u &URL) parse(ref string) ?URL {
// URL instance, even if the returned URL is identical to either the
// base or reference. If ref is an absolute URL, then resolve_reference
// ignores base and returns a copy of ref.
pub fn (u &URL) resolve_reference(ref &URL) ?URL {
pub fn (u &URL) resolve_reference(ref &URL) !URL {
mut url := *ref
if ref.scheme == '' {
url.scheme = u.scheme
@ -934,7 +934,7 @@ pub fn (u &URL) resolve_reference(ref &URL) ?URL {
// The 'absoluteURI' or 'net_path' cases.
// We can ignore the error from set_path since we know we provided a
// validly-escaped path.
url.set_path(resolve_path(ref.escaped_path(), ''))?
url.set_path(resolve_path(ref.escaped_path(), ''))!
return url
}
if ref.opaque != '' {
@ -952,7 +952,7 @@ pub fn (u &URL) resolve_reference(ref &URL) ?URL {
// The 'abs_path' or 'rel_path' cases.
url.host = u.host
url.user = u.user
url.set_path(resolve_path(u.escaped_path(), ref.escaped_path()))?
url.set_path(resolve_path(u.escaped_path(), ref.escaped_path()))!
return url
}