feat(core): virtual file system and Swift string semantics

The core gains Swift's virtual file system (paths, case-insensitive
lookup that keeps the spelling, quotas, atomic changes, the defaults of
a new device) and the string behaviour the ported code relies on without
saying so. Both are measured in the Swift oracle: 11 file system cases
(the three Swift tests among them) step by step, and trimming, lower
case, equality, ordering and Base64 case by case — whitespace and control
characters over all of Unicode.

Found by measuring:
- Swift trims NEL and ZERO WIDTH SPACE, not the BOM; JS trim() does the
  opposite. The .fls import now trims with swiftTrim.
- lowercased() maps scalar by scalar: a word-final sigma stays σ.
- Foundation decodes "====" and "Y===" as empty data.
- Foundation on Linux counts 24,735 plane-14 scalars as control
  characters, a table artefact; the port keeps Unicode Cc+Cf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 21:23:45 +00:00
co-authored by Claude Opus 5.5
parent 51a83ea68d
commit a198d10b4a
15 changed files with 5247 additions and 18 deletions
+96
View File
@@ -0,0 +1,96 @@
// Swift String behaviour, measured in the Swift core (strings.swift.json,
// tools/swift-oracle --strings): every case must come out the same here.
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import {
base64Encode,
containsSwiftControlCharacter,
swiftBase64Decode,
swiftCompare,
swiftEqual,
swiftLowercased,
swiftTrim,
} from '../src/index.js';
const here = dirname(fileURLToPath(import.meta.url));
const cases = JSON.parse(readFileSync(join(here, 'strings.cases.json'), 'utf8')) as {
trim: string[];
lowercased: string[];
equal: [string, string][];
sort: string[][];
base64: string[];
controlCharacters: string[];
};
const swift = JSON.parse(readFileSync(join(here, 'strings.swift.json'), 'utf8')) as {
trim: string[];
lowercased: string[];
equal: boolean[];
sort: string[][];
base64: (number[] | null)[];
controlCharacters: boolean[];
whitespacesAndNewlinesSet: [number, number][];
controlCharactersSet: [number, number][];
};
/** The scalars for which `test` holds, as ranges [first, last] — the oracle's shape. */
function scalarRanges(test: (scalar: string) => boolean): [number, number][] {
const ranges: [number, number][] = [];
for (let value = 0; value <= 0x10ffff; value++) {
if (value >= 0xd800 && value <= 0xdfff) continue;
if (!test(String.fromCodePoint(value))) continue;
const last = ranges.at(-1);
if (last !== undefined && last[1] === value - 1) last[1] = value;
else ranges.push([value, value]);
}
return ranges;
}
describe('Swift string semantics', () => {
it('trims like trimmingCharacters(in: .whitespacesAndNewlines)', () => {
expect(cases.trim.map(swiftTrim)).toEqual(swift.trim);
});
it('lower-cases like lowercased()', () => {
expect(cases.lowercased.map(swiftLowercased)).toEqual(swift.lowercased);
});
it('compares like ==', () => {
expect(cases.equal.map(([lhs, rhs]) => swiftEqual(lhs, rhs))).toEqual(swift.equal);
});
it('sorts like sorted()', () => {
expect(cases.sort.map((list) => [...list].sort(swiftCompare))).toEqual(swift.sort);
});
it('decodes Base64 like Data(base64Encoded:options: .ignoreUnknownCharacters)', () => {
expect(cases.base64.map((text) => {
const bytes = swiftBase64Decode(text);
return bytes === undefined ? null : [...bytes];
})).toEqual(swift.base64);
});
it('finds control characters like CharacterSet.controlCharacters', () => {
expect(cases.controlCharacters.map(containsSwiftControlCharacter)).toEqual(swift.controlCharacters);
});
it('trims exactly the scalars Swift trims, all of Unicode', () => {
expect(scalarRanges((scalar) => swiftTrim(`${scalar}a`) === 'a')).toEqual(swift.whitespacesAndNewlinesSet);
});
it('counts the same scalars as control characters, except Foundation\'s plane-14 artefact', () => {
const below = (ranges: [number, number][]) => ranges.filter(([first]) => first < 0xe0100);
expect(scalarRanges(containsSwiftControlCharacter).filter(([first]) => first < 0xe0100)).toEqual(
below(swift.controlCharactersSet).map(([first, last]) => [first, Math.min(last, 0xe00ff)]),
);
});
it('encodes Base64 so that it decodes again', () => {
for (const length of [0, 1, 2, 3, 4, 5, 255]) {
const bytes = Uint8Array.from({ length }, (_, index) => (index * 37) & 0xff);
expect(swiftBase64Decode(base64Encode(bytes))).toEqual(bytes);
}
});
});