File tree Expand file tree Collapse file tree 1 file changed +8
-17
lines changed
src/compression/runlength Expand file tree Collapse file tree 1 file changed +8
-17
lines changed Original file line number Diff line number Diff line change 14
14
* This takes O(n).
15
15
*/
16
16
function convertToAscii ( str ) {
17
- var result = '' ;
17
+ var result = [ ] ;
18
18
var currentChar = '' ;
19
19
var i = 0 ;
20
20
for ( ; i < str . length ; i += 1 ) {
21
21
currentChar = str [ i ] . charCodeAt ( 0 ) . toString ( 2 ) ;
22
- if ( currentChar . length < 8 ) {
23
- while ( 8 - currentChar . length ) {
24
- currentChar = '0' + currentChar ;
25
- }
26
- }
27
- result += currentChar ;
22
+ currentChar = new Array ( 9 - currentChar . length ) . join ( '0' ) + currentChar ;
23
+ result . push ( currentChar ) ;
28
24
}
29
- return result ;
25
+ return result . join ( '' ) ;
30
26
}
31
27
32
28
/**
33
29
* Encodes the binary string to run-length encoding.
34
30
* Takes O(n^2).
35
31
*/
36
32
function runLength ( vector ) {
37
- var result = '' ;
33
+ var result = [ ] ;
38
34
var zeros = 0 ;
39
35
var zerosTemp = '' ;
40
- var wordLength = 0 ;
41
36
var i = 0 ;
42
37
for ( ; i < vector . length ; i += 1 ) {
43
38
if ( vector [ i ] === '0' ) {
44
39
zeros += 1 ;
45
40
} else {
46
41
zerosTemp = zeros . toString ( 2 ) ;
47
- wordLength = zerosTemp . length - 1 ;
48
- while ( wordLength ) {
49
- result = result + '1' ;
50
- wordLength -= 1 ;
51
- }
52
- result += '0' + zerosTemp ;
42
+ result . push ( new Array ( zerosTemp . length ) . join ( '1' ) ) ;
43
+ result . push ( '0' + zerosTemp ) ;
53
44
zeros = 0 ;
54
45
}
55
46
}
56
- return result ;
47
+ return result . join ( '' ) ;
57
48
}
58
49
59
50
/**
You can’t perform that action at this time.
0 commit comments