Sha256: 3174a5e240c23b13d8b217e8b49813668c624dd17bbc0b63ba10ef26be0bb327

Contents?: true

Size: 713 Bytes

Versions: 28

Compression:

Stored size: 713 Bytes

Contents

function con(n) {
  return 0x80 | (n & 0x3f);
}

export function encode(str) {
  const arr = str.split("").map(x => x.charCodeAt(0));
  return _encode(arr);
}

function _encode(arr) {
  if (arr.length === 0) {
    return [];
  }

  const [n, ...ns] = arr;

  if (n < 0) {
    throw new Error("utf8");
  }

  if (n < 0x80) {
    return [n, ..._encode(ns)];
  }

  if (n < 0x800) {
    return [0xc0 | (n >>> 6), con(n), ..._encode(ns)];
  }

  if (n < 0x10000) {
    return [0xe0 | (n >>> 12), con(n >>> 6), con(n), ..._encode(ns)];
  }

  if (n < 0x110000) {
    return [
      0xf0 | (n >>> 18),
      con(n >>> 12),
      con(n >>> 6),
      con(n),
      ..._encode(ns)
    ];
  }

  throw new Error("utf8");
}

Version data entries

28 entries across 27 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.18.0 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.18.2 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.16.1 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.15.2 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.18.4 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.18.1 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.14.0 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
tang-0.2.1 spec/tang_app/node_modules/@webassemblyjs/utf8/src/encoder.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/@webassemblyjs/utf8/src/encoder.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/@webassemblyjs/utf8/src/encoder.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/@webassemblyjs/utf8/src/encoder.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/@webassemblyjs/utf8/src/encoder.js
tang-0.2.0 spec/tang_app/node_modules/@webassemblyjs/utf8/src/encoder.js
tang-0.1.0 spec/tang_app/node_modules/@webassemblyjs/utf8/src/encoder.js
tang-0.0.9 spec/tang_app/node_modules/@webassemblyjs/utf8/src/encoder.js
enju_library-0.3.8 spec/dummy/node_modules/@webassemblyjs/utf8/src/encoder.js
jester-data-8.0.0 node_modules/@webassemblyjs/utf8/src/encoder.js