dec_radix_50 0.1.0 → 0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +28 -3
- data/lib/dec_radix_50/decoder.rb +39 -0
- data/lib/dec_radix_50/encoder.rb +4 -3
- data/lib/dec_radix_50/version.rb +1 -1
- data/lib/dec_radix_50.rb +17 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ad5e6f71065350c8189ad0bc85012d93048e96232e04769ebebd500568602a6
|
4
|
+
data.tar.gz: 05a18a04c41cb74c12f4cb6d19ccd184059e0f5066a2f66920c7fd1a68067a4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be68f943d6e821e21133f97ab14906294b270de2874617206a28b28d6858a9a0ce2e46d66948860f4681da38f2ed6109d8fc4bfa88bdeba5941134c3253e6cd7
|
7
|
+
data.tar.gz: 30ae149e388de3e15bc0d2d3660fc7e75b88fabfb6dc893183da7990e244a1d2421958830d746708b96067db3ef62ba48f438c125dbdaef586d28f1df97c0da2
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,11 +26,13 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
|
29
|
+
### Encoding
|
30
|
+
|
31
|
+
DECRadix50.encode(charset, string \[, replace_char\]) => Array\<Integer\>
|
30
32
|
|
31
33
|
### Arguments
|
32
34
|
|
33
|
-
+ **charset**: character set, should be 40 characters long.
|
35
|
+
+ **charset**: character set, should be 40 characters long for the DEC RADIX-50. The charset string length defines the base.
|
34
36
|
|
35
37
|
The gem comes with two predefined characters sets:
|
36
38
|
|
@@ -44,7 +46,7 @@ DECRadix50.encode(charset, string \[, replace_char = " "\]) => Array\<Integer\>
|
|
44
46
|
|
45
47
|
+ **string**: string to encode.
|
46
48
|
|
47
|
-
+ **replace_char**: replaces unsupported characters. Default value is " " (a space).
|
49
|
+
+ **replace_char**: replaces unsupported characters in the input string. Should be included in the charset. Default value is " " (a space).
|
48
50
|
|
49
51
|
### Returns
|
50
52
|
|
@@ -59,6 +61,29 @@ DECRadix50.encode(DECRadix50::MK90_CHARSET, "ABCDEF")
|
|
59
61
|
# => [1683, 6606]
|
60
62
|
```
|
61
63
|
|
64
|
+
### Decoding
|
65
|
+
|
66
|
+
DECRadix50.decode(charset, encoded_values) => String
|
67
|
+
|
68
|
+
### Arguments
|
69
|
+
|
70
|
+
+ **charset**: character set, should be 40 characters long for the DEC RADIX-50. The charset string length defines the base.
|
71
|
+
|
72
|
+
+ **encoded_values**: array of integers that represent an encoded string.
|
73
|
+
|
74
|
+
### Returns
|
75
|
+
|
76
|
+
+ String.
|
77
|
+
|
78
|
+
### Example:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require "dec_radix_50"
|
82
|
+
|
83
|
+
DECRadix50.decode(DECRadix50::MK90_CHARSET, [2092, 1015, 34_320, 3259, 8001, 29_000, 3412, 24_567, 815, 859, 0])
|
84
|
+
=> "ALL YOUR BASE ARE BELONG TO US "
|
85
|
+
```
|
86
|
+
|
62
87
|
## Development
|
63
88
|
|
64
89
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DECRadix50
|
4
|
+
#
|
5
|
+
# Encodes strings to the DEC Radix-50 encoding.
|
6
|
+
#
|
7
|
+
class Decoder
|
8
|
+
def initialize(charset, encoded_values)
|
9
|
+
@charset = charset
|
10
|
+
@encoded_values = encoded_values
|
11
|
+
@base = charset.length
|
12
|
+
|
13
|
+
_validate_input_parameters
|
14
|
+
end
|
15
|
+
|
16
|
+
def decode
|
17
|
+
charset_values = @charset.split(//).each_with_index.to_h.invert
|
18
|
+
|
19
|
+
@encoded_values.map do |value|
|
20
|
+
x = charset_values[(value / @base / @base) % @base]
|
21
|
+
y = charset_values[(value / @base) % @base]
|
22
|
+
z = charset_values[value % @base]
|
23
|
+
"#{x}#{y}#{z}"
|
24
|
+
end.join
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def _validate_input_parameters
|
30
|
+
raise DECRadix50::ArgumentError, "charset cannot be empty" if @charset.empty?
|
31
|
+
raise DECRadix50::ArgumentError, "input array cannot be empty" if @encoded_values.empty?
|
32
|
+
|
33
|
+
max_value = @base * @base * @base
|
34
|
+
in_range = @encoded_values.all? { |e| e < max_value }
|
35
|
+
|
36
|
+
raise DECRadix50::ArgumentError, "encoded values should be less than #{max_value}" unless in_range
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/dec_radix_50/encoder.rb
CHANGED
@@ -10,6 +10,7 @@ module DECRadix50
|
|
10
10
|
|
11
11
|
@charset = charset
|
12
12
|
@string = string.upcase.gsub(/[^#{charset}]/, replace_char)
|
13
|
+
@base = charset.length
|
13
14
|
end
|
14
15
|
|
15
16
|
# Based on the @jgn/radix50.rb
|
@@ -17,15 +18,15 @@ module DECRadix50
|
|
17
18
|
def encode
|
18
19
|
charset_values = @charset.split(//).each_with_index.to_h
|
19
20
|
@string.chars.each_slice(3).map do |x, y, z|
|
20
|
-
(((charset_values[x] || 0) *
|
21
|
+
(((charset_values[x] || 0) * @base + (charset_values[y] || 0)) * @base + (charset_values[z] || 0))
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
25
|
private
|
25
26
|
|
26
27
|
def _validate_input_parameters(charset, string, replace_char)
|
27
|
-
raise DECRadix50::ArgumentError, "charset
|
28
|
-
raise DECRadix50::ArgumentError, "string
|
28
|
+
raise DECRadix50::ArgumentError, "charset cannot be empty" if charset.empty?
|
29
|
+
raise DECRadix50::ArgumentError, "string cannot be empty" if string.empty?
|
29
30
|
raise DECRadix50::ArgumentError, "replace_char should be exactly 1 character long" unless replace_char.length == 1
|
30
31
|
end
|
31
32
|
end
|
data/lib/dec_radix_50/version.rb
CHANGED
data/lib/dec_radix_50.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "dec_radix_50/decoder"
|
3
4
|
require_relative "dec_radix_50/encoder"
|
4
5
|
require_relative "dec_radix_50/version"
|
5
6
|
|
@@ -20,14 +21,14 @@ module DECRadix50
|
|
20
21
|
# Encode a string to the DEC Radix-50 encoding.
|
21
22
|
#
|
22
23
|
# @param [String] charset
|
23
|
-
# Should be 40 characters long.
|
24
24
|
#
|
25
25
|
# @param [String] string
|
26
26
|
# String to encode.
|
27
27
|
#
|
28
28
|
# @option [String] replace_char (" ")
|
29
29
|
# If a given string has characters that aren't included in the
|
30
|
-
# charset, they will be replaced with the replace_char.
|
30
|
+
# charset, they will be replaced with the replace_char. replace_char
|
31
|
+
# should be part of the charset.
|
31
32
|
#
|
32
33
|
# @return [Array<Integer>]
|
33
34
|
# Radix-50 (decimal numeral system).
|
@@ -35,4 +36,18 @@ module DECRadix50
|
|
35
36
|
def self.encode(charset, string, replace_char = " ")
|
36
37
|
Encoder.new(charset, string, replace_char).encode
|
37
38
|
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Decode array of integers to a string.
|
42
|
+
#
|
43
|
+
# @param [String] charset
|
44
|
+
#
|
45
|
+
# @param [Array<Integer>] int_array
|
46
|
+
# Integer values to decode.
|
47
|
+
#
|
48
|
+
# @return [String]
|
49
|
+
#
|
50
|
+
def self.decode(charset, int_array)
|
51
|
+
Decoder.new(charset, int_array).decode
|
52
|
+
end
|
38
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dec_radix_50
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 8bit-m8
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- dec_radix_50.gemspec
|
27
27
|
- lib/dec_radix_50.rb
|
28
|
+
- lib/dec_radix_50/decoder.rb
|
28
29
|
- lib/dec_radix_50/encoder.rb
|
29
30
|
- lib/dec_radix_50/version.rb
|
30
31
|
- sig/dec_radix_50.rbs
|