radix 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.index +7 -3
- data/.yardopts +6 -3
- data/HISTORY.md +21 -5
- data/README.md +16 -7
- data/demo/issues/004_zero_empty_string.md +18 -0
- data/lib/radix.rb +13 -40
- data/lib/radix.yml +7 -3
- data/lib/radix/base.rb +137 -20
- data/lib/radix/float.rb +206 -14
- data/lib/radix/integer.rb +221 -21
- data/lib/radix/numeric.rb +99 -26
- data/lib/radix/operator.rb +82 -0
- data/lib/radix/rational.rb +100 -27
- metadata +20 -3
data/.index
CHANGED
@@ -11,11 +11,15 @@ requirements:
|
|
11
11
|
- groups:
|
12
12
|
- build
|
13
13
|
development: true
|
14
|
-
name:
|
14
|
+
name: ergo
|
15
15
|
- groups:
|
16
16
|
- test
|
17
17
|
development: true
|
18
18
|
name: qed
|
19
|
+
- groups:
|
20
|
+
- test
|
21
|
+
development: true
|
22
|
+
name: ae
|
19
23
|
conflicts: []
|
20
24
|
alternatives: []
|
21
25
|
resources:
|
@@ -47,9 +51,9 @@ paths:
|
|
47
51
|
created: '2009-07-01'
|
48
52
|
summary: Convert to and from any base.
|
49
53
|
title: Radix
|
50
|
-
version: 2.
|
54
|
+
version: 2.2.0
|
51
55
|
name: radix
|
52
56
|
description: ! "Radix is a very easy to use Ruby library for converting numbers to
|
53
57
|
and from\nany base. It supports both Integer, Float and Rational numbers, as well
|
54
58
|
as \nrepresentational string-notations that need not be in ASCII order."
|
55
|
-
date: '2013-
|
59
|
+
date: '2013-03-20'
|
data/.yardopts
CHANGED
data/HISTORY.md
CHANGED
@@ -1,12 +1,28 @@
|
|
1
1
|
# RELEASE HISTORY
|
2
2
|
|
3
|
+
## 2.2.0 / 2013-03-20
|
4
|
+
|
5
|
+
Good documentation is so under-addressed by most developers that we really
|
6
|
+
have to roll out the bright lights when someone comes along and takes
|
7
|
+
up the time consuming mantle of documenting an API in complete detail.
|
8
|
+
This is exactly what happened with Radix this last month. A coder going
|
9
|
+
by the unassuming handle, **douglascodes**, announced his desire to
|
10
|
+
document Radix and proceeded to do so until YARD pronounced 100%
|
11
|
+
completeness. Hey, we think that is worth of a whole version bump and
|
12
|
+
a big shout-out for Douglas!
|
13
|
+
|
14
|
+
Changes:
|
15
|
+
|
16
|
+
* 100% API documentation coverage. (douglascodes)
|
17
|
+
|
18
|
+
|
3
19
|
## 2.1.1 / 2013-02-06
|
4
20
|
|
5
|
-
Minor release to fix gemspec. Which, alas,
|
21
|
+
Minor release to fix gemspec. Which, alas, Bundler cannot do without.
|
6
22
|
|
7
23
|
Changes:
|
8
24
|
|
9
|
-
* Update .gemspec file for
|
25
|
+
* Update .gemspec file for latest indexer.
|
10
26
|
|
11
27
|
|
12
28
|
## 2.1.0 / 2013-01-31
|
@@ -28,7 +44,7 @@ Changes:
|
|
28
44
|
|
29
45
|
## 2.0.1 / 2011-10-23
|
30
46
|
|
31
|
-
This release is simply an
|
47
|
+
This release is simply an administrative release to update the project
|
32
48
|
build configuration. The functionality of the library itself has not
|
33
49
|
changed. This release also transitions the project to the BSD-2-Clause
|
34
50
|
license.
|
@@ -59,8 +75,8 @@ Radix now provides an actual Numeric subclass, Radix::Number, that stores
|
|
59
75
|
the base and can be used like any other Numeric object. This makes it very
|
60
76
|
easy to convert and manipulate numbers in any base. The implementation is still
|
61
77
|
a bit nascent. For the moment, it only supports the most basic math operators
|
62
|
-
and only handles integer values, but
|
63
|
-
on it's
|
78
|
+
and only handles integer values, but future releases will continue to expand
|
79
|
+
on it's capabilities.
|
64
80
|
|
65
81
|
Changes:
|
66
82
|
|
data/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# Radix
|
2
2
|
|
3
|
-
[Website](http://rubyworks.github.com/radix)
|
4
|
-
[Report Issue](http://github.com/rubyworks/radix/issues)
|
5
|
-
[Source Code](http://github.com/rubyworks/radix)
|
6
|
-
[](http://travis-ci.org/rubyworks/radix)
|
7
|
-
[](http://badge.fury.io/rb/radix)
|
3
|
+
[Website](http://rubyworks.github.com/radix) ·
|
4
|
+
[Report Issue](http://github.com/rubyworks/radix/issues) ·
|
5
|
+
[Source Code](http://github.com/rubyworks/radix)
|
6
|
+
[](http://travis-ci.org/rubyworks/radix)
|
7
|
+
[](http://badge.fury.io/rb/radix)
|
8
|
+
[](http://flattr.com/thing/324911/Rubyworks-Ruby-Development-Fund)
|
9
|
+
|
8
10
|
|
9
11
|
Radix is a very easy to use Ruby library for converting numbers to and from
|
10
12
|
any base. It supports both Integer, Float and Rational numbers, as well as
|
@@ -34,8 +36,8 @@ But Ruby reaches it's limit at base 36.
|
|
34
36
|
|
35
37
|
Radix provides the means of converting to and from any base.
|
36
38
|
|
37
|
-
For example, a number in base 256 can be represented by the array [100, 10]
|
38
|
-
(
|
39
|
+
For example, a number in base 256 can be represented by the array `[100, 10]`
|
40
|
+
(`100**256 + 10**1`) and can be convert to base 10.
|
39
41
|
|
40
42
|
[100,10].b(256).to_a(10) #=> [2,5,6,1,0]
|
41
43
|
|
@@ -69,6 +71,13 @@ Radix follows [Ruby Setup](http://rubyworks.github.com/setup) package standard
|
|
69
71
|
so it can also be installed in an FHS compliant manner using setup.rb.
|
70
72
|
|
71
73
|
|
74
|
+
## Special Thanks
|
75
|
+
|
76
|
+
Special thanks to **douglascodes** for taking the time to fully document
|
77
|
+
Radix's API. Documentation is an under-addressed and time-consuming affair,
|
78
|
+
so your contribution is greatly appreciated. Thank you, Douglas!
|
79
|
+
|
80
|
+
|
72
81
|
## Copyrights
|
73
82
|
|
74
83
|
Copyright (c) 2009 Rubyworks
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Zero becomes empty string (#4)
|
2
|
+
|
3
|
+
Example of the issue:
|
4
|
+
|
5
|
+
0.b(10).to_s #=> ""
|
6
|
+
|
7
|
+
I would expect "0" as a result.
|
8
|
+
|
9
|
+
0.b(10).to_s #=> "0"
|
10
|
+
|
11
|
+
Okay, lets make sure this works for Floats.
|
12
|
+
|
13
|
+
0.0.b(10).to_s #=> "0.0"
|
14
|
+
|
15
|
+
And Rationals too.
|
16
|
+
|
17
|
+
[0,1].br(10).to_s #=> "0/1"
|
18
|
+
|
data/lib/radix.rb
CHANGED
@@ -1,59 +1,32 @@
|
|
1
1
|
require 'radix/base'
|
2
2
|
require 'radix/integer'
|
3
3
|
require 'radix/float'
|
4
|
-
require 'radix/rational'
|
4
|
+
require 'radix/rational'
|
5
|
+
require 'radix/operator'
|
5
6
|
|
6
7
|
module Radix
|
8
|
+
|
9
|
+
##
|
10
|
+
# Returns the metadata contained in Radix.yml
|
7
11
|
#
|
12
|
+
# @return [Hash{String=>String}]
|
8
13
|
def self.metadata
|
9
14
|
@metadata ||= (
|
10
15
|
require 'yaml'
|
11
16
|
YAML.load(File.new(File.dirname(__FILE__) + '/radix.yml'))
|
12
17
|
)
|
13
18
|
end
|
14
|
-
#
|
15
|
-
def self.const_missing(name)
|
16
|
-
key = name.to_s.downcase
|
17
|
-
metadata[key] || super(name)
|
18
|
-
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
class ::Float
|
20
|
+
##
|
21
|
+
# Gets value of name in metadata or goes up ancestry.
|
25
22
|
#
|
26
|
-
|
27
|
-
Radix::Float.new(self, base)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class ::Integer
|
23
|
+
# @param [Symbol] name
|
32
24
|
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
class ::String
|
39
|
-
#
|
40
|
-
def b(base)
|
41
|
-
if index('.')
|
42
|
-
Radix::Float.new(self, base)
|
43
|
-
else
|
44
|
-
Radix::Integer.new(self, base)
|
45
|
-
end
|
25
|
+
# @return [String]
|
26
|
+
def self.const_missing(name)
|
27
|
+
key = name.to_s.downcase
|
28
|
+
metadata.key?(key) ? metadata[key] : super(name)
|
46
29
|
end
|
47
|
-
end
|
48
30
|
|
49
|
-
class ::Array
|
50
|
-
#
|
51
|
-
def b(base)
|
52
|
-
if index('.')
|
53
|
-
Radix::Float.new(self, base)
|
54
|
-
else
|
55
|
-
Radix::Integer.new(self, base)
|
56
|
-
end
|
57
|
-
end
|
58
31
|
end
|
59
32
|
|
data/lib/radix.yml
CHANGED
@@ -11,11 +11,15 @@ requirements:
|
|
11
11
|
- groups:
|
12
12
|
- build
|
13
13
|
development: true
|
14
|
-
name:
|
14
|
+
name: ergo
|
15
15
|
- groups:
|
16
16
|
- test
|
17
17
|
development: true
|
18
18
|
name: qed
|
19
|
+
- groups:
|
20
|
+
- test
|
21
|
+
development: true
|
22
|
+
name: ae
|
19
23
|
conflicts: []
|
20
24
|
alternatives: []
|
21
25
|
resources:
|
@@ -47,9 +51,9 @@ paths:
|
|
47
51
|
created: '2009-07-01'
|
48
52
|
summary: Convert to and from any base.
|
49
53
|
title: Radix
|
50
|
-
version: 2.
|
54
|
+
version: 2.2.0
|
51
55
|
name: radix
|
52
56
|
description: ! "Radix is a very easy to use Ruby library for converting numbers to
|
53
57
|
and from\nany base. It supports both Integer, Float and Rational numbers, as well
|
54
58
|
as \nrepresentational string-notations that need not be in ASCII order."
|
55
|
-
date: '2013-
|
59
|
+
date: '2013-03-20'
|
data/lib/radix/base.rb
CHANGED
@@ -1,48 +1,90 @@
|
|
1
1
|
module Radix
|
2
2
|
|
3
|
-
|
3
|
+
##
|
4
|
+
# Namespace for common bases defined as reusable constants.
|
5
|
+
#
|
4
6
|
module BASE
|
7
|
+
# Array of chars 0 - 9
|
5
8
|
B10 = ('0'..'9').to_a
|
9
|
+
# Array of chars 0 - 9 + X + E
|
6
10
|
B12 = B10 + ['X', 'E']
|
11
|
+
# Array of chars 0 - 9 + A - F
|
7
12
|
B16 = B10 + ('A'..'F').to_a
|
13
|
+
# Array of chars 0 - 9 + A - Z
|
8
14
|
B36 = B10 + ('A'..'Z').to_a
|
15
|
+
# Array of chars 0 - 9 + A - Z + a - x
|
9
16
|
B60 = B36 + ('a'..'x').to_a
|
17
|
+
# Array of chars 0 - 9 + A - Z + a - z
|
10
18
|
B62 = B36 + ('a'..'z').to_a
|
11
|
-
|
12
|
-
# Like BASE16 but encodes with lowercase letters.
|
19
|
+
# Array of chars 0 - 9 + a - f
|
13
20
|
HEX = B10 + ('a'..'f').to_a
|
14
21
|
end
|
15
22
|
|
16
|
-
|
23
|
+
##
|
24
|
+
# Radix::Base is a functional model of a base system that can be used for
|
25
|
+
# number conversions.
|
17
26
|
#
|
27
|
+
# Radix::Base is the original Radix API. But with the advent of v2.0
|
28
|
+
# and the new Integer and Float classes, it is essentially deprecated.
|
29
|
+
#
|
30
|
+
# @example Convert any base
|
18
31
|
# b10 = Radix::Base.new(10)
|
19
32
|
# b10.convert_base([100, 10], 256)
|
20
33
|
# #=> [2,5,6,1,0]
|
21
34
|
#
|
22
|
-
#
|
23
|
-
#
|
35
|
+
# @example Convert to string notation upto base 62
|
24
36
|
# b10.convert("10", 62) #=> "62"
|
25
37
|
#
|
26
|
-
#
|
27
|
-
# can be used.
|
28
|
-
#
|
38
|
+
# @example Odd notations
|
29
39
|
# b10 = Radix::Base.new(%w{Q W E R T Y U I O U})
|
30
40
|
# b10.convert("FF", 16) #=> "EYY"
|
31
41
|
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
42
|
+
# @!attribute [r] chars
|
43
|
+
# @return [Array<String>] The ASCII character set in use.
|
44
|
+
# @!attribute [r] base
|
45
|
+
# @return [Fixnum] The base level in use.
|
46
|
+
# @!attribute [r] values
|
47
|
+
# @example Testing base hash default values.
|
48
|
+
# > test = Radix::Base.new(36)
|
49
|
+
# > test.values["F"]
|
50
|
+
# 15
|
51
|
+
# > test.values["5"]
|
52
|
+
# 5
|
53
|
+
# > test.values["Y"]
|
54
|
+
# 34
|
55
|
+
# > test.values["YY"]
|
56
|
+
# nil # Fails because "YY" is not a key in the +values+ hash.
|
57
|
+
# @return [Hash{String=>Fixnum}]
|
58
|
+
# A hash of characters and their respective value.
|
37
59
|
class Base
|
38
60
|
|
61
|
+
##
|
62
|
+
# The characters for this base level.
|
63
|
+
#
|
64
|
+
# @return [Array<String>] The ASCII character set in use.
|
39
65
|
attr :chars
|
40
66
|
|
67
|
+
##
|
68
|
+
# The base of this instance.
|
69
|
+
#
|
70
|
+
# @return [Fixnum] The base level in use.
|
41
71
|
attr :base
|
42
72
|
|
73
|
+
##
|
74
|
+
# Hash of characters and values.
|
75
|
+
#
|
76
|
+
# @return [Hash{String=>Fixnum}]
|
77
|
+
# A hash of characters and their respective value.
|
43
78
|
attr :values
|
44
79
|
|
80
|
+
##
|
45
81
|
# New Radix using +chars+ representation.
|
82
|
+
#
|
83
|
+
# @param [Array<String>, Numeric] chars
|
84
|
+
# Array of string representation of number values of the base
|
85
|
+
# or a Numeric of the Base level.
|
86
|
+
#
|
87
|
+
# @return [void]
|
46
88
|
def initialize(chars=BASE::B62)
|
47
89
|
if ::Numeric === chars
|
48
90
|
chars = BASE::B62[0...chars]
|
@@ -52,10 +94,35 @@ module Radix
|
|
52
94
|
@values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten]
|
53
95
|
end
|
54
96
|
|
55
|
-
|
97
|
+
##
|
98
|
+
# Convert a value of given radix_base to that of the base instance.
|
99
|
+
#
|
100
|
+
# @param [String, Numeric, Array<String>] number
|
101
|
+
# The value in "radix_base" context.
|
102
|
+
#
|
103
|
+
# @param [Radix::Base, Numeric] radix_base
|
104
|
+
# Numeric for the radix or instance of Radix::Base.
|
105
|
+
#
|
106
|
+
# @example Convert Testing (Binary, Decimal, Hex)
|
107
|
+
# > b = Radix::Base.new(2)
|
108
|
+
# > d = Radix::Base.new(10)
|
109
|
+
# > h = Radix::Base.new(16)
|
110
|
+
# > d.convert("A", h)
|
111
|
+
# "10"
|
112
|
+
# > h.convert("A", d)
|
113
|
+
# TypeError
|
114
|
+
# > h.convert(10, d)
|
115
|
+
# "A"
|
116
|
+
# > h.convert(10, 10)
|
117
|
+
# "A"
|
118
|
+
# > b.convert(10, d)
|
119
|
+
# "1010"
|
120
|
+
# > b.convert(10, h)
|
121
|
+
# "10000"
|
122
|
+
#
|
123
|
+
# @return [String] representation of "number" in self.base level.
|
56
124
|
def convert(number, radix_base)
|
57
125
|
radix_base = Radix::Base.new(radix_base) unless Radix::Base === radix_base
|
58
|
-
|
59
126
|
case number
|
60
127
|
when ::String, ::Numeric
|
61
128
|
digits = number.to_s.split(//)
|
@@ -74,7 +141,20 @@ module Radix
|
|
74
141
|
digits.join
|
75
142
|
end
|
76
143
|
|
77
|
-
|
144
|
+
##
|
145
|
+
# Convert any base to any other base, using array of Fixnum's. Indexes of
|
146
|
+
# the array correspond to values for each column of the number in from_base
|
147
|
+
#
|
148
|
+
# @param [Array<Fixnum>] digits
|
149
|
+
# Array of values for each digit of source base.
|
150
|
+
#
|
151
|
+
# @param [Fixnum] from_base
|
152
|
+
# Source Base
|
153
|
+
#
|
154
|
+
# @param [Fixnum] to_base
|
155
|
+
# Destination Base
|
156
|
+
#
|
157
|
+
# @return [String] The value of digits in from_base converted as to_base.
|
78
158
|
def convert_base(digits, from_base, to_base)
|
79
159
|
bignum = 0
|
80
160
|
digits.each { |digit| bignum = bignum * from_base + digit }
|
@@ -87,14 +167,26 @@ module Radix
|
|
87
167
|
converted.reverse
|
88
168
|
end
|
89
169
|
|
170
|
+
##
|
90
171
|
# Encode a string in the radix.
|
172
|
+
#
|
173
|
+
# @param [String] byte_string
|
174
|
+
# String value in this base.
|
175
|
+
#
|
176
|
+
# @return [String] Encoded string from this Base.
|
91
177
|
def encode(byte_string)
|
92
178
|
digits = byte_string.unpack("C*")
|
93
179
|
digits = Radix.convert_base(digits, 256, base)
|
94
180
|
digits.map{ |d| @chars[d] }.join
|
95
181
|
end
|
96
182
|
|
183
|
+
##
|
97
184
|
# Decode a string that was previously encoded in the radix.
|
185
|
+
#
|
186
|
+
# @param [String] encoded
|
187
|
+
# Encoded string from this Base.
|
188
|
+
#
|
189
|
+
# @return [String] Decoded string of value from this base.
|
98
190
|
def decode(encoded)
|
99
191
|
digits = encoded.split(//).map{ |c| @values[c] }
|
100
192
|
Radix.convert_base(digits, base, 256).pack("C*")
|
@@ -102,15 +194,40 @@ module Radix
|
|
102
194
|
|
103
195
|
end
|
104
196
|
|
105
|
-
|
106
|
-
#
|
197
|
+
##
|
198
|
+
# Convert a number of from_base as to_base.
|
199
|
+
#
|
200
|
+
# @param [String, Numeric, Array<String>] number
|
201
|
+
# The value in context of "radix_base".
|
202
|
+
#
|
203
|
+
# @param [Fixnum, Radix::Base] from_base
|
204
|
+
# Source Base
|
205
|
+
#
|
206
|
+
# @param [Fixnum, Radix::Base] to_base
|
207
|
+
# Destination Base
|
208
|
+
#
|
209
|
+
# @return [String]
|
210
|
+
# The value of `digits` in `from_base` converted into `to_base`.
|
107
211
|
def self.convert(number, from_base, to_base)
|
108
212
|
from_base = Radix::Base.new(from_base) unless Radix::Base === from_base
|
109
213
|
to_base = Radix::Base.new(to_base) unless Radix::Base === to_base
|
110
214
|
to_base.convert(number, from_base)
|
111
215
|
end
|
112
216
|
|
113
|
-
|
217
|
+
##
|
218
|
+
# Convert any base to any other base, using array of Fixnum's. Indexes of
|
219
|
+
# the array correspond to values for each column of the number in from_base
|
220
|
+
#
|
221
|
+
# @param [Array<Fixnum>] digits
|
222
|
+
# Array of values for each digit of source base.
|
223
|
+
#
|
224
|
+
# @param [Fixnum] from_base
|
225
|
+
# Source Base
|
226
|
+
#
|
227
|
+
# @param [Fixnum] to_base
|
228
|
+
# Destination Base
|
229
|
+
#
|
230
|
+
# @return [String] The value of digits in from_base converted as to_base.
|
114
231
|
def self.convert_base(digits, from_base, to_base)
|
115
232
|
bignum = 0
|
116
233
|
digits.each { |digit| bignum = bignum * from_base + digit }
|