Strange use of the for loop in Javascript, please explain

I found this weird javascript that I cannot understand. There is a strange syntax for the loop (many parameters), can you explain to me how it should work? Thanks

decode: function(s){
        for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
        return s.join("");
    }
+3
source share
4 answers

an interesting function, apparently transcoding a certain set of characters, a kind of esoteric and will only work with ASCII code, but here's the breakdown:

    for (var i = 0; i < s.length; i++) {
        var a = s.charCodeAt(i);

        if (a & 0x80) { // (a >= 128) if extended ascii

            var b = s.charCodeAt(i + 1);

            var specialA = (a & 0xfc) === 0xc0; // a IS [À, Á, Â or Ã] essentially [192, 193, 194, 195]
            var specialB = (b & 0xc0) === 0x80; // b >= 128 & b <= 191 eg. b is not a special Latin Ascii Letter

            if (specialA && specialB) {

                var txA = (a & 0x03) << 6; // [0, 64, 128, 192]
                var txB = b & 0x3f; // 0 - 63

                s[i] = String.fromCharCode(txA + txB);

            } else {
                s[i] = String.fromCharCode(128);
                s[++i] = "";
            }
        }
    }

hope this helps, anyway i found an interesting decoding, reminiscent of reading the original assembler, lol -ck

+2
source

This is a normal loop for, but with a very long statement varin the first part.

It's just like

var a, b, c;

for , .

, , .

+5

for, (;).

var:

var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt";

:

++i < l;

:

((a = s[i][c](0)) & 0x80) &&
        (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
        o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")

, for() ;, , , var, check update true.

, - , . ?

+1

:

  • (...)&&(...) if(...){(...)}
  • l len
  • s = s.split(...) len

.

var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";

for(var i = -1, len = s.length; ++i < len;){
    if((a = s[i][c](0)) & 0x80){
        (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "");
    }
}

  • i /
  • moved a = s[i][c](0)outside

.

var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";

for(var i = 0, len = s.length; i < len; i++){
    a = s[i][c](0);

    if(a & 0x80){
        s[i] = (a & 0xfc);
        (s[i] == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "");
    }
}

  • Designed tmpto make reading easier.
  • saved the result of the triple operation in tmp
  • divided (s[i] == 0xc0 && tmp, s[++i] = "");into if(...){s[++i] = "";}
  • replaced the new loop inside your example

.

decode: function(s){
    var tmp, a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";

    for(var i = 0, len = s.length; i < len; i++){
        a = s[i][c](0);

        if(a & 0x80){
            s[i] = (a & 0xfc);

            if(((b = s[i + 1][c](0)) & 0xc0) == 0x80){
                tmp = o(((a & 0x03) << 6) + (b & 0x3f));
            }else{
                tmp = o(128);
            }

            if(s[i] == 0xc0 && tmp){
                s[++i] = "";
            }
        }
    }

    return s.join("");
}

End result / \

+1
source

All Articles