Permalink
Browse files

Faster and more effective Base62 algorithm for the self-builds

  • Loading branch information...
1 parent b04d4b3 commit 5258ea7d2d3199a0da828b54f9ecca32026e9463 @MadRabbit committed Aug 13, 2009
@@ -21,33 +21,25 @@ def process_hashes_in(string)
end
def create_build_script(source, names_map)
- # sorting the tokens in order from the longest key to the shortest one
- # so they were not conflicting with each other when the script gets reconstructred
- names_map.sort!{ |a, b| b.split(':').first.size <=> a.split(':').first.size }
-
- "eval((function(){"+
- "var s=\"#{source.gsub("\\", "\\\\\\\\").gsub("\n", '\\n').gsub('"', '\"').gsub('\\\'', '\\\\\\\\\'')}\","+
-
- # building the replacements data
- "d=\"#{names_map.join(',')}\".split(\",\");"+
-
+ "eval((function(s,d){"+
# building the postprocessing script
- "for(var i=0;i<d.length;i++){p=d[i].split(\":\");"+
- "s=s.replace(new RegExp('#{REPLACEMENTS_PREFIX}'+p[0],'g'),p[1]);}"+
-
+ "for(var i=d.length-1;i>-1;i--)"+
+ "if(d[i])"+
+ "s=s.replace(new RegExp(i,'g'),d[i]);"+
"return s"+
- "})());"
+ "})("+
+ "\"#{source.gsub("\\", "\\\\\\\\").gsub("\n", '\\n').gsub('"', '\"').gsub('\\\'', '\\\\\\\\\'')}\","+
+ "\"#{names_map.join(',')}\".split(\",\")"+
+ "));"
end
def compact_hashes_in(string)
string = string.dup
names_map = guess_replacements_map(string, tokens_to_replace_in(string))
- names_map.each do |token|
- new_name, old_name = token.split(':')
- string.gsub! old_name do
- REPLACEMENTS_PREFIX + new_name
- end
+
+ names_map.each_with_index do |token, i|
+ string.gsub! token, "#{i}" if token != ''
end
[string, names_map]
@@ -118,36 +110,27 @@ def get_impact_tokens(string, regexp)
# Generic replacements quessing method
#
def guess_replacements_map(string, keys)
- replacements = REPLACEMENTS_CHARS + REPLACEMENTS_CHARS.collect{|c| REPLACEMENTS_CHARS.collect{|a| c+a}}.flatten
-
map = []
- used_keys = []
+ index = -1
keys.each do |old_name|
- new_name = old_name[/[a-z]/i] || 'a'
+ index += 1
- while used_keys.include?(new_name) or string.match(/#{REPLACEMENTS_PREFIX}#{new_name}/)
- new_name = replacements.shift
- break if new_name.nil? # <- safety break if no possible match found
+ while string.match(/#{index}/)
+ map << ''
+ index+= 1
end
- # removing the token so the source code was adjusted for the next changes
- string = string.gsub old_name, "#{REPLACEMENTS_PREFIX}#{new_name}"
+ map << old_name
- if new_name and new_name.size < old_name.size
- map << "#{new_name}:#{old_name}"
- used_keys << new_name
- end
+ string = string.gsub old_name, "#{index}"
end
map
end
public
- REPLACEMENTS_PREFIX = '@'
- REPLACEMENTS_CHARS = ('a'..'z').to_a + ('A'..'Z').to_a
-
- MINUMUM_REPLACEABLE_TOKEN_SIZE = 4
+ MINUMUM_REPLACEABLE_TOKEN_SIZE = 3
MAXIMUM_DICTIONARY_SIZE = 150
end
end
@@ -45,26 +45,26 @@ def build(src)
}
}
}).should == %{
- var @h = {
- @f: 1,
- @s: 2,
- @t: 3,
- @c: {
- @f: 1,
- @s: 2,
- @t: 3,
- @c: {
- @f : 1,
- @s: 2,
- @t : 3,
- @c: {
- @f : 1,
- @s: 2,
- @t : 3,
- @c: function() {
- var a = @h.@f.@s.@t.@c();
- var a = @h.@f.@s.@t.@c();
- var b = @h.@f@s.@t@c();
+ 8 7 = {
+ 5: 1,
+ 0: 2,
+ 4: 3,
+ 6: {
+ 5: 1,
+ 0: 2,
+ 4: 3,
+ 6: {
+ 5 : 1,
+ 0: 2,
+ 4 : 3,
+ 6: {
+ 5 : 1,
+ 0: 2,
+ 4 : 3,
+ 6: function() {
+ 8 a = 7.5.0.4.6();
+ 8 a = 7.5.0.4.6();
+ 8 b = 7.50.46();
}
}
}
@@ -88,15 +88,15 @@ def build(src)
}
}
}).should == %{
- var @h = {
+ var 4 = {
a: 1,
b: 2,
c: {
- @a: {},
- @b: {},
- @c: function() {
- @h.a.b.c.@a().@b.@c();
- @h.a.b.c.@a().@b.@c();
+ 3: {},
+ 5: {},
+ 0: function() {
+ 4.a.b.c.3().5.0();
+ 4.a.b.c.3().5.0();
}
}
}
@@ -115,13 +115,13 @@ def build(src)
}
}
}).should == %{
- var @h = {
- @f : 1,
- @s: 2,
- @t : function() {
- var @h = @h.f.s.t();
- var @h = @h.@f.@s.@t();
- var @h = @h.@f.@s.@t();
+ 6 0 = {
+ 5 : 1,
+ 3: 2,
+ 4 : function() {
+ 6 0 = 0.f.s.t();
+ 6 0 = 0.5.3.4();
+ 6 0 = 0.5.3.4();
}
}
}
@@ -138,12 +138,12 @@ def build(src)
}
}
}).should == %{
- var @h = {
- @c: 1,
- @b: 2,
- @a: function() {
- var @c = @h.@c.@b().@a;
- var @c = @h.@c.@b().@a;
+ 6 5 = {
+ 0: 1,
+ 4: 2,
+ 3: function() {
+ 6 0 = 5.0.4().3;
+ 6 0 = 5.0.4().3;
}
}
}
@@ -184,16 +184,16 @@ def build(src)
}
};
}).should == %{
- var @f = @f() {
- @s () {
- @w () {
+ 3 0 = 0() {
+ 1 () {
+ 2 () {
do();
}
}
};
- var @f = @f name() {
- @s () {
- @w () {
+ 3 0 = 0 name() {
+ 1 () {
+ 2 () {
do();
}
}
@@ -214,8 +214,8 @@ def build(src)
return bla;
return bla;
}).should == %{
- @r bla;
- @r bla;
+ 0 1;
+ 0 1;
}
end
@@ -232,8 +232,8 @@ def build(src)
Object.bla;
Object.bla;
}).should == %{
- @O.bla;
- @O.bla;
+ 0.1;
+ 0.1;
}
end
@@ -248,6 +248,6 @@ def build(src)
hash.first.second().third;
}
}
- }).should == "eval((function(){var s=\"\\n var @h = {\\n @f : '1',\\n @s: \\\"2\\\",\\n @t : /3/,\\n common: function() {\\n @h.@f.@s().@t;\\n @h.@f.@s().@t;\\n }\\n }\\n \",d=\"s:second,t:third,f:first,h:hash\".split(\",\");for(var i=0;i<d.length;i++){p=d[i].split(\":\");s=s.replace(new RegExp('@'+p[0],'g'),p[1]);}return s})());"
+ }).should == "eval((function(s,d){for(var i=d.length-1;i>-1;i--)if(d[i])s=s.replace(new RegExp(i,'g'),d[i]);return s})(\"\\n var 6 = {\\n 5 : '1',\\n 0: \\\"2\\\",\\n 4 : /3/,\\n common: function() {\\n 6.5.0().4;\\n 6.5.0().4;\\n }\\n }\\n \",\"second,,,,third,first,hash\".split(\",\")));"
end
end

0 comments on commit 5258ea7

Please sign in to comment.