// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Formatting of the small decimal numbers that follow an address: the port //! of an endpoint and the prefix length of a network. Internal to the library. const std = @import("std"); /// The longest text `render` can produce, `65535`. pub const max_text_len = "65535".len; /// Renders `value` in decimal into the start of `out`, returning the number of /// bytes written. /// /// All five digit characters are built and stored as one word, with the /// leading zeros shifted out beforehand; only the digits are meaningful, but /// all eight bytes of `out` are written, so the caller leaves that much room. /// This is branchless where writing digit by digit would mispredict on the /// varying widths. pub fn render(value: u16, out: *[8]u8) usize { const chars: [8]u8 = .{ '0' + @as(u8, @intCast(value / 10000)), '0' + @as(u8, @intCast(value / 1000 % 10)), '0' + @as(u8, @intCast(value / 100 % 10)), '0' + @as(u8, @intCast(value / 10 % 10)), '0' + @as(u8, @intCast(value % 10)), 0, 0, 0, }; const width: usize = 1 + @as(usize, @intFromBool(value >= 10)) + @as(usize, @intFromBool(value >= 100)) + @as(usize, @intFromBool(value >= 1000)) + @as(usize, @intFromBool(value >= 10000)); // The first character sits in the low byte, so the shift drops leading // zeros and the little-endian store puts the rest back in text order. const word = std.mem.readInt(u64, &chars, .little); std.mem.writeInt(u64, out, word >> @intCast(8 * (max_text_len - width)), .little); return width; } /// Like `render` for values that fit a byte, such as a prefix length: three /// digit characters built and stored as one word, so only three divisions /// instead of five. All four bytes of `out` are written. pub fn renderSmall(value: u8, out: *[4]u8) usize { const chars: [4]u8 = .{ '0' + value / 100, '0' + value / 10 % 10, '0' + value % 10, 0, }; const width: usize = 1 + @as(usize, @intFromBool(value >= 10)) + @as(usize, @intFromBool(value >= 100)); const word = std.mem.readInt(u32, &chars, .little); std.mem.writeInt(u32, out, word >> @intCast(8 * (3 - width)), .little); return width; } const testing = std.testing; test render { const cases = [_]struct { value: u16, text: []const u8 }{ .{ .value = 0, .text = "0" }, .{ .value = 7, .text = "7" }, .{ .value = 10, .text = "10" }, .{ .value = 99, .text = "99" }, .{ .value = 443, .text = "443" }, .{ .value = 8080, .text = "8080" }, .{ .value = 65535, .text = "65535" }, }; for (cases) |case| { var buffer: [8]u8 = undefined; try testing.expectEqualStrings(case.text, buffer[0..render(case.value, &buffer)]); } } test renderSmall { const cases = [_]struct { value: u8, text: []const u8 }{ .{ .value = 0, .text = "0" }, .{ .value = 8, .text = "8" }, .{ .value = 32, .text = "32" }, .{ .value = 128, .text = "128" }, .{ .value = 255, .text = "255" }, }; for (cases) |case| { var buffer: [4]u8 = undefined; try testing.expectEqualStrings(case.text, buffer[0..renderSmall(case.value, &buffer)]); } }