Skip to content

Commit 8bdbed5

Browse files
authored
Update StringEncode.js
1 parent bd4a9a3 commit 8bdbed5

File tree

1 file changed

+14
-54
lines changed

1 file changed

+14
-54
lines changed

StringEncode.js

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,22 @@
33
* @input {String} - The data to be encoded. This is passed to the function via the "this" keyword
44
* @returns {String} The Base85 encoded data as string.
55
*/
6-
String.prototype.toAscii85 = function()
7-
{
6+
String.prototype.toAscii85 = function(){
87
/**
98
* Turns a single character (it ASCII code) to binary padded to 8 figures when necessary.
109
* @param {String} n - The single character whose ASCII code is to be converted to binary
1110
* @returns {String} The binary of the ASCII code of n padded to 8 figures when necessary.
1211
*/
13-
var encodeToBinary = function(n)
14-
{
12+
var encodeToBinary = function(n){
1513
let bin = n.charCodeAt(0);
1614
bin = bin.toString(2);
17-
for(let i = bin.length; i < 8; i++)//Pad to 8 characters with "0"
18-
{
19-
bin = "0" + bin;
20-
}
15+
bin = "0".repeat(8-bin.length) + bin;
2116
return bin;
2217
}
2318
let txt = [...this];
2419
let bin = "";
2520
for(let i = 0; i < txt.length; i++)
26-
{
2721
bin += encodeToBinary(txt[i]);
28-
}
2922

3023
/* Pad the binary so it length is exactly divisible by 32
3124
* because we are going to divide it into chucks of 32's
@@ -34,24 +27,19 @@ String.prototype.toAscii85 = function()
3427
let padding = (32-(bin.length%32));
3528
if(padding%32 == 0) padding = 0; //If length is exactly divisible by 32 padding returns 32, so we reset it to 0
3629
for(let counter = 0; padding > 0 && counter < padding; counter++)
37-
{
3830
bin += "0";
39-
}
4031

4132
//Divide whole binary to chunk of 32 characters
4233
let chunk = [];
4334
for(let i = 32; i <= bin.length; i += 32)
44-
{
4535
chunk.push(bin.slice(i-32, i));
46-
}
4736

4837
/* Each chunk should be converted from binary to base 10
4938
* The value should then be splitted into five integers
5039
* chunk value (in base 10) = (int1*85^4) + (int2*85^3) + (int3*85^2) + (int4*85^1) + int5
5140
*/
5241
var ASCII = "";
53-
for(let i = 0; i < chunk.length; i++)
54-
{
42+
for(let i = 0; i < chunk.length; i++){
5543
let sub = parseInt(chunk[i], 2);
5644
let w1 = Math.floor(sub/Math.pow(85, 4));
5745
let w2 = Math.floor((sub-(w1*Math.pow(85, 4)))/Math.pow(85, 3));
@@ -75,8 +63,7 @@ String.prototype.toAscii85 = function()
7563
}
7664

7765
//Remove (padding/8) numbers of character from the final encoded characters
78-
for(let i = 0; (padding/8) != 0 && i < (padding/8); i++)
79-
{
66+
for(let i = 0; (padding/8) != 0 && i < (padding/8); i++){
8067
if(ASCII == "z")
8168
ASCII = "!!!!!";
8269
ASCII = ASCII.substr(0, ASCII.length-1);
@@ -89,8 +76,7 @@ String.prototype.toAscii85 = function()
8976
* @input {String} - The data to be decoded. This is passed to the function via the "this" keyword
9077
* @returns {String} The original decoded string from Base85.
9178
*/
92-
String.prototype.fromAscii85 = function()
93-
{
79+
String.prototype.fromAscii85 = function(){
9480
//Remove white space and new lines
9581
let code = this.replace(/\s|\n/g,"");
9682
//remove beginning <~ and ending ~>
@@ -105,16 +91,12 @@ String.prototype.fromAscii85 = function()
10591
let padding = 5 - (code.length%5);
10692
if(padding%5 == 0) padding = 0; //If length is exactly divisible by 5 padding returns 5, so we reset it to 0
10793
for(let i = 0; i < padding; i++)
108-
{
10994
code += "u";
110-
}
11195

11296
//Divide padded data to chunk of 5 characters
11397
let chunk = [];
11498
for(let i = 5; i <= code.length; i += 5)
115-
{
11699
chunk.push(code.slice(i-5, i));
117-
}
118100

119101
/*
120102
* Take each character in a chunk
@@ -126,8 +108,7 @@ String.prototype.fromAscii85 = function()
126108
* Convert each slice to base 10 and to the corresponding character of ASCII
127109
*/
128110
let txt = "";
129-
for(let i = 0; i < chunk.length; i++)
130-
{
111+
for(let i = 0; i < chunk.length; i++){
131112
let w1 = ((chunk[i][0]).charCodeAt(0)-33)*Math.pow(85,4);
132113
let w2 = ((chunk[i][1]).charCodeAt(0)-33)*Math.pow(85,3);
133114
let w3 = ((chunk[i][2]).charCodeAt(0)-33)*Math.pow(85,2);
@@ -149,9 +130,7 @@ String.prototype.fromAscii85 = function()
149130

150131
//Remove the number of padding added above from the strings
151132
for(let i = 0; i < padding; i++)
152-
{
153133
txt = txt.substr(0, txt.length-1);
154-
}
155134
return txt;
156135
}
157136

@@ -161,32 +140,25 @@ String.prototype.fromAscii85 = function()
161140
* @input {String} - The string to be encoded. This is passed to the function via the "this" keyword
162141
* @returns {String} The Base64 encoded data as string
163142
*/
164-
String.prototype.toBase64 = function()
165-
{
143+
String.prototype.toBase64 = function(){
166144
var __codeString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
167145

168146
/**
169147
* Turns a single character (it ASCII code) to binary padded to 8 figures when necessary.
170148
* @param {String} n - The single character whose ASCII code is to be converted to binary
171149
* @returns {String} The binary of the ASCII code of n padded to 8 figures when necessary.
172150
*/
173-
var encodeToBinary = function(n)
174-
{
151+
var encodeToBinary = function(n){
175152
let bin = n.charCodeAt(0);
176153
bin = bin.toString(2);
177-
for(let i = bin.length; i < 8; i++)//Padd to 8 characters
178-
{
179-
bin = "0" + bin;
180-
}
154+
bin = "0".repeat(8-bin.length) + bin;
181155
return bin;
182156
}
183157

184158
let txt = [...this];
185159
let bin = "";
186160
for(let i = 0; i < txt.length; i++)
187-
{
188161
bin += encodeToBinary(txt[i]);
189-
}
190162

191163
/* Pad the data so it length is exactly divisible by 24
192164
* because we are going to divide it into chucks of 24's
@@ -195,16 +167,12 @@ String.prototype.toBase64 = function()
195167
let padding = (24-(bin.length%24));
196168
if(padding%24 == 0) padding = 0; //If length is exactly divisible by 24 padding returns 24, so we reset it to 0
197169
for(let counter = 0; padding > 0 && counter < padding; counter++)
198-
{
199170
bin += "0";
200-
}
201171

202172
//Divide padded data to chunk of 24 characters
203173
let chunk = [];
204174
for(let i = 24; i <= bin.length; i += 24)
205-
{
206175
chunk.push(bin.slice(i-24, i));
207-
}
208176

209177
/*
210178
* Slice the 24 characters in each chunk into 6 characters in 4 places
@@ -213,8 +181,7 @@ String.prototype.toBase64 = function()
213181
* i.e. if the chunk in base10 is 0, it is encoded as "A" (__codeString[0])
214182
*/
215183
let BASE64 = "";
216-
for(let i = 0; i < chunk.length; i++)
217-
{
184+
for(let i = 0; i < chunk.length; i++){
218185
let w1 = (chunk[i].slice(0, 6) == "000000")?"=":__codeString[parseInt(chunk[i].slice(0, 6), 2)];
219186
let w2 = (chunk[i].slice(6, 12) == "000000")?"=":__codeString[parseInt(chunk[i].slice(6, 12), 2)];
220187
let w3 = (chunk[i].slice(12, 18) == "000000")?"=":__codeString[parseInt(chunk[i].slice(12, 18), 2)];
@@ -231,8 +198,7 @@ String.prototype.toBase64 = function()
231198
* @input {String} - The string to be decoded. This is passed to the function via the "this" keyword
232199
* @returns {String} The original decoded string form Base64
233200
*/
234-
String.prototype.fromBase64 = function()
235-
{
201+
String.prototype.fromBase64 = function(){
236202
var __codeString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
237203
let txt = [...this];
238204

@@ -243,24 +209,18 @@ String.prototype.fromBase64 = function()
243209
for(let i = 0; i < txt.length; i++)
244210
{
245211
let tx = ((__codeString.indexOf(txt[i])>0)?__codeString.indexOf(txt[i]):0).toString(2);
246-
for(let j = tx.length; j < 6; j++)
247-
{
248-
tx = "0" + tx;
249-
}
212+
tx = "0".repeat(6-tx.length) + tx;
250213
decode += tx;
251214
}
252215

253216
//Break the whole binary into chunks of 8 characters
254217
let chunk = [];
255218
for(let i = 8; i <= decode.length; i += 8)
256-
{
257219
chunk.push(decode.slice(i-8, i));
258-
}
259220

260221
//Take each chunk of 8 characters and convert it to base10 and to the corresponding character of ASCII
261222
let realText = "";
262-
for(let i = 0; i < chunk.length; i++)
263-
{
223+
for(let i = 0; i < chunk.length; i++){
264224
if(parseInt(chunk[i],2) == 0)
265225
continue;
266226
realText += String.fromCharCode(parseInt(chunk[i],2));

0 commit comments

Comments
 (0)