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 1414 * This takes O(n).
1515 */
1616 function convertToAscii ( str ) {
17- var result = '' ;
17+ var result = [ ] ;
1818 var currentChar = '' ;
1919 var i = 0 ;
2020 for ( ; i < str . length ; i += 1 ) {
2121 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 ) ;
2824 }
29- return result ;
25+ return result . join ( '' ) ;
3026 }
3127
3228 /**
3329 * Encodes the binary string to run-length encoding.
3430 * Takes O(n^2).
3531 */
3632 function runLength ( vector ) {
37- var result = '' ;
33+ var result = [ ] ;
3834 var zeros = 0 ;
3935 var zerosTemp = '' ;
40- var wordLength = 0 ;
4136 var i = 0 ;
4237 for ( ; i < vector . length ; i += 1 ) {
4338 if ( vector [ i ] === '0' ) {
4439 zeros += 1 ;
4540 } else {
4641 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 ) ;
5344 zeros = 0 ;
5445 }
5546 }
56- return result ;
47+ return result . join ( '' ) ;
5748 }
5849
5950 /**
You can’t perform that action at this time.
0 commit comments