xencoder 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d67031e0c7aa9fec0f75089aa8c77b0ac812c7da7884e871080f48515078d3ec
4
+ data.tar.gz: 02f1ef82d0d0b3036c1f5bca0268a3dda7aa690df3c2ee5bf9fbc4529fbb6066
5
+ SHA512:
6
+ metadata.gz: 2468e9a9bc3b382e46027501fcfe4e4d14176639e5e819eb6917d3f35eb40bbce0f3ad5044e9733624bbffc1ba6e80e4c187cf483c64e08db31fe11f9579c059
7
+ data.tar.gz: c60d55a94c1140b4dd7f05c6d0c086dab9c29d3b9819d9fcadc608e93bd275dc7443c5a72ba93c35f2319dd3236b7996b2f9fc181befb798b2f7fc66ab124ba3
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.5
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in xencoder.gemspec
6
+ gemspec
7
+
8
+ gem "pry", "~> 0.12.2"
9
+ gem "rubocop-rspec", "~> 1.37"
10
+ gem "simplecov", "~> 0.17.1"
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xencoder (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ docile (1.3.2)
13
+ jaro_winkler (1.5.4)
14
+ json (2.3.0)
15
+ method_source (0.9.2)
16
+ parallel (1.19.1)
17
+ parser (2.7.0.2)
18
+ ast (~> 2.4.0)
19
+ pry (0.12.2)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.9.0)
22
+ rainbow (3.0.0)
23
+ rake (10.5.0)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.1)
29
+ rspec-support (~> 3.9.1)
30
+ rspec-expectations (3.9.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.2)
37
+ rubocop (0.79.0)
38
+ jaro_winkler (~> 1.5.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.7.0.1)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 1.7)
44
+ rubocop-rspec (1.37.1)
45
+ rubocop (>= 0.68.1)
46
+ ruby-progressbar (1.10.1)
47
+ simplecov (0.17.1)
48
+ docile (~> 1.1)
49
+ json (>= 1.8, < 3)
50
+ simplecov-html (~> 0.10.0)
51
+ simplecov-html (0.10.2)
52
+ unicode-display_width (1.6.1)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ pry (~> 0.12.2)
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ rubocop-rspec (~> 1.37)
62
+ simplecov (~> 0.17.1)
63
+ xencoder!
64
+
65
+ BUNDLED WITH
66
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 xiaohui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Xencoder
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/xencoder.svg)](https://badge.fury.io/rb/xencoder)
4
+
5
+ Encode number to fixed length of characters, characters can be any digits, alphabets, symbols or even Chinese
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'xencoder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install xencoder
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'xencoder'
27
+
28
+ Xencoder.encoder.length # default length of encoded string
29
+ # => 6
30
+ Xencoder.encoder.max # the maximum value for encoding
31
+ # => 55884102751
32
+ Xencoder.encode(1)
33
+ # => "EPaPaP"
34
+ Xencoder.encode(2)
35
+ # => "HSo0u4"
36
+
37
+ Xencoder.decode('HSo0u4')
38
+ # => 2
39
+
40
+ encoder1 = Xencoder.new('0123456789abcdef', length: 4, seed: 200)
41
+ encoder1.max
42
+ # => 61439
43
+ encoder1.encode(1)
44
+ # => "1c75"
45
+ encoder1.encode(2)
46
+ # => "3ec7"
47
+
48
+ encoder2 = Xencoder.new('0123456789abcdef', length: 4, seed: 201)
49
+ encoder2.encode(1)
50
+ # => "7b5c"
51
+ encoder2.encode(2)
52
+ # => "aea6"
53
+
54
+ encoder3 = Xencoder.new('欢迎来到中国', length: 4, seed: 200)
55
+ encoder3.encode(2)
56
+ # => "来中中中"
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Testing
66
+
67
+ ```shell
68
+ bundle exec rspec
69
+ # or test specific range
70
+ EXTRA_TEST=100000,1000000 bundle exec rspec
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiaohui-zhangxh/xencoder.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xencoder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require "xencoder/version"
2
+ require 'xencoder/xbase'
3
+ require 'xencoder/encoder'
4
+
5
+ module Xencoder
6
+ class << self
7
+ def new(*args)
8
+ Encoder.new(*args)
9
+ end
10
+
11
+ def encoder
12
+ @encoder ||= Encoder.new(
13
+ '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
14
+ length: 6
15
+ )
16
+ end
17
+
18
+ def encode(num)
19
+ encoder.encode(num)
20
+ end
21
+
22
+ def decode(str)
23
+ encoder.decode(str)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ module Xencoder
2
+ class Encoder
3
+ attr_reader :chars, :length, :max, :seed
4
+
5
+ def initialize(chars, length: 6, seed: 1)
6
+ @chars = build_chars(chars).freeze
7
+ @length = length
8
+ @seed = seed
9
+ @xbase = Xbase.new(@chars)
10
+ @base = @xbase.to_i("#{chars[1]}#{chars[0] * (length - 1)}")
11
+ @max = @xbase.to_i(chars[-1] * length) - @base
12
+ end
13
+
14
+ def encode(number)
15
+ str = @xbase.to_x(number + @base)
16
+ (str.length - 1).downto(1).each do |i|
17
+ idx = mappings.index(str[i])
18
+ idx2 = (mappings.index(str[i - 1]) + idx) % mappings.size
19
+ str[i] = chars.at(idx)
20
+ str[i - 1] = chars.at(idx2)
21
+ end
22
+ str
23
+ end
24
+
25
+ def decode(str)
26
+ str = str.dup
27
+ 0.upto(str.length - 2).each do |i|
28
+ idx = chars.index(str[i + 1])
29
+ idx2 = (chars.index(str[i]) - idx + chars.size) % chars.size
30
+ str[i] = mappings.at(idx2)
31
+ str[i + 1] = mappings.at(idx)
32
+ end
33
+ @xbase.to_i(str) - @base
34
+ end
35
+
36
+ private
37
+
38
+ def mappings
39
+ return @mappings if defined?(@mappings)
40
+
41
+ @mappings = rand_with(seed) { @chars.shuffle }
42
+ end
43
+
44
+ def rand_with(seed)
45
+ old_seed = srand(seed)
46
+ yield
47
+ ensure
48
+ srand old_seed
49
+ end
50
+
51
+ def build_chars(input)
52
+ input = input.chars if input.is_a?(String)
53
+ raise "chars must be a type of String or Array" unless input.is_a?(Array)
54
+ raise "chars contains duplicate element" if input.uniq.size != input.size
55
+ input.dup
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Xencoder
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ module Xencoder
2
+ class Xbase
3
+ attr_reader :chars
4
+ def initialize(chars)
5
+ @chars = chars.dup.freeze
6
+ end
7
+
8
+ def to_i(str)
9
+ sum = 0
10
+ str.chars.reverse.each_with_index do |char, i|
11
+ sum += chars.index(char) * chars.size**i
12
+ end
13
+ sum
14
+ end
15
+
16
+ def to_x(num)
17
+ str = chars.at(num % chars.size)
18
+ num /= chars.size
19
+ str = "#{to_x(num)}#{str}" if num > 0
20
+ str
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "xencoder/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xencoder"
8
+ spec.version = Xencoder::VERSION
9
+ spec.authors = ["xiaohui"]
10
+ spec.email = ["xiaohui@tanmer.com"]
11
+
12
+ spec.summary = %q{Encode number to fixed length of characters}
13
+ spec.description = %q{Encode number to fixed length of characters, characters can be any digits, alphabets, symbols or even Chinese}
14
+ spec.homepage = "https://github.com/xiaohui-zhangxh/xencoder"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xencoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xiaohui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Encode number to fixed length of characters, characters can be any digits,
42
+ alphabets, symbols or even Chinese
43
+ email:
44
+ - xiaohui@tanmer.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/xencoder.rb
60
+ - lib/xencoder/encoder.rb
61
+ - lib/xencoder/version.rb
62
+ - lib/xencoder/xbase.rb
63
+ - xencoder.gemspec
64
+ homepage: https://github.com/xiaohui-zhangxh/xencoder
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.0.6
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Encode number to fixed length of characters
87
+ test_files: []