mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
- Create optional with ~ instead of ?. return io::EOF?; becomes return io::EOF~.
- Deprecated use of `?` to create optional.
This commit is contained in:
@@ -68,11 +68,11 @@ fn String InetAddress.to_tstring(&self)
|
||||
fn InetAddress? ipv6_from_str(String s)
|
||||
{
|
||||
uint sections = 0;
|
||||
if (s.len < 2) return INVALID_IP_STRING?;
|
||||
if (s.len < 2) return INVALID_IP_STRING~;
|
||||
foreach (c : s) if (c == ':') sections++;
|
||||
int zero_segment_len = s[0] == ':' || s[^1] == ':' ? 9 - sections : 8 - sections;
|
||||
if (zero_segment_len == 7 && s.len == 2) return { .is_ipv6 = true };
|
||||
if (zero_segment_len > 7) return INVALID_IP_STRING?;
|
||||
if (zero_segment_len > 7) return INVALID_IP_STRING~;
|
||||
usz index = 0;
|
||||
bool last_was_colon, found_zero;
|
||||
int current = -1;
|
||||
@@ -88,7 +88,7 @@ fn InetAddress? ipv6_from_str(String s)
|
||||
last_was_colon = true;
|
||||
continue;
|
||||
}
|
||||
if (current < 0 || current > 65535) return INVALID_IP_STRING?;
|
||||
if (current < 0 || current > 65535) return INVALID_IP_STRING~;
|
||||
addr.ipv6arr[index++].val = (ushort)current;
|
||||
current = -1;
|
||||
last_was_colon = true;
|
||||
@@ -96,9 +96,9 @@ fn InetAddress? ipv6_from_str(String s)
|
||||
}
|
||||
assert(current == -1);
|
||||
// Check that this is the first ::
|
||||
if (found_zero) return INVALID_IP_STRING?;
|
||||
if (found_zero) return INVALID_IP_STRING~;
|
||||
// Also check that the zeroed section is at least 2
|
||||
if (zero_segment_len < 2) return INVALID_IP_STRING?;
|
||||
if (zero_segment_len < 2) return INVALID_IP_STRING~;
|
||||
// Skip (will be zero by default
|
||||
index += zero_segment_len;
|
||||
found_zero = true;
|
||||
@@ -106,7 +106,7 @@ fn InetAddress? ipv6_from_str(String s)
|
||||
continue;
|
||||
}
|
||||
last_was_colon = false;
|
||||
if (index > 7 || !c.is_xdigit()) return INVALID_IP_STRING?;
|
||||
if (index > 7 || !c.is_xdigit()) return INVALID_IP_STRING~;
|
||||
if (current < 0) current = 0;
|
||||
current = current * 16 + (c <= '9' ? c - '0' : (c | 32) - 'a' + 10);
|
||||
}
|
||||
@@ -114,7 +114,7 @@ fn InetAddress? ipv6_from_str(String s)
|
||||
if (index == 8 && current == -1) return addr;
|
||||
|
||||
// Ends with number
|
||||
if (index != 7 || current < 0 || current > 65535) return INVALID_IP_STRING?;
|
||||
if (index != 7 || current < 0 || current > 65535) return INVALID_IP_STRING~;
|
||||
addr.ipv6arr[7].val = (ushort)current;
|
||||
return addr;
|
||||
}
|
||||
@@ -128,20 +128,20 @@ fn InetAddress? ipv4_from_str(String s)
|
||||
{
|
||||
if (c == '.')
|
||||
{
|
||||
if (current < 0) return INVALID_IP_STRING?;
|
||||
if (current > 255) return INVALID_IP_STRING?;
|
||||
if (current < 0) return INVALID_IP_STRING~;
|
||||
if (current > 255) return INVALID_IP_STRING~;
|
||||
switch (element)
|
||||
{
|
||||
case 0: addr.ipv4.a = (char)current;
|
||||
case 1: addr.ipv4.b = (char)current;
|
||||
case 2: addr.ipv4.c = (char)current;
|
||||
default: return INVALID_IP_STRING?;
|
||||
default: return INVALID_IP_STRING~;
|
||||
}
|
||||
current = -1;
|
||||
element++;
|
||||
continue;
|
||||
}
|
||||
if (element > 3 || c < '0' || c > '9') return INVALID_IP_STRING?;
|
||||
if (element > 3 || c < '0' || c > '9') return INVALID_IP_STRING~;
|
||||
if (current < 0)
|
||||
{
|
||||
current = c - '0';
|
||||
@@ -149,7 +149,7 @@ fn InetAddress? ipv4_from_str(String s)
|
||||
}
|
||||
current = current * 10 + c - '0';
|
||||
}
|
||||
if (element != 3 || current < 0 || current > 255) return INVALID_IP_STRING?;
|
||||
if (element != 3 || current < 0 || current > 255) return INVALID_IP_STRING~;
|
||||
addr.ipv4.d = (char)current;
|
||||
return addr;
|
||||
}
|
||||
@@ -258,6 +258,6 @@ fn AddrInfo*? addrinfo(String host, uint port, AIFamily ai_family, AISockType ai
|
||||
str.appendf("%d", port);
|
||||
AddrInfo hints = { .ai_family = ai_family, .ai_socktype = ai_socktype };
|
||||
AddrInfo* ai;
|
||||
if (os::getaddrinfo(zhost, str.zstr_view(), &hints, &ai)) return ADDRINFO_FAILED?;
|
||||
if (os::getaddrinfo(zhost, str.zstr_view(), &hints, &ai)) return ADDRINFO_FAILED~;
|
||||
return ai;
|
||||
}
|
||||
|
||||
@@ -37,13 +37,13 @@ fn uint? ipv4toint(String s)
|
||||
{
|
||||
if (c == '.')
|
||||
{
|
||||
if (current < 0) return INVALID_IP_STRING?;
|
||||
if (current < 0) return INVALID_IP_STRING~;
|
||||
out = out << 8 + current;
|
||||
current = -1;
|
||||
element++;
|
||||
continue;
|
||||
}
|
||||
if (element > 3 || c < '0' || c > '9') return INVALID_IP_STRING?;
|
||||
if (element > 3 || c < '0' || c > '9') return INVALID_IP_STRING~;
|
||||
if (current < 0)
|
||||
{
|
||||
current = c - '0';
|
||||
@@ -51,7 +51,7 @@ fn uint? ipv4toint(String s)
|
||||
}
|
||||
current = current * 10 + c - '0';
|
||||
}
|
||||
if (element != 3 || current < 0) return INVALID_IP_STRING?;
|
||||
if (element != 3 || current < 0) return INVALID_IP_STRING~;
|
||||
out = out << 8 + current;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ macro void? NativeSocket.close(self)
|
||||
{
|
||||
if (libc::close(self))
|
||||
{
|
||||
if (libc::errno() == errno::EBADF) return net::INVALID_SOCKET?;
|
||||
return net::GENERAL_ERROR?;
|
||||
if (libc::errno() == errno::EBADF) return net::INVALID_SOCKET~;
|
||||
return net::GENERAL_ERROR~;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ macro void? NativeSocket.set_non_blocking(self, bool non_blocking)
|
||||
}
|
||||
if (fcntl(self, F_SETFL, flags) == -1)
|
||||
{
|
||||
if (libc::errno() == errno::EBADF) return net::INVALID_SOCKET?;
|
||||
return net::GENERAL_ERROR?;
|
||||
if (libc::errno() == errno::EBADF) return net::INVALID_SOCKET~;
|
||||
return net::GENERAL_ERROR~;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ macro void? start_wsa()
|
||||
if (wsa_error > 0)
|
||||
{
|
||||
mem::@atomic_store(wsa_init, 0);
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,14 +62,14 @@ fn void? NativeSocket.set_non_blocking(self, bool non_blocking)
|
||||
{
|
||||
if (ioctlsocket(self, win32::FIONBIO, &&(CULong)non_blocking))
|
||||
{
|
||||
return socket_error()?;
|
||||
return socket_error()~;
|
||||
}
|
||||
}
|
||||
|
||||
macro void? NativeSocket.close(self)
|
||||
{
|
||||
WSAError error = closesocket(self);
|
||||
if (error) return convert_error(error)?;
|
||||
if (error) return convert_error(error)~;
|
||||
}
|
||||
|
||||
// https://github.com/wine-mirror/wine/blob/master/include/winsock.h
|
||||
|
||||
@@ -96,7 +96,7 @@ fn ulong? poll_ms(Poll[] polls, long timeout_ms)
|
||||
$else
|
||||
CInt result = os::poll((Posix_pollfd*)polls.ptr, (Posix_nfds_t)polls.len, (CInt)timeout_ms);
|
||||
$endif
|
||||
return result < 0 ? os::socket_error()? : (ulong)result;
|
||||
return result < 0 ? os::socket_error()~ : (ulong)result;
|
||||
}
|
||||
|
||||
macro Socket new_socket(fd, ai)
|
||||
@@ -133,7 +133,7 @@ fn void? Socket.set_option(&self, SocketOption option, bool value)
|
||||
{
|
||||
CInt flag = (CInt)value;
|
||||
int errcode = os::setsockopt(self.sock, os::SOL_SOCKET, option.value, &flag, CInt.sizeof);
|
||||
if (errcode != 0) return SOCKOPT_FAILED?;
|
||||
if (errcode != 0) return SOCKOPT_FAILED~;
|
||||
}
|
||||
|
||||
fn bool? Socket.get_option(&self, SocketOption option)
|
||||
@@ -141,7 +141,7 @@ fn bool? Socket.get_option(&self, SocketOption option)
|
||||
CInt flag;
|
||||
Socklen_t socklen = CInt.sizeof;
|
||||
int errcode = os::getsockopt(self.sock, os::SOL_SOCKET, option.value, &flag, &socklen);
|
||||
if (errcode != 0) return SOCKOPT_FAILED?;
|
||||
if (errcode != 0) return SOCKOPT_FAILED~;
|
||||
return (bool)flag;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ $if env::WIN32:
|
||||
$else
|
||||
isz n = libc::recv(self.sock, bytes.ptr, bytes.len, 0);
|
||||
$endif
|
||||
if (n < 0) return os::socket_error()?;
|
||||
if (n < 0) return os::socket_error()~;
|
||||
return (usz)n;
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ $if env::WIN32:
|
||||
$else
|
||||
isz n = libc::send(self.sock, bytes.ptr, bytes.len, 0);
|
||||
$endif
|
||||
if (n < 0) return os::socket_error()?;
|
||||
if (n < 0) return os::socket_error()~;
|
||||
return (usz)n;
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ fn usz? Socket.peek(&self, char[] bytes) @dynamic
|
||||
$else
|
||||
isz n = libc::recv(self.sock, bytes.ptr, bytes.len, os::MSG_PEEK);
|
||||
$endif
|
||||
if (n < 0) return os::socket_error()?;
|
||||
if (n < 0) return os::socket_error()~;
|
||||
return (usz)n;
|
||||
}
|
||||
|
||||
@@ -202,6 +202,6 @@ fn void? Socket.shutdown(&self, SocketShutdownHow how)
|
||||
{
|
||||
if (libc::shutdown(self.sock, how.native_value) < 0)
|
||||
{
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ fn Socket? connect_from_addrinfo(AddrInfo* addrinfo, SocketOption[] options) @pr
|
||||
// Keep the first successful connection.
|
||||
if (!errcode) return new_socket(sockfd, ai);
|
||||
};
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
|
||||
fn bool last_error_is_delayed_connect()
|
||||
@@ -67,7 +67,7 @@ fn Socket? connect_with_timeout_from_addrinfo(AddrInfo* addrinfo, SocketOption[]
|
||||
Duration to_remove = c.to_now().to_duration();
|
||||
if (to_remove >= timeout_left)
|
||||
{
|
||||
return CONNECTION_TIMED_OUT?;
|
||||
return CONNECTION_TIMED_OUT~;
|
||||
}
|
||||
timeout_left -= to_remove;
|
||||
}
|
||||
@@ -78,7 +78,7 @@ fn Socket? connect_with_timeout_from_addrinfo(AddrInfo* addrinfo, SocketOption[]
|
||||
Poll poll_request = { sockfd, SUBSCRIBE_ANY_WRITE, 0 };
|
||||
if (!poll((&poll_request)[:1], timeout_left)!)
|
||||
{
|
||||
return CONNECTION_TIMED_OUT?;
|
||||
return CONNECTION_TIMED_OUT~;
|
||||
}
|
||||
if (poll_request.revents & POLL_EVENT_WRITE)
|
||||
{
|
||||
@@ -87,7 +87,7 @@ fn Socket? connect_with_timeout_from_addrinfo(AddrInfo* addrinfo, SocketOption[]
|
||||
}
|
||||
}
|
||||
};
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
|
||||
fn Socket? connect_async_from_addrinfo(AddrInfo* addrinfo, SocketOption[] options) @private
|
||||
@@ -106,7 +106,7 @@ fn Socket? connect_async_from_addrinfo(AddrInfo* addrinfo, SocketOption[] option
|
||||
return new_socket(sockfd, ai);
|
||||
}
|
||||
};
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
|
||||
macro void @network_loop_over_ai(network, host, port; @body(fd, ai)) @private
|
||||
|
||||
@@ -53,7 +53,7 @@ fn TcpSocket? accept(TcpServerSocket* server_socket)
|
||||
os::start_wsa()!;
|
||||
$endif
|
||||
socket.sock = os::accept(server_socket.sock, (SockAddrPtr)&socket.ai_addr_storage, &socket.ai_addrlen);
|
||||
if (!socket.sock.is_valid()) return net::ACCEPT_FAILED?;
|
||||
if (!socket.sock.is_valid()) return net::ACCEPT_FAILED~;
|
||||
return socket;
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ fn TcpServerSocket? listen_to(AddrInfo* ai, uint backlog, SocketOption... option
|
||||
bool err = os::bind(sockfd, ai_candidate.ai_addr, ai_candidate.ai_addrlen) || os::listen(sockfd, backlog);
|
||||
if (!err) return (TcpServerSocket)net::new_socket(sockfd, ai_candidate);
|
||||
};
|
||||
return os::socket_error()?;
|
||||
return os::socket_error()~;
|
||||
}
|
||||
|
||||
struct TcpSocketPair
|
||||
@@ -88,7 +88,7 @@ fn TcpSocketPair*? TcpSocketPair.init(&self)
|
||||
listen_sock_info.ai_addrlen = listen_sock.ai_addr_storage.len;
|
||||
|
||||
int sock_result = os::getsockname(listen_sock.sock, (SockAddrPtr) &listen_sock_info.ai_addr_storage, &listen_sock_info.ai_addrlen);
|
||||
if (sock_result < 0) return os::socket_error()?;
|
||||
if (sock_result < 0) return os::socket_error()~;
|
||||
|
||||
char[] listen_port_bytes = listen_sock_info.ai_addr_storage[2:2];
|
||||
char msb = listen_port_bytes[0];
|
||||
@@ -102,7 +102,7 @@ fn TcpSocketPair*? TcpSocketPair.init(&self)
|
||||
$else
|
||||
NativeSocket[2] sockets;
|
||||
isz sockpair_result = os::socketpair(os::AF_UNIX, os::SOCK_STREAM, 0, &sockets);
|
||||
if (sockpair_result < 0) return os::socket_error()?;
|
||||
if (sockpair_result < 0) return os::socket_error()~;
|
||||
|
||||
Socket send_sock = { .sock = sockets[0] };
|
||||
TcpSocket tcp_send_sock = (TcpSocket) send_sock;
|
||||
|
||||
@@ -59,22 +59,22 @@ fn Url? tparse(String url_string) => parse(tmem, url_string);
|
||||
fn Url? parse(Allocator allocator, String url_string)
|
||||
{
|
||||
url_string = url_string.trim();
|
||||
if (!url_string) return EMPTY?;
|
||||
if (!url_string) return EMPTY~;
|
||||
Url url = { .allocator = allocator };
|
||||
|
||||
// Parse scheme
|
||||
if (try pos = url_string.index_of("://"))
|
||||
{
|
||||
if (!pos) return INVALID_SCHEME?;
|
||||
if (!pos) return INVALID_SCHEME~;
|
||||
url.scheme = url_string[:pos].copy(allocator);
|
||||
url_string = url_string[url.scheme.len + 3 ..];
|
||||
}
|
||||
else if (try pos = url_string.index_of(":"))
|
||||
{
|
||||
// Handle schemes without authority like 'mailto:'
|
||||
if (!pos) return INVALID_SCHEME?;
|
||||
if (!pos) return INVALID_SCHEME~;
|
||||
url.scheme = url_string[:pos].copy(allocator);
|
||||
url.path = decode(allocator, url_string[pos + 1 ..], PATH) ?? INVALID_PATH?!;
|
||||
url.path = decode(allocator, url_string[pos + 1 ..], PATH) ?? INVALID_PATH~!;
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -93,11 +93,11 @@ fn Url? parse(Allocator allocator, String url_string)
|
||||
{
|
||||
String[] userpass = userinfo.tsplit(":", 2);
|
||||
username = userpass[0];
|
||||
if (!username.len) return INVALID_USER?;
|
||||
if (!username.len) return INVALID_USER~;
|
||||
url.host =
|
||||
|
||||
url.username = decode(allocator, username, HOST) ?? INVALID_USER?!;
|
||||
if (userpass.len) url.password = decode(allocator, userpass[1], USERPASS) ?? INVALID_PASSWORD?!;
|
||||
url.username = decode(allocator, username, HOST) ?? INVALID_USER~!;
|
||||
if (userpass.len) url.password = decode(allocator, userpass[1], USERPASS) ?? INVALID_PASSWORD~!;
|
||||
};
|
||||
authority = authority[userinfo.len + 1 ..];
|
||||
}
|
||||
@@ -129,7 +129,7 @@ fn Url? parse(Allocator allocator, String url_string)
|
||||
}
|
||||
};
|
||||
}
|
||||
url.host = decode(allocator, host, HOST) ?? INVALID_HOST?!;
|
||||
url.host = decode(allocator, host, HOST) ?? INVALID_HOST~!;
|
||||
url_string = url_string[authority_end ..];
|
||||
}
|
||||
|
||||
@@ -140,12 +140,12 @@ fn Url? parse(Allocator allocator, String url_string)
|
||||
if (@ok(query_index) || @ok(fragment_index))
|
||||
{
|
||||
usz path_end = min(query_index ?? url_string.len, fragment_index ?? url_string.len);
|
||||
url.path = decode(allocator, url_string[:path_end], PATH) ?? INVALID_PATH?!;
|
||||
url.path = decode(allocator, url_string[:path_end], PATH) ?? INVALID_PATH~!;
|
||||
url_string = url_string[path_end ..];
|
||||
}
|
||||
else
|
||||
{
|
||||
url.path = decode(allocator, url_string, PATH) ?? INVALID_PATH?!;
|
||||
url.path = decode(allocator, url_string, PATH) ?? INVALID_PATH~!;
|
||||
url_string = "";
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ fn Url? parse(Allocator allocator, String url_string)
|
||||
// Parse fragment
|
||||
if (url_string.starts_with("#"))
|
||||
{
|
||||
url.fragment = decode(allocator, url_string[1..], FRAGMENT) ?? INVALID_FRAGMENT?!;
|
||||
url.fragment = decode(allocator, url_string[1..], FRAGMENT) ?? INVALID_FRAGMENT~!;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ fn usz? decode_len(String s, UrlEncodingMode mode) @inline
|
||||
if (c != '%') continue;
|
||||
if (i + 2 >= s.len || !s[i+1].is_xdigit() || !s[i+2].is_xdigit())
|
||||
{
|
||||
return INVALID_HEX?;
|
||||
return INVALID_HEX~;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user