base_convert 4.0.200111 → 5.0.210126
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +150 -136
- data/lib/base_convert.rb +1 -1
- data/lib/base_convert/base_convert.rb +1 -1
- data/lib/base_convert/digits.rb +2 -2
- data/lib/base_convert/number.rb +4 -4
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a70b9639fe64e9243968dfd0b74efe7656c529bccde17ceb2fead9c3165e1c68
|
4
|
+
data.tar.gz: 4b4e8bf5f5560b2c2c638db7f925f9cd69a5637311225c5a272e63bc6dd66a53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '00827b732310b9117da09b021619895b93b476e1b4818ce9d8a6acfe0b65647d83b3bd6512e4e2c6a22f75299bde139d4448aa3c467ce2e60b390c37124fbdb9'
|
7
|
+
data.tar.gz: 14de39dc7f6fb4df71ab111b2ea8266383ed69e7787d87881047ed93eb0ba0f9a930721cfa13b8b0e40b3c47091ec4d0f67779f30df58699c7739ad4eb6f7e9c
|
data/README.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
# BaseConvert
|
2
2
|
|
3
|
-
* [VERSION
|
3
|
+
* [VERSION 5.0.210126](https://github.com/carlosjhr64/base_convert/releases)
|
4
4
|
* [github](https://www.github.com/carlosjhr64/base_convert)
|
5
5
|
* [rubygems](https://rubygems.org/gems/base_convert)
|
6
6
|
|
7
|
+
## INSTALL:
|
8
|
+
|
9
|
+
```shell
|
10
|
+
$ gem install base_convert
|
11
|
+
```
|
12
|
+
|
7
13
|
## DESCRIPTION:
|
8
14
|
|
9
15
|
BaseConvert - Number base conversion.
|
@@ -17,33 +23,31 @@ See also rosettacode.org's [Non-decimal radices convert](http://rosettacode.org/
|
|
17
23
|
|
18
24
|
## SYNOPSIS:
|
19
25
|
|
20
|
-
|
26
|
+
```ruby
|
27
|
+
require 'base_convert'
|
21
28
|
|
22
|
-
|
23
|
-
|
29
|
+
#toi string, base, digits => integer
|
30
|
+
BaseConvert.toi 'FF', 16, '0123456789ABCDEF' #=> 255
|
24
31
|
|
25
|
-
|
26
|
-
|
32
|
+
#tos integer, base, digits => string
|
33
|
+
BaseConvert.tos 255, 16, '0123456789ABCDEF' #=> "FF"
|
27
34
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# Number
|
34
|
-
n = BaseConvert::Number.new 'FF', base: 16, digits: '0123456789ABCDEF'
|
35
|
-
n.to_i #=> 255
|
36
|
-
n.to_s #=> "FF"
|
37
|
-
n.inspect #=> FF 16:P95
|
38
|
-
#
|
39
|
-
n = n.to_base 64, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
40
|
-
n.to_s #=> "D/"
|
41
|
-
n.to_i #=> 255
|
42
|
-
n.inspect #=> D/ 64:B64
|
43
|
-
|
44
|
-
## INSTALL:
|
35
|
+
# FromTo
|
36
|
+
c = BaseConvert::FromTo.new base: 16, digits: '0123456789ABCDEF', to_base: 7, to_digits: 'abcdefg'
|
37
|
+
c['FFF'] #=> "begea"
|
38
|
+
c.inspect #=> "16:P95,7:abfg"
|
45
39
|
|
46
|
-
|
40
|
+
# Number
|
41
|
+
n = BaseConvert::Number.new 'FF', base: 16, digits: '0123456789ABCDEF'
|
42
|
+
n.to_i #=> 255
|
43
|
+
n.to_s #=> "FF"
|
44
|
+
n.inspect #=> FF 16:P95
|
45
|
+
#
|
46
|
+
n = n.to_base 64, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
47
|
+
n.to_s #=> "D/"
|
48
|
+
n.to_i #=> 255
|
49
|
+
n.inspect #=> D/ 64:B64
|
50
|
+
```
|
47
51
|
|
48
52
|
## BUT WAIT, THERE'S MORE:
|
49
53
|
|
@@ -55,24 +59,26 @@ See also rosettacode.org's [Non-decimal radices convert](http://rosettacode.org/
|
|
55
59
|
|
56
60
|
Exemplar:
|
57
61
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
```ruby
|
63
|
+
class MyClass
|
64
|
+
include BaseConvert
|
65
|
+
attr_accessor :to_s, :to_i, :base, :digits
|
66
|
+
end
|
62
67
|
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
obj = MyClass.new
|
69
|
+
obj.digits = '!@#$%^&*()'
|
70
|
+
obj.base = 10
|
66
71
|
|
67
|
-
|
68
|
-
|
72
|
+
obj.to_s = '@'
|
73
|
+
obj.toi #=> 1
|
69
74
|
|
70
|
-
|
71
|
-
|
75
|
+
obj.to_i = 3
|
76
|
+
obj.tos #=> "$"
|
72
77
|
|
73
|
-
|
74
|
-
|
75
|
-
|
78
|
+
obj.ascii_ordered? #=> false
|
79
|
+
obj.digits = 'ABCDEFGHIJKLMNOP'
|
80
|
+
obj.ascii_ordered? #=> true
|
81
|
+
```
|
76
82
|
|
77
83
|
### Hash DIGITS
|
78
84
|
|
@@ -87,23 +93,25 @@ Exemplar:
|
|
87
93
|
|
88
94
|
Exemplar:
|
89
95
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
96
|
+
```ruby
|
97
|
+
include BaseConvert
|
98
|
+
DIGITS.get(:P95) #=> :alnum_bangs_typers_operators_separators_scapes_groupers_quoters_spacers
|
99
|
+
DIGITS[:P95]
|
100
|
+
#=> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?$&@*+-/<=>^~,.:;|#\\()[]{}%\"'`_ "
|
101
|
+
DIGITS.registry #=> [:P95, :B64, :U47, :G94, :Q91, :W63]
|
102
|
+
DIGITS.registry('347') #=> :U47
|
103
|
+
DIGITS.registry('0') #=> :P95
|
104
|
+
DIGITS.registry('AB') #=> :B64
|
105
|
+
DIGITS.registry('Cukoe') #=> nil
|
106
|
+
DIGITS.label('Cukoe') #=> :Cuoe
|
107
|
+
DIGITS.label('AaBbCcXxYyZz') #=> :AaZz
|
108
|
+
DIGITS[:N] #=> "0123456789"
|
109
|
+
DIGITS.get(:N) #=> nil
|
110
|
+
DIGITS.memoize!(:N)
|
111
|
+
DIGITS.get(:N) #=> "0123456789"
|
112
|
+
DIGITS.forget!(:N)
|
113
|
+
DIGITS.get(:N) #=> nil
|
114
|
+
```
|
107
115
|
|
108
116
|
|
109
117
|
#### DIGITS constructions
|
@@ -112,71 +120,73 @@ Exemplar:
|
|
112
120
|
See [Ruby-Doc's Regexp](https://ruby-doc.org/core-2.7.0/Regexp.html) documentation
|
113
121
|
for a full list of keys. The following provides an exemplar survey:
|
114
122
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
123
|
+
```ruby
|
124
|
+
# Character Classes
|
125
|
+
# Selected from ASCII 32..126
|
126
|
+
DIGITS[:w] #=> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
|
127
|
+
DIGITS[:d] #=> "0123456789"
|
128
|
+
# Note: :h was overridden, see :xdigit.
|
129
|
+
DIGITS[:h] #=> "0123456789ABCDEF"
|
130
|
+
DIGITS[:alpha] #=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
131
|
+
DIGITS[:graph]
|
132
|
+
#=> "!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
133
|
+
DIGITS[:lower] #=> "abcdefghijklmnopqrstuvwxyz"
|
134
|
+
DIGITS[:punct] #=> "!\"\#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
135
|
+
DIGITS[:upper] #=> "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
136
|
+
DIGITS[:xdigit] #=> "0123456789ABCDEFabcdef"
|
137
|
+
|
138
|
+
# Character Properties
|
139
|
+
# Selected from ASCII 32..126
|
140
|
+
DIGITS[:Alnum] #=> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
141
|
+
DIGITS[:Any]
|
142
|
+
#=> " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
143
|
+
|
144
|
+
# General Category
|
145
|
+
# Selected from ASCII 32..126
|
146
|
+
DIGITS[:Ps] #=> "([{"
|
147
|
+
DIGITS[:Pe] #=> ")]}"
|
148
|
+
DIGITS[:S] #=> "$+<=>^`|~"
|
149
|
+
|
150
|
+
# Ranged Selections
|
151
|
+
# v<hex>w<hex>_<filter>
|
152
|
+
DIGITS[:v1d7d8w1d7e1_Any] #=> "𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡"
|
153
|
+
# i<dec>j<dec>_<filter>
|
154
|
+
DIGITS[:i120488j120513_Any] #=> "𝚨𝚩𝚪𝚫𝚬𝚭𝚮𝚯𝚰𝚱𝚲𝚳𝚴𝚵𝚶𝚷𝚸𝚹𝚺𝚻𝚼𝚽𝚾𝚿𝛀𝛁"
|
155
|
+
|
156
|
+
# Specified Characters
|
157
|
+
# u<hex>
|
158
|
+
DIGITS[:u61u62] #=> "ab"
|
159
|
+
# k<dec>
|
160
|
+
DIGITS[:k97k98] #=> "ab"
|
161
|
+
|
162
|
+
# BaseConvert's Custom Sets
|
163
|
+
DIGITS[:bangs] #=> "!?"
|
164
|
+
DIGITS[:typers] #=> "$&@"
|
165
|
+
DIGITS[:operators] #=> "*+-/<=>^~"
|
166
|
+
DIGITS[:separators] #=> ",.:;|"
|
167
|
+
DIGITS[:scapes] #=> "#\\"
|
168
|
+
DIGITS[:groupers] #=> "()[]{}"
|
169
|
+
DIGITS[:quotes] #=> "\"'`"
|
170
|
+
DIGITS[:quoters] #=> "%\"'`"
|
171
|
+
DIGITS[:spacers] #=> "_ "
|
172
|
+
DIGITS[:ambiguous] #=> "012568BDGIOQSZl"
|
173
|
+
|
174
|
+
# Composition, add merge:
|
175
|
+
DIGITS[:d_ambiguous] #=> "0123456789BDGIOQSZl"
|
176
|
+
# Composition, add top:
|
177
|
+
DIGITS[:'d+ambiguous'] #=> "3479012568BDGIOQSZl"
|
178
|
+
# Composition, subtract:
|
179
|
+
DIGITS[:'d-ambiguous'] #=> "3479"
|
180
|
+
|
181
|
+
# Compositions used in BaseConvert
|
182
|
+
# :P95 is:
|
183
|
+
DIGITS[:alnum_bangs_typers_operators_separators_scapes_groupers_quoters_spacers]
|
184
|
+
#=> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?$&@*+-/<=>^~,.:;|#\\()[]{}%\"'`_ "
|
185
|
+
# :B64 is:
|
186
|
+
DIGITS[:LN_k43k47] #=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
187
|
+
# :U47 is:
|
188
|
+
DIGITS[:'alnum-ambiguous'] #=> "3479ACEFHJKLMNPRTUVWXYabcdefghijkmnopqrstuvwxyz"
|
189
|
+
```
|
180
190
|
|
181
191
|
### class FromTo
|
182
192
|
|
@@ -186,12 +196,14 @@ for a full list of keys. The following provides an exemplar survey:
|
|
186
196
|
|
187
197
|
Example:
|
188
198
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
199
|
+
```ruby
|
200
|
+
h2b = BaseConvert::FromTo.new(base: 16, digits: :P95, to_base: 64, to_digits: :B64)
|
201
|
+
h2b #=> 16:P95,64:B64
|
202
|
+
h2b['FFF'] #=> "//"
|
203
|
+
b2h = BaseConvert::FromTo.new(base: 64, digits: :B64, to_base: 16, to_digits: :P95)
|
204
|
+
b2h #=> 64:B64,16:P95
|
205
|
+
b2h['//'] #=> "FFF"
|
206
|
+
```
|
195
207
|
|
196
208
|
### class Number
|
197
209
|
|
@@ -206,21 +218,23 @@ Example:
|
|
206
218
|
|
207
219
|
Example:
|
208
220
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
221
|
+
```ruby
|
222
|
+
a = BaseConvert::Number.new('FFF', base: 16, digits: :P95)
|
223
|
+
a #=> FFF 16:P95
|
224
|
+
a.to_i #=> 4095
|
225
|
+
b = a.to_digits(:U47)
|
226
|
+
b #=> RRR 16:U47
|
227
|
+
b.to_i #=> 4095
|
228
|
+
c = b.to_base(64, :B64)
|
229
|
+
c #=> // 64:B64
|
230
|
+
c.to_i #=> 4095
|
231
|
+
```
|
218
232
|
|
219
233
|
## LICENSE:
|
220
234
|
|
221
235
|
(The MIT License)
|
222
236
|
|
223
|
-
Copyright (c)
|
237
|
+
Copyright (c) 2021 CarlosJHR64
|
224
238
|
|
225
239
|
Permission is hereby granted, free of charge, to any person obtaining
|
226
240
|
a copy of this software and associated documentation files (the
|
data/lib/base_convert.rb
CHANGED
data/lib/base_convert/digits.rb
CHANGED
@@ -55,14 +55,14 @@ class Digits < Hash
|
|
55
55
|
return digits
|
56
56
|
when Integer
|
57
57
|
raise 'need digits to cover base' if key > 95
|
58
|
-
return self[:P95] # Defined
|
58
|
+
return self[:P95] # Defined in configuration.rb
|
59
59
|
end
|
60
60
|
raise 'digits must be String|Symbol|Integer'
|
61
61
|
end
|
62
62
|
|
63
63
|
def registry(d=nil)
|
64
64
|
# BaseConvert::Number memoizes and uses specifically :P95, :B64, and :U47;
|
65
|
-
# giving these precedence above the rest.
|
65
|
+
# giving these precedence above the rest. Defined in configuration.rb.
|
66
66
|
@registry ||= [:P95, :B64, :U47, :G94, :Q91, :W63]
|
67
67
|
d ? @registry.detect{|_|self[_].start_with? d}: @registry
|
68
68
|
end
|
data/lib/base_convert/number.rb
CHANGED
@@ -56,7 +56,7 @@ class Number
|
|
56
56
|
|
57
57
|
# validate
|
58
58
|
if @validate
|
59
|
-
raise 'digits must cover base'
|
59
|
+
raise 'digits must cover base' if @base > @digits.length
|
60
60
|
unless string.nil? or string.empty?
|
61
61
|
indeces = string.chars.map{|_|@digits.index(_)}
|
62
62
|
if missing = indeces.any?{|_|_.nil?} or exceeding = indeces.any?{|_|_>=@base}
|
@@ -66,15 +66,15 @@ class Number
|
|
66
66
|
missing = indeces.any?{|_|_.nil?} or exceeding = indeces.any?{|_|_>=@base}
|
67
67
|
end
|
68
68
|
raise 'digits must cover string' if missing
|
69
|
-
raise 'digits in string must be under base'
|
69
|
+
raise 'digits in string must be under base' if exceeding
|
70
70
|
end
|
71
71
|
end
|
72
72
|
unless @integer.nil?
|
73
|
-
raise 'integer can not be negative'
|
73
|
+
raise 'integer can not be negative' if @integer < 0
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
@integer = toi(string)
|
77
|
+
@integer = toi(string) if @integer.nil?
|
78
78
|
end
|
79
79
|
|
80
80
|
def inspect
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.210126
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- carlosjhr64
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
BaseConvert - Number base conversion.
|
@@ -35,7 +35,7 @@ homepage: https://github.com/carlosjhr64/base_convert
|
|
35
35
|
licenses:
|
36
36
|
- MIT
|
37
37
|
metadata: {}
|
38
|
-
post_install_message:
|
38
|
+
post_install_message:
|
39
39
|
rdoc_options: []
|
40
40
|
require_paths:
|
41
41
|
- lib
|
@@ -50,9 +50,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '0'
|
52
52
|
requirements:
|
53
|
-
- 'ruby: ruby
|
54
|
-
rubygems_version: 3.
|
55
|
-
signing_key:
|
53
|
+
- 'ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]'
|
54
|
+
rubygems_version: 3.2.3
|
55
|
+
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: BaseConvert - Number base conversion.
|
58
58
|
test_files: []
|