strrand_regex 0.0.1
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.
- checksums.yaml +15 -0
- data/lib/strrand_regex.rb +179 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmU0MDhkMzM0NjE3ZWFlYTc0MWEwOTM3YTE4MDNjMDk0N2I2MzliYg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
Nzk0MDczMjQwYmZjZTM1NzRmMzZlYzZmYTBkMzA1ZGRkMDgzMjU2MQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDEwYjlmMDU1YzMyMDYwYjhhOTUxZWNkM2RmZjc5YjkzNmYwZDRlZTVhMTI3
|
10
|
+
Y2ViZGY2NTEwMDBmZmNkZTgwYzYzMDMwNzVhODU4ZGJmNWMwNzMyZjM5NWUz
|
11
|
+
OTg4NmZkOWE5YTI5YjE2MDY2NTYzZmUxNTE3YjI2YWYxYzllMjI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NGY4MTE1ODYzN2E3NmNmMmEyNmVjNGU1MmI4NDY1ZDViZDQ1NzAzMTc1OTA0
|
14
|
+
YWE2MTZlMDE5NzlkZWE3MDhkYjU2N2I2MmUzNWFjNTA1MTI4MTNkZTYxNGI1
|
15
|
+
Y2IyY2UzN2FkZTRkZDVkMTc0ZTVjYjNlYjBiMTJjNWFiMTI0MmQ=
|
@@ -0,0 +1,179 @@
|
|
1
|
+
class StrrandRegex
|
2
|
+
Upper = Array('A'..'Z')
|
3
|
+
Lower = Array('a'..'z')
|
4
|
+
Digit = Array('0'..'9')
|
5
|
+
Punct = [33..47, 58..64, 91..96, 123..126].map { |r| r.map { |val| val.chr } }.flatten
|
6
|
+
Any = Upper | Lower | Digit | Punct
|
7
|
+
Salt = Upper | Lower | Digit | ['.', '/']
|
8
|
+
Binary = (0..255).map { |val| val.chr }
|
9
|
+
|
10
|
+
# These are the regex-based patterns.
|
11
|
+
Pattern = {
|
12
|
+
# These are the regex-equivalents.
|
13
|
+
'.' => Any,
|
14
|
+
'\d' => Digit,
|
15
|
+
'\D' => Upper | Lower | Punct,
|
16
|
+
'\w' => Upper | Lower | Digit | ['_'],
|
17
|
+
'\W' => Punct.reject { |val| val == '_' },
|
18
|
+
'\s' => [' ', "\t"],
|
19
|
+
'\S' => Upper | Lower | Digit | Punct,
|
20
|
+
|
21
|
+
# These are translated to their double quoted equivalents.
|
22
|
+
'\t' => ["\t"],
|
23
|
+
'\n' => ["\n"],
|
24
|
+
'\r' => ["\r"],
|
25
|
+
'\f' => ["\f"],
|
26
|
+
'\a' => ["\a"],
|
27
|
+
'\e' => ["\e"]
|
28
|
+
}
|
29
|
+
|
30
|
+
#
|
31
|
+
# Singleton method version of random_regex.
|
32
|
+
#
|
33
|
+
def self.random_regex(patterns)
|
34
|
+
StrrandRegex.new.random_regex(patterns)
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# _max_ is default length for creating random string
|
39
|
+
#
|
40
|
+
def initialize(max = 10)
|
41
|
+
@max = max
|
42
|
+
@regch = {
|
43
|
+
"\\" => method(:regch_slash),
|
44
|
+
'.' => method(:regch_dot),
|
45
|
+
'[' => method(:regch_bracket),
|
46
|
+
'*' => method(:regch_asterisk),
|
47
|
+
'+' => method(:regch_plus),
|
48
|
+
'?' => method(:regch_question),
|
49
|
+
'{' => method(:regch_brace)
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Returns a random string that will match
|
55
|
+
# the regular expression passed in the list argument.
|
56
|
+
#
|
57
|
+
def random_regex(patterns)
|
58
|
+
return _random_regex(patterns) unless patterns.instance_of?(Array)
|
59
|
+
|
60
|
+
result = []
|
61
|
+
patterns.each do |pattern|
|
62
|
+
result << _random_regex(pattern)
|
63
|
+
end
|
64
|
+
result
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def _random_regex(pattern)
|
70
|
+
string = []
|
71
|
+
string_result = []
|
72
|
+
chars = pattern.split(//)
|
73
|
+
non_ch = /[\$\^\*\(\)\+\{\}\]\|\?]/ # not supported chars
|
74
|
+
|
75
|
+
while ch = chars.shift
|
76
|
+
if @regch.has_key?(ch)
|
77
|
+
@regch[ch].call(ch, chars, string)
|
78
|
+
else
|
79
|
+
warn "'#{ch}' not implemented. treating literally." if ch =~ non_ch
|
80
|
+
string << [ch]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
result = ''
|
85
|
+
string.each do |ch|
|
86
|
+
result << ch[rand(ch.size)]
|
87
|
+
end
|
88
|
+
result.chars.to_a.shuffle.join
|
89
|
+
end
|
90
|
+
|
91
|
+
#-
|
92
|
+
# The folloing methods are defined for regch.
|
93
|
+
# These characters are treated specially in random_regex.
|
94
|
+
#+
|
95
|
+
|
96
|
+
def regch_slash(ch, chars, string)
|
97
|
+
raise 'regex not terminated' if chars.empty?
|
98
|
+
|
99
|
+
tmp = chars.shift
|
100
|
+
if tmp == 'x'
|
101
|
+
# This is supposed to be a number in hex, so
|
102
|
+
# there had better be at least 2 characters left.
|
103
|
+
tmp = chars.shift + chars.shift
|
104
|
+
string << tmp.hex.chr
|
105
|
+
elsif tmp =~ /[0-7]/
|
106
|
+
warn 'octal parsing not implemented. treating literally.'
|
107
|
+
string << tmp
|
108
|
+
elsif Pattern.has_key?(ch + tmp)
|
109
|
+
string << Pattern[ch + tmp]
|
110
|
+
else
|
111
|
+
warn "'\\#{tmp}' being treated as literal '#{tmp}'"
|
112
|
+
string << tmp
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def regch_dot(ch, chars, string)
|
117
|
+
string << Pattern[ch]
|
118
|
+
end
|
119
|
+
|
120
|
+
def regch_bracket(ch, chars, string)
|
121
|
+
tmp = []
|
122
|
+
|
123
|
+
while ch = chars.shift and ch != ']'
|
124
|
+
if ch == '-' and !chars.empty? and !tmp.empty?
|
125
|
+
max = chars.shift
|
126
|
+
min = tmp.last
|
127
|
+
tmp << min = min.succ while min < max
|
128
|
+
else
|
129
|
+
warn "${ch}' will be treated literally inside []" if ch =~ /\W/
|
130
|
+
tmp << ch
|
131
|
+
end
|
132
|
+
end
|
133
|
+
raise 'unmatched []' if ch != ']'
|
134
|
+
|
135
|
+
string << tmp
|
136
|
+
end
|
137
|
+
|
138
|
+
def regch_asterisk(ch, chars, string)
|
139
|
+
chars = '{0,}'.split('').concat(chars)
|
140
|
+
end
|
141
|
+
|
142
|
+
def regch_plus(ch, chars, string)
|
143
|
+
chars = '{1,}'.split('').concat(chars)
|
144
|
+
end
|
145
|
+
|
146
|
+
def regch_question(ch, chars, string)
|
147
|
+
chars = '{0,1}'.split('').concat(chars)
|
148
|
+
end
|
149
|
+
|
150
|
+
def regch_brace(ch, chars, string)
|
151
|
+
# { isn't closed, so treat it literally.
|
152
|
+
return string << ch unless chars.include?('}')
|
153
|
+
|
154
|
+
tmp = ''
|
155
|
+
while ch = chars.shift and ch != '}'
|
156
|
+
raise "'#{ch}' inside {} not supported" unless ch =~ /[\d,]/
|
157
|
+
tmp << ch
|
158
|
+
end
|
159
|
+
|
160
|
+
tmp = if tmp =~ /,/
|
161
|
+
raise "malformed range {#{tmp}}" unless tmp =~ /^(\d*),(\d*)$/
|
162
|
+
|
163
|
+
min = $1.length.nonzero? ? $1.to_i : 0
|
164
|
+
max = $2.length.nonzero? ? $2.to_i : @max
|
165
|
+
raise "bad range {#{tmp}}" if min > max
|
166
|
+
|
167
|
+
min == max ? min : min + rand(max - min + 1)
|
168
|
+
else
|
169
|
+
tmp.to_i
|
170
|
+
end
|
171
|
+
|
172
|
+
if tmp.nonzero?
|
173
|
+
last = string.last
|
174
|
+
(tmp - 1).times { string << last }
|
175
|
+
else
|
176
|
+
string.pop
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strrand_regex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Alves Lobo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Generate a string with a regexp
|
14
|
+
email: alveslobo.michael@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/strrand_regex.rb
|
20
|
+
homepage: https://github.com/kairel/strrand_regex
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Random string with regexp
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|