mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add defaults to compare_exchange, small fix in printf. Disallow obviously wrong code that returns the pointer to a variable on the stack.
This commit is contained in:
@@ -6,4 +6,40 @@ fault NetError
|
||||
URL_TOO_LONG,
|
||||
INVALID_SOCKET,
|
||||
GENERAL_ERROR,
|
||||
INVALID_IP_STRING,
|
||||
}
|
||||
|
||||
fn uint! ipv4toint(String s)
|
||||
{
|
||||
uint out;
|
||||
int element;
|
||||
int current = -1;
|
||||
foreach (c : s)
|
||||
{
|
||||
if (c == '.')
|
||||
{
|
||||
if (current < 0) return NetError.INVALID_IP_STRING!;
|
||||
out = out << 8 + current;
|
||||
current = -1;
|
||||
element++;
|
||||
continue;
|
||||
}
|
||||
if (element > 3 || c < '0' || c > '9') return NetError.INVALID_IP_STRING!;
|
||||
if (current < 0)
|
||||
{
|
||||
current = c - '0';
|
||||
continue;
|
||||
}
|
||||
current = current * 10 + c - '0';
|
||||
}
|
||||
if (element != 3 || current < 0) return NetError.INVALID_IP_STRING!;
|
||||
out = out << 8 + current;
|
||||
return out;
|
||||
}
|
||||
|
||||
fn String! inttoipv4(uint val, Allocator* allocator = mem::current_allocator())
|
||||
{
|
||||
char[3 * 4 + 3 + 1] buffer;
|
||||
String res = (String)io::bprintf(&buffer, "%d.%d.%d.%d", val >> 24, (val >> 16) & 0xFF, (val >> 8) & 0xFF, val & 0xFF)?;
|
||||
return res.copyz(allocator);
|
||||
}
|
||||
Reference in New Issue
Block a user