Skip to content

Commit

Permalink
Working now
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-vin committed Apr 29, 2019
1 parent cf3c6f5 commit b3af601
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/constants.cr
Expand Up @@ -5,6 +5,7 @@ class Crowbar
NUMBERS = '0'...'9'

module Regex
EACH_CHAR = //
UPPER_ALPHA = /[A-Z]/
LOWER_ALPHA = /[a-z]/
DIGIT = /\d/
Expand Down
40 changes: 22 additions & 18 deletions src/crowbar.cr
@@ -1,6 +1,7 @@
require "perlin_noise"

require "./constants"
require "./match"

require "./selector"
require "./selectors/*"
Expand All @@ -16,44 +17,44 @@ class Crowbar
VERSION = "0.0.0.1"

getter seed : Int32 = 0
getter iteration : UInt32 = 0
getter iteration : Int32 = 0
getter input : String = ""
getter working_input : String = ""

getter selectors : Array(Selector) = [] of Selector

getter noise : PerlinNoise = PerlinNoise.new


def initialize(@input = "", @seed = 1234)
restart
yield self
end

def << (selector : Crowbar::Selector)
def << (selector : Selector)
@selectors << selector
end

# Give the next test string
def next : String
output = input
matches = [] of Crowbar::Match
# Shuffle mutators, select number via range
@noise.shuffle(@iteration, selectors)[0..@noise.int(@iteration, 0, @selectors.size)].each do |selector|
effect = noise.height_float(iteration, iteration, 1)
shuffled_selectors = noise.shuffle(iteration, selector.select(input))
used_selectors = shuffled_selectors[0..(shuffled_selectors.size * effect).to_i]
used_selectors.each do |match|
effect = noise.height_float(iteration, iteration, 1)
shuffled_mutators = noise.shuffle(iteration, selector.mutators)
used_mutators = shuffled_mutators[0..(selector.mutators.size * effect).to_i]
mutated = match.match
used_mutators.each do |mutator|
mutated = mutator.mutate(mutated)
@working_input = @input
noise.shuffle(@iteration, selectors)[0..noise.int(@iteration, 0, @selectors.size)].each do |selector|
noise.shuffle(@iteration, selector.mutators)[0..noise.int(@iteration, 0, selector.mutators.size)].each do |mutator|
mutants = selector.matches.map_with_index do |match, index|
if match.matched? && noise.bool(@iteration, index, 1, 5)
string = mutator.mutate(match)
match.string = string
end
match
end
temp = ""
mutants.each do |match|
temp += match.string
end
@working_input = temp
end
end
@iteration += 1
output
@working_input
end

# give next test string and replace input with it
Expand All @@ -64,6 +65,9 @@ class Crowbar
# Restart back to beginning
def restart
@noise = PerlinNoise.new @seed
@working_input = @input
@iteration = 0

#TODO: RESET ALL MUTATORS, GENERATORS, SELECTORS!
end
end
18 changes: 12 additions & 6 deletions src/generators/decimals.cr
@@ -1,17 +1,23 @@
class Crowbar::Generator::Decimals < Crowbar::Generator
def initialize(crowbar, length_limit = (0..10))
super crowbar, length_limit
property? quoted = false
property? float = false
def initialize(mutator, length_limit = (0..10))
super mutator, length_limit
end

def make : String
length = self.crowbar.noise.int(self.crowbar.iteration, length_limit.begin, length_limit.end)
length = self.crowbar.noise.int(self.crowbar.iteration, self.length_limit.begin, self.length_limit.end)
output = ""
point = self.crowbar.noise.int(self.crowbar.iteration, iteration, 0, length-1)
length.times do |x|
digit_index = self.crowbar.noise.int(self.crowbar.iteration + x, @iteration, 0, 1000)
digit = Constants::NUMBERS.to_a[digit_index % Constants::NUMBERS.size].to_s
digit_index = self.crowbar.noise.int(self.crowbar.iteration + x, self.iteration, 0, 1000)
digit = Crowbar::Constants::NUMBERS.to_a[digit_index % Crowbar::Constants::NUMBERS.size].to_s
output += digit
if float? && x == point
output += '.'
end
end
@iteration += 1
output.to_i.to_s
quoted? ? "\"" + output.to_i.to_s + "\"" : output.to_i.to_s
end
end
7 changes: 6 additions & 1 deletion src/match.cr
@@ -1,5 +1,10 @@
class Crowbar::Match
property start = 0
property finish = 0
property match = ""
property string = ""
property? matched = false

def range
(start..finish)
end
end
4 changes: 2 additions & 2 deletions src/mutator.cr
@@ -1,6 +1,6 @@
abstract class Crowbar::Mutator
# Keeps track of parent selector
getter selector : Selector
getter selector : Crowbar::Selector
# Keeps track of children generators
getter generators = [] of Crowbar::Generator
getter iteration = 0
Expand All @@ -18,5 +18,5 @@ abstract class Crowbar::Mutator
selector.crowbar
end

abstract def mutate(input : String) : String
abstract def mutate(match : Crowbar::Match) : String
end
42 changes: 21 additions & 21 deletions src/mutators/duplicator.cr
@@ -1,23 +1,23 @@
# Duplicates symbols next to each other
class Crowbar::Mutator::RepeaterDuplicator < Crowbar::Mutator
@regex : Regex = Constants::Regex::SYMBOLS
def initialize(crowbar, @regex = Constants::Regex::SYMBOLS)
super crowbar
end
# # Duplicates symbols next to each other
# class Crowbar::Mutator::Repeater < Crowbar::Mutator
# @regex : Regex = Constants::Regex::SYMBOLS
# def initialize(crowbar, @regex = Constants::Regex::SYMBOLS)
# super crowbar
# end

def mutate(input)
# detect symbols in text
matches = input.scan(@regex)
# def mutate(input)
# # detect symbols in text
# matches = input.scan(@regex)

#TODO: FIX THIS FOR MULTI CHAR INSERTS
effect = self.crowbar.noise.height_float(self.crowbar.iteration, self.crowbar.iteration, 1)
index_offset = 0
output = input
symbols_shuffle = self.crowbar.noise.shuffle(self.crowbar.iteration, matches)
symbols_shuffle[0..(matches.size*effect).to_i].sort{|a,b| a.begin.as(Int32) <=> b.begin.as(Int32)}.each do |match|
output = output.insert(match.begin.as(Int32) + index_offset, match[0])
index_offset += 1
end
output
end
end
# #TODO: FIX THIS FOR MULTI CHAR INSERTS
# effect = self.crowbar.noise.height_float(self.crowbar.iteration, self.crowbar.iteration, 1)
# index_offset = 0
# output = input
# symbols_shuffle = self.crowbar.noise.shuffle(self.crowbar.iteration, matches)
# symbols_shuffle[0..(matches.size*effect).to_i].sort{|a,b| a.begin.as(Int32) <=> b.begin.as(Int32)}.each do |match|
# output = output.insert(match.begin.as(Int32) + index_offset, match[0])
# index_offset += 1
# end
# output
# end
# end
52 changes: 26 additions & 26 deletions src/mutators/remover.cr
@@ -1,28 +1,28 @@
# Removes symbols
class Crowbar::Mutator::Remover < Crowbar::Mutator
@regex : Regex = Constants::Regex::SYMBOLS
def initialize(crowbar, @regex = Constants::Regex::SYMBOLS)
super crowbar
end
# # Removes symbols
# class Crowbar::Mutator::Remover < Crowbar::Mutator
# @regex : Regex = Constants::Regex::SYMBOLS
# def initialize(crowbar, @regex = Constants::Regex::SYMBOLS)
# super crowbar
# end

def mutate(input)
# detect symbols in text
matches = [] of Int32
input.scan(@regex) do |match|
if match.begin
matches << match.begin.as(Int32)
end
end
# def mutate(input)
# # detect symbols in text
# matches = [] of Int32
# input.scan(@regex) do |match|
# if match.begin
# matches << match.begin.as(Int32)
# end
# end

# TODO: FIX THIS FOR MULTICHAR DELETES
effect = self.crowbar.noise.height_float(self.crowbar.iteration, self.crowbar.iteration, 1)
index_offset = 0
output = input
symbols_shuffle = self.crowbar.noise.shuffle(self.crowbar.iteration, matches)
symbols_shuffle[0..(matches.size*effect).to_i].sort.each do |index|
output = output.sub(index + index_offset, "")
index_offset -= 1
end
output
end
end
# # TODO: FIX THIS FOR MULTICHAR DELETES
# effect = self.crowbar.noise.height_float(self.crowbar.iteration, self.crowbar.iteration, 1)
# index_offset = 0
# output = input
# symbols_shuffle = self.crowbar.noise.shuffle(self.crowbar.iteration, matches)
# symbols_shuffle[0..(matches.size*effect).to_i].sort.each do |index|
# output = output.sub(index + index_offset, "")
# index_offset -= 1
# end
# output
# end
# end
29 changes: 8 additions & 21 deletions src/mutators/replacer.cr
@@ -1,31 +1,18 @@
# Replaces symbols
class Crowbar::Mutator::Replacer < Crowbar::Mutator
@regex : Regex

def initialize(selector, @regex = Crowbar::Constants::Regex::IN_QUOTES)
def initialize(selector)
super selector do |m|
yield m
end
end

def mutate(input) : String
# detect symbols in text
matches = input.scan(@regex)

effect = self.crowbar.noise.height_float(self.crowbar.iteration, self.crowbar.iteration, 1)
shuffled_matches = self.crowbar.noise.shuffle(self.crowbar.iteration, matches)
used_matches = shuffled_matches[0..(matches.size*effect).to_i].sort{|a,b| a.begin.as(Int32) <=> b.begin.as(Int32)}

offset = 0
output = input
used_matches.each do |match|
start = match.begin.as(Int32) + offset
finish = match.end.as(Int32) + offset
range = (start...finish)
replacement = @generator.make
output = output.sub(range, replacement)
offset += replacement.size - range.size
def mutate(match : Crowbar::Match) : String
if @generators.size >= 2
item = crowbar.noise.item(iteration, crowbar.iteration, @generators).make
else
item = @generators[0].make
end
output
@iteration += 1
item
end
end
11 changes: 7 additions & 4 deletions src/sandbox.cr
@@ -1,15 +1,18 @@
require "./crowbar"

cr = Crowbar.new("{ \"json\" : \"A String\", \"x\" : 0x123AA}") do |cr|
cr = Crowbar.new("{ \"json\" : \"A String\", \"x\" : 0x123AA }") do |cr|
Crowbar::Selector::Regex.new(cr, Crowbar::Constants::Regex::IN_QUOTES) do |s|
Crowbar::Mutator::Replacer.new(s) do |m|
Crowbar::Generator::Decimals.new(m)
end
end
end



Crowbar::Selector::Regex.new(cr, Crowbar::Constants::Regex::EACH_CHAR) do |s|
Crowbar::Mutator::Replacer.new(s) do |m|
Crowbar::Generator::Decimals.new(m)
end
end
end

20.times do |x|
puts cr.next
Expand Down
5 changes: 2 additions & 3 deletions src/selector.cr
@@ -1,17 +1,16 @@
abstract class Crowbar::Selector
getter crowbar : Crowbar
getter mutators = [] of Mutator
getter mutators = [] of Crowbar::Mutator

getter iteration = 0

def initialize(@crowbar)
@crowbar << self
yield self
end

def << (mutator)
@mutators << mutator
end

abstract def select(input) : Array(Crowbar::Match)
abstract def matches : Array(Crowbar::Match)
end

0 comments on commit b3af601

Please sign in to comment.