-module(bytesize). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([bytes/1, b/1, kb/1, mb/1, gb/1, tb/1, pb/1, kib/1, mib/1, gib/1, tib/1, pib/1, to_string/1, to_string_decimal/1]). -export_type([byte_size/0, units/0]). -type byte_size() :: {byte_size, integer()}. -type units() :: units_decimal | units_binary. -spec bytes(byte_size()) -> integer(). bytes(Size) -> {byte_size, B} = Size, B. -spec to_string_1(float()) -> binary(). to_string_1(N) -> X = gleam@float:round(N * 10.0), _assert_subject = begin _pipe = gleam@int:to_string(X), _pipe@1 = gleam@string:reverse(_pipe), gleam@string:pop_grapheme(_pipe@1) end, {ok, {C, Cs}} = case _assert_subject of {ok, {_, _}} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"bytesize"/utf8>>, function => <<"to_string_1"/utf8>>, line => 153}) end, <<<<(gleam@string:reverse(Cs))/binary, "."/utf8>>/binary, C/binary>>. -spec b(integer()) -> byte_size(). b(Size) -> {byte_size, Size * 1}. -spec kb(integer()) -> byte_size(). kb(Size) -> {byte_size, Size * 1000}. -spec mb(integer()) -> byte_size(). mb(Size) -> {byte_size, Size * 1000000}. -spec gb(integer()) -> byte_size(). gb(Size) -> {byte_size, Size * 1000000000}. -spec tb(integer()) -> byte_size(). tb(Size) -> {byte_size, Size * 1000000000000}. -spec pb(integer()) -> byte_size(). pb(Size) -> {byte_size, Size * 1000000000000000}. -spec kib(integer()) -> byte_size(). kib(Size) -> {byte_size, Size * 1024}. -spec mib(integer()) -> byte_size(). mib(Size) -> {byte_size, Size * 1048576}. -spec gib(integer()) -> byte_size(). gib(Size) -> {byte_size, Size * 1073741824}. -spec tib(integer()) -> byte_size(). tib(Size) -> {byte_size, Size * 1099511627776}. -spec pib(integer()) -> byte_size(). pib(Size) -> {byte_size, Size * 1125899906842624}. -spec to_string_units(integer(), units()) -> binary(). to_string_units(Bytes, Units) -> {Unit, Unit_base, Unit_suffix} = case Units of units_binary -> {1024, 6.907755279, <<"iB"/utf8>>}; units_decimal -> {1000, 6.931471806, <<"B"/utf8>>} end, case Bytes < Unit of true -> <<(gleam@int:to_string(Bytes))/binary, " B"/utf8>>; false -> Size = gleam@int:to_float(Bytes), Exp = case gleam@float:truncate(case Unit_base of +0.0 -> +0.0; -0.0 -> -0.0; Gleam@denominator -> math:log(Size) / Gleam@denominator end) of E when E =:= 0 -> 1; E@1 -> E@1 end, _assert_subject = gleam@int:power(Unit, gleam@int:to_float(Exp)), {ok, X} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"bytesize"/utf8>>, function => <<"to_string_units"/utf8>>, line => 130}) end, Unit_prefix = case Exp of 1 when Units =:= units_decimal -> <<"k"/utf8>>; 1 -> <<"K"/utf8>>; 2 -> <<"M"/utf8>>; 3 -> <<"G"/utf8>>; 4 -> <<"T"/utf8>>; 5 -> <<"P"/utf8>>; 6 -> <<"E"/utf8>>; _ -> erlang:error(#{gleam_error => panic, message => <<"exponent too big"/utf8>>, module => <<"bytesize"/utf8>>, function => <<"to_string_units"/utf8>>, line => 139}) end, <<<<<<(to_string_1(case X of +0.0 -> +0.0; -0.0 -> -0.0; Gleam@denominator@1 -> Size / Gleam@denominator@1 end))/binary, " "/utf8>>/binary, Unit_prefix/binary>>/binary, Unit_suffix/binary>> end. -spec to_string(byte_size()) -> binary(). to_string(Size) -> {byte_size, Bytes} = Size, to_string_units(Bytes, units_binary). -spec to_string_decimal(byte_size()) -> binary(). to_string_decimal(Size) -> {byte_size, Bytes} = Size, to_string_units(Bytes, units_decimal).