From 5c4e40de730019b30566e63afa3f905f987b2f06 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Mon, 22 Apr 2024 01:11:12 -0400 Subject: [PATCH 1/3] ascii_printable_visible --- lib/random/formatter.rb | 29 +++++++++++++++++++++++++++++ test/ruby/test_random_formatter.rb | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 037f9d8..0513214 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -42,6 +42,12 @@ # prng.alphanumeric(10) #=> "aOxAg8BAJe" # Random.alphanumeric #=> "TmP9OsJHJLtaZYhP" # +# Generate strings from the visible printable ASSCII characters - characters 33–126 +# +# prng.ascii_printable_visable(20) #=> "BSJsDtHia\\8/P\"Ag#fy^" +# prng.ascii_printable_visable(20) #=> "L^4iJK#W%R(5PeFa;4ss" +# prng.ascii_printable_visable(20) #=> "!#=j)6u_7cO^DP "2d931510-d99f-494a-8c67-87feb05e1594" @@ -370,4 +376,27 @@ def alphanumeric(n = nil, chars: ALPHANUMERIC) n = 16 if n.nil? choose(chars, n) end + + + ASCII_PRINTABLE_VISIBLE = ('!'..'~').to_a + + # Generate a random string of characters from the printable visible ASCII space, + # characters 33–126, which is more or less the set of unaccented characters typable + # on a typical english keyboard. + # + # The argument _n_ specifies the length, in characters, of the + # string to be generated. + # + # If _n_ is not specified or is nil, 16 is assumed. + # It may be larger in the future. + # + # require 'random/formatter' + # + # Random.ascii_printable_visible #=> "A3\\HDEY?+YpZ806_" + # # or + # prng = Random.new + # prng.ascii_printable_visible(10) #=> "n5v&~Mi8{'" + def ascii_printable_visible(n = nil) + alphanumeric(n, chars: ASCII_PRINTABLE_VISIBLE) + end end diff --git a/test/ruby/test_random_formatter.rb b/test/ruby/test_random_formatter.rb index f927522..256f244 100644 --- a/test/ruby/test_random_formatter.rb +++ b/test/ruby/test_random_formatter.rb @@ -124,6 +124,14 @@ def test_alphanumeric end end + def test_ascii_printable_visible + 65.times do |n| + an = @it.ascii_printable_visible(n) + assert_match(/\A[!-~]*\z/, an) + assert_equal(n, an.length) + end + end + def test_alphanumeric_chars [ [[*"0".."9"], /\A\d*\z/], From 33091108a367d02a43ed2ecb88855e9e1bac682e Mon Sep 17 00:00:00 2001 From: John Bachir Date: Mon, 22 Apr 2024 02:33:35 -0400 Subject: [PATCH 2/3] Update lib/random/formatter.rb Co-authored-by: Olle Jonsson --- lib/random/formatter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 0513214..2d45405 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -44,9 +44,9 @@ # # Generate strings from the visible printable ASSCII characters - characters 33–126 # -# prng.ascii_printable_visable(20) #=> "BSJsDtHia\\8/P\"Ag#fy^" -# prng.ascii_printable_visable(20) #=> "L^4iJK#W%R(5PeFa;4ss" -# prng.ascii_printable_visable(20) #=> "!#=j)6u_7cO^DP "BSJsDtHia\\8/P\"Ag#fy^" +# prng.ascii_printable_visible(20) #=> "L^4iJK#W%R(5PeFa;4ss" +# prng.ascii_printable_visible(20) #=> "!#=j)6u_7cO^DP Date: Mon, 22 Apr 2024 02:33:45 -0400 Subject: [PATCH 3/3] Update lib/random/formatter.rb Co-authored-by: Olle Jonsson --- lib/random/formatter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 2d45405..e5e3de9 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -382,7 +382,7 @@ def alphanumeric(n = nil, chars: ALPHANUMERIC) # Generate a random string of characters from the printable visible ASCII space, # characters 33–126, which is more or less the set of unaccented characters typable - # on a typical english keyboard. + # on a typical English keyboard. # # The argument _n_ specifies the length, in characters, of the # string to be generated.