Created
December 20, 2010 17:17
-
-
Save hsribei/748665 to your computer and use it in GitHub Desktop.
downcase with accents for latin1 (sort of)
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
# -*- coding: utf-8 -*- | |
class String | |
def downcase_with_accents | |
return nil if s.nil? | |
norm = self.downcase | |
norm.tr!('ÁÉÍÓÚÇ', 'aeiouç') | |
norm.tr!('ÀÈÌÒÙ', 'aeiou') | |
norm.tr!('ÄËÏÖÜ', 'aeiou') | |
norm.tr!('ÂÊÎÔÛ', 'aeiou') | |
norm.tr!('áéíóú', 'aeiou') | |
norm.tr!('àèìòù', 'aeiou') | |
norm.tr!('äëïöü', 'aeiou') | |
norm.tr!('âêîôû', 'aeiou') | |
norm | |
end mapping = { | |
'Á' => 'á', | |
'É' => 'é', | |
'Í' => 'í', | |
'Ó' => 'ó', | |
'Ú' => 'ú', | |
'À' => 'à', | |
'Â' => 'â', | |
'Ê' => 'ê', | |
'Ô' => 'ô', | |
'Ã' => 'ã', | |
'Õ' => 'õ', | |
'Ç' => 'ç' | |
} | |
mapping.inject(self.downcase_without_accents) { |str, m| | |
str.tr(m[0], m[1]) | |
} | |
end | |
alias :downcase_without_accents :downcase | |
alias :downcase :downcase_with_accents | |
def capitalize_with_accents | |
self[0..0] + self[1..-1].downcase | |
end | |
alias :capitalize_without_accents :capitalize | |
alias :capitalize :capitalize_with_accents | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment