Skip to content

Commit

Permalink
Set default to IEC
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Feb 7, 2019
1 parent 3d7d558 commit 2633941
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ describe "BigInt" do
end

describe "#humanize_bytes" do
it { BigInt.new("1180591620717411303424").humanize_bytes.should eq("1.0ZB") }
it { BigInt.new("1208925819614629174706176").humanize_bytes.should eq("1.0YB") }
it { BigInt.new("1180591620717411303424").humanize_bytes.should eq("1.0ZiB") }
it { BigInt.new("1208925819614629174706176").humanize_bytes.should eq("1.0YiB") }
end
end

Expand Down
35 changes: 19 additions & 16 deletions spec/std/int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -715,22 +715,25 @@ describe "Int" do
end

describe "#humanize_bytes" do
it { 0.humanize_bytes.should eq "0B" }
it { 1.humanize_bytes.should eq "1B" }
it { 1000.humanize_bytes.should eq "1000B" }
it { 1014.humanize_bytes.should eq "0.99KB" }
it { 1015.humanize_bytes.should eq "1.0KB" }
it { 1024.humanize_bytes.should eq "1.0KB" }
it { 1025.humanize_bytes.should eq "1.01KB" }
it { 2048.humanize_bytes.should eq "2.0KB" }

it { 1536.humanize_bytes.should eq("1.5KB") }
it { 524288.humanize_bytes.should eq("512KB") }
it { 1048576.humanize_bytes.should eq("1.0MB") }
it { 1073741824.humanize_bytes.should eq("1.0GB") }
it { 1099511627776.humanize_bytes.should eq("1.0TB") }
it { 1125899906842624.humanize_bytes.should eq("1.0PB") }
it { 1152921504606846976.humanize_bytes.should eq("1.0EB") }
# default IEC
it { 1024.humanize_bytes.should eq "1.0kiB" }

it { 0.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "0B" }
it { 1.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "1B" }
it { 1000.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "1000B" }
it { 1014.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "0.99KB" }
it { 1015.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "1.0KB" }
it { 1024.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "1.0KB" }
it { 1025.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "1.01KB" }
it { 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq "2.0KB" }

it { 1536.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.5KB") }
it { 524288.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("512KB") }
it { 1048576.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.0MB") }
it { 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.0GB") }
it { 1099511627776.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.0TB") }
it { 1125899906842624.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.0PB") }
it { 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC).should eq("1.0EB") }

it { 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC).should eq "1.0kiB" }
it { 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC).should eq "1.0GiB" }
Expand Down
18 changes: 10 additions & 8 deletions src/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,14 @@ struct Int
#
# Values with binary measurements such as computer storage (e.g. RAM size) are
# typically expressed using unit prefixes based on 1024 (instead of multiples
# of 1000 as per SI standard). This method by default uses an extended range
# of the `BinaryPrefixFormat::JEDEC` units (`K`, `M`, `G`, `T`, `P`, `E`, `Z`,
# `Y`) which equals to the prefixes of the SI system except for uppercase `K`
# and is based on powers of 1024.
# The IEC standard prefixes (`Ki`, `Mi`, `Gi`, `Ti`, `Pi`, `Ei`, `Zi`, `Yi`)
# based on powers of 1000 will be used if *format* is `BinaryPrefixFormat::IEC`.
# of 1000 as per SI standard). This method by default uses the IEC standard
# prefixes (`Ki`, `Mi`, `Gi`, `Ti`, `Pi`, `Ei`, `Zi`, `Yi`) based on powers of
# 1000 (see `BinaryPrefixFormat::IEC`).
#
# *format* can be set to use the extended range of JEDEC units (`K`, `M`, `G`,
# `T`, `P`, `E`, `Z`, `Y`) which equals to the prefixes of the SI system
# except for uppercase `K` and is based on powers of 1024 (see
# `BinaryPrefixFormat::JEDEC`).
#
# ```
# 1.humanize_bytes # => "1B"
Expand All @@ -565,7 +567,7 @@ struct Int
# ```
#
# See `Number#humanize` for more details on the behaviour and arguments.
def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, format : BinaryPrefixFormat = :JEDEC) : Nil
def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, format : BinaryPrefixFormat = :IEC) : Nil
humanize(io, precision, separator, nil, base: 1024, significant: significant) do |magnitude|
magnitude = Number.prefix_index(magnitude)

Expand All @@ -584,7 +586,7 @@ struct Int
end

# ditto
def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, format : BinaryPrefixFormat = :JEDEC) : String
def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, format : BinaryPrefixFormat = :IEC) : String
String.build do |io|
humanize_bytes(io, precision, separator, significant: significant, format: format)
end
Expand Down

0 comments on commit 2633941

Please sign in to comment.