Skip to content

Commit 140560e

Browse files
committed
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
1 parent 0efb15c commit 140560e

3 files changed

Lines changed: 38 additions & 30 deletions

File tree

lib/unicode_normalize.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,6 @@
66
#--
77
# additions to class String for Unicode normalization
88
#++
9-
class String
10-
# :call-seq:
11-
# str.unicode_normalized?(form=:nfc)
12-
#
13-
# Checks whether +str+ is in Unicode normalization form +form+,
14-
# which can be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
15-
# The default is +:nfc+.
16-
#
17-
# If the string is not in a Unicode Encoding, then an Exception is raised.
18-
# For details, see String#unicode_normalize.
19-
#
20-
# "a\u0300".unicode_normalized? #=> false
21-
# "a\u0300".unicode_normalized?(:nfd) #=> true
22-
# "\u00E0".unicode_normalized? #=> true
23-
# "\u00E0".unicode_normalized?(:nfd) #=> false
24-
# "\xE0".force_encoding('ISO-8859-1').unicode_normalized?
25-
# #=> Encoding::CompatibilityError raised
26-
#
27-
def unicode_normalized?(form = :nfc)
28-
require 'unicode_normalize/normalize.rb'
29-
unicode_normalized? form
30-
end
31-
end
329

10+
###### This file is empty after it's contents has been converted to C
11+
###### and moved to string.c. This file will be removed soon.

lib/unicode_normalize/normalize.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,3 @@ def self.normalized?(string, form = :nfc)
158158
end
159159
end
160160
end # module
161-
162-
class String # :nodoc:
163-
def unicode_normalized?(form = :nfc)
164-
UnicodeNormalize.normalized?(self, form)
165-
end
166-
end

string.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9583,6 +9583,7 @@ str_scrub_bang(int argc, VALUE *argv, VALUE str)
95839583
}
95849584

95859585
static VALUE id_normalize;
9586+
static VALUE id_normalized_p;
95869587
static VALUE mUnicodeNormalize;
95879588
static int UnicodeNormalizeRequired = 0;
95889589

@@ -9645,6 +9646,38 @@ rb_str_unicode_normalize_bang(int argc, VALUE *argv, VALUE str)
96459646
rb_raise(rb_eArgError, "too many arguments to unicode_normalize!");
96469647
}
96479648

9649+
/* call-seq:
9650+
* str.unicode_normalized?(form=:nfc)
9651+
*
9652+
* Checks whether +str+ is in Unicode normalization form +form+,
9653+
* which can be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
9654+
* The default is +:nfc+.
9655+
*
9656+
* If the string is not in a Unicode Encoding, then an Exception is raised.
9657+
* For details, see String#unicode_normalize.
9658+
*
9659+
* "a\u0300".unicode_normalized? #=> false
9660+
* "a\u0300".unicode_normalized?(:nfd) #=> true
9661+
* "\u00E0".unicode_normalized? #=> true
9662+
* "\u00E0".unicode_normalized?(:nfd) #=> false
9663+
* "\xE0".force_encoding('ISO-8859-1').unicode_normalized?
9664+
* #=> Encoding::CompatibilityError raised
9665+
*/
9666+
static VALUE
9667+
rb_str_unicode_normalized_p(int argc, VALUE *argv, VALUE str)
9668+
{
9669+
if (!UnicodeNormalizeRequired) {
9670+
rb_require("unicode_normalize/normalize.rb");
9671+
UnicodeNormalizeRequired = 1;
9672+
}
9673+
if (argc==0)
9674+
return rb_funcall(mUnicodeNormalize, id_normalized_p, 1, str);
9675+
else if (argc==1)
9676+
return rb_funcall(mUnicodeNormalize, id_normalized_p, 2, str, argv[0]);
9677+
else
9678+
rb_raise(rb_eArgError, "too many arguments to unicode_normalized?");
9679+
}
9680+
96489681
/**********************************************************************
96499682
* Document-class: Symbol
96509683
*
@@ -10293,12 +10326,14 @@ Init_String(void)
1029310326
rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
1029410327
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
1029510328

10296-
/* define module here so that we don't have to look it up */
10329+
/* define UnicodeNormalize module here so that we don't have to look it up */
1029710330
mUnicodeNormalize = rb_define_module("UnicodeNormalize");
1029810331
id_normalize = rb_intern("normalize");
10332+
id_normalized_p = rb_intern("normalized?");
1029910333

1030010334
rb_define_method(rb_cString, "unicode_normalize", rb_str_unicode_normalize, -1);
1030110335
rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
10336+
rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
1030210337

1030310338
rb_fs = Qnil;
1030410339
rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);

0 commit comments

Comments
 (0)