mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
move definition of String#unicode_normalized? to C to make sure it is documented
* lib/unicode_normalize.rb: Remove definition of String#unicode_normalized? (including documentation). Leave a comment explaining that the file is now empty. * string.c: Define String#unicode_normalized? in rb_str_unicode_normalized_p in C, (including documentation) * lib/unicode_normalize/normalize.rb: Remove (re)definition of String#unicode_normalized? to avoid warnings (when $VERBOSE==true) and problems when String is frozen git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58555 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0efb15cda2
commit
140560e4ee
3 changed files with 38 additions and 30 deletions
|
@ -6,27 +6,6 @@
|
|||
#--
|
||||
# additions to class String for Unicode normalization
|
||||
#++
|
||||
class String
|
||||
# :call-seq:
|
||||
# str.unicode_normalized?(form=:nfc)
|
||||
#
|
||||
# Checks whether +str+ is in Unicode normalization form +form+,
|
||||
# which can be 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.
|
||||
#
|
||||
# "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?
|
||||
# #=> Encoding::CompatibilityError raised
|
||||
#
|
||||
def unicode_normalized?(form = :nfc)
|
||||
require 'unicode_normalize/normalize.rb'
|
||||
unicode_normalized? form
|
||||
end
|
||||
end
|
||||
|
||||
###### This file is empty after it's contents has been converted to C
|
||||
###### and moved to string.c. This file will be removed soon.
|
||||
|
|
|
@ -158,9 +158,3 @@ module UnicodeNormalize # :nodoc:
|
|||
end
|
||||
end
|
||||
end # module
|
||||
|
||||
class String # :nodoc:
|
||||
def unicode_normalized?(form = :nfc)
|
||||
UnicodeNormalize.normalized?(self, form)
|
||||
end
|
||||
end
|
||||
|
|
37
string.c
37
string.c
|
@ -9583,6 +9583,7 @@ str_scrub_bang(int argc, VALUE *argv, VALUE str)
|
|||
}
|
||||
|
||||
static VALUE id_normalize;
|
||||
static VALUE id_normalized_p;
|
||||
static VALUE mUnicodeNormalize;
|
||||
static int UnicodeNormalizeRequired = 0;
|
||||
|
||||
|
@ -9645,6 +9646,38 @@ rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
|
|||
rb_raise(rb_eArgError, "too many arguments to unicode_normalize!");
|
||||
}
|
||||
|
||||
/* call-seq:
|
||||
* str.unicode_normalized?(form=:nfc)
|
||||
*
|
||||
* Checks whether +str+ is in Unicode normalization form +form+,
|
||||
* which can be 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.
|
||||
*
|
||||
* "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?
|
||||
* #=> Encoding::CompatibilityError raised
|
||||
*/
|
||||
static VALUE
|
||||
rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
|
||||
{
|
||||
if (!UnicodeNormalizeRequired) {
|
||||
rb_require("unicode_normalize/normalize.rb");
|
||||
UnicodeNormalizeRequired = 1;
|
||||
}
|
||||
if (argc==0)
|
||||
return rb_funcall(mUnicodeNormalize, id_normalized_p, 1, str);
|
||||
else if (argc==1)
|
||||
return rb_funcall(mUnicodeNormalize, id_normalized_p, 2, str, argv[0]);
|
||||
else
|
||||
rb_raise(rb_eArgError, "too many arguments to unicode_normalized?");
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
* Document-class: Symbol
|
||||
*
|
||||
|
@ -10293,12 +10326,14 @@ Init_String(void)
|
|||
rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
|
||||
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
|
||||
|
||||
/* define module here so that we don't have to look it up */
|
||||
/* define UnicodeNormalize module here so that we don't have to look it up */
|
||||
mUnicodeNormalize = rb_define_module("UnicodeNormalize");
|
||||
id_normalize = rb_intern("normalize");
|
||||
id_normalized_p = rb_intern("normalized?");
|
||||
|
||||
rb_define_method(rb_cString, "unicode_normalize", rb_str_unicode_normalize, -1);
|
||||
rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
|
||||
rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
|
||||
|
||||
rb_fs = Qnil;
|
||||
rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
|
||||
|
|
Loading…
Reference in a new issue