Advertisement
giovani-rubim

UTF-8

Oct 10th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const utf8 = {
  2.     encode: (string, output = []) => {
  3.         const {length} = string;
  4.         let j = 0;
  5.         for (let i=0; i<length; ++i) {
  6.             const code = string.charCodeAt(i);
  7.             if (code < (0b10000000)) {
  8.                 output[j++] = code;
  9.             } else if (code < (1 << 11)) {
  10.                 output[j++] = 0b11000000|(code >>> 6);
  11.                 output[j++] = 0b10000000|(code & 0b00111111);
  12.             } else {
  13.                 output[j++] = 0b11100000|(code >>> 12);
  14.                 output[j++] = 0b10000000|((code >>> 6) & 0b00111111);
  15.                 output[j++] = 0b10000000|(code & 0b00111111);
  16.             }
  17.         }
  18.         return output;
  19.     },
  20.     decode: (bytes) => {
  21.         let i = 0;
  22.         let string = '';
  23.         const {length} = bytes;
  24.         while (i < length) {
  25.             const code = bytes[i++];
  26.             if (code < 0b10000000) {
  27.                 string += String.fromCharCode(code);
  28.             } else if (code < 0b11100000) {
  29.                 string += String.fromCharCode(((code & 0b00011111) << 6)
  30.                     |(bytes[i++] & 0b00111111));
  31.             } else {
  32.                 const mid = bytes[i++];
  33.                 string += String.fromCharCode(((code & 0b00011111) << 12)
  34.                     |((mid & 0b00111111) << 6)
  35.                     |(bytes[i++] & 0b00111111));
  36.             }
  37.         }
  38.         return string;
  39.     }
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement