From d19f69fac8d3baf28145d6702bac34fb6252cdf2 Mon Sep 17 00:00:00 2001 From: Sean Eshbaugh Date: Fri, 17 Aug 2018 11:36:44 -0500 Subject: [PATCH 1/3] Add support for parsing negative filesizes. --- lib/filesize.rb | 11 ++++++----- spec/lib/filesize_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/filesize.rb b/lib/filesize.rb index 745ab06..93ba2b5 100644 --- a/lib/filesize.rb +++ b/lib/filesize.rb @@ -13,14 +13,14 @@ class Filesize # Set of rules describing file sizes according to SI units. SI = { - :regexp => /^([\d,.]+)?[[:space:]]?([kmgtpezy]?)b?$/i, + :regexp => /^(-?)([\d,.]+)?[[:space:]]?([kmgtpezy]?)b?$/i, :multiplier => 1000, :prefixes => TYPE_PREFIXES[:SI], :presuffix => '' # deprecated } # Set of rules describing file sizes according to binary units. BINARY = { - :regexp => /^([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i, + :regexp => /^(-?)([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i, :multiplier => 1024, :prefixes => TYPE_PREFIXES[:BINARY], :presuffix => 'i' # deprecated @@ -151,10 +151,11 @@ def parse(string) end } - prefix = $2 || '' - size = ($1 || 0).to_f + sign = $1 == '' ? 1 : -1 + prefix = $3 || '' + size = ($2 || 0).to_f * sign - return { :prefix => prefix, :size => size, :type => type} + return { :prefix => prefix, :size => size, :type => type } end end diff --git a/spec/lib/filesize_spec.rb b/spec/lib/filesize_spec.rb index 711bd8f..ada02b7 100644 --- a/spec/lib/filesize_spec.rb +++ b/spec/lib/filesize_spec.rb @@ -42,6 +42,18 @@ it 'parses megabytes without b suffix' do expect(Filesize.from('1 M').to).to eq 1000 * 1000 end + + it 'parses negative kilobytes' do + expect(Filesize.from('-1 kB').to).to eq -1000 + end + + it 'parses negative megabytes' do + expect(Filesize.from('-1 MB').to).to eq -1000 * 1000 + end + + it 'parses negative megabytes without b suffix' do + expect(Filesize.from('-1 M').to).to eq -1000 * 1000 + end end context 'BINARY units' do @@ -56,6 +68,18 @@ it 'parses mebibytes without b suffix' do expect(Filesize.from('1 Mi').to).to eq 1024 * 1024 end + + it 'parses negative kilobytes' do + expect(Filesize.from('-1 KiB').to).to eq -1024 + end + + it 'parses negative megabytes' do + expect(Filesize.from('-1 MiB').to).to eq -1024 * 1024 + end + + it 'parses negative mebibytes without b suffix' do + expect(Filesize.from('-1 Mi').to).to eq -1024 * 1024 + end end end From 9c001c2dbf000bf5cb509294f90e9596f729f4e0 Mon Sep 17 00:00:00 2001 From: Sean Eshbaugh Date: Fri, 17 Aug 2018 12:40:47 -0500 Subject: [PATCH 2/3] Added support for pretty printing negative filesizes. --- lib/filesize.rb | 6 +++--- spec/lib/filesize_spec.rb | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/filesize.rb b/lib/filesize.rb index 93ba2b5..c72aa1f 100644 --- a/lib/filesize.rb +++ b/lib/filesize.rb @@ -71,14 +71,14 @@ def to_s(unit = 'B') # @return [String] # @see #to_s def pretty - size = @bytes + size = @bytes.abs if size < @type[:multiplier] - unit = "B" + unit = 'B' else pos = (Math.log(size) / Math.log(@type[:multiplier])).floor pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1 - unit = @type[:prefixes][pos-1] + "B" + unit = @type[:prefixes][pos-1] + 'B' end to_s(unit) diff --git a/spec/lib/filesize_spec.rb b/spec/lib/filesize_spec.rb index ada02b7..54178a6 100644 --- a/spec/lib/filesize_spec.rb +++ b/spec/lib/filesize_spec.rb @@ -129,12 +129,24 @@ end describe '#pretty' do - it 'returns the number of the most matching prefix with its unit (BINARY default)' do - expect(Filesize.new(1024).pretty).to eq '1.00 KiB' + context 'BINARY units (default)' do + it 'returns the number of the most matching prefix with its unit' do + expect(Filesize.new(1024).pretty).to eq '1.00 KiB' + end + + it 'returns the negative number of the most matching prefix with its unit' do + expect(Filesize.new(-1024).pretty).to eq '-1.00 KiB' + end end - it 'returns the number of the most matching prefix with its unit (SI)' do - expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB' + context 'SI units' do + it 'returns the number of the most matching prefix with its unit' do + expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB' + end + + it 'returns the negative number of the most matching prefix with its unit' do + expect(Filesize.new(-1000, Filesize::SI).pretty).to eq '-1.00 kB' + end end end From 73a0d78a540f8022b20b72d825a01a1c9a529ff2 Mon Sep 17 00:00:00 2001 From: Sean Eshbaugh Date: Fri, 17 Aug 2018 12:58:43 -0500 Subject: [PATCH 3/3] Updated readme. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1efaa90..0450138 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ That means: ### Parsing a string ```ruby Filesize.from("1 GiB") -# => #/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}> +# => #/^(-?)([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i, :multiplier=>1024, :prefixes=>["Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"], :presuffix=>"i"}> ``` ### Converting filesizes