DoxigAlpha

parse

Function parameters

Parameters

#
buf:[]const u8
port:u16

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#
tcpConnectToHost
All memory allocated with `allocator` will be freed before this function returns.
getAddressList
Call `AddressList.deinit` on the result.

Error sets in this namespace

Error Sets

#

= switch (native_os) { .windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false, .wasi => false, else => true, }

Values

#
has_unix_sockets
= switch (native_os) { .windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false, .wasi => false, else => true, }

Source

Implementation

#
pub fn parse(buf: []const u8, port: u16) IPv4ParseError!Ip4Address {
    var result: Ip4Address = .{
        .sa = .{
            .port = mem.nativeToBig(u16, port),
            .addr = undefined,
        },
    };
    const out_ptr = mem.asBytes(&result.sa.addr);

    var x: u8 = 0;
    var index: u8 = 0;
    var saw_any_digits = false;
    var has_zero_prefix = false;
    for (buf) |c| {
        if (c == '.') {
            if (!saw_any_digits) {
                return error.InvalidCharacter;
            }
            if (index == 3) {
                return error.InvalidEnd;
            }
            out_ptr[index] = x;
            index += 1;
            x = 0;
            saw_any_digits = false;
            has_zero_prefix = false;
        } else if (c >= '0' and c <= '9') {
            if (c == '0' and !saw_any_digits) {
                has_zero_prefix = true;
            } else if (has_zero_prefix) {
                return error.NonCanonical;
            }
            saw_any_digits = true;
            x = try std.math.mul(u8, x, 10);
            x = try std.math.add(u8, x, c - '0');
        } else {
            return error.InvalidCharacter;
        }
    }
    if (index == 3 and saw_any_digits) {
        out_ptr[index] = x;
        return result;
    }

    return error.Incomplete;
}