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

.downcase(Unicode::CaseOptions::Fold) using char ranges #4512

Merged

Conversation

akzhan
Copy link
Contributor

@akzhan akzhan commented Jun 4, 2017

The Fold option is required for correct Unicode string comparison using fold case.

Reimplementation of #4511 using char ranges.

Less memory used.

private def self.check_downcase_fold(char, options)
if options.fold?
result = search_ranges(casefold_ranges, char.ord)
return [char.ord + result] if result
Copy link
Member

Choose a reason for hiding this comment

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

Use a tuple to avoid a memory allocation:

return {char.ord + result} if result

@akzhan
Copy link
Contributor Author

akzhan commented Jun 5, 2017

String.compare will be reworked by another PR

@akzhan akzhan force-pushed the string-and-char-casefold-using-ranges branch from 2f940e5 to 4dcaab3 Compare June 7, 2017 20:17
@akzhan
Copy link
Contributor Author

akzhan commented Jun 9, 2017

Is it GTG?

I need these changes to continue Unicode support.

Travis build is broken somewhere unrelated to PR.

@asterite
Copy link
Member

@akzhan I'm not sure why this is useful. Where would one use it? Plus, if it's to solve another issue (the one about unicode normalization) I would send a single PR targeting that. Is there any other language where you can downcase and specify to fold case?

@akzhan
Copy link
Contributor Author

akzhan commented Jun 11, 2017

@asterite full Unicode case mapping added in Ruby 2.4 with String.downcase(:fold).

Take a note that case folding is not usual normalization, see Section 3.13 of the Unicode standard.

Also X.casefold() == Y.casefold() in Python 3 implements the "default caseless matching" (D144).

Real case fold comparison doing like:

def NFD(text):
    return unicodedata.normalize('NFD', text)

def canonical_caseless(text):
    return NFD(NFD(text).casefold())

@akzhan
Copy link
Contributor Author

akzhan commented Jun 11, 2017

String.normalize will be subject of another PR.

@asterite
Copy link
Member

@akzhan Thank you! I'll review this today or during this week and probably merge it.

@asterite asterite merged commit 96e7222 into crystal-lang:master Jun 11, 2017
@asterite asterite added this to the Next milestone Jun 11, 2017
@akzhan akzhan deleted the string-and-char-casefold-using-ranges branch June 11, 2017 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants