pub const Limit = enum(usize) pub fn limited(n: usize) Limitmath.maxInt(usize) is interpreted to mean .unlimited.
pub fn limited64(n: u64) LimitAny value grater than math.maxInt(usize) is interpreted to mean .unlimited.
pub fn countVec(data: []const []const u8) Limitpub fn min(a: Limit, b: Limit) Limitpub fn max(a: Limit, b: Limit) Limitpub fn minInt(l: Limit, n: usize) usizepub fn minInt64(l: Limit, n: u64) usizepub fn slice(l: Limit, s: []u8) []u8pub fn sliceConst(l: Limit, s: []const u8) []const u8pub fn toInt(l: Limit) ?usizepub fn toInt64(l: Limit) ?u64pub fn slice1(l: Limit, non_empty_buffer: []u8) []u8Reduces a slice to account for the limit, leaving room for one extra byte above the limit, allowing for the use case of differentiating between end-of-stream and reaching the limit.
pub fn nonzero(l: Limit) boolpub fn subtract(l: Limit, amount: usize) ?LimitReturn a new limit reduced by amount or return null indicating limit would be exceeded.
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811pub const Limit = enum(usize) {
nothing = 0,
unlimited = math.maxInt(usize),
_,
/// `math.maxInt(usize)` is interpreted to mean `.unlimited`.
pub fn limited(n: usize) Limit {
return @fromBackingInt(@intCast(n));
}
/// Any value grater than `math.maxInt(usize)` is interpreted to mean
/// `.unlimited`.
pub fn limited64(n: u64) Limit {
return @fromBackingInt(@intCast(@min(n, math.maxInt(usize))));
}
pub fn countVec(data: []const []const u8) Limit {
var total: usize = 0;
for (data) |d| total += d.len;
return .limited(total);
}
pub fn min(a: Limit, b: Limit) Limit {
return @fromBackingInt(@intCast(@min(@backingInt(a), @backingInt(b))));
}
pub fn max(a: Limit, b: Limit) Limit {
if (a == .unlimited or b == .unlimited) {
return .unlimited;
}
return @fromBackingInt(@intCast(@max(@backingInt(a), @backingInt(b))));
}
pub fn minInt(l: Limit, n: usize) usize {
return @min(n, @backingInt(l));
}
pub fn minInt64(l: Limit, n: u64) usize {
return @min(n, @backingInt(l));
}
pub fn slice(l: Limit, s: []u8) []u8 {
return s[0..l.minInt(s.len)];
}
pub fn sliceConst(l: Limit, s: []const u8) []const u8 {
return s[0..l.minInt(s.len)];
}
pub fn toInt(l: Limit) ?usize {
return switch (l) {
else => @backingInt(l),
.unlimited => null,
};
}
pub fn toInt64(l: Limit) ?u64 {
return switch (l) {
else => @backingInt(l),
.unlimited => null,
};
}
/// Reduces a slice to account for the limit, leaving room for one extra
/// byte above the limit, allowing for the use case of differentiating
/// between end-of-stream and reaching the limit.
pub fn slice1(l: Limit, non_empty_buffer: []u8) []u8 {
assert(non_empty_buffer.len >= 1);
return non_empty_buffer[0..@min(@backingInt(l) +| 1, non_empty_buffer.len)];
}
pub fn nonzero(l: Limit) bool {
return l != .nothing;
}
/// Return a new limit reduced by `amount` or return `null` indicating
/// limit would be exceeded.
pub fn subtract(l: Limit, amount: usize) ?Limit {
if (l == .unlimited) return .unlimited;
if (amount > @backingInt(l)) return null;
return @fromBackingInt(@intCast(@backingInt(l) - amount));
}
}