Skip to content
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

Use full case folding outside of character classes #383

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

natecook1000
Copy link
Member

This should get us part way to proper full case folding support. To handle case folding that results in multiple characters, we'll need to switch to more of a sequence-based comparison than just matching a single character.

This should get us part way to proper full case folding support. To
handle case folding that results in multiple characters, we'll need
to switch to more of a sequence-based comparison than just matching
a single character.
@natecook1000 natecook1000 requested a review from Azoy May 6, 2022 15:39
Copy link
Contributor

@Azoy Azoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

guard #available(SwiftStdlib 5.7, *) else { fatalError() }
let foldedSelf = unicodeScalars.map(\.properties._caseFolded).joined()
let foldedOther = c.unicodeScalars.map(\.properties._caseFolded).joined()
return foldedSelf == foldedOther
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of intermediary arrays, which is unfortunate because nearly all Characters fit in the small-string form, and those that don't are usually case invariant. Does .lazy.map(...).elementsEqual( work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we really want is a stdlib API that can do the case-folded comparison — it's wasteful even to UTF-8 encode these when we should be able to convert, canonicalize, and compare character-by-character.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can write the code here or in the stdlib (I agree stdlib SPI makes sense). But in the meantime, can we write the code we want the stdlib to have, or at least approximate it by making the mapping lazy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, it looks like case-insensitive matching could do multiple array allocations for every single character in the input

@natecook1000 natecook1000 marked this pull request as ready for review May 9, 2022 16:23
@natecook1000
Copy link
Member Author

@swift-ci Please test

@stephentyrone
Copy link
Contributor

Nate, what's the status of this PR now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants