parseIntSizeSuffix
Parses a number like '2G', '2Gi', or '2GiB'.
Function parameters
Parameters
- buf:[]const u8
- digit_base:u8
Type definitions in this namespace
Types
- FormatOptions
- Deprecated in favor of `Options`.
- Parser
- A stream based parser for format strings.
Deprecated in favor of `Writer.print`.
Functions
- format
- Deprecated in favor of `Writer.print`.
- printInt
- Asserts the rendered integer value fits in `buffer`.
- digits2
- Converts values in the range [0, 100) to a base 10 string.
- Formatter
- Deprecated in favor of `Alt`.
- Alt
- Creates a type suitable for instantiating and passing to a "{f}" placeholder.
- alt
- Helper for calling alternate format methods besides one named "format".
- parseInt
- Parses the string `buf` as signed or unsigned representation in the
- parseIntWithGenericCharacter
- Like `parseInt`, but with a generic `Character` type.
- parseUnsigned
- Parses the string `buf` as unsigned representation in the specified base
- parseIntSizeSuffix
- Parses a number like '2G', '2Gi', or '2GiB'.
- bufPrint
- Print a Formatter string into `buf`.
- count
- Count the characters needed for format.
- bytesToHex
- Encodes a sequence of bytes as hexadecimal digits.
- hexToBytes
- Decodes the sequence of bytes represented by the specified string of
- hex
- Converts an unsigned integer of any multiple of u8 to an array of lowercase
Error sets in this namespace
Error Sets
= 3
Values
- hex_charset
- = "0123456789abcdef"
Source
Implementation
pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
var without_B = buf;
if (mem.endsWith(u8, buf, "B")) without_B.len -= 1;
var without_i = without_B;
var magnitude_base: usize = 1000;
if (mem.endsWith(u8, without_B, "i")) {
without_i.len -= 1;
magnitude_base = 1024;
}
if (without_i.len == 0) return error.InvalidCharacter;
const orders_of_magnitude: usize = switch (without_i[without_i.len - 1]) {
'k', 'K' => 1,
'M' => 2,
'G' => 3,
'T' => 4,
'P' => 5,
'E' => 6,
'Z' => 7,
'Y' => 8,
'R' => 9,
'Q' => 10,
else => 0,
};
var without_suffix = without_i;
if (orders_of_magnitude > 0) {
without_suffix.len -= 1;
} else if (without_i.len != without_B.len) {
return error.InvalidCharacter;
}
const multiplier = math.powi(usize, magnitude_base, orders_of_magnitude) catch |err| switch (err) {
error.Underflow => unreachable,
error.Overflow => return error.Overflow,
};
const number = try std.fmt.parseInt(usize, without_suffix, digit_base);
return math.mul(usize, number, multiplier);
}