1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
# Adapted from CRuby
unless String.method_defined? :unicode_normalize
if (Regexp.compile("[\u{11100}-\u{11102}]") rescue false)
class String
def unicode_normalize(form = :nfc)
require 'backports/tools/normalize' unless defined? UnicodeNormalize
## The following line can be uncommented to avoid repeated checking for
## UnicodeNormalize. However, tests didn't show any noticeable speedup
## when doing this. This comment also applies to the commented out lines
## in String#unicode_normalize! and String#unicode_normalized?.
# String.send(:define_method, :unicode_normalize, ->(form = :nfc) { UnicodeNormalize.normalize(self, form) } )
UnicodeNormalize.normalize(self, form)
end
def unicode_normalize!(form = :nfc)
require 'backports/tools/normalize' unless defined? UnicodeNormalize
# String.send(:define_method, :unicode_normalize!, ->(form = :nfc) { replace(unicode_normalize(form)) } )
replace(unicode_normalize(form))
end
def unicode_normalized?(form = :nfc)
require 'backports/tools/normalize' unless defined? UnicodeNormalize
# String.send(:define_method, :unicode_normalized?, ->(form = :nfc) { UnicodeNormalize.normalized?(self, form) } )
UnicodeNormalize.normalized?(self, form)
end
end
end
end
|