string_pattern 1.2.0 → 1.3.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/README.md +10 -0
- data/lib/string_pattern.rb +366 -340
- 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: dac5331eb5b7b3a9aadc5f207306069595b542eee8027ae7f73864f00efea9d4
|
4
|
+
data.tar.gz: 20160f3cead72e7a75e6bdbe4da25374e7fb93751cf15bcc78f564f9d37c107c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 936d83dc4c33e3ca09f209b6c6cd03d7d8b05ad530e7ea83e027ab1c0c29cbae579fbafeec8886d0968ee8c754e587310e80608a06d6e9df6ba705b3b15791d0
|
7
|
+
data.tar.gz: 11690843c4c23514d4754991da06ea0d762794633b01aa0df82ad1acef1e6c94fe45f2c18aa9c0dbfc1a585aafe5077e0533ac541ffe685db0591ec3f17e0734
|
data/README.md
CHANGED
@@ -75,6 +75,16 @@ p gen "3:N"
|
|
75
75
|
#>443
|
76
76
|
```
|
77
77
|
|
78
|
+
If you want to generate for example 1000 strings and be sure all those strings are different you can use:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
StringPattern.dont_repeat = true #default: false
|
82
|
+
1000.times {
|
83
|
+
puts :"6-20:L/N/".gen
|
84
|
+
}
|
85
|
+
StringPattern.cache_values = Hash.new() #to clean the generated values from memory
|
86
|
+
```
|
87
|
+
|
78
88
|
#### Custom characters
|
79
89
|
|
80
90
|
Also it's possible to provided the characters we want. To do that we'll use the symbol_type [characters]
|
data/lib/string_pattern.rb
CHANGED
@@ -16,13 +16,17 @@ require_relative 'string/pattern/add_to_ruby' if SP_ADD_TO_RUBY
|
|
16
16
|
# Set of characters that will be used when using T pattern
|
17
17
|
# optimistic: (TrueFalse, default: true)
|
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
|
+
# dont_repeat: (TrueFalse, default: false)
|
20
|
+
# If you want to generate for example 1000 strings and be sure all those strings are different you can set it to true
|
19
21
|
class StringPattern
|
20
22
|
class << self
|
21
|
-
attr_accessor :national_chars, :optimistic, :dont_repeat, :cache
|
23
|
+
attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values
|
22
24
|
end
|
23
25
|
@national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join
|
24
26
|
@optimistic = true
|
25
27
|
@cache = Hash.new()
|
28
|
+
@cache_values = Hash.new()
|
29
|
+
@dont_repeat = false
|
26
30
|
NUMBER_SET = ('0'..'9').to_a
|
27
31
|
SPECIAL_SET = [' ', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', "'", ';', ':', '?', '>', '<', '`', '|', '/', '"']
|
28
32
|
ALPHA_SET_LOWER = ('a'..'z').to_a
|
@@ -273,383 +277,405 @@ class StringPattern
|
|
273
277
|
# the generated string
|
274
278
|
###############################################
|
275
279
|
def StringPattern.generate(pattern, expected_errors: [], **synonyms)
|
276
|
-
|
280
|
+
tries = 0
|
281
|
+
begin
|
282
|
+
good_result = true
|
283
|
+
tries+=1
|
284
|
+
string=''
|
277
285
|
|
278
|
-
|
286
|
+
expected_errors=synonyms[:errors] if synonyms.keys.include?(:errors)
|
279
287
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
}
|
303
|
-
return string
|
304
|
-
elsif pattern.kind_of?(String) or pattern.kind_of?(Symbol)
|
305
|
-
patt=StringPattern.analyze(pattern)
|
306
|
-
min_length=patt.min_length
|
307
|
-
max_length=patt.max_length
|
308
|
-
symbol_type=patt.symbol_type
|
309
|
-
|
310
|
-
required_data=patt.required_data
|
311
|
-
excluded_data=patt.excluded_data
|
312
|
-
string_set=patt.string_set
|
313
|
-
all_characters_set=patt.all_characters_set
|
314
|
-
|
315
|
-
required_chars=Array.new
|
316
|
-
unless required_data.size==0
|
317
|
-
required_data.each {|rd|
|
318
|
-
required_chars<<rd if rd.size==1
|
288
|
+
if expected_errors.kind_of?(Symbol)
|
289
|
+
expected_errors=[expected_errors]
|
290
|
+
end
|
291
|
+
|
292
|
+
if pattern.kind_of?(Array)
|
293
|
+
pattern.each {|pat|
|
294
|
+
if pat.kind_of?(Symbol)
|
295
|
+
if pat.to_s.scan(/^!?\d+-?\d*:.+/).size>0
|
296
|
+
string<<StringPattern.generate(pat.to_s, expected_errors: expected_errors)
|
297
|
+
else
|
298
|
+
string<<pat.to_s
|
299
|
+
end
|
300
|
+
elsif pat.kind_of?(String) then
|
301
|
+
if @optimistic and pat.to_s.scan(/^!?\d+-?\d*:.+/).size>0
|
302
|
+
string<<StringPattern.generate(pat.to_s, expected_errors: expected_errors)
|
303
|
+
else
|
304
|
+
string<<pat
|
305
|
+
end
|
306
|
+
else
|
307
|
+
puts "StringPattern.generate: it seems you supplied wrong array of patterns: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
308
|
+
return ''
|
309
|
+
end
|
319
310
|
}
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
311
|
+
return string
|
312
|
+
elsif pattern.kind_of?(String) or pattern.kind_of?(Symbol)
|
313
|
+
patt=StringPattern.analyze(pattern)
|
314
|
+
min_length=patt.min_length
|
315
|
+
max_length=patt.max_length
|
316
|
+
symbol_type=patt.symbol_type
|
317
|
+
|
318
|
+
required_data=patt.required_data
|
319
|
+
excluded_data=patt.excluded_data
|
320
|
+
string_set=patt.string_set
|
321
|
+
all_characters_set=patt.all_characters_set
|
322
|
+
|
323
|
+
required_chars=Array.new
|
324
|
+
unless required_data.size==0
|
325
|
+
required_data.each {|rd|
|
326
|
+
required_chars<<rd if rd.size==1
|
327
|
+
}
|
328
|
+
unless excluded_data.size==0
|
329
|
+
if (required_chars.flatten & excluded_data.flatten).size>0
|
330
|
+
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}"
|
331
|
+
return ''
|
332
|
+
end
|
324
333
|
end
|
325
334
|
end
|
326
|
-
|
327
|
-
string_set_not_allowed=Array.new
|
335
|
+
string_set_not_allowed=Array.new
|
328
336
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
allow_empty=false
|
336
|
-
deny_pattern=false
|
337
|
-
if symbol_type[0..0]=='!'
|
338
|
-
deny_pattern=true
|
339
|
-
possible_errors=[:length, :value, :required_data, :excluded_data, :string_set_not_allowed]
|
340
|
-
(rand(possible_errors.size)+1).times {
|
341
|
-
expected_errors<<possible_errors.sample
|
342
|
-
}
|
343
|
-
expected_errors.uniq!
|
344
|
-
if symbol_type[1..1]=='0'
|
345
|
-
allow_empty=true
|
346
|
-
end
|
347
|
-
elsif symbol_type[0..0]=='0' then
|
348
|
-
allow_empty=true
|
349
|
-
end
|
350
|
-
|
351
|
-
if expected_errors.include?(:min_length) or expected_errors.include?(:length) or
|
352
|
-
expected_errors.include?(:max_length)
|
353
|
-
allow_empty=!allow_empty
|
354
|
-
elsif expected_errors.include?(:value) or
|
355
|
-
expected_errors.include?(:excluded_data) or
|
356
|
-
expected_errors.include?(:required_data) or
|
357
|
-
expected_errors.include?(:string_set_not_allowed) and allow_empty
|
358
|
-
allow_empty=false
|
359
|
-
end
|
337
|
+
else
|
338
|
+
puts "pattern argument not valid on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
339
|
+
return pattern.to_s
|
340
|
+
end
|
360
341
|
|
361
|
-
length=min_length
|
362
|
-
symbol_type_orig=symbol_type
|
363
342
|
|
364
|
-
|
343
|
+
allow_empty=false
|
344
|
+
deny_pattern=false
|
345
|
+
if symbol_type[0..0]=='!'
|
346
|
+
deny_pattern=true
|
347
|
+
possible_errors=[:length, :value, :required_data, :excluded_data, :string_set_not_allowed]
|
348
|
+
(rand(possible_errors.size)+1).times {
|
349
|
+
expected_errors<<possible_errors.sample
|
350
|
+
}
|
351
|
+
expected_errors.uniq!
|
352
|
+
if symbol_type[1..1]=='0'
|
353
|
+
allow_empty=true
|
354
|
+
end
|
355
|
+
elsif symbol_type[0..0]=='0' then
|
356
|
+
allow_empty=true
|
357
|
+
end
|
358
|
+
|
359
|
+
if expected_errors.include?(:min_length) or expected_errors.include?(:length) or
|
360
|
+
expected_errors.include?(:max_length)
|
361
|
+
allow_empty=!allow_empty
|
362
|
+
elsif expected_errors.include?(:value) or
|
363
|
+
expected_errors.include?(:excluded_data) or
|
364
|
+
expected_errors.include?(:required_data) or
|
365
|
+
expected_errors.include?(:string_set_not_allowed) and allow_empty
|
366
|
+
allow_empty=false
|
367
|
+
end
|
365
368
|
|
366
|
-
|
367
|
-
|
368
|
-
unless deny_pattern
|
369
|
-
if required_data.size==0 and expected_errors_left.include?(:required_data)
|
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}"
|
371
|
-
return ''
|
372
|
-
end
|
369
|
+
length=min_length
|
370
|
+
symbol_type_orig=symbol_type
|
373
371
|
|
374
|
-
|
375
|
-
puts "excluded 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}"
|
376
|
-
return ''
|
377
|
-
end
|
372
|
+
expected_errors_left=expected_errors.dup
|
378
373
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
374
|
+
symbol_type=symbol_type_orig
|
375
|
+
|
376
|
+
unless deny_pattern
|
377
|
+
if required_data.size==0 and expected_errors_left.include?(:required_data)
|
378
|
+
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}"
|
383
379
|
return ''
|
384
|
-
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
if expected_errors_left.include?(:min_length) or
|
389
|
-
expected_errors_left.include?(:max_length) or
|
390
|
-
expected_errors_left.include?(:length)
|
391
|
-
if expected_errors_left.include?(:min_length) or
|
392
|
-
(min_length>0 and expected_errors_left.include?(:length) and rand(2)==0)
|
393
|
-
if min_length>0
|
394
|
-
if allow_empty
|
395
|
-
length=rand(min_length).to_i
|
396
|
-
else
|
397
|
-
length=rand(min_length-1).to_i+1
|
398
|
-
end
|
399
|
-
if required_data.size>length and required_data.size<min_length
|
400
|
-
length=required_data.size
|
401
|
-
end
|
402
|
-
expected_errors_left.delete(:length)
|
403
|
-
expected_errors_left.delete(:min_length)
|
404
|
-
else
|
405
|
-
puts "min_length is 0 so it won't be possible to generate a wrong string smaller than 0 characters. StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
406
|
-
return ''
|
407
|
-
end
|
408
|
-
elsif expected_errors_left.include?(:max_length) or expected_errors_left.include?(:length)
|
409
|
-
length=max_length+1+rand(max_length).to_i
|
410
|
-
expected_errors_left.delete(:length)
|
411
|
-
expected_errors_left.delete(:max_length)
|
412
|
-
end
|
413
|
-
else
|
414
|
-
if allow_empty and rand(7)==1
|
415
|
-
length=0
|
416
|
-
else
|
417
|
-
if max_length==min_length
|
418
|
-
length=min_length
|
419
|
-
else
|
420
|
-
length=min_length + rand(max_length - min_length + 1)
|
421
|
-
end
|
422
|
-
end
|
423
|
-
end
|
380
|
+
end
|
424
381
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
382
|
+
if excluded_data.size==0 and expected_errors_left.include?(:excluded_data)
|
383
|
+
puts "excluded 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}"
|
384
|
+
return ''
|
385
|
+
end
|
429
386
|
|
430
|
-
|
431
|
-
|
432
|
-
|
387
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
388
|
+
string_set_not_allowed=all_characters_set-string_set
|
389
|
+
if string_set_not_allowed.size==0 then
|
390
|
+
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}"
|
391
|
+
return ''
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
433
395
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
396
|
+
if expected_errors_left.include?(:min_length) or
|
397
|
+
expected_errors_left.include?(:max_length) or
|
398
|
+
expected_errors_left.include?(:length)
|
399
|
+
if expected_errors_left.include?(:min_length) or
|
400
|
+
(min_length>0 and expected_errors_left.include?(:length) and rand(2)==0)
|
401
|
+
if min_length>0
|
402
|
+
if allow_empty
|
403
|
+
length=rand(min_length).to_i
|
404
|
+
else
|
405
|
+
length=rand(min_length-1).to_i+1
|
406
|
+
end
|
407
|
+
if required_data.size>length and required_data.size<min_length
|
408
|
+
length=required_data.size
|
409
|
+
end
|
410
|
+
expected_errors_left.delete(:length)
|
411
|
+
expected_errors_left.delete(:min_length)
|
412
|
+
else
|
413
|
+
puts "min_length is 0 so it won't be possible to generate a wrong string smaller than 0 characters. StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
414
|
+
return ''
|
415
|
+
end
|
416
|
+
elsif expected_errors_left.include?(:max_length) or expected_errors_left.include?(:length)
|
417
|
+
length=max_length+1+rand(max_length).to_i
|
418
|
+
expected_errors_left.delete(:length)
|
419
|
+
expected_errors_left.delete(:max_length)
|
420
|
+
end
|
421
|
+
else
|
422
|
+
if allow_empty and rand(7)==1
|
423
|
+
length=0
|
424
|
+
else
|
425
|
+
if max_length==min_length
|
426
|
+
length=min_length
|
427
|
+
else
|
428
|
+
length=min_length + rand(max_length - min_length + 1)
|
429
|
+
end
|
430
|
+
end
|
438
431
|
end
|
439
|
-
end
|
440
432
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
433
|
+
if deny_pattern
|
434
|
+
if required_data.size==0 and expected_errors_left.include?(:required_data)
|
435
|
+
expected_errors_left.delete(:required_data)
|
436
|
+
end
|
445
437
|
|
446
|
-
|
438
|
+
if excluded_data.size==0 and expected_errors_left.include?(:excluded_data)
|
439
|
+
expected_errors_left.delete(:excluded_data)
|
440
|
+
end
|
447
441
|
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
if required_data.size>0
|
455
|
-
positions_to_set=(0..(string.size-1)).to_a
|
456
|
-
required_data.each {|rd|
|
457
|
-
if (string.chars & rd).size>0
|
458
|
-
rd_to_set=(string.chars & rd).sample
|
459
|
-
else
|
460
|
-
rd_to_set=rd.sample
|
461
|
-
end
|
462
|
-
if ((0 ... string.length).find_all {|i| string[i, 1] == rd_to_set}).size==0
|
463
|
-
if positions_to_set.size==0
|
464
|
-
puts "pattern not valid on StringPattern.generate, not possible to generate a valid string: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
465
|
-
return ''
|
466
|
-
else
|
467
|
-
k=positions_to_set.sample
|
468
|
-
string[k]=rd_to_set
|
469
|
-
positions_to_set.delete(k)
|
470
|
-
end
|
471
|
-
else
|
472
|
-
k=((0 ... string.length).find_all {|i| string[i, 1] == rd_to_set}).sample
|
473
|
-
positions_to_set.delete(k)
|
474
|
-
end
|
475
|
-
}
|
476
|
-
end
|
477
|
-
excluded_data.each {|ed|
|
478
|
-
if (string.chars & ed).size>0
|
479
|
-
(string.chars & ed).each {|s|
|
480
|
-
string.gsub!(s, string_set.sample)
|
481
|
-
}
|
482
|
-
end
|
483
|
-
}
|
442
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
443
|
+
string_set_not_allowed=all_characters_set-string_set
|
444
|
+
if string_set_not_allowed.size==0
|
445
|
+
expected_errors_left.delete(:string_set_not_allowed)
|
446
|
+
end
|
447
|
+
end
|
484
448
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
return ''
|
490
|
-
end
|
491
|
-
(rand(string.size)+1).times {
|
492
|
-
string[rand(string.size)]=(all_characters_set-string_set).sample
|
493
|
-
}
|
494
|
-
expected_errors_left.delete(:value)
|
495
|
-
end
|
449
|
+
if symbol_type=='!@' and expected_errors_left.size==0 and !expected_errors.include?(:length) and
|
450
|
+
(expected_errors.include?(:required_data) or expected_errors.include?(:excluded_data))
|
451
|
+
expected_errors_left.push(:value)
|
452
|
+
end
|
496
453
|
|
497
|
-
|
498
|
-
(rand(required_data.size)+1).times {
|
499
|
-
chars_to_remove=required_data.sample
|
500
|
-
chars_to_remove.each {|char_to_remove|
|
501
|
-
string.gsub!(char_to_remove, (string_set-chars_to_remove).sample)
|
502
|
-
}
|
503
|
-
}
|
504
|
-
expected_errors_left.delete(:required_data)
|
505
|
-
end
|
454
|
+
end
|
506
455
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
456
|
+
string = ''
|
457
|
+
if symbol_type!='@' and symbol_type!='!@' and length!=0 and string_set.size!=0
|
458
|
+
if string_set.size!=0
|
459
|
+
1.upto(length) {|i| string << string_set.sample.to_s
|
460
|
+
}
|
461
|
+
end
|
462
|
+
if required_data.size>0
|
463
|
+
positions_to_set=(0..(string.size-1)).to_a
|
464
|
+
required_data.each {|rd|
|
465
|
+
if (string.chars & rd).size>0
|
466
|
+
rd_to_set=(string.chars & rd).sample
|
467
|
+
else
|
468
|
+
rd_to_set=rd.sample
|
469
|
+
end
|
470
|
+
if ((0 ... string.length).find_all {|i| string[i, 1] == rd_to_set}).size==0
|
471
|
+
if positions_to_set.size==0
|
472
|
+
puts "pattern not valid on StringPattern.generate, not possible to generate a valid string: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
473
|
+
return ''
|
474
|
+
else
|
475
|
+
k=positions_to_set.sample
|
476
|
+
string[k]=rd_to_set
|
477
|
+
positions_to_set.delete(k)
|
478
|
+
end
|
479
|
+
else
|
480
|
+
k=((0 ... string.length).find_all {|i| string[i, 1] == rd_to_set}).sample
|
481
|
+
positions_to_set.delete(k)
|
482
|
+
end
|
483
|
+
}
|
484
|
+
end
|
485
|
+
excluded_data.each {|ed|
|
486
|
+
if (string.chars & ed).size>0
|
487
|
+
(string.chars & ed).each {|s|
|
488
|
+
string.gsub!(s, string_set.sample)
|
489
|
+
}
|
490
|
+
end
|
491
|
+
}
|
513
492
|
|
514
|
-
|
515
|
-
|
516
|
-
|
493
|
+
if expected_errors_left.include?(:value)
|
494
|
+
string_set_not_allowed=all_characters_set-string_set if string_set_not_allowed.size==0
|
495
|
+
if string_set_not_allowed.size==0
|
496
|
+
puts "Not possible to generate a non valid string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
497
|
+
return ''
|
498
|
+
end
|
517
499
|
(rand(string.size)+1).times {
|
518
|
-
string[rand(string.size)]=
|
500
|
+
string[rand(string.size)]=(all_characters_set-string_set).sample
|
519
501
|
}
|
520
|
-
expected_errors_left.delete(:
|
521
|
-
|
522
|
-
end
|
502
|
+
expected_errors_left.delete(:value)
|
503
|
+
end
|
523
504
|
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
expected_errors.uniq!
|
534
|
-
expected_errors_left.uniq!
|
535
|
-
end
|
505
|
+
if expected_errors_left.include?(:required_data) and required_data.size>0
|
506
|
+
(rand(required_data.size)+1).times {
|
507
|
+
chars_to_remove=required_data.sample
|
508
|
+
chars_to_remove.each {|char_to_remove|
|
509
|
+
string.gsub!(char_to_remove, (string_set-chars_to_remove).sample)
|
510
|
+
}
|
511
|
+
}
|
512
|
+
expected_errors_left.delete(:required_data)
|
513
|
+
end
|
536
514
|
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
alpha_set=ALPHA_SET_LOWER + ALPHA_SET_CAPITAL
|
544
|
-
string_set=alpha_set + NUMBER_SET + ['.'] + ['_'] + ['-']
|
545
|
-
string_set_not_allowed=all_characters_set-string_set
|
515
|
+
if expected_errors_left.include?(:excluded_data) and excluded_data.size>0
|
516
|
+
(rand(string.size)+1).times {
|
517
|
+
string[rand(string.size)]=excluded_data.sample.sample
|
518
|
+
}
|
519
|
+
expected_errors_left.delete(:excluded_data)
|
520
|
+
end
|
546
521
|
|
547
|
-
|
548
|
-
|
522
|
+
if expected_errors_left.include?(:string_set_not_allowed)
|
523
|
+
string_set_not_allowed=all_characters_set-string_set if string_set_not_allowed.size==0
|
524
|
+
if string_set_not_allowed.size>0
|
525
|
+
(rand(string.size)+1).times {
|
526
|
+
string[rand(string.size)]=string_set_not_allowed.sample
|
527
|
+
}
|
528
|
+
expected_errors_left.delete(:string_set_not_allowed)
|
529
|
+
end
|
530
|
+
end
|
549
531
|
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
expected_errors_left.delete(:value)
|
563
|
-
else
|
564
|
-
1.upto(rand(3)+2) {|i| extension << alpha_set.sample.downcase
|
565
|
-
}
|
566
|
-
end
|
567
|
-
if rand(2)==1
|
568
|
-
at_sign=(string_set-['@']).sample
|
569
|
-
expected_errors_left.delete(:value)
|
570
|
-
expected_errors_left.delete(:required_data)
|
571
|
-
end
|
572
|
-
else
|
573
|
-
if length>6
|
574
|
-
1.upto(rand(3)+2) {|i| extension << alpha_set.sample.downcase
|
575
|
-
}
|
576
|
-
else
|
577
|
-
1.upto(2) {|i| extension << alpha_set.sample.downcase
|
578
|
-
}
|
579
|
-
end
|
580
|
-
end
|
581
|
-
length_e=length-extension.size - 1
|
582
|
-
length1=rand(length_e-1) + 1
|
583
|
-
length2=length_e-length1
|
584
|
-
1.upto(length1) {|i| string << string_set.sample}
|
532
|
+
elsif (symbol_type=='@' or symbol_type=='!@') and length>0
|
533
|
+
if min_length>6 and length<6
|
534
|
+
length=6
|
535
|
+
end
|
536
|
+
if deny_pattern and
|
537
|
+
(expected_errors.include?(:required_data) or expected_errors.include?(:excluded_data) or
|
538
|
+
expected_errors.include?(:string_set_not_allowed))
|
539
|
+
expected_errors_left.push(:value)
|
540
|
+
expected_errors.push(:value)
|
541
|
+
expected_errors.uniq!
|
542
|
+
expected_errors_left.uniq!
|
543
|
+
end
|
585
544
|
|
586
|
-
|
545
|
+
expected_errors_left_orig=expected_errors_left.dup
|
546
|
+
tries=0
|
547
|
+
begin
|
548
|
+
expected_errors_left=expected_errors_left_orig.dup
|
549
|
+
tries+=1
|
550
|
+
string=''
|
551
|
+
alpha_set=ALPHA_SET_LOWER + ALPHA_SET_CAPITAL
|
552
|
+
string_set=alpha_set + NUMBER_SET + ['.'] + ['_'] + ['-']
|
553
|
+
string_set_not_allowed=all_characters_set-string_set
|
554
|
+
|
555
|
+
extension='.'
|
556
|
+
at_sign='@'
|
557
|
+
|
558
|
+
if expected_errors_left.include?(:value)
|
559
|
+
if rand(2)==1
|
560
|
+
extension=(all_characters_set-['.']).sample
|
561
|
+
expected_errors_left.delete(:value)
|
562
|
+
expected_errors_left.delete(:required_data)
|
563
|
+
end
|
564
|
+
if rand(2)==1
|
565
|
+
1.upto(rand(7)) {|i| extension << alpha_set.sample.downcase
|
566
|
+
}
|
567
|
+
(rand(extension.size)+1).times {
|
568
|
+
extension[rand(extension.size)]=(string_set-alpha_set-['.']).sample
|
569
|
+
}
|
570
|
+
expected_errors_left.delete(:value)
|
571
|
+
else
|
572
|
+
1.upto(rand(3)+2) {|i| extension << alpha_set.sample.downcase
|
573
|
+
}
|
574
|
+
end
|
575
|
+
if rand(2)==1
|
576
|
+
at_sign=(string_set-['@']).sample
|
577
|
+
expected_errors_left.delete(:value)
|
578
|
+
expected_errors_left.delete(:required_data)
|
579
|
+
end
|
580
|
+
else
|
581
|
+
if length>6
|
582
|
+
1.upto(rand(3)+2) {|i| extension << alpha_set.sample.downcase
|
583
|
+
}
|
584
|
+
else
|
585
|
+
1.upto(2) {|i| extension << alpha_set.sample.downcase
|
586
|
+
}
|
587
|
+
end
|
588
|
+
end
|
589
|
+
length_e=length-extension.size - 1
|
590
|
+
length1=rand(length_e-1) + 1
|
591
|
+
length2=length_e-length1
|
592
|
+
1.upto(length1) {|i| string << string_set.sample}
|
587
593
|
|
588
|
-
|
589
|
-
domain_set=alpha_set + NUMBER_SET + ['.'] + ['-']
|
590
|
-
1.upto(length2) {|i| domain << domain_set.sample.downcase
|
591
|
-
}
|
594
|
+
string << at_sign
|
592
595
|
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
expected_errors_left.delete(:value)
|
598
|
-
end
|
599
|
-
string << domain << extension
|
596
|
+
domain=''
|
597
|
+
domain_set=alpha_set + NUMBER_SET + ['.'] + ['-']
|
598
|
+
1.upto(length2) {|i| domain << domain_set.sample.downcase
|
599
|
+
}
|
600
600
|
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
601
|
+
if expected_errors.include?(:value) and rand(2)==1 and domain.size>0
|
602
|
+
(rand(domain.size)+1).times {
|
603
|
+
domain[rand(domain.size)]=(all_characters_set-domain_set).sample
|
604
|
+
}
|
605
|
+
expected_errors_left.delete(:value)
|
606
|
+
end
|
607
|
+
string << domain << extension
|
608
|
+
|
609
|
+
if expected_errors_left.include?(:value) or expected_errors_left.include?(:string_set_not_allowed)
|
610
|
+
(rand(string.size)+1).times {
|
611
|
+
string[rand(string.size)]=string_set_not_allowed.sample
|
612
|
+
}
|
613
|
+
expected_errors_left.delete(:value)
|
614
|
+
expected_errors_left.delete(:string_set_not_allowed)
|
615
|
+
end
|
608
616
|
|
609
|
-
|
617
|
+
error_regular_expression=false
|
610
618
|
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
619
|
+
if deny_pattern and expected_errors.include?(:length)
|
620
|
+
good_result=true #it is already with wrong length
|
621
|
+
else
|
622
|
+
# I'm doing this because many times the regular expression checking hangs with these characters
|
623
|
+
wrong=%w(.. __ -- ._ _. .- -. _- -_ @. @_ @- .@ _@ -@ @@)
|
624
|
+
if !(Regexp.union(*wrong) === string) #don't include any or the wrong strings
|
625
|
+
if string.index('@').to_i>0 and
|
626
|
+
string[0..(string.index('@')-1)].scan(/([a-z0-9]+([\+\._\-][a-z0-9]|)*)/i).join==string[0..(string.index('@')-1)] and
|
627
|
+
string[(string.index('@')+1)..-1].scan(/([0-9a-z]+([\.-][a-z0-9]|)*)/i).join==string[string[(string.index('@')+1)..-1]]
|
628
|
+
error_regular_expression=false
|
629
|
+
else
|
630
|
+
error_regular_expression=true
|
631
|
+
end
|
632
|
+
else
|
633
|
+
error_regular_expression=true
|
634
|
+
end
|
627
635
|
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
636
|
+
if expected_errors.size==0
|
637
|
+
if error_regular_expression
|
638
|
+
good_result=false
|
639
|
+
else
|
640
|
+
good_result=true
|
641
|
+
end
|
642
|
+
elsif expected_errors_left.size==0 and
|
643
|
+
(expected_errors-[:length, :min_length, :max_length]).size==0
|
644
|
+
good_result=true
|
645
|
+
elsif expected_errors!=[:length]
|
646
|
+
if !error_regular_expression
|
647
|
+
good_result=false
|
648
|
+
elsif expected_errors.include?(:value)
|
649
|
+
good_result=true
|
650
|
+
end
|
651
|
+
end
|
652
|
+
end
|
645
653
|
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
654
|
+
end until good_result or tries>100
|
655
|
+
unless good_result
|
656
|
+
puts "Not possible to generate an email on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
657
|
+
return ''
|
658
|
+
end
|
659
|
+
end
|
660
|
+
if @dont_repeat
|
661
|
+
if @cache_values[pattern.to_s].nil?
|
662
|
+
@cache_values[pattern.to_s]=Array.new()
|
663
|
+
@cache_values[pattern.to_s].push(string)
|
664
|
+
good_result=true
|
665
|
+
elsif @cache_values[pattern.to_s].include?(string)
|
666
|
+
good_result = false
|
667
|
+
else
|
668
|
+
@cache_values[pattern.to_s].push(string)
|
669
|
+
good_result=true
|
670
|
+
end
|
671
|
+
end
|
672
|
+
end until good_result or tries>10000
|
673
|
+
unless good_result
|
674
|
+
puts "Not possible to generate the string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
|
675
|
+
puts "Take in consideration if you are using StringPattern.dont_repeat=true that you don't try to generate more strings that are possible to be generated"
|
676
|
+
return ''
|
651
677
|
end
|
652
|
-
|
678
|
+
|
653
679
|
return string
|
654
680
|
end
|
655
681
|
|
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.3.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-09-01 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
|