pxeger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0877ed32f8ad811f3895d403a79be3a29b590fc2
4
+ data.tar.gz: d16bdb90598c836877681cf503267a43cf4f7d12
5
+ SHA512:
6
+ metadata.gz: 76536e4899af8c1c01897ba0452cf77033f8d2c79d2fafbb9bf785d121cd9751c88d5ebd215eed294eb3de16fb6c209ecd54c10b52a67f4c1d0a8f5c6b2bc28b
7
+ data.tar.gz: b90ce8e6b3167ffce2276e29878e3553df94e355ec17b2c4e27cfe6f0b0a44ee075a42ef9af0b294ff66b1fedfbb62498c1abe0c9040c61d130215a2e42f70b4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.1.6
5
+ - 2.0.0
6
+ before_install: gem install bundler -v 1.10.3
7
+ script: bundle exec rspec
8
+ branches:
9
+ only:
10
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pxeger.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Naoki Shimizu
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,49 @@
1
+ # Pxeger
2
+ [![Build Status](https://travis-ci.org/deme0607/pxeger.svg?branch=master)](https://travis-ci.org/deme0607/pxeger)
3
+
4
+ Random string gemerator from regular expression.
5
+ Refered to [cho45/String_random.js](https://github.com/cho45/String_random.js)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pxeger'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pxeger
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'pxeger'
27
+
28
+ pxeger = Pxeger.new(/[a-z]{10}/)
29
+ pxeger.generate #=> "ovwjqgerwb"
30
+
31
+ pxeger = Pxeger.new(/(ワー?ン!)?+(ワン!){1,4}/)
32
+ pxeger.generate #=> "ワン!"
33
+ pxeger.generate #=> "ワーン!ワン!ワン!ワン!ワン!"
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ 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).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pxeger.
45
+
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://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 "pxeger"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,263 @@
1
+ require "pxeger/version"
2
+ require "pxeger/tree"
3
+ require "pxeger/constants"
4
+
5
+ class Pxeger
6
+ def initialize(pattern)
7
+ @pattern = pattern.instance_of?(Regexp) ? pattern.source : pattern
8
+ end
9
+
10
+ def generate
11
+ @reference = {}
12
+ tree = process_grouping(@pattern.chars)
13
+ tree = process_select(tree)
14
+ process_others(tree)
15
+ end
16
+
17
+ private
18
+
19
+ def multi_backword_reference?
20
+ return @is_multi_back_ref unless @is_multi_back_ref.nil?
21
+ ref_indexes = @pattern.scan(/\\\d+/).map{|r| r.sub('\\','').to_i}
22
+ @is_multi_back_ref = ref_indexes.any? {|i| i >= 2}
23
+ end
24
+
25
+ def process_grouping(pattern_array)
26
+ tree = Tree.new
27
+ stack = Tree.new([tree])
28
+ n = 1
29
+
30
+ while (char = pattern_array.shift)
31
+ if (char == "\\")
32
+ next_char = pattern_array.shift
33
+ if (%w| ( ) |.include?(next_char))
34
+ stack[0].push(next_char)
35
+ else
36
+ stack[0].push(char, next_char)
37
+ end
38
+ elsif (char == '(')
39
+ inner = Tree.new
40
+ stack[0].push(inner)
41
+ stack.unshift(inner)
42
+
43
+ next_char = pattern_array.shift
44
+ if (next_char == '?')
45
+ next_char = pattern_array.shift
46
+ if (next_char == ':')
47
+ # nothing
48
+ else
49
+ raise "Invalid Group"
50
+ end
51
+ elsif (%w| ( ) |.include?(next_char))
52
+ pattern_array.unshift(next_char)
53
+ else
54
+ inner.num = n
55
+ n += 1
56
+ inner.push(next_char)
57
+ end
58
+ elsif (char == ')')
59
+ stack.shift
60
+ else
61
+ stack[0].push(char)
62
+ end
63
+ end
64
+
65
+ return tree
66
+ end
67
+
68
+ def process_select(tree)
69
+ candinates = Tree.new([Tree.new])
70
+
71
+ while (char = tree.shift)
72
+ if (char == '\\')
73
+ next_char = tree.shift
74
+ if (next_char == '|')
75
+ candinates[0].push(next_char)
76
+ else
77
+ candinates[0].push(char, next_char)
78
+ end
79
+ elsif (char == '[')
80
+ candinates[0].push(char)
81
+ while (char = tree.shift)
82
+ candinates[0].push(char)
83
+ if (char == '\\')
84
+ next_char = tree.shift
85
+ candinates[0].push(next_char)
86
+ elsif (char == ']')
87
+ break
88
+ end
89
+ end
90
+ elsif (char == '|')
91
+ candinates.unshift(Tree.new)
92
+ else
93
+ candinates[0].push(char)
94
+ end
95
+ end
96
+
97
+ candinates.each do |it|
98
+ tree.push(it)
99
+ len = it.length
100
+ j = 0
101
+ while ( j < len )
102
+ process_select(it[j]) if it[j].instance_of?(Pxeger::Tree)
103
+ j += 1
104
+ end
105
+ end
106
+
107
+ return Tree.new([tree])
108
+ end
109
+
110
+ def process_others(tree)
111
+ ret = ''
112
+ candinates = Tree.new
113
+ tree = tree.dup
114
+
115
+ while (char = tree.shift)
116
+ case char
117
+ when '^'
118
+ when '$'
119
+ when '*'
120
+ rand(10).times { ret << choose(candinates) }
121
+ candinates = Tree.new
122
+ when '+'
123
+ (rand(10) + 1).times { ret << choose(candinates) }
124
+ candinates = Tree.new
125
+ when '{'
126
+ brace = ''
127
+ while (char = tree.shift)
128
+ if (char == '}')
129
+ break
130
+ else
131
+ brace << char
132
+ end
133
+ end
134
+
135
+ if (char != '}')
136
+ raise "missmatch brace: #{char}"
137
+ end
138
+
139
+ dd = brace.split(',')
140
+ min = dd[0].to_i
141
+ max = (dd.length == 1) ? min : (dd[1].to_i || 10)
142
+
143
+ (rand(max - min + 1) + min).times { ret << choose(candinates) }
144
+ candinates = Tree.new
145
+ when '?'
146
+ if rand(2) > 0
147
+ ret << choose(candinates)
148
+ end
149
+ candinates = Tree.new
150
+ when '\\'
151
+ ret << choose(candinates)
152
+ escaped = tree.shift
153
+
154
+ if (escaped.match(/^[1-9]$/))
155
+ candinates = [ @reference[escaped.to_i] || '' ]
156
+ else
157
+ if (%w(b B).include?(escaped))
158
+ raise "\\b and \\B is not supported"
159
+ end
160
+ candinates = CLASSES[escaped]
161
+ end
162
+
163
+ unless candinates
164
+ candinates = Tree.new([escaped])
165
+ end
166
+ when '['
167
+ ret << choose(candinates)
168
+
169
+ sets = Tree.new
170
+ negative = false
171
+
172
+ while (char = tree.shift)
173
+ if (char == '\\')
174
+ next_char = tree.shift
175
+ if (CLASSES[next_char])
176
+ sets = sets + CLASSES[next_char]
177
+ else
178
+ sets.push(next_char)
179
+ end
180
+ elsif (char == ']')
181
+ break
182
+ elsif (char == '^')
183
+ before_char = sets.last
184
+ if (!before_char)
185
+ negative = true
186
+ else
187
+ sets.push(char)
188
+ end
189
+ elsif (char == '-')
190
+ next_char = tree.shift
191
+ if (next_char == ']')
192
+ sets.push(char)
193
+ char = next_char
194
+ break
195
+ end
196
+
197
+ before_char = sets.last
198
+ if (!before_char)
199
+ sets.push(char)
200
+ else
201
+ ((before_char.ord + 1)...(next_char.ord)).each do |i|
202
+ begin
203
+ sets.push(i.chr("UTF-8"))
204
+ rescue RangeError => e
205
+ warn e if ENV["DEBUG"]
206
+ end
207
+ end
208
+ end
209
+ else
210
+ sets.push(char)
211
+ end
212
+ end
213
+
214
+ if (char != ']')
215
+ raise "missmatch bracket: #{char}"
216
+ end
217
+
218
+ if (negative)
219
+ neg = {}
220
+ sets.each do |set|
221
+ neg[set] = true
222
+ end
223
+
224
+ candinates = Tree.new
225
+ ALL.each do |char|
226
+ candinates.push(char) unless neg[char]
227
+ end
228
+ else
229
+ candinates = sets
230
+ end
231
+ when '.'
232
+ ret << choose(candinates)
233
+ candinates = ALL
234
+ else
235
+ ret << choose(candinates)
236
+ candinates = char
237
+ end
238
+ end
239
+
240
+ return ret << choose(candinates)
241
+ end
242
+
243
+ def choose(candinates)
244
+ candinates = candinates.dup
245
+ candinates = candinates.chars if candinates.instance_of?(String)
246
+
247
+ if candinates.instance_of?(Pxeger::Tree) && multi_backword_reference?
248
+ ret = candinates.pick
249
+ else
250
+ ret = candinates.sample
251
+ end
252
+
253
+ if ((defined? ret) && ret.instance_of?(Pxeger::Tree))
254
+ ret = process_others(ret)
255
+ end
256
+
257
+ if (candinates.respond_to?(:num) && candinates.num)
258
+ @reference[candinates.num] = ret
259
+ end
260
+
261
+ return ret || ''
262
+ end
263
+ end
@@ -0,0 +1,29 @@
1
+ class Pxeger
2
+ private
3
+
4
+ UPPERS = ('A'..'Z').to_a
5
+ LOWERS = ('a'..'z').to_a
6
+ DIGITS = ('0'..'9').to_a
7
+ SPACES = [" ", "\n", "\t"]
8
+ OTHERS = [
9
+ '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*',
10
+ '+', ',', '-', '.', '/', ':', ';', '<', '=', '>',
11
+ '?', '@', '[', '\\', ']', '^', '`', '{', '|', '}', '~',
12
+ ]
13
+ ALL = UPPERS + LOWERS + DIGITS + [" "] + OTHERS + ["_"]
14
+
15
+ CLASSES = {
16
+ 'd' => DIGITS,
17
+ 'D' => UPPERS + LOWERS + SPACES + OTHERS + ['_'],
18
+ 'w' => UPPERS + LOWERS + DIGITS + ['_'],
19
+ 'W' => SPACES + OTHERS,
20
+ 't' => [ '\t' ],
21
+ 'n' => [ '\n' ],
22
+ 'v' => [ '\u000B' ],
23
+ 'f' => [ '\u000C' ],
24
+ 'r' => [ '\r' ],
25
+ 's' => SPACES,
26
+ 'S' => UPPERS + LOWERS + DIGITS + OTHERS + ['_'],
27
+ '0' => [ '\0' ]
28
+ }
29
+ end
@@ -0,0 +1,23 @@
1
+ class Pxeger
2
+ class Tree < Array
3
+ attr_accessor :num
4
+
5
+ def depth
6
+ return @depth if @depth
7
+ return @depth = 1 unless self.any? {|t| t.instance_of?(self.class)}
8
+ @depth = self.pick.depth + 1
9
+ end
10
+
11
+ def pick
12
+ return self.sample unless self.any? {|t| t.instance_of?(self.class)}
13
+
14
+ child_trees = self.select {|c| c.instance_of?(self.class)}
15
+
16
+ max = child_trees.max do |a, b|
17
+ a.depth <=> b.depth
18
+ end
19
+
20
+ Tree.new(child_trees.select {|t| t.depth == max.depth}.sample)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ class Pxeger
2
+ VERSION = "0.1.0"
3
+ end
@@ -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 'pxeger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pxeger"
8
+ spec.version = Pxeger::VERSION
9
+ spec.authors = ["Naoki Shimizu"]
10
+ spec.email = ["hcs0035@gmail.com"]
11
+
12
+ spec.summary = %q{Random string generator from regular expression}
13
+ spec.description = %q{Random String generator from regular expression}
14
+ spec.homepage = "https://github.com/deme0607/pxeger"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>= 2.0.0'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pxeger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Naoki Shimizu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-20 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ description: Random String generator from regular expression
56
+ email:
57
+ - hcs0035@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/pxeger.rb
72
+ - lib/pxeger/constants.rb
73
+ - lib/pxeger/tree.rb
74
+ - lib/pxeger/version.rb
75
+ - pxeger.gemspec
76
+ homepage: https://github.com/deme0607/pxeger
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.0.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Random string generator from regular expression
100
+ test_files: []