printByteSize
Format option precision is ignored when value is less than 1kB
Function parameters
Parameters
- w:*Writer
- value:u64
- options:std.fmt.Options
Type definitions in this namespace
Types
- Allocating
- Maintains `Writer` state such that it writes to the unused capacity of an
Writes to `buffer` and returns `error.WriteFailed` when it is full.
Functions
- fixed
- Writes to `buffer` and returns `error.WriteFailed` when it is full.
- buffered
- Returns the contents not yet drained.
- writeVec
- If the total number of bytes of `data` fits inside `unusedCapacitySlice`,
- writeSplat
- If the number of bytes to write based on `data` and `splat` fits inside
- writeSplatHeader
- Returns how many bytes were consumed from `header` and `data`.
- writeSplatHeaderLimit
- Equivalent to `writeSplatHeader` but writes at most `limit` bytes.
- flush
- Drains all remaining buffered data.
- defaultFlush
- Repeatedly calls `VTable.drain` until `end` is zero.
- noopFlush
- Does nothing.
- writableArray
- Asserts the provided buffer has total capacity enough for `len`.
- writableSlice
- Asserts the provided buffer has total capacity enough for `len`.
- writableSliceGreedy
- Asserts the provided buffer has total capacity enough for `minimum_len`.
- writableSliceGreedyPreserve
- Asserts the provided buffer has total capacity enough for `minimum_len`
- writableSlicePreserve
- Asserts the provided buffer has total capacity enough for `len`.
- advance
- After calling `writableSliceGreedy`, this function tracks how many bytes
- writeVecAll
- The `data` parameter is mutable because this function needs to mutate the
- writeSplatAll
- The `data` parameter is mutable because this function needs to mutate the
- writeAll
- Calls `drain` as many times as necessary such that all of `bytes` are
- Renders fmt string with args, calling `writer` with slices of bytes.
- writeByte
- Calls `drain` as many times as necessary such that `byte` is transferred.
- writeBytePreserve
- When draining the buffer, ensures that at least `preserve` bytes
- splatByteAll
- Writes the same byte many times, performing the underlying write call as
- splatByte
- Writes the same byte many times, allowing short writes.
- splatBytesAll
- Writes the same slice many times, performing the underlying write call as
- splatBytes
- Writes the same slice many times, allowing short writes.
- writeInt
- Asserts the `buffer` was initialized with a capacity of at least `@sizeOf(T)` bytes.
- writeStruct
- The function is inline to avoid the dead code in case `endian` is
- sendFile
- Unlike `writeSplat` and `writeVec`, this function will call into `VTable`
- sendFileHeader
- Returns how many bytes from `header` and `file_reader` were consumed.
- sendFileReading
- Asserts nonzero buffer capacity and nonzero `limit`.
- sendFileAll
- Number of bytes logically written is returned.
- sendFileReadingAll
- Equivalent to `sendFileAll` but uses direct `pread` and `read` calls on
- printValue
- Asserts `buffer` capacity of at least 2 if `value` is a union.
- printIntAny
- In general, prefer `printInt` to avoid generic explosion.
- printFloat
- Uses a larger stack buffer; asserts mode is decimal or scientific.
- printFloatHexOptions
- Uses a smaller stack buffer; asserts mode is not decimal or scientific.
- printByteSize
- Format option `precision` is ignored when `value` is less than 1kB
- printDuration
- Writes number of nanoseconds according to its signed magnitude:
- writeUleb128
- Write a single unsigned integer as LEB128 to the given writer.
- writeSleb128
- Write a single signed integer as LEB128 to the given writer.
- writeLeb128
- Write a single integer as LEB128 to the given writer.
- consume
- Removes the first `n` bytes from `buffer` by shifting buffer contents,
- consumeAll
- Shortcut for setting `end` to zero and returning zero.
- unimplementedSendFile
- For use when the `Writer` implementation can cannot offer a more efficient
- fixedDrain
- When this function is called it usually means the buffer got full, so it's
- Hashed
- Provides a `Writer` implementation based on calling `Hasher.update`, sending
- Hashing
- Provides a `Writer` implementation based on calling `Hasher.update`,
Error sets in this namespace
Error Sets
= .{ .vtable = &.{ .drain = failingDrain, .sendFile = failingSendFile, .rebase = failingRebase, }, .buffer = &.{}, }
Values
- failing
- = .{ .vtable = &.{ .drain = failingDrain, .sendFile = failingSendFile, .rebase = failingRebase, }, .buffer = &.{}, }
Source
Implementation
pub fn printByteSize(
w: *Writer,
value: u64,
comptime units: ByteSizeUnits,
options: std.fmt.Options,
) Error!void {
if (value == 0) return w.alignBufferOptions("0B", options);
// The worst case in terms of space needed is 32 bytes + 3 for the suffix.
var buf: [std.fmt.float.min_buffer_size + 3]u8 = undefined;
const mags_si = " kMGTPEZY";
const mags_iec = " KMGTPEZY";
const log2 = std.math.log2(value);
const base = switch (units) {
.decimal => 1000,
.binary => 1024,
};
const magnitude = switch (units) {
.decimal => @min(log2 / comptime std.math.log2(1000), mags_si.len - 1),
.binary => @min(log2 / 10, mags_iec.len - 1),
};
const new_value = std.math.lossyCast(f64, value) / std.math.pow(f64, std.math.lossyCast(f64, base), std.math.lossyCast(f64, magnitude));
const suffix = switch (units) {
.decimal => mags_si[magnitude],
.binary => mags_iec[magnitude],
};
const s = switch (magnitude) {
0 => buf[0..std.fmt.printInt(&buf, value, 10, .lower, .{})],
else => std.fmt.float.render(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => unreachable,
},
};
var i: usize = s.len;
if (suffix == ' ') {
buf[i] = 'B';
i += 1;
} else switch (units) {
.decimal => {
buf[i..][0..2].* = [_]u8{ suffix, 'B' };
i += 2;
},
.binary => {
buf[i..][0..3].* = [_]u8{ suffix, 'i', 'B' };
i += 3;
},
}
return w.alignBufferOptions(buf[0..i], options);
}