ruby_sscanf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df915cc66b1691fb7e9c74d48aba08cb60cec10d
4
+ data.tar.gz: 81529318abc9886ad74ebe0f749bbe8d3b3efe96
5
+ SHA512:
6
+ metadata.gz: 61ecf231774391b5a9b956179a08c1e04153b1a6e7f2faf505e38906431608419054cfbca1b9cda961fac4a20600c9ceb94e7e6f9c13579eef526f7de6002c85
7
+ data.tar.gz: 65adab881ced2faf3c4440d779db0997aeb0e8c48fa9cf31053b4063f3f3ff7bf955fe0996ca1f3a120046351ac5aa34823ccf35b245236c5e386accae023c24
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ docs/.~lock.*
16
+ *.bat
17
+ *.zip
18
+ *.tmp
19
+ rdoc
20
+ test/tmp
21
+ test/version_tmp
22
+ tmp
23
+ temp.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_sscanf.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Peter Camilleri
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.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # RubySscanf
2
+
3
+ The ruby_sscanf gem monkey patches the String class to support the sscanf
4
+ instance method. This method is modeled after the POSIX "C" standard sscanf
5
+ but with alterations and omissions to suit the Ruby programming language.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby_sscanf'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby_sscanf
22
+
23
+ ## Usage
24
+
25
+ The basic usage for sscanf is:
26
+
27
+ ```ruby
28
+ "<input string>".sscanf("<format string>")
29
+ ```
30
+ Where the input string is a collection of formatted information and the
31
+ format string is a description of that format. The output of the sscanf method
32
+ is an array of data extracted from the input string.
33
+
34
+ The format string consists of literal string components and format specifiers.
35
+
36
+ Literal string components match themselves in the input string. If the literal
37
+ has a trailing space, then this matches zero or more spaces. The special
38
+ sequence '%%' matches one '%'.
39
+
40
+ The layout of a format specifier is:
41
+
42
+ %[skip_flag][width]format
43
+
44
+ * The % sign is the lead-in character.
45
+ * The optional skip flag, the * causes any data extracted to be ignored.
46
+ * The width field is an integer field that determines the amount of text to be
47
+ parsed.
48
+ * The format field determines the type of data being parsed.
49
+
50
+ The supported format field values are:
51
+ <br>
52
+ * b - Scan for an (optionally signed) binary number with an optional
53
+ leading '0b' or '0B'.
54
+ * c - Grab the next character. If a positive width is specified, grab width
55
+ characters. For a negative width, grab characters to the position from the
56
+ end of the input. For example a width of -1 will grab all of the remaining
57
+ input data.
58
+ * d - Scan for an (optionally signed) decimal number.
59
+ * f - Scan for an (optionally signed) floating point number.
60
+ * i - Scan for an (optionally signed) integer. If the number begins with '0x'
61
+ or '0X', process hexadecimal; with '0b' or '0B', process binary, if '0', '0o',
62
+ or '0O', process octal, else process decimal.
63
+ * j - Scan for an (optionally signed) complex number in the form
64
+ [+-]?float[+-]float[ij]
65
+ * o - Scan for an (optionally signed) octal number with an optional
66
+ leading '0', '0o' or '0O'.
67
+ * q - Scan for a quoted string. That is a string enclosed by either '...'
68
+ or "...".
69
+ * r - Scan for an (optionally signed) rational number in the form
70
+ [+-]?decimal/decimal[r]?
71
+ * s - Scan for a space terminated string.
72
+ * u - Scan for a decimal number.
73
+ * x - Scan for an (optionally signed) hexadecimal number with an optional
74
+ leading '0x' or '0X'.
75
+ * [chars] - Scan for a contiguous string of characters in the set [chars].
76
+ * [^chars] - Scan for a contiguous string of characters not in the set [^chars]
77
+
78
+ ## Contributing
79
+
80
+ #### Plan A
81
+
82
+ 1. Fork it ( https://github.com/PeterCamilleri/format_engine/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
87
+
88
+ #### Plan B
89
+
90
+ Go to the GitHub repository and raise an issue calling attention to some
91
+ aspect that could use some TLC or a suggestion or idea. Apply labels to
92
+ the issue that match the point you are trying to make. Then follow your
93
+ issue and keep up-to-date as it is worked on. Or not as pleases you.
94
+ All input are greatly appreciated.
95
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env rake
2
+ # coding: utf-8
3
+
4
+ require 'rake/testtask'
5
+ require 'rdoc/task'
6
+ require "bundler/gem_tasks"
7
+
8
+ #Generate internal documentation with rdoc.
9
+ RDoc::Task.new do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+
12
+ #List out all the files to be documented.
13
+ rdoc.rdoc_files.include("lib/**/*.rb", "license.txt", "README.md")
14
+
15
+ #Make all access levels visible.
16
+ rdoc.options << '--visibility' << 'private'
17
+ #rdoc.options << '--verbose'
18
+ #rdoc.options << '--coverage-report'
19
+
20
+ #Set a title.
21
+ rdoc.options << '--title' << 'fOOrth Language Internals'
22
+
23
+ end
24
+
25
+ #Run the format_engine unit test suite.
26
+ Rake::TestTask.new do |t|
27
+ #List out all the test files.
28
+ t.test_files = FileList['tests/**/*.rb']
29
+ t.verbose = false
30
+ end
31
+
32
+ desc "Run a scan for smelly code!"
33
+ task :reek do |t|
34
+ `reek --no-color lib > reek.txt`
35
+ end
36
+
37
+ desc "Run an IRB Session with format_engine loaded."
38
+ task :console do
39
+ require 'irb'
40
+ require 'irb/completion'
41
+ require './lib/ruby_sscanf'
42
+ puts "Starting an IRB console with ruby_sscanf."
43
+ puts "Use 'quit' to exit."
44
+ puts
45
+ ARGV.clear
46
+ IRB.start
47
+ end
48
+
49
+ desc "What version of code is this?"
50
+ task :vers do |t|
51
+ puts
52
+ puts "ruby_sscanf version = #{RubySscanf::VERSION}"
53
+ end
@@ -0,0 +1,69 @@
1
+
2
+ require 'format_engine'
3
+ require_relative 'ruby_sscanf/version'
4
+
5
+ class String
6
+
7
+ DECIMAL = /[+-]?\d+/
8
+ HEX = /[+-]?(0[xX])?\h+/
9
+ OCTAL = /[+-]?(0[oO])?[0-7]+/
10
+ BINARY = /[+-]?(0[bB])?[01]+/
11
+ INTEGER = /[+-]?((0[xX]\h+)|(0[bB][01]+)|(0[oO]?[0-7]*)|([1-9]\d*))/
12
+ FLOAT = /[+-]?\d+(\.\d+)?([eE][+-]?\d+)?/
13
+ RATIONAL = /[+-]?\d+\/\d+(r)?/
14
+ COMPLEX = %r{(?<num> \d+(\.\d+)?([eE][+-]?\d+)?){0}
15
+ [+-]?\g<num>[+-]\g<num>[ij]
16
+ }x
17
+ QUOTED = /("([^\\"]|\\.)*")|('([^\\']|\\.)*')/
18
+
19
+ #Get the parsing engine.
20
+ def self.get_engine
21
+ Thread.current[:ruby_sscanf_engine] ||= FormatEngine::Engine.new(
22
+ "%b" => lambda {parse(BINARY) ? dst << found.to_i(2) : :break},
23
+ "%*b" => lambda {parse(BINARY) || :break},
24
+
25
+ "%c" => lambda {dst << grab},
26
+ "%*c" => lambda {grab},
27
+
28
+ "%d" => lambda {parse(DECIMAL) ? dst << found.to_i : :break},
29
+ "%*d" => lambda {parse(DECIMAL) || :break},
30
+
31
+ "%f" => lambda {parse(FLOAT) ? dst << found.to_f : :break},
32
+ "%*f" => lambda {parse(FLOAT) || :break},
33
+
34
+ "%i" => lambda {parse(INTEGER) ? dst << found.to_i(0) : :break},
35
+ "%*i" => lambda {parse(INTEGER) || :break},
36
+
37
+ "%j" => lambda {parse(COMPLEX) ? dst << Complex(found) : :break},
38
+ "%*j" => lambda {parse(COMPLEX) || :break},
39
+
40
+ "%o" => lambda {parse(OCTAL) ? dst << found.to_i(8) : :break},
41
+ "%*o" => lambda {parse(OCTAL) || :break},
42
+
43
+ "%q" => lambda do
44
+ parse(QUOTED) ? dst << found[1..-2].gsub(/\\./) {|seq| seq[-1]} : :break
45
+ end,
46
+ "%*q" => lambda {parse(QUOTED) || :break},
47
+
48
+ "%r" => lambda {parse(RATIONAL) ? dst << found.to_r : :break},
49
+ "%*r" => lambda {parse(RATIONAL) || :break},
50
+
51
+ "%s" => lambda {parse(/\S+/) ? dst << found : :break},
52
+ "%*s" => lambda {parse(/\S+/) || :break},
53
+
54
+ "%u" => lambda {parse(/\d+/) ? dst << found.to_i : :break},
55
+ "%*u" => lambda {parse(/\d+/) || :break},
56
+
57
+ "%x" => lambda {parse(HEX) ? dst << found.to_i(16) : :break},
58
+ "%*x" => lambda {parse(HEX) || :break},
59
+
60
+ "%[" => lambda {parse(fmt.regex) ? dst << found : :break},
61
+ "%*[" => lambda {parse(fmt.regex) || :break})
62
+ end
63
+
64
+ #Scan the formatted input.
65
+ def sscanf(format)
66
+ String.get_engine.do_parse(self, [], format)
67
+ end
68
+
69
+ end
@@ -0,0 +1,3 @@
1
+ module RubySscanf
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_sscanf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_sscanf"
8
+ spec.version = RubySscanf::VERSION
9
+ spec.authors = ["Peter Camilleri"]
10
+ spec.email = ["peter.c.camilleri@gmail.com"]
11
+
12
+ spec.summary = "A string parser."
13
+ spec.description = "A formatted string data parser for Ruby"
14
+ spec.homepage = "http://teuthida-technologies.com/"
15
+ spec.license = "MIT"
16
+
17
+ raw_list = `git ls-files`.split($/)
18
+ spec.files = raw_list.keep_if {|entry| !entry.start_with?("docs") }
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 1.9.3'
24
+
25
+ spec.add_runtime_dependency "format_engine", ">= 0.5"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.11"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency 'minitest', "~> 5.5.1"
30
+ spec.add_development_dependency 'minitest_visible', "~> 0.0.1"
31
+ spec.add_development_dependency 'rdoc', "~> 4.0.1"
32
+
33
+ end
@@ -0,0 +1,66 @@
1
+ require_relative '../lib/ruby_sscanf'
2
+ gem 'minitest'
3
+ require 'minitest/autorun'
4
+ require 'minitest_visible'
5
+
6
+
7
+ # Test the internals of the parser engine. This is not the normal interface.
8
+ class ScanTester < Minitest::Test
9
+
10
+ #Track mini-test progress.
11
+ MinitestVisible.track self, __FILE__
12
+
13
+ def test_that_it_can_scan
14
+ result = "12 34 -56".sscanf "%d %2d %4d"
15
+ assert_equal([12, 34, -56] , result)
16
+
17
+ result = "255 0b11111111 0377 0xFF 0 ".sscanf "%i %i %i %i %i"
18
+ assert_equal([255, 255, 255, 255, 0] , result)
19
+
20
+ result = "7 10 377".sscanf "%o %o %o"
21
+ assert_equal([7, 8, 255] , result)
22
+
23
+ result = "10 10011 11110000".sscanf "%b %b %b"
24
+ assert_equal([2, 19, 240] , result)
25
+
26
+ result = "0 F FF FFF FFFF".sscanf "%x %x %x %x %x"
27
+ assert_equal([0, 15, 255, 4095, 65535] , result)
28
+
29
+ result = "Hello Silly World".sscanf "%s %*s %s"
30
+ assert_equal(["Hello", "World"] , result)
31
+
32
+ result = "Hello Silly World".sscanf "%5c %*5c %5c"
33
+ assert_equal(["Hello", "World"] , result)
34
+
35
+ result = "42 The secret is X".sscanf "%i %-1c"
36
+ assert_equal([42, "The secret is X"] , result)
37
+
38
+ result = "42 The secret is X".sscanf "%i %-2c%c"
39
+ assert_equal([42, "The secret is ", "X"] , result)
40
+
41
+ result = "42 The secret is X".sscanf "%i %*-2c%c"
42
+ assert_equal([42, "X"] , result)
43
+
44
+ result = "9.99 1.234e56 -1e100".sscanf "%f %f %f"
45
+ assert_equal([9.99, 1.234e56, -1e100] , result)
46
+
47
+ result = "85% 75%".sscanf "%f%% %f%%"
48
+ assert_equal([85, 75] , result)
49
+
50
+ result = "12 34 -56".sscanf "%u %u %u"
51
+ assert_equal([12, 34] , result)
52
+
53
+ result = "1/2 3/4r -5/6".sscanf "%r %r %r"
54
+ assert_equal(['1/2'.to_r, '3/4'.to_r, '-5/6'.to_r] , result)
55
+
56
+ result = "1+2i 3+4j -5e10-6.2i".sscanf "%j %j %j"
57
+ assert_equal([Complex('1+2i'), Complex('3+4j'), Complex('-5e10-6.2i')] , result)
58
+
59
+ result = "'quote' 'silly' \"un quote\" 'a \\'' ".sscanf "%q %*q %q %q"
60
+ assert_equal(["quote", "un quote", "a '"] , result)
61
+
62
+ result = "a b c".sscanf "%[a] %[b] %[c]"
63
+ assert_equal(["a", "b", "c"] , result)
64
+ end
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_sscanf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Camilleri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: format_engine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.5.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.5.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest_visible
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.0.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: rdoc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.1
97
+ description: A formatted string data parser for Ruby
98
+ email:
99
+ - peter.c.camilleri@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/ruby_sscanf.rb
110
+ - lib/ruby_sscanf/version.rb
111
+ - ruby_sscanf.gemspec
112
+ - tests/scan_tests.rb
113
+ homepage: http://teuthida-technologies.com/
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 1.9.3
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: A string parser.
137
+ test_files: []