1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

normalize.rb: trivial optimizations

* lib/unicode_normalize/normalize.rb (nfc_one, normalize): trivial
  optimizations.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48309 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-11-06 15:00:17 +00:00
parent 64034372b7
commit b8788417f0

View file

@ -106,7 +106,7 @@ module UnicodeNormalize
string string
end end
def self.nfc_one (string) def self.nfc_one(string)
nfd_string = nfd_one string nfd_string = nfd_one string
start = nfd_string[0] start = nfd_string[0]
last_class = CLASS_TABLE[start]-1 last_class = CLASS_TABLE[start]-1
@ -116,7 +116,7 @@ module UnicodeNormalize
if last_class<accent_class and composite = COMPOSITION_TABLE[start+accent] if last_class<accent_class and composite = COMPOSITION_TABLE[start+accent]
start = composite start = composite
else else
accents += accent accents << accent
last_class = accent_class last_class = accent_class
end end
end end
@ -125,7 +125,8 @@ module UnicodeNormalize
def self.normalize(string, form = :nfc) def self.normalize(string, form = :nfc)
encoding = string.encoding encoding = string.encoding
if encoding == Encoding::UTF_8 case encoding
when Encoding::UTF_8
case form case form
when :nfc then when :nfc then
string.gsub REGEXP_C, NF_HASH_C string.gsub REGEXP_C, NF_HASH_C
@ -138,9 +139,9 @@ module UnicodeNormalize
else else
raise ArgumentError, "Invalid normalization form #{form}." raise ArgumentError, "Invalid normalization form #{form}."
end end
elsif encoding == Encoding::US_ASCII when Encoding::US_ASCII
string string
elsif UNICODE_ENCODINGS.include? encoding when *UNICODE_ENCODINGS
normalize(string.encode(Encoding::UTF_8), form).encode(encoding) normalize(string.encode(Encoding::UTF_8), form).encode(encoding)
else else
raise Encoding::CompatibilityError, "Unicode Normalization not appropriate for #{encoding}" raise Encoding::CompatibilityError, "Unicode Normalization not appropriate for #{encoding}"