Skip to content

Commit

Permalink
tweaked some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
benburkert committed Jul 8, 2008
1 parent cbbf872 commit 069079f
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -0,0 +1,5 @@
== 0.1.0 "Very Beta" 2008-07-08
* Initial version of randexp!
* Has support for very simple regular expressions.
* Randgen has limited methods.
* Dictionary is reading from the local words file.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 Ben Burkert

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83 changes: 83 additions & 0 deletions README
@@ -0,0 +1,83 @@
randexp
by Ben Burkert
http://github.com/benburkert/randexp

== DESCRIPTION:

andexp makes it easy to generate random string from most regular expressions.

== REQUIREMENTS:

* none!

== INSTALL:

$ gem sources -a http://gems.github.com/ (you only need to do this once)
$ gem install benburkert-randexp

== USAGE:

randexp adds the #generate (or #gen, for short) method to the Regexp class,
which generates a 'random' string that will match your regular expression.

/abc|def/.gen
# => "def"

== Valid Regexp's

randexp can only generate matching string from simple regular expression.
Except for a few circumstances, wildcards are generally not allowed in the
regular expression. is pretty domain specific, so trying to guess when to
terminate a random pattern would produce unhelpful data:

>> /Aa{3}h*!/.gen
# => RuntimeError: Sorry, "h*" is too vague, try setting a range: "h{0,3}"
>> /Aa{3}h{3,15}!/.gen
=> "Aaaahhhhh!"

>> /(never gonna (give you up|let you down), )*/.gen
=> RuntimeError: Sorry, "(...)*" is too vague, try setting a range: "(...){0, 3}"
>> /(never gonna (give you up|let you down), ){3,5}/.gen
=> "never gonna give you up, never gonna let you down, never gonna give you up, never gonna give you up, "

The exception being word characters (\w), which generate a random word from the Dictionary class.

>> /\w+/.gen
=> "groveling"

= Primitives & Complex matches

The single character matchers supported are words(\w), whitespace(\s), and digits(\d).

>> /\d{50}/.gen
=> "50315410741096763188525493528315906035878741451037"

When a multiplicity constraint is placed on a word character, a word with the valid length is generated.

>> /\w{10}/.gen # a word with 10 letters
=> "Chaucerism"

>> /\w{5,15}/.gen
=> "cabalistic"

Complex matchers use the [:...:] syntax within the regular expression.

>> /[:sentence:]/.gen
=> "Nonhearer demetricize toppiece filicic possessedness rhodizite zoomagnetism earwigginess steady"

Complex matchers can also be added by extending the Randgen class.

class Randgen
def self.serial_number(options = {})
/XX\d{4}-\w-\d{5}/.gen
end
end

>> /[:serial_number:]/.gen
=> "XX3770-M-33114"

= Dictionary

The Dictionary loads the local users' words file, allowing randomly generated words to be chosen from
thousands of entries to the words file. Words are mapped by their length to allow words to be randomly
chosen based on size.
107 changes: 107 additions & 0 deletions README.textile
@@ -0,0 +1,107 @@
h1. randexp
bq. by Ben Burkert
bq. http://github.com/benburkert/randexp

h2. DESCRIPTION:

andexp makes it easy to generate random string from most regular expressions.

h2. REQUIREMENTS:

* none!

h2. INSTALL:

$ gem sources -a http://gems.github.com/ (you only need to do this once)
$ gem install benburkert-randexp

h2. USAGE:

randexp adds the #generate (or #gen, for short) method to the Regexp class,
which generates a 'random' string that will match your regular expression.

/abc|def/.gen
# => "def"

h2. Valid Regexp's

randexp can only generate matching string from simple regular expression.
Except for a few circumstances, wildcards are generally not allowed in the
regular expression. is pretty domain specific, so trying to guess when to
terminate a random pattern would produce unhelpful data:

<pre>
<code>
>> /Aa{3}h*!/.gen
# => RuntimeError: Sorry, "h*" is too vague, try setting a range: "h{0,3}"
>> /Aa{3}h{3,15}!/.gen
=> "Aaaahhhhh!"

>> /(never gonna (give you up|let you down), )*/.gen
=> RuntimeError: Sorry, "(...)*" is too vague, try setting a range: "(...){0, 3}"
>> /(never gonna (give you up|let you down), ){3,5}/.gen
=> "never gonna give you up, never gonna let you down, never gonna give you up, never gonna give you up, "
</code>
</pre>

The exception being word characters (\w), which generate a random word from the Dictionary class.

<pre>
<code>
>> /\w+/.gen
=> "groveling"
</code>
</pre>

h3. Primitives & Complex matches

The single character matchers supported are words(\w), whitespace(\s), and digits(\d).

<pre>
<code>
>> /\d{50}/.gen
=> "50315410741096763188525493528315906035878741451037"
</code>
</pre>

When a multiplicity constraint is placed on a word character, a word with the valid length is generated.

<pre>
<code>
>> /\w{10}/.gen # a word with 10 letters
=> "Chaucerism"

>> /\w{5,15}/.gen
=> "cabalistic"
</code>
</pre>

Complex matchers use the [:...:] syntax within the regular expression.

<pre>
<code>
>> /[:sentence:]/.gen
=> "Nonhearer demetricize toppiece filicic possessedness rhodizite zoomagnetism earwigginess steady"
</code>
</pre>

Complex matchers can also be added by extending the Randgen class.

<pre>
<code>
class Randgen
def self.serial_number(options = {})
/XX\d{4}-\w-\d{5}/.gen
end
end

>> /[:serial_number:]/.gen
=> "XX3770-M-33114"
</code>
</pre>

h3. Dictionary

The Dictionary loads the local users' words file, allowing randomly generated words to be chosen from
thousands of entries to the words file. Words are mapped by their length to allow words to be randomly
chosen based on size.
26 changes: 0 additions & 26 deletions Rakefile
Expand Up @@ -7,32 +7,6 @@ require 'rake/rdoctask'
require "spec"
require "spec/rake/spectask"

DIR = File.dirname(__FILE__)
NAME = 'randexp'
SUMMARY =<<-EOS
Library for generating random strings.
EOS
GEM_VERSION = "0.1.0"

spec = Gem::Specification.new do |s|
s.name = NAME
s.summary = SUMMARY

s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY

s.has_rdoc = true

s.require_path = "lib"
s.files = %w(Rakefile) + Dir["lib/**/*"]
end

Rake::GemPackageTask.new(spec) do |package|
package.gem_spec = spec
package.need_zip = true
package.need_tar = true
end

##############################################################################
# rSpec & rcov
##############################################################################
Expand Down
2 changes: 0 additions & 2 deletions lib/randexp.rb
@@ -1,6 +1,4 @@
class Randexp
VERSION = "0.1.0"

attr_accessor :sexp

def initialize(source)
Expand Down
6 changes: 6 additions & 0 deletions lib/randexp/parser.rb
Expand Up @@ -17,6 +17,12 @@ def self.parse(source)
when /(.*)\\([wsdc])\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(random($2), ($3.to_i)..($4.to_i)))
when /(.*)\\([wsdc])\{(\d+)\}$/ then union(parse($1), quantify(random($2), $3.to_i))
when /(.*)\\([wsdc])$/ then union(parse($1), random($2))
when /\((.*)\)(\*|\*\?|\+|\+\?|\?)$/ then quantify(parse($1), $2.to_sym)
when /\((.*)\)\{(\d+)\,(\d+)\}$/ then quantify(parse($1), ($2.to_i)..($3.to_i))
when /\((.*)\)\{(\d+)\}$/ then quantify(parse($1), $3.to_i)
when /(.*)(.|\s)(\*|\*\?|\+|\+\?|\?)$/ then union(parse($1), quantify(literal($2), $3.to_sym))
when /(.*)(.|\s)\{(\d+)\,(\d+)\}$/ then union(parse($1), quantify(literal($2), ($3.to_i)..($4.to_i)))
when /(.*)(.|\s)\{(\d+)\}$/ then union(parse($1), quantify(literal($2), $3.to_i))
when /(.*)(.|\s)$/ then union(parse($1), literal($2))
else nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/randexp/reducer.rb
Expand Up @@ -26,8 +26,8 @@ def self.literal(cell, quantity = nil)
when :'?' then ([''] + cell).pick * ''
when :+, :'+?' then raise "Sorry, \"#{cell * ''}+\" is too vague, try setting a range: \"#{cell * ''}{1,3}\""
when :*, :'*?' then raise "Sorry, \"#{cell * ''}*\" is too vague, try setting a range: \"#{cell * ''}{0,3}\""
when Range then quant.pick.of { cell * '' } * ''
when Integer then quant.of { cell * '' } * ''
when Range then quantity.pick.of { cell * '' } * ''
when Integer then quantity.of { cell * '' } * ''
when nil then cell * ''
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/randgen.rb
Expand Up @@ -2,31 +2,31 @@ class Randgen
WORDS_PER_SENTENCE = 3..20
SENTENCES_PER_PARAGRAPH = 3..8

def self.bool
def self.bool(options = {})
['true', 'false'].pick
end

def self.lchar
def self.lchar(options = {})
('a'..'z').to_a.pick
end

def self.uchar
def self.uchar(options = {})
('A'..'Z').to_a.pick
end

def self.char
def self.char(options = {})
[lchar, uchar].pick
end

def self.whitespace
def self.whitespace(options = {})
["\t", "\n", "\r", "\f"].pick
end

def self.digit
def self.digit(options = {})
('0'..'9').to_a.pick
end

def self.alpha_numeric
def self.alpha_numeric(options = {})
[char, digit].pick
end

Expand Down
16 changes: 16 additions & 0 deletions randexp.gemspec
@@ -0,0 +1,16 @@
Gem::Specification.new do |s|
s.name = "randexp"
s.version = "0.1.0"
s.date = "2008-07-08"
s.summary = "Library for generating random strings"
s.email = "ben@benburkert.com"
s.homepage = "http://github.com/benburkert/randexp"
s.description = "randexp makes it easy to generate random string from most regular expressions."
s.has_rdoc = true
s.authors = ["Ben Burkert"]
s.files = %w(Rakefile CHANGELOG LICENSE README) + Dir["lib/**/*"]
s.test_files = Dir["spec/**/*"]
s.rdoc_options = ["--main", "README"]
s.extra_rdoc_files = ["CHANGELOG", "README"]
s.require_path = "lib"
end

0 comments on commit 069079f

Please sign in to comment.