alt_characters 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 012a91963f053d07e7d35fef6f15a8e3e4da01734f09a6b9e56175d97a8b15a0
4
- data.tar.gz: 99dc241e55c9fc9e429092f6c1a99d8e8e6202d85414747a9c44c71e3679a653
3
+ metadata.gz: 398b7281c3d04fcc95e7f582e6a3d015cf0de9f5005a1b98c25f67f54b1434a1
4
+ data.tar.gz: ae48022b091825de49fa68895e1a4370bd6741fd82de1370f2ec0d9a38d2678f
5
5
  SHA512:
6
- metadata.gz: '049cbaa7235713cc261006194aa72d8433053250476aad1c88aed50434c99c603656d9921f7ffd929ebee1eaf36a196c15e03efae899d903465b44b481a4c48d'
7
- data.tar.gz: 4965699fa651b349d3d52b91cc7c12cf9697bf39c2d0cacabb7c856f3b31218121a726335adcf915fb0f0cd5d363c548d7012fc40334d5d9c12e3bcafe5bf897
6
+ metadata.gz: 72b8a389ba131af60ae215e678844a839a81fe5cb9a598a31cde492efcea4a1f1175f1e63699a8bbcf8c91b117129d5ad7de74cf5b3e09cd8be15c0f449a1f73
7
+ data.tar.gz: 8d96e0351393d54918122fa95220a33fc3ea4c4a4c8dd7df0db00803f1028cd37298402729e9cea96e5ab23211baed0e9f33073cfdc5ef0574b2aeedc8053ad4
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # AltCharacters
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/alt_characters`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ alt_characters alternate base32 without numeric characters and padding
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,44 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Quick start
24
+
25
+ ```ruby
26
+ require 'alt_characters'
27
+
28
+ encoded_text = AltCharacters.alt32_encode('foo')
29
+ # => "Nmhgw"
30
+
31
+ AltCharacters.alt32_decode(encoded_text)
32
+ # => "foo"
33
+ ```
34
+
35
+ or
36
+
37
+ ```ruby
38
+ require 'alt_characters'
39
+
40
+ encoded_text = Alt32.new.encode('foo')
41
+ # => "Nmhgw"
42
+
43
+ Alt32.new.decode(encoded_text)
44
+ # => "foo"
45
+ ```
46
+
47
+
48
+ ### Modifying encodable characters
49
+
50
+ ```ruby
51
+ require 'alt_characters'
52
+
53
+ encodable_characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
54
+
55
+ encoded_text = Alt32.new(characters: encodable_characters).encode('foo')
56
+ # => "MZXW6"
57
+
58
+ Alt32.new(characters: encodable_characters).decode(encoded_text)
59
+ # => "foo"
60
+ ```
26
61
 
27
62
  ## Development
28
63
 
@@ -32,7 +67,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
67
 
33
68
  ## Contributing
34
69
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/alt_characters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/booink/alt_characters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
71
 
37
72
  ## License
38
73
 
data/lib/alt32.rb CHANGED
@@ -1,30 +1,3 @@
1
- class Alt32
2
- DEFAULT_CHARACTERS = 'ABCDEFGHJKLMNPQRSTWXYfghkmprstwx'.freeze
1
+ require 'alt_characters/alt32'
3
2
 
4
- class DecodeError < StandardError; end
5
-
6
- def initialize(characters: DEFAULT_CHARACTERS)
7
- @characters = characters.split(//) if characters.class == String
8
- end
9
-
10
- def encode(text)
11
- binary = text.to_s.unpack("B*").first
12
- binary.chars.each_slice(5).map do |chunk|
13
- chunk << "0" * (5 - chunk.count) if chunk.count < 5
14
- i = chunk.join.to_i(2)
15
- @characters[i]
16
- end.join
17
- end
18
-
19
- def decode(encoded_text)
20
- binary = ""
21
- encoded_text.to_s.chars.each do |character|
22
- i = @characters.index(character)
23
- raise DecodeError, "character(#{character}) does not exists" if i.nil?
24
- binary += format("%05d", i.to_s(2))
25
- end
26
- string = [binary].pack("B*")
27
- # string.encoding # => ASCII-8BIT
28
- string.force_encoding("UTF-8").gsub("\x00", "")
29
- end
30
- end
3
+ Alt32 = AltCharacters::Alt32
@@ -0,0 +1,11 @@
1
+ require "alt_characters/alt_base"
2
+
3
+ module AltCharacters
4
+ class Alt32 < AltBase
5
+ DEFAULT_CHARACTERS = 'ABCDEFGHJKLMNPQRSTWXYfghkmprstwx'.freeze
6
+
7
+ def initialize(characters: DEFAULT_CHARACTERS)
8
+ super(32, characters: characters)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ module AltCharacters
2
+ class AltBase
3
+ class DecodeError < StandardError; end
4
+ class EncodableCharactersError < StandardError; end
5
+
6
+ def initialize(length, characters: nil)
7
+ @length = length
8
+ @logarithm = Math.log2(length)
9
+ @characters = characters
10
+ @characters = characters.split(//) if characters.class == String
11
+ count = @characters.count
12
+ if count < length
13
+ raise EncodableCharactersError, "Not enough characters. #{count} characters specified"
14
+ elsif count > length
15
+ warn "[warn] Too many characters specified. The last #{count - length} characters are not used"
16
+ end
17
+ end
18
+
19
+ def encode(text)
20
+ binary = text.to_s.unpack("B*").first
21
+ binary.chars.each_slice(@logarithm).map do |chunk|
22
+ chunk << "0" * (@logarithm - chunk.count) if chunk.count < @logarithm
23
+ i = chunk.join.to_i(2)
24
+ @characters[i]
25
+ end.join
26
+ end
27
+
28
+ def decode(encoded_text)
29
+ binary = ""
30
+ encoded_text.to_s.chars.each do |character|
31
+ i = @characters.index(character)
32
+ raise DecodeError, "character(#{character}) does not exists" if i.nil?
33
+ binary += format("%0#{@logarithm}d", i.to_s(2))
34
+ end
35
+ string = [binary].pack("B*")
36
+ # string.encoding # => ASCII-8BIT
37
+ string.force_encoding("UTF-8").gsub("\x00", "")
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module AltCharacters
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alt_characters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - booink
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-16 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,8 @@ files:
75
75
  - exe/setup
76
76
  - lib/alt32.rb
77
77
  - lib/alt_characters.rb
78
+ - lib/alt_characters/alt32.rb
79
+ - lib/alt_characters/alt_base.rb
78
80
  - lib/alt_characters/version.rb
79
81
  homepage: https://github.com/booink/alt_characters
80
82
  licenses: