-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(marshal)!: compare strings by codepoint
- Loading branch information
Showing
8 changed files
with
269 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable no-bitwise, @endo/restrict-comparison-operands */ | ||
import { Fail, q } from '@endo/errors'; | ||
|
||
import { | ||
makeEncodePassable, | ||
makeDecodePassable, | ||
} from '../src/encodePassable.js'; | ||
import { compareRank, makeComparatorKit } from '../src/rankOrder.js'; | ||
|
||
const buffers = { | ||
__proto__: null, | ||
r: [], | ||
'?': [], | ||
'!': [], | ||
}; | ||
const resetBuffers = () => { | ||
buffers.r = []; | ||
buffers['?'] = []; | ||
buffers['!'] = []; | ||
}; | ||
const cursors = { | ||
__proto__: null, | ||
r: 0, | ||
'?': 0, | ||
'!': 0, | ||
}; | ||
const resetCursors = () => { | ||
cursors.r = 0; | ||
cursors['?'] = 0; | ||
cursors['!'] = 0; | ||
}; | ||
|
||
const encodeThing = (prefix, r) => { | ||
buffers[prefix].push(r); | ||
// With this encoding, all things with the same prefix have the same rank | ||
return prefix; | ||
}; | ||
|
||
const decodeThing = (prefix, e) => { | ||
prefix === e || | ||
Fail`expected encoding ${q(e)} to simply be the prefix ${q(prefix)}`; | ||
(cursors[prefix] >= 0 && cursors[prefix] < buffers[prefix].length) || | ||
Fail`while decoding ${q(e)}, expected cursors[${q(prefix)}], i.e., ${q( | ||
cursors[prefix], | ||
)} <= ${q(buffers[prefix].length)}`; | ||
const thing = buffers[prefix][cursors[prefix]]; | ||
cursors[prefix] += 1; | ||
return thing; | ||
}; | ||
|
||
const encodePassableInternal = makeEncodePassable({ | ||
encodeRemotable: r => encodeThing('r', r), | ||
encodePromise: p => encodeThing('?', p), | ||
encodeError: er => encodeThing('!', er), | ||
}); | ||
|
||
export const encodePassableInternal2 = makeEncodePassable({ | ||
encodeRemotable: r => encodeThing('r', r), | ||
encodePromise: p => encodeThing('?', p), | ||
encodeError: er => encodeThing('!', er), | ||
format: 'compactOrdered', | ||
}); | ||
|
||
export const encodePassable = passable => { | ||
resetBuffers(); | ||
return encodePassableInternal(passable); | ||
}; | ||
|
||
export const encodePassable2 = passable => { | ||
resetBuffers(); | ||
return encodePassableInternal2(passable); | ||
}; | ||
export const decodePassableInternal = makeDecodePassable({ | ||
decodeRemotable: e => decodeThing('r', e), | ||
decodePromise: e => decodeThing('?', e), | ||
decodeError: e => decodeThing('!', e), | ||
}); | ||
|
||
export const decodePassable = encoded => { | ||
resetCursors(); | ||
return decodePassableInternal(encoded); | ||
}; | ||
|
||
const compareRemotables = (x, y) => | ||
compareRank(encodeThing('r', x), encodeThing('r', y)); | ||
|
||
export const { comparator: compareFull } = makeComparatorKit(compareRemotables); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import test from '@endo/ses-ava/prepare-endo.js'; | ||
|
||
import { compareRank } from '../src/rankOrder.js'; | ||
import { encodePassable } from './encodePassable-for-testing.js'; | ||
|
||
/** | ||
* Essentially a ponyfill for Array.prototype.toSorted, for use before | ||
* we can always rely on the platform to provide it. | ||
* | ||
* @param {string[]} strings | ||
* @param {( | ||
* left: string, | ||
* right: string | ||
* ) => import('../src/types.js').RankComparison} comp | ||
* @returns {string[]} | ||
*/ | ||
const sorted = (strings, comp) => [...strings].sort(comp); | ||
|
||
test('unicode code point order', t => { | ||
// Test case from | ||
// https://icu-project.org/docs/papers/utf16_code_point_order.html | ||
const str0 = '\u{ff61}'; | ||
const str3 = '\u{d800}\u{dc02}'; | ||
|
||
// str1 and str2 become impossible examples once we prohibit | ||
// non - well - formed strings. | ||
// See https://github.com/endojs/endo/pull/2002 | ||
const str1 = '\u{d800}X'; | ||
const str2 = '\u{d800}\u{ff61}'; | ||
|
||
// harden to ensure it is not sorted in place, just for sanity | ||
const strs = harden([str0, str1, str2, str3]); | ||
|
||
/** | ||
* @param {string} left | ||
* @param {string} right | ||
* @returns {import('../src/types.js').RankComparison} | ||
*/ | ||
const nativeComp = (left, right) => | ||
// eslint-disable-next-line no-nested-ternary | ||
left < right ? -1 : left > right ? 1 : 0; | ||
|
||
const nativeSorted = sorted(strs, nativeComp); | ||
|
||
t.deepEqual(nativeSorted, [str1, str3, str2, str0]); | ||
|
||
const rankSorted = sorted(strs, compareRank); | ||
|
||
t.deepEqual(rankSorted, [str1, str2, str0, str3]); | ||
|
||
const nativeEncComp = (left, right) => | ||
nativeComp(encodePassable(left), encodePassable(right)); | ||
|
||
const nativeEncSorted = sorted(strs, nativeEncComp); | ||
|
||
t.deepEqual(nativeEncSorted, nativeSorted); | ||
|
||
const rankEncComp = (left, right) => | ||
compareRank(encodePassable(left), encodePassable(right)); | ||
|
||
const rankEncSorted = sorted(strs, rankEncComp); | ||
|
||
t.deepEqual(rankEncSorted, rankSorted); | ||
}); |
This file contains 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
Oops, something went wrong.