Function parse [src]

Prototype

pub fn parse(buf: []const u8, port: u16) IPv4ParseError!Ip4Address

Parameters

buf: []const u8port: u16

Possible Errors

Incomplete IPParseError
InvalidCharacter IPParseError
InvalidEnd IPParseError
NonCanonical
Overflow IPParseError

Source

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; }