alt_characters 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +13 -0
- data/Gemfile +5 -2
- data/Guardfile +9 -2
- data/Rakefile +5 -3
- data/alt_characters.gemspec +15 -14
- data/exe/console +4 -3
- data/lib/alt32.rb +2 -0
- data/lib/alt_characters.rb +4 -2
- data/lib/alt_characters/alt32.rb +4 -2
- data/lib/alt_characters/alt_base.rb +46 -16
- data/lib/alt_characters/version.rb +3 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e4562fc3e5218bf88704fea4709d2f54df4956a1ece72cc2a225e3e7bf77905
|
4
|
+
data.tar.gz: 64d970ef8e084d106cc2fc20862f689278a11a10807fd9d0dcaabf65cfca8680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fa6ba3460c1d5028f7d3973605d0caae772353a722dc79c2d26b5282e30c59b1855eeac7363d19e03daa8d382e06ba465b36a635df2cff757f2f99604f3f5f2
|
7
|
+
data.tar.gz: d915cf82e15365ac3d62fbe4d32f62c5e3bd8b85c68f6d2fc2bb0b83900b7c7ae4fa272614791014c34fc64cf00946416180a9b95cbfaf245c0395c1f3be436b
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in alt_characters.gemspec
|
6
8
|
gemspec
|
7
9
|
|
8
10
|
gem 'guard-rspec'
|
11
|
+
gem 'guard-rubocop'
|
data/Guardfile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
guard :rspec, cmd: 'bundle exec rspec -fd' do
|
4
|
+
require 'guard/rspec/dsl'
|
3
5
|
dsl = Guard::RSpec::Dsl.new(self)
|
4
6
|
|
5
7
|
# RSpec files
|
@@ -12,3 +14,8 @@ guard :rspec, cmd: "bundle exec rspec -fd" do
|
|
12
14
|
ruby = dsl.ruby
|
13
15
|
dsl.watch_spec_files_for(ruby.lib_files)
|
14
16
|
end
|
17
|
+
|
18
|
+
guard :rubocop do
|
19
|
+
watch(/.+\.rb$/)
|
20
|
+
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
|
21
|
+
end
|
data/Rakefile
CHANGED
data/alt_characters.gemspec
CHANGED
@@ -1,36 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require 'alt_characters/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'alt_characters'
|
8
9
|
spec.version = AltCharacters::VERSION
|
9
10
|
spec.authors = ['booink']
|
10
11
|
spec.email = ['booink.work@gmail.com']
|
11
12
|
|
12
|
-
spec.summary =
|
13
|
+
spec.summary = 'alt_characters alternate base32 without numeric characters and padding'
|
13
14
|
spec.description = spec.summary
|
14
15
|
spec.homepage = 'https://github.com/booink/alt_characters'
|
15
|
-
spec.license =
|
16
|
+
spec.license = 'MIT'
|
16
17
|
|
17
18
|
if spec.respond_to?(:metadata)
|
18
|
-
spec.metadata[
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
19
20
|
else
|
20
|
-
raise
|
21
|
-
|
21
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
22
|
+
'public gem pushes.'
|
22
23
|
end
|
23
24
|
|
24
25
|
# Specify which files should be added to the gem when it is released.
|
25
26
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
-
spec.files
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
27
28
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
29
|
end
|
29
|
-
spec.bindir =
|
30
|
+
spec.bindir = 'exe'
|
30
31
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
-
spec.require_paths = [
|
32
|
+
spec.require_paths = ['lib']
|
32
33
|
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
37
|
end
|
data/exe/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'alt_characters'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "alt_characters"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/lib/alt32.rb
CHANGED
data/lib/alt_characters.rb
CHANGED
data/lib/alt_characters/alt32.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'alt_characters/alt_base'
|
2
4
|
|
3
5
|
module AltCharacters
|
4
6
|
class Alt32 < AltBase
|
5
|
-
DEFAULT_CHARACTERS = 'ABCDEFGHJKLMNPQRSTWXYfghkmprstwx'
|
7
|
+
DEFAULT_CHARACTERS = 'ABCDEFGHJKLMNPQRSTWXYfghkmprstwx'
|
6
8
|
|
7
9
|
def initialize(characters: DEFAULT_CHARACTERS)
|
8
10
|
super(32, characters: characters)
|
@@ -1,40 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AltCharacters
|
2
4
|
class AltBase
|
3
5
|
class DecodeError < StandardError; end
|
4
6
|
class EncodableCharactersError < StandardError; end
|
5
7
|
|
6
|
-
|
8
|
+
BYTE = 8 # bit
|
9
|
+
|
10
|
+
def initialize(length, characters: nil, padding_character: '=')
|
7
11
|
@length = length
|
8
|
-
@
|
12
|
+
@padding_character = padding_character
|
13
|
+
@logarithm = Math.log2(length).to_i # 2**n
|
9
14
|
@characters = characters
|
10
15
|
@characters = characters.split(//) if characters.class == String
|
11
16
|
count = @characters.count
|
12
|
-
if count < length
|
13
|
-
|
14
|
-
|
15
|
-
warn "[warn] Too many characters specified. The last #{count - length} characters are not used"
|
16
|
-
end
|
17
|
+
raise EncodableCharactersError, "Not enough characters. #{count} characters specified" if count < length
|
18
|
+
|
19
|
+
warn "[warn] Too many characters specified. The last #{count - length} characters are not used" if count > length
|
17
20
|
end
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
binary.
|
22
|
-
|
22
|
+
# rubocop:disable Metrics/AbcSize
|
23
|
+
def encode(text, padding: false)
|
24
|
+
binary = text.to_s.unpack1('B*')
|
25
|
+
encoded_characters = binary.chars.each_slice(@logarithm).map do |chunk|
|
26
|
+
chunk << '0' * (@logarithm - chunk.count) if chunk.count < @logarithm
|
23
27
|
i = chunk.join.to_i(2)
|
24
28
|
@characters[i]
|
25
|
-
end
|
29
|
+
end
|
30
|
+
encoded_characters = add_padding(encoded_characters) if padding && need_padding?(encoded_characters)
|
31
|
+
|
32
|
+
encoded_characters.join
|
26
33
|
end
|
34
|
+
# rubocop:enable Metrics/AbcSize
|
27
35
|
|
28
|
-
def decode(encoded_text)
|
29
|
-
binary =
|
36
|
+
def decode(encoded_text, padding: false)
|
37
|
+
binary = ''
|
30
38
|
encoded_text.to_s.chars.each do |character|
|
39
|
+
break if padding && character == @padding_character
|
40
|
+
|
31
41
|
i = @characters.index(character)
|
32
42
|
raise DecodeError, "character(#{character}) does not exists" if i.nil?
|
43
|
+
|
33
44
|
binary += format("%0#{@logarithm}d", i.to_s(2))
|
34
45
|
end
|
35
|
-
string = [binary].pack(
|
46
|
+
string = [binary].pack('B*')
|
36
47
|
# string.encoding # => ASCII-8BIT
|
37
|
-
string.force_encoding(
|
48
|
+
string.force_encoding('UTF-8').delete("\x00")
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def missing_chunks(characters)
|
54
|
+
number_of_group_characters = @logarithm.lcm(BYTE) / @logarithm
|
55
|
+
modulo = characters.count % number_of_group_characters
|
56
|
+
modulo.zero? ? 0 : number_of_group_characters - modulo
|
57
|
+
end
|
58
|
+
|
59
|
+
def need_padding?(characters)
|
60
|
+
missing_chunks(characters) != 0
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_padding(characters)
|
64
|
+
missing_chunks(characters).times do
|
65
|
+
characters << @padding_character
|
66
|
+
end
|
67
|
+
characters
|
38
68
|
end
|
39
69
|
end
|
40
70
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alt_characters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- booink
|
@@ -63,6 +63,7 @@ extra_rdoc_files: []
|
|
63
63
|
files:
|
64
64
|
- ".gitignore"
|
65
65
|
- ".rspec"
|
66
|
+
- ".rubocop.yml"
|
66
67
|
- ".travis.yml"
|
67
68
|
- CODE_OF_CONDUCT.md
|
68
69
|
- Gemfile
|