overpunch 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +3 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +8 -0
- data/lib/overpunch/version.rb +3 -0
- data/lib/overpunch.rb +33 -0
- data/overpunch.gemspec +27 -0
- data/spec/overpunch_spec.rb +102 -0
- data/spec/spec_helper.rb +76 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 62fbb5c8fc73e0121256c00cda49b226a3476c91
|
4
|
+
data.tar.gz: cb0fdb7e6d14c1837a19e49a37fc0be9168653f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 975e157d5b2dae02806efc73a968f632bad7a52f5ea37b2e1a52459a7270083af7de6a689b6790c1836dceb3b84bdf41b75f7fe60301cae4b77f3dd4409e9c91
|
7
|
+
data.tar.gz: e591f471e9078fe9211d67f0c03b524403297c9d34691b1e9ddfad5fa03b7d3e96654b9e3c77cea79ab70e81f69e3b65690bedb8f9ee92693959ffe687fc74c6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 John Carney
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Overpunch
|
2
|
+
|
3
|
+
Simple module for parsing and formatting numbers in signed overpunch format.
|
4
|
+
|
5
|
+
[Signed overpunch](http://wikipedia.org/wiki/Signed_overpunch) is a code used
|
6
|
+
to indicate the sign of a number by replacing the last digit with a signed
|
7
|
+
overpunch code. For example, -45 would be encoded as "4N".
|
8
|
+
|
9
|
+
| Sign | Digit | Code |
|
10
|
+
|:----:|:-----:|:----:|
|
11
|
+
| + | 0 | { |
|
12
|
+
| + | 1 | A |
|
13
|
+
| + | 2 | B |
|
14
|
+
| + | 3 | C |
|
15
|
+
| + | 4 | D |
|
16
|
+
| + | 5 | E |
|
17
|
+
| + | 6 | F |
|
18
|
+
| + | 7 | G |
|
19
|
+
| + | 8 | H |
|
20
|
+
| + | 9 | I |
|
21
|
+
| - | 0 | } |
|
22
|
+
| - | 1 | J |
|
23
|
+
| - | 2 | K |
|
24
|
+
| - | 3 | L |
|
25
|
+
| - | 4 | M |
|
26
|
+
| - | 5 | N |
|
27
|
+
| - | 6 | O |
|
28
|
+
| - | 7 | P |
|
29
|
+
| - | 8 | Q |
|
30
|
+
| - | 9 | R |
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
Add this line to your application's Gemfile:
|
35
|
+
|
36
|
+
gem 'overpunch'
|
37
|
+
|
38
|
+
And then execute:
|
39
|
+
|
40
|
+
$ bundle
|
41
|
+
|
42
|
+
Or install it yourself as:
|
43
|
+
|
44
|
+
$ gem install overpunch
|
45
|
+
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
require "overpunch"
|
49
|
+
|
50
|
+
Overpunch.parse("032B")
|
51
|
+
# => 322
|
52
|
+
|
53
|
+
Overpunch.parse("1}")
|
54
|
+
# => -10
|
55
|
+
|
56
|
+
Overpunch.format(110)
|
57
|
+
# => "11{"
|
58
|
+
|
59
|
+
Overpunch.format(-272)
|
60
|
+
# => "27K"
|
61
|
+
|
62
|
+
Overpunch.format(20, width: 6)
|
63
|
+
# => "00002{"
|
64
|
+
|
65
|
+
## Requirements
|
66
|
+
|
67
|
+
* Ruby 2.0.0 or later
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it ( http://github.com/johncarney/overpunch/fork )
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/overpunch.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "overpunch/version"
|
2
|
+
|
3
|
+
module Overpunch
|
4
|
+
POS_OVERPUNCH_SET = '{ABCDEFGHI'.freeze
|
5
|
+
NEG_OVERPUNCH_SET = '}JKLMNOPQR'.freeze
|
6
|
+
|
7
|
+
OVERPUNCH_PATTERN = /\A\s*0*(\d*(([#{POS_OVERPUNCH_SET}])|([#{NEG_OVERPUNCH_SET}])))\s*\Z/.freeze
|
8
|
+
|
9
|
+
def self.parse(string)
|
10
|
+
unless matches = string.match(OVERPUNCH_PATTERN)
|
11
|
+
raise ArgumentError, "Invalid overpunch string: \"#{string}\""
|
12
|
+
end
|
13
|
+
|
14
|
+
sign, overpunch_set = if matches[3]
|
15
|
+
[ 1, POS_OVERPUNCH_SET ]
|
16
|
+
else
|
17
|
+
[ -1, NEG_OVERPUNCH_SET ]
|
18
|
+
end
|
19
|
+
sign * matches[1].tr(overpunch_set, '0-9').to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.format(number, width: nil)
|
23
|
+
overpunch_set = if number < 0
|
24
|
+
NEG_OVERPUNCH_SET
|
25
|
+
else
|
26
|
+
POS_OVERPUNCH_SET
|
27
|
+
end
|
28
|
+
abs_number = number.abs
|
29
|
+
formatted = abs_number.to_s[0..-2] + overpunch_set[abs_number % 10]
|
30
|
+
padding = [ (width || 0) - formatted.size, 0 ].max
|
31
|
+
("0" * padding) + formatted
|
32
|
+
end
|
33
|
+
end
|
data/overpunch.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'overpunch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "overpunch"
|
8
|
+
spec.version = Overpunch::VERSION
|
9
|
+
spec.authors = ["John Carney"]
|
10
|
+
spec.email = ["john@carney.id.au"]
|
11
|
+
spec.summary = %q{Parses and formats numbers in signed overpunch format.}
|
12
|
+
spec.description = %q{Simple module for parsing and formatting numbers in signed overpunch format.}
|
13
|
+
spec.homepage = "https://github.com/johncarney/overpunch"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "rspec-its"
|
27
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require "overpunch"
|
2
|
+
|
3
|
+
RSpec::Matchers.define :be_zero_padded do |expected_zeroes|
|
4
|
+
def pluralize_zero(number)
|
5
|
+
if number == 1
|
6
|
+
"#{number} zero"
|
7
|
+
else
|
8
|
+
"#{number} zeroes"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
match do |actual|
|
13
|
+
actual.match(/\A0{#{expected_zeroes}}[^0]/)
|
14
|
+
end
|
15
|
+
|
16
|
+
description do
|
17
|
+
"be padded with #{pluralize_zero(expected_zeroes)}"
|
18
|
+
end
|
19
|
+
|
20
|
+
failure_message do |actual|
|
21
|
+
"expected \"#{actual}\" to be padded with exactly #{pluralize_zero(expected_zeroes)}"
|
22
|
+
end
|
23
|
+
|
24
|
+
failure_message_when_negated do |actual|
|
25
|
+
"did not expect \"#{actual}\" to be padded with #{pluralize_zero(expected_zeroes)}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
describe Overpunch do
|
31
|
+
describe ".parse" do
|
32
|
+
subject(:parse) { Overpunch.parse(value) }
|
33
|
+
|
34
|
+
context "given a positive overpunch string" do
|
35
|
+
let(:expected_value) { rand 0..1000 }
|
36
|
+
let(:value) { "%d%s" % [ expected_value / 10, "{ABCDEFGHI"[expected_value % 10] ] }
|
37
|
+
|
38
|
+
it { is_expected.to eq expected_value }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "given a negative overpunch string" do
|
42
|
+
let(:expected_value) { -rand(0..1000) }
|
43
|
+
let(:value) { "%d%s" % [ expected_value.abs / 10, "}JKLMNOPQR"[expected_value.abs % 10] ] }
|
44
|
+
|
45
|
+
it { is_expected.to eq expected_value }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "given leading zeroes" do
|
49
|
+
let(:expected_value) { rand 0..1000 }
|
50
|
+
let(:value) { "%s%d%s" % [ "0" * rand(1..2), expected_value / 10, "{ABCDEFGHI"[expected_value % 10] ] }
|
51
|
+
|
52
|
+
it { is_expected.to eq expected_value }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "given an invalid overpunch string" do
|
56
|
+
let(:value) { "123X" }
|
57
|
+
|
58
|
+
example { expect { parse }.to raise_error ArgumentError, "Invalid overpunch string: \"#{value}\"" }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '.format' do
|
63
|
+
let(:width) { nil }
|
64
|
+
|
65
|
+
subject { Overpunch.format(value, width: width) }
|
66
|
+
|
67
|
+
{
|
68
|
+
0 => "{",
|
69
|
+
5 => "E",
|
70
|
+
10 => "1{",
|
71
|
+
321 => "32A",
|
72
|
+
219 => "21I",
|
73
|
+
-2 => "K",
|
74
|
+
-10 => "1}",
|
75
|
+
-431 => "43J",
|
76
|
+
-729 => "72R"
|
77
|
+
}.each do |integer_value, expected_overpunch|
|
78
|
+
context "given #{integer_value}" do
|
79
|
+
let(:value) { integer_value }
|
80
|
+
|
81
|
+
it { is_expected.to eq expected_overpunch }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "zero-padding" do
|
86
|
+
let(:width) { 4 }
|
87
|
+
|
88
|
+
{ 1000 => 0,
|
89
|
+
-1000 => 0,
|
90
|
+
1 => 3,
|
91
|
+
-10 => 2,
|
92
|
+
123 => 1
|
93
|
+
}.each do |integer_value, expected_zeroes|
|
94
|
+
context "given #{integer_value}" do
|
95
|
+
let(:value) { integer_value }
|
96
|
+
|
97
|
+
it { is_expected.to be_zero_padded(expected_zeroes) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, make a
|
10
|
+
# separate helper file that requires this one and then use it only in the specs
|
11
|
+
# that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
require "rspec/its"
|
19
|
+
|
20
|
+
# These two settings work together to allow you to limit a spec run
|
21
|
+
# to individual examples or groups you care about by tagging them with
|
22
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
23
|
+
# get run.
|
24
|
+
# config.filter_run :focus
|
25
|
+
# config.run_all_when_everything_filtered = true
|
26
|
+
|
27
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
28
|
+
# file, and it's useful to allow more verbose output when running an
|
29
|
+
# individual spec file.
|
30
|
+
if config.files_to_run.one?
|
31
|
+
# Use the documentation formatter for detailed output,
|
32
|
+
# unless a formatter has already been configured
|
33
|
+
# (e.g. via a command-line flag).
|
34
|
+
config.default_formatter = 'doc'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Print the 10 slowest examples and example groups at the
|
38
|
+
# end of the spec run, to help surface which specs are running
|
39
|
+
# particularly slow.
|
40
|
+
config.profile_examples = 10
|
41
|
+
|
42
|
+
# Run specs in random order to surface order dependencies. If you find an
|
43
|
+
# order dependency and want to debug it, you can fix the order by providing
|
44
|
+
# the seed, which is printed after each run.
|
45
|
+
# --seed 1234
|
46
|
+
config.order = :random
|
47
|
+
|
48
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
49
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
50
|
+
# test failures related to randomization by passing the same `--seed` value
|
51
|
+
# as the one that triggered the failure.
|
52
|
+
Kernel.srand config.seed
|
53
|
+
|
54
|
+
# rspec-expectations config goes here. You can use an alternate
|
55
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
56
|
+
# assertions if you prefer.
|
57
|
+
config.expect_with :rspec do |expectations|
|
58
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
59
|
+
# For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
expectations.syntax = :expect
|
62
|
+
end
|
63
|
+
|
64
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
65
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
66
|
+
config.mock_with :rspec do |mocks|
|
67
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
68
|
+
# For more details, see:
|
69
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
70
|
+
mocks.syntax = :expect
|
71
|
+
|
72
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
73
|
+
# a real object. This is generally recommended.
|
74
|
+
mocks.verify_partial_doubles = true
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: overpunch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Carney
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-its
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Simple module for parsing and formatting numbers in signed overpunch
|
70
|
+
format.
|
71
|
+
email:
|
72
|
+
- john@carney.id.au
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/overpunch.rb
|
85
|
+
- lib/overpunch/version.rb
|
86
|
+
- overpunch.gemspec
|
87
|
+
- spec/overpunch_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: https://github.com/johncarney/overpunch
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 2.0.0
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Parses and formats numbers in signed overpunch format.
|
113
|
+
test_files:
|
114
|
+
- spec/overpunch_spec.rb
|
115
|
+
- spec/spec_helper.rb
|