diff --git a/HISTORY.md b/HISTORY.md index 74c142f3..938ba6b3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -14,10 +14,15 @@ Changes: `from_base` raises `ArgumentError` for a character that isn't a digit. In base 256 the digits are bytes: `256.to_base(256)` is `"\x01\x00"`, a binary string, and `from_base(256)` reads a string's bytes. - * `Array#from_base` turns digit values in any base, most significant - first, back into a number: `[1, 0].from_base(256)` is 256. It reverses - Ruby's `Integer#digits`, as `n.digits(b).reverse.from_base(b)`. All - three load with `require 'facets'`, and are based on the Radix gem. + * Given an array, `to_base` gives an array, with no limit on the base. + An array holding a number is the base, and the result is the digit + values: `1000.to_base([100])` is `[10, 0]`. Any other array is the + digits, which can be any objects: `5.to_base([:zero, :one])` is + `[:one, :zero, :one]`. + * `Array#from_base` turns such an array back into a number, given the + same base: `[10, 0].from_base([100])` is 1000, and + `[1, 0].from_base(256)` is 256. All three load with + `require 'facets'`, and are based on the Radix gem. * `String#margin` takes a `:flush` option, which gives every line the same margin and doesn't keep their relative indentation: `"abc\n xyz".margin(2, flush: true)` is `" abc\n xyz"`. This is diff --git a/lib/core/facets/array/from_base.rb b/lib/core/facets/array/from_base.rb index 873e8d5b..487ac2f4 100644 --- a/lib/core/facets/array/from_base.rb +++ b/lib/core/facets/array/from_base.rb @@ -14,25 +14,54 @@ class Array # 255.digits(16) #=> [15, 15] # 255.digits(16).reverse.from_base(16) #=> 255 # + # The base can also be given as Integer#to_base takes it for an + # array: as an array holding the base, or as an array of the digits, + # starting with the digit for zero. Whatever array `to_base` was + # given reads its result back. + # + # [10, 0].from_base([100]) #=> 1000 + # %w[L O L].from_base(%w[O L T W F S]) #=> 37 + # [:one, :zero, :one].from_base([:zero, :one]) #=> 5 + # # Raises ArgumentError for an empty array, and for an element that - # isn't an Integer from 0 to one less than the base. + # isn't a digit of the base: an Integer from 0 to one less than the + # base, or one of the digits given. # # String#from_base reads a number written as a string. # # Since: 4.2.0 def from_base(base) - unless base.is_a?(::Integer) - raise TypeError, "base must be an Integer, not #{base.class}" + values = nil + case base + when ::Integer + # digit values + when ::Array + if base.size == 1 && base.first.is_a?(::Integer) + base = base.first + else + if base.size < 2 + raise ArgumentError, "a base needs at least two digits" + end + if base.uniq.size < base.size + raise ArgumentError, "digits must not repeat" + end + values = {} + base.each_with_index { |d, i| values[d] = i } + base = base.size + end + else + raise TypeError, "base must be an Integer or Array, not #{base.class}" end raise ArgumentError, "invalid radix #{base}" if base < 2 raise ArgumentError, "no digits" if empty? inject(0) do |n, d| - unless d.is_a?(::Integer) && d >= 0 && d < base + v = values ? values[d] : d + unless v.is_a?(::Integer) && v >= 0 && v < base raise ArgumentError, "invalid digit #{d.inspect} for base #{base}" end - n * base + d + n * base + v end end diff --git a/lib/core/facets/integer/to_base.rb b/lib/core/facets/integer/to_base.rb index b11e7a98..41cb0829 100644 --- a/lib/core/facets/integer/to_base.rb +++ b/lib/core/facets/integer/to_base.rb @@ -12,12 +12,10 @@ class Integer # 809145531659995.to_base(62) #=> "3HLszsQsP" # -255.to_base(62) #=> "-47" # - # To use other digits, give them as a string or an array of - # single characters, starting with the digit for zero. The number of - # digits is the base. + # To use other digits, give them as a string, starting with the digit + # for zero. The number of digits is the base. # # 37.to_base("OLTWFS") #=> "LOL" - # 37.to_base(%w[O L T W F S]) #=> "LOL" # # In base 256 the digits are bytes, so the result is the number's # bytes, most significant first, as a binary string. @@ -39,9 +37,26 @@ class Integer # digits, in which case it raises ArgumentError. That includes base # 256, where "-" is byte 45. # - # String#from_base converts back. For digit values in any base, as - # an array, use Ruby's `digits(base).reverse`, and Array#from_base to - # convert back. + # Given an array, the result is an array, with one element for each + # digit, most significant first. An array holding one number is a + # base, of any size from 2 up, and the result is the digit values: + # + # 1000.to_base([100]) #=> [10, 0] + # 256.to_base([256]) #=> [1, 0] + # 255.to_base([16]) #=> [15, 15] + # + # Any other array is the digits themselves, starting with the digit + # for zero. They can be any objects: + # + # 37.to_base(%w[O L T W F S]) #=> ["L", "O", "L"] + # 37.to_base(%w[O L T W F S]).join #=> "LOL" + # 5.to_base([:zero, :one]) #=> [:one, :zero, :one] + # + # An array can't show a sign, so a negative number raises + # ArgumentError. + # + # String#from_base converts a string back, and Array#from_base an + # array. # # Since: 4.2.0 @@ -57,15 +72,13 @@ def to_base(base) return [hex].pack('H*') end unless base >= 2 && base <= 62 - raise ArgumentError, "invalid radix #{base}" + hint = base > 62 ? "; for digit values use to_base([#{base}])" : "" + raise ArgumentError, "invalid radix #{base}#{hint}" end return to_s(base) if base <= 36 digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[0, base] - when ::String, ::Array - digits = base.is_a?(::Array) ? base.map(&:to_s) : base.chars - unless digits.all? { |d| d.length == 1 } - raise ArgumentError, "digits must be single characters" - end + when ::String + digits = base.chars if digits.size < 2 raise ArgumentError, "a base needs at least two digits" end @@ -75,7 +88,22 @@ def to_base(base) if negative? && digits.include?('-') raise ArgumentError, "can't show a negative number when '-' is a digit" end - digits = digits.join + digits = base + when ::Array + if negative? + raise ArgumentError, "can't show a negative number as an array" + end + if base.size == 1 && base.first.is_a?(::Integer) + raise ArgumentError, "invalid radix #{base.first}" if base.first < 2 + return digits(base.first).reverse + end + if base.size < 2 + raise ArgumentError, "a base needs at least two digits" + end + if base.uniq.size < base.size + raise ArgumentError, "digits must not repeat" + end + return digits(base.size).reverse.map { |d| base[d] } else raise TypeError, "base must be an Integer, String or Array, not #{base.class}" end diff --git a/lib/core/facets/string/from_base.rb b/lib/core/facets/string/from_base.rb index 9929f0be..323c30a0 100644 --- a/lib/core/facets/string/from_base.rb +++ b/lib/core/facets/string/from_base.rb @@ -29,7 +29,8 @@ class String # base, and for an empty string. A leading "-" makes the number # negative, unless "-" is one of the digits, as it is in base 256. # - # Integer#to_base converts back. + # Integer#to_base converts back. Given an array of digits, it gives + # an array, which Array#from_base reads. # # Since: 4.2.0 diff --git a/test/core/array/test_from_base.rb b/test/core/array/test_from_base.rb index b57476fc..bef895d4 100644 --- a/test/core/array/test_from_base.rb +++ b/test/core/array/test_from_base.rb @@ -39,8 +39,40 @@ ArgumentError.assert.raised? { [0].from_base(1) } end - test "raises TypeError for a base that isn't an Integer" do + test "raises TypeError for a base that isn't an Integer or an Array" do TypeError.assert.raised? { [1].from_base(10.0) } + TypeError.assert.raised? { [1].from_base("ab") } + end + + test "takes the base in an array" do + [10, 0].from_base([100]).assert == 1000 + [1, 0].from_base([256]).assert == 256 + end + + test "takes an array of digits" do + %w[L O L].from_base(%w[O L T W F S]).assert == 37 + [:one, :zero, :one].from_base([:zero, :one]).assert == 5 + [7, 7, 5].from_base([5, 7]).assert == 6 + end + + test "reads back what Integer#to_base gave for the same array" do + [0, 1, 255, 1000, 2**100].each do |n| + [[2], [100], [256], [1000], %w[a b c], [:zero, :one], [5, 7]].each do |b| + n.to_base(b).from_base(b).assert == n + end + end + end + + test "raises ArgumentError for an element that isn't one of the digits" do + ArgumentError.assert.raised? { [:two].from_base([:zero, :one]) } + ArgumentError.assert.raised? { [0].from_base(%w[a b]) } + end + + test "raises ArgumentError for a bad array of digits" do + ArgumentError.assert.raised? { [1].from_base([1]) } + ArgumentError.assert.raised? { [1].from_base([]) } + ArgumentError.assert.raised? { %w[a].from_base(%w[a]) } + ArgumentError.assert.raised? { %w[a].from_base(%w[a b a]) } end end diff --git a/test/core/integer/test_to_base.rb b/test/core/integer/test_to_base.rb index 73211ee1..794676b3 100644 --- a/test/core/integer/test_to_base.rb +++ b/test/core/integer/test_to_base.rb @@ -30,10 +30,40 @@ -37.to_base("OLTWFS").assert == "-LOL" end - test "takes custom digits as a string or an array" do + test "takes custom digits as a string" do 37.to_base("OLTWFS").assert == "LOL" - 37.to_base(%w[O L T W F S]).assert == "LOL" - 37.to_base(%i[O L T W F S]).assert == "LOL" + end + + test "given an array holding a base, gives the digit values" do + 1000.to_base([100]).assert == [10, 0] + 256.to_base([256]).assert == [1, 0] + 255.to_base([16]).assert == [15, 15] + 0.to_base([100]).assert == [0] + (2**70).to_base([1000]).assert == (2**70).digits(1000).reverse + end + + test "given an array of digits, gives an array of them" do + 37.to_base(%w[O L T W F S]).assert == %w[L O L] + 37.to_base(%i[O L T W F S]).assert == %i[L O L] + 5.to_base([:zero, :one]).assert == [:one, :zero, :one] + 5.to_base(%w[zero one]).assert == %w[one zero one] + 0.to_base(%w[a b]).assert == %w[a] + end + + test "takes numbers as digits when there is more than one" do + 6.to_base([5, 7]).assert == [7, 7, 5] + end + + test "given an array, raises ArgumentError for a negative number" do + ArgumentError.assert.raised? { -5.to_base([100]) } + ArgumentError.assert.raised? { -5.to_base(%w[a b]) } + end + + test "given an array, raises ArgumentError for a bad base or digits" do + ArgumentError.assert.raised? { 5.to_base([1]) } + ArgumentError.assert.raised? { 5.to_base([]) } + ArgumentError.assert.raised? { 5.to_base(%w[a]) } + ArgumentError.assert.raised? { 5.to_base(%w[a b a]) } end test "base 256 gives the bytes as a binary string" do @@ -57,7 +87,6 @@ test "raises ArgumentError for bad digits" do ArgumentError.assert.raised? { 1.to_base("O") } ArgumentError.assert.raised? { 1.to_base("OLO") } - ArgumentError.assert.raised? { 1.to_base(%w[OL TW]) } end test "raises ArgumentError for a negative number when minus is a digit" do