Class: Packr::Base62
Constant Summary
collapse
- WORDS =
/\b[\da-zA-Z]\b|\w{2,}/
- ENCODE10 =
"String"
- ENCODE36 =
"function(c){return c.toString(36)}"
- ENCODE62 =
"function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))}"
- UNPACK =
lambda do |p,a,c,k,e,r|
"eval(function(p,a,c,k,e,r){e=#{e};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];" +
"k=[function(e){return r[e]||e}];e=function(){return'#{r}'};c=1};while(c--)if(k[c])p=p." +
"replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('#{p}',#{a},#{c},'#{k}'.split('|'),0,{}))"
end
Instance Method Summary
collapse
Methods inherited from Encoder
#initialize
Constructor Details
This class inherits a constructor from Packr::Encoder
Instance Method Details
#encode(script) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/packr/base62.rb', line 16
def encode(script)
words = search(script)
words.sort!
encoded = Collection.new words.size.times { |i| encoded.put(Packr.encode62(i), i) }
replacement = lambda { |word| words.get(word).replacement }
index = 0
words.each do |word, key|
if encoded.has?(word)
word.index = encoded.get(word)
def word.to_s; ""; end
else
index += 1 while words.has?(Packr.encode62(index))
word.index = index
index += 1
if word.count == 1
def word.to_s; ""; end
end
end
word.replacement = Packr.encode62(word.index)
if word.replacement.length == word.to_s.length
def word.to_s; ""; end
end
end
words.sort! { |word1, word2| word1.index - word2.index }
words = words.slice(0, get_key_words(words).split("|").length)
script = script.gsub(get_pattern(words), &replacement)
p = escape(script)
a = "[]"
c = get_count(words)
k = get_key_words(words)
e = get_encoder(words)
d = get_decoder(words)
UNPACK.call(p,a,c,k,e,d)
end
|
#escape(script) ⇒ Object
71
72
73
74
75
|
# File 'lib/packr/base62.rb', line 71
def escape(script)
script.gsub(/([\\'])/) { |match| "\\#{$1}" }.gsub(/[\r\n]+/, "\\n")
end
|
#get_count(words) ⇒ Object
77
78
79
80
|
# File 'lib/packr/base62.rb', line 77
def get_count(words)
size = words.size
size.zero? ? 1 : size
end
|
#get_decoder(words) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/packr/base62.rb', line 82
def get_decoder(words)
trim = RegexpGroup.new.
put("(\\d)(\\|\\d)+\\|(\\d)", "\\1-\\3").
put("([a-z])(\\|[a-z])+\\|([a-z])", "\\1-\\3").
put("([A-Z])(\\|[A-Z])+\\|([A-Z])", "\\1-\\3").
put("\\|", "")
pattern = trim.exec(words.map { |word, key|
word.to_s.empty? ? "" : word.replacement
}[0...62].join("|"))
return "^$" if pattern.empty?
pattern = "[#{pattern}]"
size = words.size
if size > 62
pattern = "(#{pattern}|"
c = Packr.encode62(size)[0].chr
if c > "9"
pattern += "[\\\\d"
if c >= "a"
pattern += "a"
if c >= "z"
pattern += "-z"
if c >= "A"
pattern += "A"
pattern += "-#{c}" if c > "A"
end
elsif c == "b"
pattern += "-#{c}"
end
end
pattern += "]"
elsif c == "9"
pattern += "\\\\d"
elsif c == "2"
pattern += "[12]"
elsif c == "1"
pattern += "1"
else
pattern += "[1-#{c}]"
end
pattern += "\\\\w)"
end
pattern
end
|
#get_encoder(words) ⇒ Object
132
133
134
135
|
# File 'lib/packr/base62.rb', line 132
def get_encoder(words)
c = words.size
self.class.const_get("ENCODE#{c > 10 ? (c > 36 ? 62 : 36) : 10}")
end
|
#get_key_words(words) ⇒ Object
137
138
139
|
# File 'lib/packr/base62.rb', line 137
def get_key_words(words)
words.map { |word, key| word.to_s }.join("|").gsub(/\|+$/, "")
end
|
#get_pattern(words) ⇒ Object
141
142
143
144
145
146
|
# File 'lib/packr/base62.rb', line 141
def get_pattern(words)
words = words.map { |word, key| word.to_s }.join("|").gsub(/\|{2,}/, "|").gsub(/^\|+|\|+$/, "")
words = "\\x0" if words == ""
string = "\\b(#{words})\\b"
%r{#{string}}
end
|
#search(script) ⇒ Object
65
66
67
68
69
|
# File 'lib/packr/base62.rb', line 65
def search(script)
words = Words.new
script.scan(WORDS).each { |word| words.add(word) }
words
end
|