tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

utf8-encode.js (4282B)


      1 // |reftest| skip-if(!xulRuntime.shell)
      2 
      3 var BUGNUMBER = 1561567;
      4 var summary = 'JS_EncodeStringToUTF8BufferPartial - Encode string as UTF-8 into a byte array';
      5 
      6 print(BUGNUMBER + ": " + summary);
      7 
      8 var concat = [
      9  {
     10    head: "a",
     11    tail: "b",
     12    expected: "ab",
     13    name: "Latin1 and Latin1",
     14  },
     15  {
     16    head: "α",
     17    tail: "β",
     18    expected: "αβ",
     19    name: "UTF-16 and UTF-16",
     20  },
     21  {
     22    head: "a",
     23    tail: "β",
     24    expected: "aβ",
     25    name: "Latin1 and UTF-16",
     26  },
     27  {
     28    head: "α",
     29    tail: "b",
     30    expected: "αb",
     31    name: "UTF-16 and Latin1",
     32  },
     33  {
     34    head: "\uD83D",
     35    tail: "\uDE03",
     36    expected: "\uD83D\uDE03",
     37    name: "Surrogate pair",
     38  },
     39  {
     40    head: "a\uD83D",
     41    tail: "\uDE03b",
     42    expected: "a\uD83D\uDE03b",
     43    name: "Surrogate pair with prefix and suffix",
     44  },
     45  {
     46    head: "\uD83D",
     47    tail: "b",
     48    expected: "\uFFFDb",
     49    name: "Unpaired high surrogate and Latin1",
     50  },
     51  {
     52    head: "a\uD83D",
     53    tail: "b",
     54    expected: "a\uFFFDb",
     55    name: "Prefixed unpaired high surrogate and Latin1",
     56  },
     57  {
     58    head: "\uD83D",
     59    tail: "β",
     60    expected: "\uFFFDβ",
     61    name: "Unpaired high surrogate and UTF-16",
     62  },
     63  {
     64    head: "a\uD83D",
     65    tail: "β",
     66    expected: "a\uFFFDβ",
     67    name: "Prefixed unpaired high surrogate and UTF-16",
     68  },
     69 
     70  {
     71    head: "\uDE03",
     72    tail: "b",
     73    expected: "\uFFFDb",
     74    name: "Unpaired low surrogate and Latin1",
     75  },
     76  {
     77    head: "a\uDE03",
     78    tail: "b",
     79    expected: "a\uFFFDb",
     80    name: "Prefixed unpaired low surrogate and Latin1",
     81  },
     82  {
     83    head: "\uDE03",
     84    tail: "β",
     85    expected: "\uFFFDβ",
     86    name: "Unpaired low surrogate and UTF-16",
     87  },
     88  {
     89    head: "a\uDE03",
     90    tail: "β",
     91    expected: "a\uFFFDβ",
     92    name: "Prefixed unpaired low surrogate and UTF-16",
     93  },
     94 
     95  {
     96    head: "a",
     97    tail: "\uDE03",
     98    expected: "a\uFFFD",
     99    name: "Latin1 and unpaired low surrogate",
    100  },
    101  {
    102    head: "a",
    103    tail: "\uDE03b",
    104    expected: "a\uFFFDb",
    105    name: "Latin1 and suffixed unpaired low surrogate",
    106  },
    107  {
    108    head: "α",
    109    tail: "\uDE03",
    110    expected: "α\uFFFD",
    111    name: "UTF-16 and unpaired low surrogate",
    112  },
    113  {
    114    head: "α",
    115    tail: "\uDE03b",
    116    expected: "α\uFFFDb",
    117    name: "UTF-16 and suffixed unpaired low surrogate",
    118  },
    119 
    120  {
    121    head: "a",
    122    tail: "\uD83D",
    123    expected: "a\uFFFD",
    124    name: "Latin1 and unpaired high surrogate",
    125  },
    126  {
    127    head: "a",
    128    tail: "\uD83Db",
    129    expected: "a\uFFFDb",
    130    name: "Latin1 and suffixed unpaired high surrogate",
    131  },
    132  {
    133    head: "α",
    134    tail: "\uD83D",
    135    expected: "α\uFFFD",
    136    name: "UTF-16 and unpaired high surrogate",
    137  },
    138  {
    139    head: "α",
    140    tail: "\uD83Db",
    141    expected: "α\uFFFDb",
    142    name: "UTF-16 and suffixed unpaired high surrogate",
    143  },
    144 ];
    145 
    146 assertEq(isSameCompartment(newRope, this), true);
    147 
    148 function checkUtf8Equal(first, second) {
    149  var firstBuffer = new Uint8Array(first.length * 3);
    150  var secondBuffer = new Uint8Array(second.length * 3);
    151 
    152  var [firstRead, firstWritten] = encodeAsUtf8InBuffer(first, firstBuffer);
    153  var [secondRead, secondWritten] = encodeAsUtf8InBuffer(second, secondBuffer);
    154 
    155  assertEq(first.length, firstRead);
    156  assertEq(second.length, secondRead);
    157 
    158  assertEq(firstWritten, secondWritten);
    159 
    160  for (var i = 0; i < firstWritten; ++i) {
    161    assertEq(firstBuffer[i], secondBuffer[i]);
    162  }
    163 }
    164 
    165 concat.forEach(function(t) {
    166  var filler = "012345678901234567890123456789";
    167  var rope = newRope(t.head, newRope(t.tail, filler));
    168  checkUtf8Equal(rope, t.expected + filler);
    169 });
    170 
    171 {
    172  var filler = "012345678901234567890123456789";
    173 
    174  var a = newRope(filler, "a");
    175  var ab = newRope(a, "b");
    176  var abc = newRope(ab, "c");
    177 
    178  var e = newRope(filler, "e");
    179  var ef = newRope(e, "f");
    180  var def = newRope("d", ef);
    181 
    182  var abcdef = newRope(abc, def);
    183  var abcdefab = newRope(abcdef, ab);
    184  checkUtf8Equal(
    185    abcdefab,
    186    "012345678901234567890123456789abcd012345678901234567890123456789ef012345678901234567890123456789ab"
    187  );
    188 }
    189 
    190 {
    191  var filler = "012345678901234567890123456789";
    192 
    193  var right = newRope("\ude0a", filler);
    194  var rope = newRope("\ud83d", right);
    195  checkUtf8Equal(rope, "\ud83d\ude0a012345678901234567890123456789");
    196 }
    197 
    198 if (typeof reportCompare === "function")
    199  reportCompare(true, true);