diff --git a/README.md b/README.md index d93f3f8..a7dfce0 100644 --- a/README.md +++ b/README.md @@ -185,8 +185,8 @@ Globals (`src/lib.ts`): (`error`, `suppressed`) - `Temporal`, the ISO 8601 calendar's part: `Duration`, `Instant`, `PlainDate`, `PlainTime`, `PlainDateTime` and `Now` (`instant`, `plainDateTimeISO`, `plainDateISO`, `plainTimeISO`), with - `from` (ISO 8601 text), `compare`, `add`, `subtract`, `until`, `since`, `equals`, `toString` and - `toJSON`, and `_with` for `with`, a keyword here. Not here: `ZonedDateTime`, `PlainYearMonth`, + `from` (ISO 8601 text), `compare`, `add`, `subtract`, `until`, `since`, `equals`, `toString`, + `toJSON` and `with`. Not here: `ZonedDateTime`, `PlainYearMonth`, `PlainMonthDay`, time zones other than UTC and the system's present offset, other calendars, and the options bags: `until` and `since` give their default largest unit, and an overflowing day is constrained to the month's last. Without a date to count from, a `Duration` with years, months or diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 44a19cb..3aa164f 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -675,20 +675,10 @@ class Array { count = length - start; } - // The removed elements are moved out, not read: they go to the end, the first of them - // last, and are popped off in order. Two reversals take them there - of everything from - // `start`, then of what stays - where each used to be swapped down one place at a time. - const end: int = length; + // the built-in splice gives back the removed elements, which the array no longer owns + const removed = this.data.splice(start, count); const from: int = start; - const kept: int = end - count; - if (count > 0) { - __reverseRange(this.data, from, end); - __reverseRange(this.data, from, kept); - } - - const removed: T[] = []; - for (let k = 0; k < count; k++) - removed.push(this.data.pop()); + const kept: int = this.data.length; // a few items go in one at a time, as in unshift if (items.length < 8) { @@ -861,7 +851,7 @@ class Array { } @sharesElements - public _with(index: int, value: T) { + public with(index: int, value: T) { return this.data._with(index, value); } @@ -1279,8 +1269,7 @@ class TypedArray { return this.slice().sort(callbackfn); } - // `with`, which is a keyword here, as Array's is - public _with(index: int, value: T): TypedArray { + public with(index: int, value: T): TypedArray { if (index < 0) { index = index + this.count; } @@ -1316,76 +1305,36 @@ type Float32Array = TypedArray; type Float64Array = TypedArray; // IEEE 754 binary16, the element of Float16Array and of DataView's getFloat16 and setFloat16, and -// what Math.f16round rounds to. Converted here, bit by bit, not through an f16: LLVM lowers that to -// the __truncsfhf2 and __extendhfsf2 helpers, which the MSVC runtime does not have. Generic only so -// that a program that never uses them does not get them (see sleep). +// what Math.f16round rounds to: an f16, kept as its bits. Generic only so that a program that never +// uses them does not get them (see sleep). namespace __Float16 { - function bitsOf(x: number): u64 { - let value: f64 = x; - let bits: u64 = 0; - memcpy(Ref(bits), Ref(value), 8); - return bits; - } - // the nearest binary16, ties to even, as JavaScript rounds: too large is Infinity, too small 0 function encode(x: number): u16 { - const b = bitsOf(x); - const sign = ((b >> 48) & 0x8000); - const exponent = ((b >> 52) & 0x7FF); - const fraction: u64 = b & 0xFFFFFFFFFFFFF; - if (exponent == 0x7FF) { - return fraction != 0 ? 0x7E00 : sign | 0x7C00; - } - - const e = exponent - 1023 + 15; - if (e >= 31) { - return sign | 0x7C00; - } - - if (e <= 0) { - // a subnormal, or 0: the 53 bits, the leading 1 among them, shifted down to 10 - const shift = 43 - e; - if (shift > 53) { - return sign; - } - - const full: u64 = fraction | 0x10000000000000; - let h = full >> shift; - const rest = full & ((1 << shift) - 1); - const half = 1 << (shift - 1); - if (rest > half || (rest == half && (h & 1) != 0)) h++; - return sign | h; - } - - // a carry out of the fraction goes into the exponent, which is right: up to Infinity - let h: u64 = (e << 10) | (fraction >> 42); - const rest = fraction & 0x3FFFFFFFFFF; - if (rest > 0x20000000000 || (rest == 0x20000000000 && (h & 1) != 0)) h++; - return sign | h; + let half: f16 = x; + let bits: u16 = 0; + memcpy(Ref(bits), Ref(half), 2); + return bits; } function decode(bits: u16): number { - const e = (bits >> 10) & 0x1F; - const m = bits & 0x3FF; - let value: number; - if (e == 0) { - value = m * Math.pow(2, -24); - } else if (e == 31) { - value = m != 0 ? Number.NaN : Number.POSITIVE_INFINITY; - } else { - value = (1 + m / 1024) * Math.pow(2, e - 15); - } - - // times -1, not -value: the compiler's unary minus is 0 - value, which makes -0 of 0 - return (bits & 0x8000) != 0 ? value * -1 : value; + let given: u16 = bits; + let half: f16 = 0; + memcpy(Ref(half), Ref(given), 2); + return half; } } -// A ConvertingTypedArray's element on the way in and out: S, the kept element, says which kind it -// is (see ConvertingTypedArray). Functions here, not static methods: a generic class's static -// generic method cannot be called. -namespace __ConvertedElement { - function encode(value: number): E { +// The two typed arrays whose element is not a C type: Uint8ClampedArray keeps a byte, and a number +// written to it is clamped into 0..255 and rounded half to even, where Uint8Array wraps it; +// Float16Array keeps the bits of a binary16 (see __Float16). Each element is a `number` to the +// program, as in JavaScript, and is converted on the way in and out. S, the kept element, tells the +// two apart: u8 is Uint8ClampedArray, u16 is Float16Array. Otherwise as TypedArray: a window that +// subarray() shares, a fixed length, and the same methods. +class ConvertingTypedArray { + private elements: TypedArray; + + // an element on the way in and out: E, the kept element, says which kind it is (see above) + private static encode(value: number): E { if (sizeof() == 1) { // JavaScript's ToUint8Clamp if (!(value > 0)) return 0; @@ -1400,28 +1349,18 @@ namespace __ConvertedElement { return __Float16.encode(value); } - function decode(element: E): number { + private static decode(element: E): number { if (sizeof() == 1) { return element; } return __Float16.decode(element); } -} - -// The two typed arrays whose element is not a C type: Uint8ClampedArray keeps a byte, and a number -// written to it is clamped into 0..255 and rounded half to even, where Uint8Array wraps it; -// Float16Array keeps the bits of a binary16 (see __Float16). Each element is a `number` to the -// program, as in JavaScript, and is converted on the way in and out. S, the kept element, tells the -// two apart: u8 is Uint8ClampedArray, u16 is Float16Array. Otherwise as TypedArray: a window that -// subarray() shares, a fixed length, and the same methods, with `_with` for `with`. -class ConvertingTypedArray { - private elements: TypedArray; constructor(data: number[] = []) { this.elements = new TypedArray(data.length); for (let i = 0; i < data.length; i++) { - this.elements[i] = __ConvertedElement.encode(data[i]); + this.elements[i] = ConvertingTypedArray.encode(data[i]); } } @@ -1439,11 +1378,11 @@ class ConvertingTypedArray { // outside the window a read gives 0 and a write does nothing, as TypedArray's public get(index: int): number { - return __ConvertedElement.decode(this.elements[index]); + return ConvertingTypedArray.decode(this.elements[index]); } public set(index: int, value: number) { - this.elements[index] = __ConvertedElement.encode(value); + this.elements[index] = ConvertingTypedArray.encode(value); } public get length() { @@ -1543,7 +1482,7 @@ class ConvertingTypedArray { // the value is converted once, as JavaScript does public fill(value: number, start = 0, end = this.length): ConvertingTypedArray { - this.elements.fill(__ConvertedElement.encode(value), start, end); + this.elements.fill(ConvertingTypedArray.encode(value), start, end); return this; } @@ -1716,8 +1655,7 @@ class ConvertingTypedArray { return this.slice().sort(callbackfn); } - // `with`, which is a keyword here - public _with(index: int, value: number): ConvertingTypedArray { + public with(index: int, value: number): ConvertingTypedArray { if (index < 0) { index = index + this.length; } diff --git a/src/lib.d.ts b/src/lib.d.ts index a2fbbb0..08beab3 100644 --- a/src/lib.d.ts +++ b/src/lib.d.ts @@ -967,7 +967,7 @@ declare namespace Temporal { subtract(duration: Duration): PlainDate; until(other: PlainDate): Duration; since(other: PlainDate): Duration; - _with(fields: { year?: int, month?: int, day?: int }): PlainDate; + with(fields: { year?: int, month?: int, day?: int }): PlainDate; equals(other: PlainDate): boolean; toPlainDateTime(time?: PlainTime): PlainDateTime; toString(): string; @@ -994,7 +994,7 @@ declare namespace Temporal { subtract(duration: Duration): PlainTime; until(other: PlainTime): Duration; since(other: PlainTime): Duration; - _with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime; + with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime; equals(other: PlainTime): boolean; toString(): string; toJSON(): string; @@ -1031,7 +1031,7 @@ declare namespace Temporal { subtract(duration: Duration): PlainDateTime; until(other: PlainDateTime): Duration; since(other: PlainDateTime): Duration; - _with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int, + with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainDateTime; equals(other: PlainDateTime): boolean; toPlainDate(): PlainDate; diff --git a/src/lib.ts b/src/lib.ts index ace95c9..7ed33af 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -4272,8 +4272,8 @@ export namespace Temporal { return new Duration(0, 0, 0, this.__epochDays() - other.__epochDays()); } - // `with`, which is a keyword here, as Array's is: a copy with the fields given replaced - public _with(fields: { year?: int, month?: int, day?: int }): PlainDate { + // a copy with the fields given replaced + public with(fields: { year?: int, month?: int, day?: int }): PlainDate { const year = fields.year != undefined ? fields.year : this.isoYear; const month = fields.month != undefined ? fields.month : this.isoMonth; let day = fields.day != undefined ? fields.day : this.isoDay; @@ -4366,8 +4366,8 @@ export namespace Temporal { return spanOfTime(this).minus(spanOfTime(other)).toDuration(HOURS); } - // `with`, a copy with the fields given replaced - public _with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime { + // a copy with the fields given replaced + public with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime { return new PlainTime( fields.hour != undefined ? fields.hour : this.parts[0], fields.minute != undefined ? fields.minute : this.parts[1], @@ -4470,8 +4470,8 @@ export namespace Temporal { return this.span().minus(other.span()).toDuration(DAYS); } - // `with`, a copy with the fields given replaced - public _with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int, + // a copy with the fields given replaced + public with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainDateTime { const year = fields.year != undefined ? fields.year : this.date.year; const month = fields.month != undefined ? fields.month : this.date.month; diff --git a/tests/temporal_plain.ts b/tests/temporal_plain.ts index d8a3b98..36fc8e8 100644 --- a/tests/temporal_plain.ts +++ b/tests/temporal_plain.ts @@ -30,7 +30,7 @@ function main() { assert(date.since(from).toString() == "P19782D", "since"); assert(Temporal.PlainDate.compare(from, date) == -1 && date.equals(Temporal.PlainDate.from("2024-02-29")), "compare and equals"); - assert(date._with({ month: 4, day: 31 }).toString() == "2024-04-30", "with, constrained"); + assert(date.with({ month: 4, day: 31 }).toString() == "2024-04-30", "with, constrained"); assert(new Temporal.PlainDate(-44, 3, 15).toString() == "-000044-03-15", "an expanded year"); assert(Temporal.PlainDate.from("1969-12-31").dayOfWeek == 3, "a Wednesday before 1970"); @@ -41,7 +41,7 @@ function main() { assert(time.add(Temporal.Duration.from("PT11H")).toString() == "00:45:30.25", "around midnight"); assert(time.subtract(Temporal.Duration.from("PT14H")).toString() == "23:45:30.25", "back around midnight"); assert(new Temporal.PlainTime(8).until(time).toString() == "PT5H45M30.25S", "until, in hours and smaller"); - assert(time._with({ second: 0, millisecond: 0 }).toString() == "13:45:00", "with"); + assert(time.with({ second: 0, millisecond: 0 }).toString() == "13:45:00", "with"); const dt = Temporal.PlainDateTime.from("2024-12-31T23:30"); assert(dt.year == 2024 && dt.hour == 23 && dt.minute == 30, "date and time fields"); diff --git a/tests/typed_array_clamped.ts b/tests/typed_array_clamped.ts index 8568c42..17ac23a 100644 --- a/tests/typed_array_clamped.ts +++ b/tests/typed_array_clamped.ts @@ -35,7 +35,7 @@ function main() { assert(filled.join() == "255,255,255", "fill clamps"); const doubled = given.map((v: number, i: number) => v * 2); assert(doubled.join() == "0,255,255", "map clamps"); - const changed = given._with(0, 77.5); + const changed = given.with(0, 77.5); assert(changed[0] == 78 && given[0] == 0, "with clamps, and copies"); // subarray shares the elements, slice copies them diff --git a/tests/typed_array_methods.ts b/tests/typed_array_methods.ts index 782f770..5ffd555 100644 --- a/tests/typed_array_methods.ts +++ b/tests/typed_array_methods.ts @@ -44,7 +44,7 @@ function main() { assert(ints.filter((v) => v % 2 == 0).join() == "2,4", "filter"); assert(ints.toReversed().join() == "4,3,2,1" && ints.join() == "1,2,3,4", "toReversed leaves it"); assert(new Int32Array([3, 1, 2]).toSorted().join() == "1,2,3", "toSorted"); - assert(ints._with(-1, 9).join() == "1,2,3,9" && ints[3] == 4, "with"); + assert(ints.with(-1, 9).join() == "1,2,3,9" && ints[3] == 4, "with"); // in place, each giving the array itself const g = new Int16Array([1, 2, 3, 4, 5]);