From 26de7a3513901dcdd5a5d588a2f38f25fda0e626 Mon Sep 17 00:00:00 2001 From: GasInfinity Date: Sat, 17 Jan 2026 19:32:43 +0100 Subject: [PATCH] feat(std.mem): add `boundedOrderZ` --- lib/std/mem.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 068167183d..437faa2eb6 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -662,10 +662,15 @@ pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { /// Compares two many-item pointers with NUL-termination lexicographically. pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order { + return boundedOrderZ(T, lhs, rhs, std.math.maxInt(usize)); +} + +/// Compares two many-item pointers with NUL-termination lexicographically until some specified bound. +pub fn boundedOrderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T, bound: usize) math.Order { if (lhs == rhs) return .eq; var i: usize = 0; - while (lhs[i] == rhs[i] and lhs[i] != 0) : (i += 1) {} - return math.order(lhs[i], rhs[i]); + while (lhs[i] == rhs[i] and lhs[i] != 0 and i < bound) : (i += 1) {} + return if (i < bound) math.order(lhs[i], rhs[i]) else .eq; } test order {