2014-10-19 11:29:58 -04:00
|
|
|
# coding: utf-8
|
2014-10-18 21:04:10 -04:00
|
|
|
|
2014-10-18 21:08:13 -04:00
|
|
|
# Copyright Ayumu Nojima (野島 歩) and Martin J. Dürst (duerst@it.aoyama.ac.jp)
|
2014-10-18 21:04:10 -04:00
|
|
|
|
2014-10-19 04:26:35 -04:00
|
|
|
# additions to class String for Unicode normalization
|
2014-10-18 21:04:10 -04:00
|
|
|
class String
|
2014-10-19 04:26:35 -04:00
|
|
|
# === Unicode Normalization
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# str.unicode_normalize(form=:nfc)
|
|
|
|
#
|
|
|
|
# Returns a normalized form of +str+, using Unicode normalizations
|
|
|
|
# NFC, NFD, NFKC, or NFKD. The normalization form used is determined
|
|
|
|
# by +form+, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd.
|
|
|
|
# The default is :nfc.
|
|
|
|
#
|
|
|
|
# If the string is not in a Unicode Encoding, then an Exception is raised.
|
|
|
|
# In this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE,
|
2014-10-19 04:37:40 -04:00
|
|
|
# and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything
|
|
|
|
# else than UTF-8 is implemented by converting to UTF-8,
|
|
|
|
# which makes it slower than UTF-8.
|
2014-10-19 04:26:35 -04:00
|
|
|
#
|
|
|
|
# _Examples_
|
|
|
|
#
|
|
|
|
# "a\u0300".unicode_normalize #=> 'à' (same as "\u00E0")
|
|
|
|
# "a\u0300".unicode_normalize(:nfc) #=> 'à' (same as "\u00E0")
|
|
|
|
# "\u00E0".unicode_normalize(:nfd) #=> 'à' (same as "a\u0300")
|
|
|
|
# "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
|
|
|
|
# #=> Encoding::CompatibilityError raised
|
|
|
|
#
|
2014-10-18 21:12:26 -04:00
|
|
|
def unicode_normalize(form = :nfc)
|
2014-10-19 04:54:08 -04:00
|
|
|
require 'unicode_normalize/normalize.rb' 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?.
|
2014-10-19 23:37:39 -04:00
|
|
|
# String.send(:define_method, :unicode_normalize, ->(form = :nfc) { UnicodeNormalize.normalize(self, form) } )
|
2014-10-18 21:10:12 -04:00
|
|
|
UnicodeNormalize.normalize(self, form)
|
2014-10-18 21:04:10 -04:00
|
|
|
end
|
|
|
|
|
2014-10-19 04:26:35 -04:00
|
|
|
# :call-seq:
|
|
|
|
# str.unicode_normalize(form=:nfc)
|
|
|
|
#
|
|
|
|
# Destructive version of String#unicode_normalize, doing Unicode
|
|
|
|
# normalization in place.
|
|
|
|
#
|
2014-10-18 21:12:26 -04:00
|
|
|
def unicode_normalize!(form = :nfc)
|
2014-10-19 04:54:08 -04:00
|
|
|
require 'unicode_normalize/normalize.rb' unless defined? UnicodeNormalize
|
2014-10-19 23:37:39 -04:00
|
|
|
# String.send(:define_method, :unicode_normalize!, ->(form = :nfc) { replace(unicode_normalize(form)) } )
|
2014-10-19 23:46:50 -04:00
|
|
|
replace(unicode_normalize(form))
|
2014-10-18 21:04:10 -04:00
|
|
|
end
|
|
|
|
|
2014-10-19 04:26:35 -04:00
|
|
|
# :call-seq:
|
|
|
|
# str.unicode_normalized?(form=:nfc)
|
|
|
|
#
|
|
|
|
# Checks whether +str+ is in Unicode normalization form +form+,
|
|
|
|
# which is any of the four values :nfc, :nfd, :nfkc, or :nfkd.
|
|
|
|
# The default is :nfc.
|
|
|
|
#
|
|
|
|
# If the string is not in a Unicode Encoding, then an Exception is raised.
|
|
|
|
# For details, see String#unicode_normalize.
|
|
|
|
#
|
|
|
|
# _Examples_
|
|
|
|
#
|
|
|
|
# "a\u0300".unicode_normalized? #=> false
|
|
|
|
# "a\u0300".unicode_normalized?(:nfd) #=> true
|
|
|
|
# "\u00E0".unicode_normalized? #=> true
|
|
|
|
# "\u00E0".unicode_normalized?(:nfd) #=> false
|
|
|
|
# "\xE0".force_encoding('ISO-8859-1').unicode_normalized?
|
2014-10-19 04:54:08 -04:00
|
|
|
# #=> Encoding::CompatibilityError raised
|
2014-10-19 04:26:35 -04:00
|
|
|
#
|
2014-10-18 21:12:26 -04:00
|
|
|
def unicode_normalized?(form = :nfc)
|
2014-10-19 04:54:08 -04:00
|
|
|
require 'unicode_normalize/normalize.rb' unless defined? UnicodeNormalize
|
|
|
|
# String.send(:define_method, :unicode_normalized?, ->(form = :nfc) { UnicodeNormalize.normalized?(self, form) } )
|
2014-10-18 21:10:12 -04:00
|
|
|
UnicodeNormalize.normalized?(self, form)
|
2014-10-18 21:04:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|