This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// larger buffer => faster | |
const BUFFER_SIZE = 1024; | |
let buffer; | |
/** | |
* @param {string} text | |
* @returns {number} | |
*/ | |
function utf8Length(text) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm ls $(jq -r '.packages | to_entries[] | select(.value.deprecated != null) | .key | split("node_modules/")[-1]' package-lock.json) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.16; | |
import "./StateMachines.sol"; | |
contract Pausable is StateMachines { | |
StateMachine m = initial("unpaused"); | |
event Paused(bool paused); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function* chain<T, U>(g: AsyncGenerator<T, void>, f: (x: T) => AsyncGenerator<U, void>): AsyncGenerator<U, void> { | |
type R<I> = IteratorResult<I extends number ? U : T, void>; | |
type IR<I> = [I, R<I>]; | |
const k = <I>(i: I) => (res: R<I>): IR<I> => [i, res]; | |
const running = new Set<AsyncGenerator<unknown, void>>([g]); | |
let p = g.next().then(k(null)); | |
const ps: Promise<IR<number>>[] = []; | |
const gs: AsyncGenerator<U, void>[] = []; | |
const queue = new Set<Promise<IR<null> | IR<number>>>([p]); | |
try { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function* concurrently<T>(...gs: AsyncGenerator<T, void>[]): AsyncGenerator<T, void> { | |
const k = (i: number) => (res: IteratorResult<T>) => [i, res] as const; | |
const running = new Set(gs); | |
try { | |
const ps = gs.map((g, i) => g.next().then(k(i))); | |
const queue = new Set(ps); | |
while (queue.size > 0) { | |
const [i, res] = await Promise.race(queue); | |
const g = gs[i]!; | |
queue.delete(ps[i]!); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.0; | |
function keccak256_2(bytes memory data) external pure returns (bytes32) { | |
bytes32 h1 = keccak256(data); | |
bytes32 h2; | |
/// @solidity memory-safe-assembly | |
assembly { | |
mstore(0, h1) | |
h2 := keccak256(0, 32) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
error Overflow(); | |
/// Multiply-and-divide without intermediate overflow. | |
/// @return r = (x * y) / d | |
function mulDiv(uint x, uint y, uint d) pure returns (uint r) { unchecked { | |
(uint z1, uint z0) = mul_1_1_2(x, y); | |
(r,) = div_2_1_1(z1, z0, d); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
error Overflow(); | |
uint constant b = 2 ** 128; | |
/// Divides a two word number by one word number. | |
/// Returns q and r such that u1 << 256 + u0 = q * v + r. | |
function divlu(uint u1, uint u0, uint v) pure returns (uint q, uint r) { unchecked { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nlz(uint x) pure returns (uint n) { | |
uint y; | |
n = 256; | |
y = x >> 128; if (y != 0) { n = n - 128; x = y; } | |
y = x >> 64; if (y != 0) { n = n - 64; x = y; } | |
y = x >> 32; if (y != 0) { n = n - 32; x = y; } | |
y = x >> 16; if (y != 0) { n = n - 16; x = y; } | |
y = x >> 8; if (y != 0) { n = n - 8; x = y; } | |
y = x >> 4; if (y != 0) { n = n - 4; x = y; } | |
y = x >> 2; if (y != 0) { n = n - 2; x = y; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned divlu(unsigned u1, unsigned u0, unsigned v, | |
unsigned *r) { | |
const unsigned b = 65536; // Number base (16 bits). | |
unsigned un1, un0, // Norm. dividend LSD's. | |
vn1, vn0, // Norm. divisor digits. | |
q1, q0, // Quotient digits. | |
un32, un21, un10, // Divident digit pairs. | |
rhat; // A remainder. | |
int s; // Shift amount for norm. |