-
Notifications
You must be signed in to change notification settings - Fork 156
Fix: Use lazy encoding for utf-8 encoded string comparison #2299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
714f02b
Use lazy encoding for utf-8 encoded string comparison
milaGGL 52cfb54
use compareBlobs function
milaGGL dcf4df7
add unit test
milaGGL 3fe9ce3
format, change the compareUtf8Strings to match other SDKs
milaGGL 17fc594
rename unit test
milaGGL 0eb67b2
fix unit test to not include invalid surrogate
milaGGL ae9d73f
port changes from java
milaGGL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,3 +284,219 @@ describe('Order', () => { | |
} | ||
}); | ||
}); | ||
|
||
class StringPair { | ||
constructor( | ||
readonly s1: string, | ||
readonly s2: string | ||
) {} | ||
} | ||
|
||
class StringPairGenerator { | ||
constructor(private stringGenerator: StringGenerator) {} | ||
|
||
next(): StringPair { | ||
const prefix = this.stringGenerator.next(); | ||
const s1 = prefix + this.stringGenerator.next(); | ||
const s2 = prefix + this.stringGenerator.next(); | ||
return new StringPair(s1, s2); | ||
} | ||
} | ||
|
||
class StringGenerator { | ||
private static readonly DEFAULT_SURROGATE_PAIR_PROBABILITY = 0.33; | ||
private static readonly DEFAULT_MAX_LENGTH = 20; | ||
|
||
private readonly rnd: Random; | ||
private readonly surrogatePairProbability: number; | ||
private readonly maxLength: number; | ||
|
||
constructor(seed: number); | ||
constructor(rnd: Random, surrogatePairProbability: number, maxLength: number); | ||
constructor( | ||
seedOrRnd: number | Random, | ||
surrogatePairProbability?: number, | ||
maxLength?: number | ||
) { | ||
if (typeof seedOrRnd === 'number') { | ||
this.rnd = new Random(seedOrRnd); | ||
this.surrogatePairProbability = | ||
StringGenerator.DEFAULT_SURROGATE_PAIR_PROBABILITY; | ||
this.maxLength = StringGenerator.DEFAULT_MAX_LENGTH; | ||
} else { | ||
this.rnd = seedOrRnd; | ||
this.surrogatePairProbability = StringGenerator.validateProbability( | ||
surrogatePairProbability! | ||
); | ||
this.maxLength = StringGenerator.validateLength(maxLength!); | ||
} | ||
} | ||
|
||
private static validateProbability(probability: number): number { | ||
if (!Number.isFinite(probability)) { | ||
throw new Error( | ||
`invalid surrogate pair probability: ${probability} (must be between 0.0 and 1.0, inclusive)` | ||
); | ||
} else if (probability < 0.0) { | ||
throw new Error( | ||
`invalid surrogate pair probability: ${probability} (must be greater than or equal to zero)` | ||
); | ||
} else if (probability > 1.0) { | ||
throw new Error( | ||
`invalid surrogate pair probability: ${probability} (must be less than or equal to 1)` | ||
); | ||
} | ||
return probability; | ||
} | ||
|
||
private static validateLength(length: number): number { | ||
if (length < 0) { | ||
throw new Error( | ||
`invalid maximum string length: ${length} (must be greater than or equal to zero)` | ||
); | ||
} | ||
return length; | ||
} | ||
|
||
next(): string { | ||
const length = this.rnd.nextInt(this.maxLength + 1); | ||
const sb = new StringBuilder(); | ||
while (sb.length() < length) { | ||
const codePoint = this.nextCodePoint(); | ||
sb.appendCodePoint(codePoint); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private isNextSurrogatePair(): boolean { | ||
return StringGenerator.nextBoolean(this.rnd, this.surrogatePairProbability); | ||
} | ||
|
||
private static nextBoolean(rnd: Random, probability: number): boolean { | ||
if (probability === 0.0) { | ||
return false; | ||
} else if (probability === 1.0) { | ||
return true; | ||
} else { | ||
return rnd.nextFloat() < probability; | ||
} | ||
} | ||
|
||
private nextCodePoint(): number { | ||
if (this.isNextSurrogatePair()) { | ||
return this.nextSurrogateCodePoint(); | ||
} else { | ||
return this.nextNonSurrogateCodePoint(); | ||
} | ||
} | ||
|
||
private nextSurrogateCodePoint(): number { | ||
const highSurrogateMin = 0xd800; | ||
const highSurrogateMax = 0xdbff; | ||
const lowSurrogateMin = 0xdc00; | ||
const lowSurrogateMax = 0xdfff; | ||
|
||
const highSurrogate = this.nextCodePointRange( | ||
highSurrogateMin, | ||
highSurrogateMax | ||
); | ||
const lowSurrogate = this.nextCodePointRange( | ||
lowSurrogateMin, | ||
lowSurrogateMax | ||
); | ||
|
||
return (highSurrogate - 0xd800) * 0x400 + (lowSurrogate - 0xdc00) + 0x10000; | ||
} | ||
|
||
private nextNonSurrogateCodePoint(): number { | ||
let codePoint; | ||
do { | ||
codePoint = this.nextCodePointRange(0, 0xffff); // BMP range | ||
} while (codePoint >= 0xd800 && codePoint <= 0xdfff); // Exclude surrogate range | ||
|
||
return codePoint; | ||
} | ||
|
||
private nextCodePointRange(min: number, max: number): number { | ||
const rangeSize = max - min + 1; | ||
const offset = this.rnd.nextInt(rangeSize); | ||
return min + offset; | ||
} | ||
} | ||
|
||
class Random { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use |
||
private seed: number; | ||
|
||
constructor(seed: number) { | ||
this.seed = seed; | ||
} | ||
|
||
nextInt(max: number): number { | ||
this.seed = (this.seed * 9301 + 49297) % 233280; | ||
const rnd = this.seed / 233280; | ||
return Math.floor(rnd * max); | ||
} | ||
|
||
nextFloat(): number { | ||
this.seed = (this.seed * 9301 + 49297) % 233280; | ||
return this.seed / 233280; | ||
} | ||
} | ||
|
||
class StringBuilder { | ||
private buffer: string[] = []; | ||
|
||
append(str: string): StringBuilder { | ||
this.buffer.push(str); | ||
return this; | ||
} | ||
|
||
appendCodePoint(codePoint: number): StringBuilder { | ||
this.buffer.push(String.fromCodePoint(codePoint)); | ||
return this; | ||
} | ||
|
||
toString(): string { | ||
return this.buffer.join(''); | ||
} | ||
|
||
length(): number { | ||
return this.buffer.join('').length; | ||
} | ||
} | ||
|
||
describe('CompareUtf8Strings', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: You can move the StringPair, StringPairGenerator, StringBuilder, etc classes into this |
||
it('compareUtf8Strings should return correct results', () => { | ||
const errors = []; | ||
const seed = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); | ||
let passCount = 0; | ||
const stringGenerator = new StringGenerator(new Random(seed), 0.33, 20); | ||
const stringPairGenerator = new StringPairGenerator(stringGenerator); | ||
|
||
for (let i = 0; i < 1000000 && errors.length < 10; i++) { | ||
const {s1, s2} = stringPairGenerator.next(); | ||
|
||
const actual = order.compareUtf8Strings(s1, s2); | ||
const expected = Buffer.from(s1, 'utf8').compare(Buffer.from(s2, 'utf8')); | ||
|
||
if (actual === expected) { | ||
passCount++; | ||
} else { | ||
errors.push( | ||
`compareUtf8Strings(s1="${s1}", s2="${s2}") returned ${actual}, ` + | ||
`but expected ${expected} (i=${i}, s1.length=${s1.length}, s2.length=${s2.length})` | ||
); | ||
} | ||
} | ||
|
||
if (errors.length > 0) { | ||
console.error( | ||
`${errors.length} test cases failed, ${passCount} test cases passed, seed=${seed};` | ||
); | ||
errors.forEach((error, index) => | ||
console.error(`errors[${index}]: ${error}`) | ||
); | ||
throw new Error('Test failed'); | ||
} | ||
}).timeout(20000); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.