[前][次][番号順一覧][スレッド一覧]

ruby-changes:46440

From: duerst <ko1@a...>
Date: Thu, 4 May 2017 11:00:24 +0900 (JST)
Subject: [ruby-changes:46440] duerst:r58555 (trunk): move definition of String#unicode_normalized? to C to make sure it is documented

duerst	2017-05-04 11:00:19 +0900 (Thu, 04 May 2017)

  New Revision: 58555

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58555

  Log:
    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

  Modified files:
    trunk/lib/unicode_normalize/normalize.rb
    trunk/lib/unicode_normalize.rb
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 58554)
+++ string.c	(revision 58555)
@@ -9583,6 +9583,7 @@ str_scrub_bang(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/string.c#L9583
 }
 
 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, https://github.com/ruby/ruby/blob/trunk/string.c#L9646
 	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) https://github.com/ruby/ruby/blob/trunk/string.c#L10326
     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);
Index: lib/unicode_normalize.rb
===================================================================
--- lib/unicode_normalize.rb	(revision 58554)
+++ lib/unicode_normalize.rb	(revision 58555)
@@ -6,27 +6,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/unicode_normalize.rb#L6
 #--
 # 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.
Index: lib/unicode_normalize/normalize.rb
===================================================================
--- lib/unicode_normalize/normalize.rb	(revision 58554)
+++ lib/unicode_normalize/normalize.rb	(revision 58555)
@@ -158,9 +158,3 @@ module UnicodeNormalize  # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/unicode_normalize/normalize.rb#L158
     end
   end
 end # module
-
-class String  # :nodoc:
-  def unicode_normalized?(form = :nfc)
-    UnicodeNormalize.normalized?(self, form)
-  end
-end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]