Sha256: d974bb6bd24e555b1c17991147ff409635f82491b974145df25a5cf63088da2e

Contents?: true

Size: 1.38 KB

Versions: 28

Compression:

Stored size: 1.38 KB

Contents

function con(b) {
  if ((b & 0xc0) === 0x80) {
    return b & 0x3f;
  } else {
    throw new Error("invalid UTF-8 encoding");
  }
}

function code(min, n) {
  if (n < min || (0xd800 <= n && n < 0xe000) || n >= 0x10000) {
    throw new Error("invalid UTF-8 encoding");
  } else {
    return n;
  }
}

export function decode(bytes) {
  return _decode(bytes)
    .map(x => String.fromCharCode(x))
    .join("");
}

function _decode(bytes) {
  if (bytes.length === 0) {
    return [];
  }

  /**
   * 1 byte
   */
  {
    const [b1, ...bs] = bytes;

    if (b1 < 0x80) {
      return [code(0x0, b1), ..._decode(bs)];
    }

    if (b1 < 0xc0) {
      throw new Error("invalid UTF-8 encoding");
    }
  }

  /**
   * 2 bytes
   */
  {
    const [b1, b2, ...bs] = bytes;

    if (b1 < 0xe0) {
      return [code(0x80, ((b1 & 0x1f) << 6) + con(b2)), ..._decode(bs)];
    }
  }

  /**
   * 3 bytes
   */
  {
    const [b1, b2, b3, ...bs] = bytes;

    if (b1 < 0xf0) {
      return [
        code(0x800, ((b1 & 0x0f) << 12) + (con(b2) << 6) + con(b3)),
        ..._decode(bs)
      ];
    }
  }

  /**
   * 4 bytes
   */
  {
    const [b1, b2, b3, b4, ...bs] = bytes;

    if (b1 < 0xf8) {
      return [
        code(
          0x10000,
          ((((b1 & 0x07) << 18) + con(b2)) << 12) + (con(b3) << 6) + con(b4)
        ),
        ..._decode(bs)
      ];
    }
  }

  throw new Error("invalid UTF-8 encoding");
}

Version data entries

28 entries across 27 versions & 9 rubygems

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