Skip to content

Instantly share code, notes, and snippets.

let hex = new Uint8Array(0);
const td = new TextDecoder();
const te = new TextEncoder();
function packBigInt(blocks, blockSize) {
function encode(blocks, length) {
const u32 = new Uint32Array(hex.buffer);
let k = (length >> 2) - 1;//TODO:!?
@Yaffle
Yaffle / jsHelper64.js
Created March 29, 2023 08:11
jsHelper64.js
const uint64 = new BigUint64Array(1);
const uint32 = new Uint32Array(uint64.buffer);
function clz64(bigint) {
uint64[0] = bigint;
let x = Math.clz32(uint32[1]);
let y = Math.clz32(uint32[0]);
return x + (x === 32 ? y : 0);
}
@Yaffle
Yaffle / wast2wasm.js
Last active March 18, 2023 11:13
conversion of wast to wasm (WebAssembley Text Format to WebAssemlby binary-code) - no dependencies
/*jshint esversion:6, bitwise:false*/
// https://webassembly.github.io/wabt/demo/wat2wasm/index.html helps a lot
function wast2wasm(sExpression) {
function sExpressionToJSON(s) {
return JSON.parse(s.trim().replace(/\s*\)/g, ']').replace(/\(\s*/g, '[').replace(/\s+/g, ',').replaceAll('"', '').replace(/([^\[\],]+)/g, '"$1"'));
}
// CPU L2 cache size
function ASMModule1(stdlib, foreign, heap) {
"use asm";
var memory = new stdlib.Uint32Array(heap);
function loop(c) {
c = c | 0;
var k = 0;
var j = 0;
for (j = c; (j | 0) != 0; j = (j - 1) | 0) {
k = memory[k >> 2] << 2;
@Yaffle
Yaffle / continuedFractionForSqrt.js
Created September 6, 2021 10:53
continued fraction for sqrt(n)
function* continuedFractionForSqrt(n) {
// https://trizenx.blogspot.com/2018/10/continued-fraction-factorization-method.html
function floorDiv(a, b) {
return typeof a === "bigint" && typeof b === "bigint" ? a / b : Math.floor(a / b);
}
n = BigInt(n);
if (n < 0n) {
throw new RangeError();
}
const root = BigInt(sqrt(n));
@Yaffle
Yaffle / test.js
Last active September 8, 2024 12:56
get/set caret position in contenteditable element
// Usage:
// var x = document.querySelector('[contenteditable]');
// var caretPosition = getSelectionDirection(x) !== 'forward' ? getSelectionStart(x) : getSelectionEnd(x);
// setSelectionRange(x, caretPosition + 1, caretPosition + 1);
// var value = getValue(x);
// it will not work with "<img /><img />" and, perhaps, in many other cases.
function isAfter(container, offset, node) {
var c = node;
We couldn’t find that file to show.
@Yaffle
Yaffle / decodeURIComponent.js
Last active April 23, 2020 10:01
decodeURIComponent using TextDecoder
// Implementation of the standard decodeURIComponent using TextDecoder.
// The original JavaScript specification does not separate UTF-8 decoding this way.
// The purpose is to show the possible implementation in JavaScript language.
function hexDigit(code) {
if (code >= '0'.charCodeAt(0) && code <= '9'.charCodeAt(0)) {
return code - '0'.charCodeAt(0);
} else if (code >= 'A'.charCodeAt(0) && code <= 'F'.charCodeAt(0)) {
return code + (10 - 'A'.charCodeAt(0));
} else if (code >= 'a'.charCodeAt(0) && code <= 'f'.charCodeAt(0)) {
/*jslint strict:global*/
/*global self, fetch, caches, console, Promise, Request*/
"use strict";
var CACHE_NAME_PREFIX = "ru~";
var CACHE_NAME = "ru~20180818T075107Z";
var URLS_TO_CACHE = "./;js.js?20180818T075107Z;css.css?20180818T075107";
self.addEventListener("install", function (event) {
function replaceSimpleDigit(code) {
if (code >= 0x0660 && code <= 0x669) {
return {code: code - 0x0660 + 48, name: "arab"};
}
if (code >= 0x06F0 && code <= 0x6f9) {
return {code: code - 0x06F0 + 48, name: "arabext"};
}
if (code >= 0x0966 && code <= 0x96f) {
return {code: code - 0x0966 + 48, name: "deva"};
}