Skip to content

Commit

Permalink
&.downcase(Unicode::CaseOptions::Fold) options for casefolded compari…
Browse files Browse the repository at this point in the history
…son of strings and characters.
  • Loading branch information
akzhan committed Jun 4, 2017
1 parent a93ea60 commit 5952165
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec"
require "unicode"

describe "Char" do
describe "upcase" do
Expand All @@ -9,6 +10,7 @@ describe "Char" do
describe "downcase" do
it { 'A'.downcase.should eq('a') }
it { '1'.downcase.should eq('1') }
it { '\u00df'.downcase(Unicode::CaseOptions::Fold) { |equiv| equiv.should eq('s') } }
end

describe "succ" do
Expand Down
4 changes: 4 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ describe "String" do
it { "AEIİOU".downcase(Unicode::CaseOptions::Turkic).should eq("aeıiou") }
it { "ÁEÍOÚ".downcase(Unicode::CaseOptions::ASCII).should eq("ÁeÍoÚ") }
it { "İ".downcase.should eq("") }
it { "Baffle".downcase(Unicode::CaseOptions::Fold).should eq("baffle") }
it { "".downcase(Unicode::CaseOptions::Fold).should eq("ff") }
it { "tschüß".downcase(Unicode::CaseOptions::Fold).should eq("tschüss") }
it { "ΣίσυφοςfiÆ".downcase(Unicode::CaseOptions::Fold).should eq("σίσυφοσfiæ") }
end

describe "upcase" do
Expand Down
19 changes: 19 additions & 0 deletions src/unicode/unicode.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module Unicode
# 'ı'.upcase(Unicode::CaseOptions::Turkic) # => 'I'
# ```
Turkic

# Unicode case folding, which is more far-reaching than Unicode case mapping.
Fold
end

def self.upcase(char : Char, options : CaseOptions)
Expand Down Expand Up @@ -94,6 +97,9 @@ module Unicode
result = check_downcase_turkic(char, options)
return result if result

results = check_downcase_fold(char, options)
return results[0].unsafe_chr if results && results.size == 1

check_downcase_ranges(char)
end

Expand All @@ -110,6 +116,12 @@ module Unicode
return
end

result = check_downcase_fold(char, options)
if result
result.each { |c| yield c.unsafe_chr if c != 0 }
return
end

result = special_cases_downcase[char.ord]?
if result
result.each { |c| yield c.unsafe_chr if c != 0 }
Expand Down Expand Up @@ -141,6 +153,13 @@ module Unicode
nil
end

private def self.check_downcase_fold(char, options)
if options.fold?
return fold_cases[char.ord]?
end
nil
end

private def self.check_downcase_ranges(char)
result = search_ranges(downcase_ranges, char.ord)
return char + result if result
Expand Down

0 comments on commit 5952165

Please sign in to comment.