string_pattern 1.1.2 → 1.2.0
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 +4 -4
- data/.yardopts +4 -4
- data/README.md +20 -21
- data/lib/string_pattern.rb +111 -90
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 745a8f7a9278e7d26e8a33a1f8e90e55bcf0808ef4230499aba861f3a7b035ae
|
4
|
+
data.tar.gz: da00480f45da0b8446a98be85089e8d14f97430a9be2a85398a846cb00bfc79c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d86438c102dd0b413fd5dd2a04fa0f9cf1c47f4802ce835cd79689e328663ad3528eb0d31c9be4ab9cbc5a8b470dc9ba2bee1dbb4730c543c35be5b3a5c34be9
|
7
|
+
data.tar.gz: c86bc81b7513a4ae6c06993e185d88017c1aef1dd29b1fd2ce909e0e81f619dc09357b1177277ba092c3024110fdc5f45c3484f1e0384c382414d9ba4c1224b7
|
data/.yardopts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
--readme README.md
|
2
|
-
--title 'string_pattern - You can easily generate strings supplying a very simple pattern. Also you can validate if a text fulfill an specific pattern or even generate a string following a pattern and returning wrong length, value... for testing your applications.'
|
3
|
-
--charset utf-8
|
4
|
-
--markup markdown
|
1
|
+
--readme README.md
|
2
|
+
--title 'string_pattern - You can easily generate strings supplying a very simple pattern. Also you can validate if a text fulfill an specific pattern or even generate a string following a pattern and returning wrong length, value... for testing your applications.'
|
3
|
+
--charset utf-8
|
4
|
+
--markup markdown
|
5
5
|
'lib/**/*.rb' - '*.md' - 'LICENSE'
|
data/README.md
CHANGED
@@ -48,32 +48,31 @@ A pattern is a string where we supply these elements "a-b:c" where a is min_leng
|
|
48
48
|
To generate a string following a pattern you can do it using directly the StringPattern class or the generate method in the class, be aware you can always use also the alias method: gen
|
49
49
|
|
50
50
|
```ruby
|
51
|
+
require 'string_pattern'
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
p StringPattern.gen "5:X"
|
58
|
-
#>JDDDK
|
53
|
+
#StringPattern class
|
54
|
+
p StringPattern.generate "10:N"
|
55
|
+
#>3448910834
|
56
|
+
p StringPattern.gen "5:X"
|
57
|
+
#>JDDDK
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
#String class
|
60
|
+
p "4:Nx".gen
|
61
|
+
#>xaa3
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
#Symbol class
|
64
|
+
p :"10:T".generate
|
65
|
+
#>AccBdjklñD
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
#Array class
|
68
|
+
p [:"3:N", "fixed", :"3:N"].gen
|
69
|
+
#>334fixed920
|
70
|
+
p "(,3:N,) ,3:N,-,2:N,-,2:N".split(',').generate
|
71
|
+
#>(937) 980-65-05
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
|
73
|
+
#Kernel
|
74
|
+
p gen "3:N"
|
75
|
+
#>443
|
77
76
|
```
|
78
77
|
|
79
78
|
#### Custom characters
|
data/lib/string_pattern.rb
CHANGED
@@ -18,10 +18,11 @@ require_relative 'string/pattern/add_to_ruby' if SP_ADD_TO_RUBY
|
|
18
18
|
# If true it will check on the strings of the array positions if they have the pattern format and assume in that case that is a pattern.
|
19
19
|
class StringPattern
|
20
20
|
class << self
|
21
|
-
attr_accessor :national_chars, :optimistic
|
21
|
+
attr_accessor :national_chars, :optimistic, :dont_repeat, :cache
|
22
22
|
end
|
23
23
|
@national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join
|
24
24
|
@optimistic = true
|
25
|
+
@cache = Hash.new()
|
25
26
|
NUMBER_SET = ('0'..'9').to_a
|
26
27
|
SPECIAL_SET = [' ', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', "'", ';', ':', '?', '>', '<', '`', '|', '/', '"']
|
27
28
|
ALPHA_SET_LOWER = ('a'..'z').to_a
|
@@ -36,6 +37,7 @@ class StringPattern
|
|
36
37
|
# min_length, max_length, symbol_type, required_data, excluded_data, data_provided, string_set, all_characters_set
|
37
38
|
###############################################
|
38
39
|
def StringPattern.analyze(pattern, silent:false)
|
40
|
+
return @cache[pattern.to_s] unless @cache[pattern.to_s].nil?
|
39
41
|
min_length, max_length, symbol_type=pattern.to_s.scan(/(\d+)-(\d+):(.+)/)[0]
|
40
42
|
if min_length.nil?
|
41
43
|
min_length, symbol_type=pattern.to_s.scan(/^!?(\d+):(.+)/)[0]
|
@@ -45,6 +47,7 @@ class StringPattern
|
|
45
47
|
return pattern.to_s
|
46
48
|
end
|
47
49
|
end
|
50
|
+
|
48
51
|
symbol_type='!'+symbol_type if pattern.to_s[0]=='!'
|
49
52
|
min_length=min_length.to_i
|
50
53
|
max_length=max_length.to_i
|
@@ -57,7 +60,7 @@ class StringPattern
|
|
57
60
|
a=symbol_type
|
58
61
|
begin_provided=a.index('[')
|
59
62
|
excluded_end_tag=false
|
60
|
-
|
63
|
+
unless begin_provided.nil?
|
61
64
|
c=begin_provided+1
|
62
65
|
until c==a.size or (a[c..c]==']' and a[c..c+1]!=']]')
|
63
66
|
if a[c..c+1]==']]'
|
@@ -117,93 +120,97 @@ class StringPattern
|
|
117
120
|
|
118
121
|
required=false
|
119
122
|
required_symbol=''
|
120
|
-
symbol_type.
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
123
|
+
if symbol_type.include?("/")
|
124
|
+
symbol_type.chars.each {|stc|
|
125
|
+
if stc=='/'
|
126
|
+
if !required
|
127
|
+
required=true
|
128
|
+
else
|
129
|
+
required=false
|
130
|
+
end
|
131
|
+
else
|
132
|
+
if required
|
133
|
+
required_symbol+=stc
|
134
|
+
end
|
135
|
+
end
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
national_set=@national_chars.chars
|
140
|
+
|
141
|
+
if symbol_type.include?('L') then
|
142
|
+
alpha_set = ALPHA_SET_LOWER + ALPHA_SET_CAPITAL
|
143
|
+
elsif symbol_type.include?('x')
|
136
144
|
alpha_set=ALPHA_SET_LOWER
|
137
|
-
|
145
|
+
if symbol_type.include?('X')
|
138
146
|
alpha_set = alpha_set + ALPHA_SET_CAPITAL
|
139
147
|
end
|
140
|
-
|
141
|
-
|
142
|
-
alpha_set=ALPHA_SET_CAPITAL
|
143
|
-
elsif !symbol_type['L'].nil? then
|
144
|
-
alpha_set = ALPHA_SET_LOWER + ALPHA_SET_CAPITAL
|
145
|
-
else
|
146
|
-
alpha_set = ALPHA_SET_LOWER + ALPHA_SET_CAPITAL
|
147
|
-
end
|
148
|
+
elsif symbol_type.include?('X')
|
149
|
+
alpha_set=ALPHA_SET_CAPITAL
|
148
150
|
end
|
149
|
-
|
151
|
+
if symbol_type.include?('T')
|
150
152
|
alpha_set=alpha_set+national_set
|
151
153
|
end
|
152
154
|
|
153
|
-
unless required_symbol
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
155
|
+
unless required_symbol.nil?
|
156
|
+
if required_symbol.include?('x')
|
157
|
+
required_data.push ALPHA_SET_LOWER
|
158
|
+
end
|
159
|
+
if required_symbol.include?('X')
|
160
|
+
required_data.push ALPHA_SET_CAPITAL
|
161
|
+
end
|
162
|
+
if required_symbol.include?('L')
|
163
|
+
required_data.push(ALPHA_SET_CAPITAL+ALPHA_SET_LOWER)
|
164
|
+
end
|
165
|
+
if required_symbol.include?('T')
|
166
|
+
required_data.push national_set
|
167
|
+
end
|
168
|
+
required_symbol=required_symbol.downcase
|
169
|
+
end
|
166
170
|
string_set=Array.new
|
167
171
|
all_characters_set=ALPHA_SET_CAPITAL+ALPHA_SET_LOWER+NUMBER_SET+SPECIAL_SET+data_provided+national_set
|
168
172
|
|
169
|
-
|
170
|
-
|
173
|
+
if symbol_type.include?('_')
|
174
|
+
unless symbol_type.include?('$')
|
171
175
|
string_set.push(' ')
|
172
176
|
end
|
173
|
-
|
177
|
+
if required_symbol.include?('_')
|
174
178
|
required_data.push([' '])
|
175
179
|
end
|
176
180
|
end
|
177
181
|
|
178
182
|
symbol_type = symbol_type.downcase
|
179
183
|
|
180
|
-
if
|
184
|
+
if symbol_type.include?('x') or symbol_type.include?('l') or symbol_type.include?('t')
|
181
185
|
string_set = string_set + alpha_set
|
182
186
|
end
|
183
|
-
|
187
|
+
if symbol_type.include?('n')
|
184
188
|
string_set = string_set + NUMBER_SET
|
185
189
|
end
|
186
|
-
|
190
|
+
if symbol_type.include?('$')
|
187
191
|
string_set = string_set + SPECIAL_SET
|
188
192
|
end
|
189
|
-
|
193
|
+
if symbol_type.include?('*')
|
190
194
|
string_set = string_set+all_characters_set
|
191
195
|
end
|
192
196
|
if data_provided.size!=0
|
193
197
|
string_set = string_set + data_provided
|
194
198
|
end
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
199
|
+
unless required_symbol.empty?
|
200
|
+
if required_symbol.include?('n')
|
201
|
+
required_data.push NUMBER_SET
|
202
|
+
end
|
203
|
+
if required_symbol.include?('$')
|
204
|
+
required_data.push SPECIAL_SET
|
205
|
+
end
|
206
|
+
end
|
207
|
+
unless excluded_data.empty?
|
202
208
|
string_set=string_set-excluded_data.flatten
|
203
209
|
end
|
204
210
|
string_set.uniq!
|
205
|
-
|
211
|
+
@cache[pattern.to_s]=Pattern.new(min_length, max_length, symbol_type, required_data, excluded_data, data_provided,
|
206
212
|
string_set, all_characters_set)
|
213
|
+
return @cache[pattern.to_s]
|
207
214
|
end
|
208
215
|
|
209
216
|
###############################################
|
@@ -273,7 +280,7 @@ class StringPattern
|
|
273
280
|
if expected_errors.kind_of?(Symbol)
|
274
281
|
expected_errors=[expected_errors]
|
275
282
|
end
|
276
|
-
|
283
|
+
|
277
284
|
if pattern.kind_of?(Array)
|
278
285
|
pattern.each {|pat|
|
279
286
|
if pat.kind_of?(Symbol)
|
@@ -295,7 +302,7 @@ class StringPattern
|
|
295
302
|
}
|
296
303
|
return string
|
297
304
|
elsif pattern.kind_of?(String) or pattern.kind_of?(Symbol)
|
298
|
-
|
305
|
+
patt=StringPattern.analyze(pattern)
|
299
306
|
min_length=patt.min_length
|
300
307
|
max_length=patt.max_length
|
301
308
|
symbol_type=patt.symbol_type
|
@@ -305,23 +312,27 @@ class StringPattern
|
|
305
312
|
string_set=patt.string_set
|
306
313
|
all_characters_set=patt.all_characters_set
|
307
314
|
|
308
|
-
|
309
|
-
required_data.
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
315
|
+
required_chars=Array.new
|
316
|
+
unless required_data.size==0
|
317
|
+
required_data.each {|rd|
|
318
|
+
required_chars<<rd if rd.size==1
|
319
|
+
}
|
320
|
+
unless excluded_data.size==0
|
321
|
+
if (required_chars.flatten & excluded_data.flatten).size>0
|
322
|
+
puts "pattern argument not valid on StringPattern.generate, a character cannot be required and excluded at the same time: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
323
|
+
return ''
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
string_set_not_allowed=Array.new
|
318
328
|
|
319
329
|
else
|
320
330
|
puts "pattern argument not valid on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
321
331
|
return pattern.to_s
|
322
332
|
end
|
323
333
|
|
324
|
-
|
334
|
+
|
335
|
+
allow_empty=false
|
325
336
|
deny_pattern=false
|
326
337
|
if symbol_type[0..0]=='!'
|
327
338
|
deny_pattern=true
|
@@ -336,6 +347,7 @@ class StringPattern
|
|
336
347
|
elsif symbol_type[0..0]=='0' then
|
337
348
|
allow_empty=true
|
338
349
|
end
|
350
|
+
|
339
351
|
if expected_errors.include?(:min_length) or expected_errors.include?(:length) or
|
340
352
|
expected_errors.include?(:max_length)
|
341
353
|
allow_empty=!allow_empty
|
@@ -352,8 +364,8 @@ class StringPattern
|
|
352
364
|
expected_errors_left=expected_errors.dup
|
353
365
|
|
354
366
|
symbol_type=symbol_type_orig
|
355
|
-
|
356
|
-
|
367
|
+
|
368
|
+
unless deny_pattern
|
357
369
|
if required_data.size==0 and expected_errors_left.include?(:required_data)
|
358
370
|
puts "required data not supplied on pattern so it won't be possible to generate a wrong string. StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
359
371
|
return ''
|
@@ -364,9 +376,12 @@ class StringPattern
|
|
364
376
|
return ''
|
365
377
|
end
|
366
378
|
|
367
|
-
if
|
368
|
-
|
369
|
-
|
379
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
380
|
+
string_set_not_allowed=all_characters_set-string_set
|
381
|
+
if string_set_not_allowed.size==0 then
|
382
|
+
puts "all characters are allowed so it won't be possible to generate a wrong string. StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
383
|
+
return ''
|
384
|
+
end
|
370
385
|
end
|
371
386
|
end
|
372
387
|
|
@@ -416,9 +431,12 @@ class StringPattern
|
|
416
431
|
expected_errors_left.delete(:excluded_data)
|
417
432
|
end
|
418
433
|
|
419
|
-
if
|
420
|
-
|
421
|
-
|
434
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
435
|
+
string_set_not_allowed=all_characters_set-string_set
|
436
|
+
if string_set_not_allowed.size==0
|
437
|
+
expected_errors_left.delete(:string_set_not_allowed)
|
438
|
+
end
|
439
|
+
end
|
422
440
|
|
423
441
|
if symbol_type=='!@' and expected_errors_left.size==0 and !expected_errors.include?(:length) and
|
424
442
|
(expected_errors.include?(:required_data) or expected_errors.include?(:excluded_data))
|
@@ -427,7 +445,6 @@ class StringPattern
|
|
427
445
|
|
428
446
|
end
|
429
447
|
|
430
|
-
|
431
448
|
string = ''
|
432
449
|
if symbol_type!='@' and symbol_type!='!@' and length!=0 and string_set.size!=0
|
433
450
|
if string_set.size!=0
|
@@ -437,8 +454,8 @@ class StringPattern
|
|
437
454
|
if required_data.size>0
|
438
455
|
positions_to_set=(0..(string.size-1)).to_a
|
439
456
|
required_data.each {|rd|
|
440
|
-
if (string.
|
441
|
-
rd_to_set=(string.
|
457
|
+
if (string.chars & rd).size>0
|
458
|
+
rd_to_set=(string.chars & rd).sample
|
442
459
|
else
|
443
460
|
rd_to_set=rd.sample
|
444
461
|
end
|
@@ -458,14 +475,15 @@ class StringPattern
|
|
458
475
|
}
|
459
476
|
end
|
460
477
|
excluded_data.each {|ed|
|
461
|
-
if (string.
|
462
|
-
(string.
|
478
|
+
if (string.chars & ed).size>0
|
479
|
+
(string.chars & ed).each {|s|
|
463
480
|
string.gsub!(s, string_set.sample)
|
464
481
|
}
|
465
482
|
end
|
466
483
|
}
|
467
484
|
|
468
485
|
if expected_errors_left.include?(:value)
|
486
|
+
string_set_not_allowed=all_characters_set-string_set if string_set_not_allowed.size==0
|
469
487
|
if string_set_not_allowed.size==0
|
470
488
|
puts "Not possible to generate a non valid string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
471
489
|
return ''
|
@@ -493,11 +511,14 @@ class StringPattern
|
|
493
511
|
expected_errors_left.delete(:excluded_data)
|
494
512
|
end
|
495
513
|
|
496
|
-
if expected_errors_left.include?(:string_set_not_allowed)
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
514
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
515
|
+
string_set_not_allowed=all_characters_set-string_set if string_set_not_allowed.size==0
|
516
|
+
if string_set_not_allowed.size>0
|
517
|
+
(rand(string.size)+1).times {
|
518
|
+
string[rand(string.size)]=string_set_not_allowed.sample
|
519
|
+
}
|
520
|
+
expected_errors_left.delete(:string_set_not_allowed)
|
521
|
+
end
|
501
522
|
end
|
502
523
|
|
503
524
|
elsif (symbol_type=='@' or symbol_type=='!@') and length>0
|
@@ -797,7 +818,7 @@ class StringPattern
|
|
797
818
|
if symbol_type!='@'
|
798
819
|
if required_data.size>0
|
799
820
|
required_data.each {|rd|
|
800
|
-
if (text_to_validate.
|
821
|
+
if (text_to_validate.chars & rd).size==0
|
801
822
|
detected_errors.push(:value)
|
802
823
|
detected_errors.push(:required_data)
|
803
824
|
break
|
@@ -805,13 +826,13 @@ class StringPattern
|
|
805
826
|
}
|
806
827
|
end
|
807
828
|
if excluded_data.size>0
|
808
|
-
if (excluded_data & text_to_validate.
|
829
|
+
if (excluded_data & text_to_validate.chars).size>0
|
809
830
|
detected_errors.push(:value)
|
810
831
|
detected_errors.push(:excluded_data)
|
811
832
|
end
|
812
833
|
end
|
813
834
|
string_set_not_allowed=all_characters_set-string_set
|
814
|
-
text_to_validate.
|
835
|
+
text_to_validate.chars.each {|st|
|
815
836
|
if string_set_not_allowed.include?(st)
|
816
837
|
detected_errors.push(:value)
|
817
838
|
detected_errors.push(:string_set_not_allowed)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_pattern
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: You can easily generate strings supplying a very simple pattern. Also
|
14
14
|
you can validate if a text fulfill an specific pattern or even generate a string
|