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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 398b7281c3d04fcc95e7f582e6a3d015cf0de9f5005a1b98c25f67f54b1434a1
4
- data.tar.gz: ae48022b091825de49fa68895e1a4370bd6741fd82de1370f2ec0d9a38d2678f
3
+ metadata.gz: 9e4562fc3e5218bf88704fea4709d2f54df4956a1ece72cc2a225e3e7bf77905
4
+ data.tar.gz: 64d970ef8e084d106cc2fc20862f689278a11a10807fd9d0dcaabf65cfca8680
5
5
  SHA512:
6
- metadata.gz: 72b8a389ba131af60ae215e678844a839a81fe5cb9a598a31cde492efcea4a1f1175f1e63699a8bbcf8c91b117129d5ad7de74cf5b3e09cd8be15c0f449a1f73
7
- data.tar.gz: 8d96e0351393d54918122fa95220a33fc3ea4c4a4c8dd7df0db00803f1028cd37298402729e9cea96e5ab23211baed0e9f33073cfdc5ef0574b2aeedc8053ad4
6
+ metadata.gz: 6fa6ba3460c1d5028f7d3973605d0caae772353a722dc79c2d26b5282e30c59b1855eeac7363d19e03daa8d382e06ba465b36a635df2cff757f2f99604f3f5f2
7
+ data.tar.gz: d915cf82e15365ac3d62fbe4d32f62c5e3bd8b85c68f6d2fc2bb0b83900b7c7ae4fa272614791014c34fc64cf00946416180a9b95cbfaf245c0395c1f3be436b
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'Guardfile'
10
+ - 'spec/**/*.rb'
11
+
12
+ Metrics/LineLength:
13
+ Max: 120
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
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
- guard :rspec, cmd: "bundle exec rspec -fd" do
2
- require "guard/rspec/dsl"
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
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,36 +1,37 @@
1
+ # frozen_string_literal: true
1
2
 
2
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "alt_characters/version"
5
+ require 'alt_characters/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "alt_characters"
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 = %q{alt_characters alternate base32 without numeric characters and padding}
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 = "MIT"
16
+ spec.license = 'MIT'
16
17
 
17
18
  if spec.respond_to?(:metadata)
18
- spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata['homepage_uri'] = spec.homepage
19
20
  else
20
- raise "RubyGems 2.0 or newer is required to protect against " \
21
- "public gem pushes."
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 = Dir.chdir(File.expand_path('..', __FILE__)) do
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 = "exe"
30
+ spec.bindir = 'exe'
30
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
32
+ spec.require_paths = ['lib']
32
33
 
33
- spec.add_development_dependency "bundler", "~> 1.17"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", "~> 3.0"
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
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "alt_characters"
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 "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'alt_characters/alt32'
2
4
 
3
5
  Alt32 = AltCharacters::Alt32
@@ -1,5 +1,7 @@
1
- require "alt_characters/version"
2
- require "alt32"
1
+ # frozen_string_literal: true
2
+
3
+ require 'alt_characters/version'
4
+ require 'alt32'
3
5
 
4
6
  module AltCharacters
5
7
  class << self
@@ -1,8 +1,10 @@
1
- require "alt_characters/alt_base"
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'.freeze
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
- def initialize(length, characters: nil)
8
+ BYTE = 8 # bit
9
+
10
+ def initialize(length, characters: nil, padding_character: '=')
7
11
  @length = length
8
- @logarithm = Math.log2(length)
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
- 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
+ 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
- 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
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.join
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("B*")
46
+ string = [binary].pack('B*')
36
47
  # string.encoding # => ASCII-8BIT
37
- string.force_encoding("UTF-8").gsub("\x00", "")
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AltCharacters
2
- VERSION = "0.1.1"
4
+ VERSION = '0.2.0'
3
5
  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.1.1
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