Skip to content

Instantly share code, notes, and snippets.

@fadeev
Created September 11, 2011 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadeev/1209552 to your computer and use it in GitHub Desktop.
Save fadeev/1209552 to your computer and use it in GitHub Desktop.
Downcase, upcase and “delatin” functions for strings in cyrillic
# encoding: utf-8
class String
def cyr_downcase
self
.unpack("U*")
.map { |c| if (1040..1071) === c then (c + 32) elsif (1025 == c) then 1105 else c end }
.pack("U*")
end
def cyr_upcase
self
.unpack("U*")
.map { |c| if (1072..1103) === c then (c - 32) elsif (1105 == c) then 1025 else c end }
.pack("U*")
end
def cyr_delatin
# Some latin symbols look like cyrillic ones.
# String#cyr_delatin substitutes the former with the latter.
h = {
'A' => 'А', 'B' => 'В', 'E' => 'Е', 'K' => 'К', 'M' => 'М', 'H' => 'Н', 'O' => 'О',
'P' => 'Р', 'C' => 'С', 'T' => 'Т', 'X' => 'Х',
'a' => 'а', 'e' => 'е', 'y' => 'у', 'x' => 'х'
}
self.chars.map { |c| if h.has_key?(c) then h[c] else c end }.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment