string_pattern 2.1.0 → 2.1.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.
@@ -1,294 +1,294 @@
1
- class Array
2
- # It will generate an string following the pattern specified
3
- # The positions with string patterns need to be supplied like symbols:
4
- # [:"10:N", "fixed", :"10-20:XN/x/"].generate #> "1024320001fixed4OZjNMTnuBibwwj"
5
- def generate(expected_errors: [], **synonyms)
6
- StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
7
- end
8
-
9
- alias gen generate
10
-
11
- # it will validate an string following the pattern specified
12
- def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
13
- StringPattern.validate(text: string_to_validate, pattern: self, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
14
- end
15
-
16
- alias val validate
17
- end
18
-
19
- class String
20
- # it will generate an string following the pattern specified
21
- def generate(expected_errors: [], **synonyms)
22
- StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
23
- end
24
-
25
- alias gen generate
26
-
27
- # it will validate an string following the pattern specified
28
- def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
29
- StringPattern.validate(text: string_to_validate, pattern: self, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
30
- end
31
-
32
- alias val validate
33
-
34
- ########################################################
35
- # Convert to CamelCase a string
36
- ########################################################
37
- def to_camel_case
38
- return self if self !~ /_/ && self !~ /\s/ && self =~ /[A-Z]+.*/
39
-
40
- gsub(/\W/, '_')
41
- .split('_').map(&:capitalize).join
42
- end
43
- end
44
-
45
- class Symbol
46
- # it will generate an string following the pattern specified
47
- def generate(expected_errors: [], **synonyms)
48
- StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
49
- end
50
-
51
- alias gen generate
52
-
53
- # it will validate an string following the pattern specified
54
- def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
55
- StringPattern.validate(text: string_to_validate, pattern: to_s, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
56
- end
57
-
58
- alias val validate
59
- end
60
-
61
- class Regexp
62
-
63
- # it will generate an string following the pattern specified
64
- def generate(expected_errors: [], **synonyms)
65
- StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
66
- end
67
-
68
- alias gen generate
69
-
70
- # adds method to convert a Regexp to StringPattern
71
- # returns an array of string patterns or just one string pattern
72
- def to_sp
73
- regexp_s = self.to_s
74
- return StringPattern.cache[regexp_s] unless StringPattern.cache[regexp_s].nil?
75
- regexp = Regexp.new regexp_s
76
- require 'regexp_parser'
77
- default_infinite = StringPattern.default_infinite
78
- pata = []
79
- pats = ''
80
- patg = [] # for (aa|bb|cc) group
81
- set = false
82
- capture = false
83
-
84
- range = ''
85
- fixed_text=false
86
- last_char = (regexp.to_s.gsub("?-mix:",'').length)-2
87
- Regexp::Scanner.scan regexp do |type, token, text, ts, te|
88
- if type == :escape
89
- if token == :dot
90
- token = :literal
91
- text = '.'
92
- elsif token == :literal and text.size == 2
93
- text = text[1]
94
- else
95
- puts "Report token not controlled: type: #{type}, token: #{token}, text: '#{text}' [#{ts}..#{te}]"
96
- end
97
- end
98
-
99
-
100
- unless set || (token == :interval) || (token == :zero_or_one) ||
101
- (token == :zero_or_more) || (token == :one_or_more) || (pats == '')
102
- if (pats[0] == '[') && (pats[-1] == ']')
103
- pats[0] = ''
104
- if (token == :alternation) || !patg.empty?
105
- if fixed_text
106
- if patg.size==0
107
- patg << (pata.pop + pats.chop)
108
- else
109
- patg[-1] += pats.chop
110
- end
111
- else
112
- patg << pats.chop
113
- end
114
- else
115
- if fixed_text
116
- pata[-1]+=pats.chop
117
- else
118
- if pats.size==2
119
- pata << pats.chop #jal
120
- else
121
- pata << "1:[#{pats}" #jal
122
- end
123
- if last_char==te and type==:literal and token==:literal
124
- pata << text
125
- pats = ""
126
- next
127
- end
128
- end
129
- end
130
- else
131
- if (token == :alternation) || !patg.empty?
132
- patg << "1:#{pats}"
133
- else
134
- pata << "1:#{pats}"
135
- end
136
- end
137
- pats = ''
138
- end
139
- fixed_text=false
140
-
141
- case token
142
- when :open
143
- set = true
144
- pats += '['
145
- when :close
146
- if type == :set
147
- set = false
148
- if pats[-1] == '['
149
- pats.chop!
150
- else
151
- pats += ']'
152
- end
153
- elsif type == :group
154
- capture = false
155
- unless patg.empty?
156
- patg << pats if pats.to_s != ''
157
- pata << patg
158
- patg = []
159
- pats = ''
160
- end
161
- end
162
- when :capture
163
- capture = true if type == :group
164
- when :alternation
165
- if type == :meta
166
- if pats != ''
167
- patg << pats
168
- pats = ''
169
- elsif patg.empty?
170
- # for the case the first element was not added to patg and was on pata fex: (a+|b|c)
171
- patg << pata.pop
172
- end
173
- end
174
- when :range
175
- range = pats[-1]
176
- pats.chop!
177
- when :digit
178
- pats += 'n'
179
- when :nondigit
180
- pats += '*[%0123456789%]'
181
- when :space
182
- pats += '_'
183
- when :nonspace
184
- pats += '*[% %]'
185
- when :word
186
- pats += 'Ln_'
187
- when :nonword
188
- pats += '$'
189
- when :word_boundary
190
- pats += '$'
191
- when :dot
192
- pats += '*'
193
- when :literal
194
- if range == ''
195
- if text.size > 1
196
- fixed_text=true
197
- if !patg.empty?
198
- patg << text.chop
199
- else
200
- pata << text.chop
201
- end
202
- pats = text[-1]
203
- else
204
- pats += text
205
- end
206
- else
207
- range = range + '-' + text
208
- if range == 'a-z'
209
- pats = 'x' + pats
210
- elsif range == 'A-Z'
211
- pats = 'X' + pats
212
- elsif range == '0-9'
213
- pats = 'n' + pats
214
- else
215
- pats += if set
216
- (range[0]..range[2]).to_a.join
217
- else
218
- '[' + (range[0]..range[2]).to_a.join + ']'
219
- end
220
-
221
- end
222
- range = ''
223
- end
224
- pats = '[' + pats + ']' unless set
225
- when :interval
226
- size = text.sub(',', '-').sub('{', '').sub('}', '')
227
- size.chop! if size[-1] == '-'
228
- pats = size + ':' + pats
229
- if !patg.empty?
230
- patg << pats
231
- else
232
- pata << pats
233
- end
234
- pats = ''
235
- when :zero_or_one
236
- pats = '0-1:' + pats
237
- if !patg.empty?
238
- patg << pats
239
- else
240
- pata << pats
241
- end
242
- pats = ''
243
- when :zero_or_more
244
- pats = "0-#{default_infinite}:" + pats
245
- if !patg.empty?
246
- patg << pats
247
- else
248
- pata << pats
249
- end
250
- pats = ''
251
- when :one_or_more
252
- pats = "1-#{default_infinite}:" + pats
253
- if !patg.empty?
254
- patg << pats
255
- else
256
- pata << pats
257
- end
258
- pats = ''
259
- end
260
- end
261
- if pats!=""
262
- if pata.empty?
263
- if pats[0]=="[" and pats[-1]=="]" #fex: /[12ab]/
264
- pata = ["1:#{pats}"]
265
- end
266
- else
267
- pata[-1]+=pats[1] #fex: /allo/
268
- end
269
- end
270
- if pata.size==1 and pata[0].kind_of?(String)
271
- res = pata[0]
272
- else
273
- res = pata
274
- end
275
- StringPattern.cache[regexp_s] = res
276
- return res
277
- end
278
- end
279
-
280
- module Kernel
281
- public
282
-
283
- # if string or symbol supplied it will generate a string with the supplied pattern specified on the string
284
- # if array supplied then it will generate a string with the supplied patterns. If a position contains a pattern supply it as symbol, for example: [:"10:N", "fixed", :"10-20:XN/x/"]
285
- def generate(pattern, expected_errors: [], **synonyms)
286
- if pattern.is_a?(String) || pattern.is_a?(Array) || pattern.is_a?(Symbol) || pattern.is_a?(Regexp)
287
- StringPattern.generate(pattern, expected_errors: expected_errors, **synonyms)
288
- else
289
- puts " Kernel generate method: class not recognized:#{pattern.class}"
290
- end
291
- end
292
-
293
- alias gen generate
294
- end
1
+ class Array
2
+ # It will generate an string following the pattern specified
3
+ # The positions with string patterns need to be supplied like symbols:
4
+ # [:"10:N", "fixed", :"10-20:XN/x/"].generate #> "1024320001fixed4OZjNMTnuBibwwj"
5
+ def generate(expected_errors: [], **synonyms)
6
+ StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
7
+ end
8
+
9
+ alias gen generate
10
+
11
+ # it will validate an string following the pattern specified
12
+ def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
13
+ StringPattern.validate(text: string_to_validate, pattern: self, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
14
+ end
15
+
16
+ alias val validate
17
+ end
18
+
19
+ class String
20
+ # it will generate an string following the pattern specified
21
+ def generate(expected_errors: [], **synonyms)
22
+ StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
23
+ end
24
+
25
+ alias gen generate
26
+
27
+ # it will validate an string following the pattern specified
28
+ def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
29
+ StringPattern.validate(text: string_to_validate, pattern: self, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
30
+ end
31
+
32
+ alias val validate
33
+
34
+ ########################################################
35
+ # Convert to CamelCase a string
36
+ ########################################################
37
+ def to_camel_case
38
+ return self if self !~ /_/ && self !~ /\s/ && self =~ /[A-Z]+.*/
39
+
40
+ gsub(/\W/, '_')
41
+ .split('_').map(&:capitalize).join
42
+ end
43
+ end
44
+
45
+ class Symbol
46
+ # it will generate an string following the pattern specified
47
+ def generate(expected_errors: [], **synonyms)
48
+ StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
49
+ end
50
+
51
+ alias gen generate
52
+
53
+ # it will validate an string following the pattern specified
54
+ def validate(string_to_validate, expected_errors: [], not_expected_errors: [], **synonyms)
55
+ StringPattern.validate(text: string_to_validate, pattern: to_s, expected_errors: expected_errors, not_expected_errors: not_expected_errors, **synonyms)
56
+ end
57
+
58
+ alias val validate
59
+ end
60
+
61
+ class Regexp
62
+
63
+ # it will generate an string following the pattern specified
64
+ def generate(expected_errors: [], **synonyms)
65
+ StringPattern.generate(self, expected_errors: expected_errors, **synonyms)
66
+ end
67
+
68
+ alias gen generate
69
+
70
+ # adds method to convert a Regexp to StringPattern
71
+ # returns an array of string patterns or just one string pattern
72
+ def to_sp
73
+ regexp_s = self.to_s
74
+ return StringPattern.cache[regexp_s] unless StringPattern.cache[regexp_s].nil?
75
+ regexp = Regexp.new regexp_s
76
+ require 'regexp_parser'
77
+ default_infinite = StringPattern.default_infinite
78
+ pata = []
79
+ pats = ''
80
+ patg = [] # for (aa|bb|cc) group
81
+ set = false
82
+ capture = false
83
+
84
+ range = ''
85
+ fixed_text=false
86
+ last_char = (regexp.to_s.gsub("?-mix:",'').length)-2
87
+ Regexp::Scanner.scan regexp do |type, token, text, ts, te|
88
+ if type == :escape
89
+ if token == :dot
90
+ token = :literal
91
+ text = '.'
92
+ elsif token == :literal and text.size == 2
93
+ text = text[1]
94
+ else
95
+ puts "Report token not controlled: type: #{type}, token: #{token}, text: '#{text}' [#{ts}..#{te}]"
96
+ end
97
+ end
98
+
99
+
100
+ unless set || (token == :interval) || (token == :zero_or_one) ||
101
+ (token == :zero_or_more) || (token == :one_or_more) || (pats == '')
102
+ if (pats[0] == '[') && (pats[-1] == ']')
103
+ pats[0] = ''
104
+ if (token == :alternation) || !patg.empty?
105
+ if fixed_text
106
+ if patg.size==0
107
+ patg << (pata.pop + pats.chop)
108
+ else
109
+ patg[-1] += pats.chop
110
+ end
111
+ else
112
+ patg << pats.chop
113
+ end
114
+ else
115
+ if fixed_text
116
+ pata[-1]+=pats.chop
117
+ else
118
+ if pats.size==2
119
+ pata << pats.chop #jal
120
+ else
121
+ pata << "1:[#{pats}" #jal
122
+ end
123
+ if last_char==te and type==:literal and token==:literal
124
+ pata << text
125
+ pats = ""
126
+ next
127
+ end
128
+ end
129
+ end
130
+ else
131
+ if (token == :alternation) || !patg.empty?
132
+ patg << "1:#{pats}"
133
+ else
134
+ pata << "1:#{pats}"
135
+ end
136
+ end
137
+ pats = ''
138
+ end
139
+ fixed_text=false
140
+
141
+ case token
142
+ when :open
143
+ set = true
144
+ pats += '['
145
+ when :close
146
+ if type == :set
147
+ set = false
148
+ if pats[-1] == '['
149
+ pats.chop!
150
+ else
151
+ pats += ']'
152
+ end
153
+ elsif type == :group
154
+ capture = false
155
+ unless patg.empty?
156
+ patg << pats if pats.to_s != ''
157
+ pata << patg
158
+ patg = []
159
+ pats = ''
160
+ end
161
+ end
162
+ when :capture
163
+ capture = true if type == :group
164
+ when :alternation
165
+ if type == :meta
166
+ if pats != ''
167
+ patg << pats
168
+ pats = ''
169
+ elsif patg.empty?
170
+ # for the case the first element was not added to patg and was on pata fex: (a+|b|c)
171
+ patg << pata.pop
172
+ end
173
+ end
174
+ when :range
175
+ range = pats[-1]
176
+ pats.chop!
177
+ when :digit
178
+ pats += 'n'
179
+ when :nondigit
180
+ pats += '*[%0123456789%]'
181
+ when :space
182
+ pats += '_'
183
+ when :nonspace
184
+ pats += '*[% %]'
185
+ when :word
186
+ pats += 'Ln_'
187
+ when :nonword
188
+ pats += '$'
189
+ when :word_boundary
190
+ pats += '$'
191
+ when :dot
192
+ pats += '*'
193
+ when :literal
194
+ if range == ''
195
+ if text.size > 1
196
+ fixed_text=true
197
+ if !patg.empty?
198
+ patg << text.chop
199
+ else
200
+ pata << text.chop
201
+ end
202
+ pats = text[-1]
203
+ else
204
+ pats += text
205
+ end
206
+ else
207
+ range = range + '-' + text
208
+ if range == 'a-z'
209
+ pats = 'x' + pats
210
+ elsif range == 'A-Z'
211
+ pats = 'X' + pats
212
+ elsif range == '0-9'
213
+ pats = 'n' + pats
214
+ else
215
+ pats += if set
216
+ (range[0]..range[2]).to_a.join
217
+ else
218
+ '[' + (range[0]..range[2]).to_a.join + ']'
219
+ end
220
+
221
+ end
222
+ range = ''
223
+ end
224
+ pats = '[' + pats + ']' unless set
225
+ when :interval
226
+ size = text.sub(',', '-').sub('{', '').sub('}', '')
227
+ size.chop! if size[-1] == '-'
228
+ pats = size + ':' + pats
229
+ if !patg.empty?
230
+ patg << pats
231
+ else
232
+ pata << pats
233
+ end
234
+ pats = ''
235
+ when :zero_or_one
236
+ pats = '0-1:' + pats
237
+ if !patg.empty?
238
+ patg << pats
239
+ else
240
+ pata << pats
241
+ end
242
+ pats = ''
243
+ when :zero_or_more
244
+ pats = "0-#{default_infinite}:" + pats
245
+ if !patg.empty?
246
+ patg << pats
247
+ else
248
+ pata << pats
249
+ end
250
+ pats = ''
251
+ when :one_or_more
252
+ pats = "1-#{default_infinite}:" + pats
253
+ if !patg.empty?
254
+ patg << pats
255
+ else
256
+ pata << pats
257
+ end
258
+ pats = ''
259
+ end
260
+ end
261
+ if pats!=""
262
+ if pata.empty?
263
+ if pats[0]=="[" and pats[-1]=="]" #fex: /[12ab]/
264
+ pata = ["1:#{pats}"]
265
+ end
266
+ else
267
+ pata[-1]+=pats[1] #fex: /allo/
268
+ end
269
+ end
270
+ if pata.size==1 and pata[0].kind_of?(String)
271
+ res = pata[0]
272
+ else
273
+ res = pata
274
+ end
275
+ StringPattern.cache[regexp_s] = res
276
+ return res
277
+ end
278
+ end
279
+
280
+ module Kernel
281
+ public
282
+
283
+ # if string or symbol supplied it will generate a string with the supplied pattern specified on the string
284
+ # if array supplied then it will generate a string with the supplied patterns. If a position contains a pattern supply it as symbol, for example: [:"10:N", "fixed", :"10-20:XN/x/"]
285
+ def generate(pattern, expected_errors: [], **synonyms)
286
+ if pattern.is_a?(String) || pattern.is_a?(Array) || pattern.is_a?(Symbol) || pattern.is_a?(Regexp)
287
+ StringPattern.generate(pattern, expected_errors: expected_errors, **synonyms)
288
+ else
289
+ puts " Kernel generate method: class not recognized:#{pattern.class}"
290
+ end
291
+ end
292
+
293
+ alias gen generate
294
+ end