Skip to content

Commit 5fee67c

Browse files
committed
move definition of String#unicode_normalize to C to make sure it is documented
* lib/unicode_normalize.rb: Remove definition of String#unicode_normalize (including documentation) * string.c: Define String#unicode_normalize in rb_str_unicode_normalize in C, (including documentation) * lib/unicode_normalize/normalize.rb: Remove (re)definition of String#unicode_normalize to avoid warnings (when $VERBOSE==true) and problems when String is frozen git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58550 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 8b51a72 commit 5fee67c

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

lib/unicode_normalize.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,6 @@
88
#++
99
class String
1010

11-
# :call-seq:
12-
# str.unicode_normalize(form=:nfc)
13-
#
14-
# Unicode Normalization---Returns a normalized form of +str+,
15-
# using Unicode normalizations NFC, NFD, NFKC, or NFKD.
16-
# The normalization form used is determined by +form+, which can
17-
# be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
18-
# The default is +:nfc+.
19-
#
20-
# If the string is not in a Unicode Encoding, then an Exception is raised.
21-
# In this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE,
22-
# and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE.
23-
# Anything other than UTF-8 is implemented by converting to UTF-8,
24-
# which makes it slower than UTF-8.
25-
#
26-
# "a\u0300".unicode_normalize #=> 'à' (same as "\u00E0")
27-
# "a\u0300".unicode_normalize(:nfc) #=> 'à' (same as "\u00E0")
28-
# "\u00E0".unicode_normalize(:nfd) #=> 'à' (same as "a\u0300")
29-
# "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
30-
# #=> Encoding::CompatibilityError raised
31-
#
32-
def unicode_normalize(form = :nfc)
33-
require 'unicode_normalize/normalize.rb'
34-
unicode_normalize form # no recursion, because redefined in unicode_normalize/normalize.rb
35-
end
36-
3711
# :call-seq:
3812
# str.unicode_normalize!(form=:nfc)
3913
#

lib/unicode_normalize/normalize.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ def self.normalized?(string, form = :nfc)
160160
end # module
161161

162162
class String # :nodoc:
163-
def unicode_normalize(form = :nfc)
164-
UnicodeNormalize.normalize(self, form)
165-
end
166-
167163
def unicode_normalize!(form = :nfc)
168164
replace(UnicodeNormalize.normalize(self, form))
169165
end

string.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9582,6 +9582,48 @@ str_scrub_bang(int argc, VALUE *argv, VALUE str)
95829582
return str;
95839583
}
95849584

9585+
static VALUE id_normalize;
9586+
static VALUE mUnicodeNormalize;
9587+
static int UnicodeNormalizeRequired = 0;
9588+
9589+
/*
9590+
* call-seq:
9591+
* str.unicode_normalize(form=:nfc)
9592+
*
9593+
* Unicode Normalization---Returns a normalized form of +str+,
9594+
* using Unicode normalizations NFC, NFD, NFKC, or NFKD.
9595+
* The normalization form used is determined by +form+, which can
9596+
* be any of the four values +:nfc+, +:nfd+, +:nfkc+, or +:nfkd+.
9597+
* The default is +:nfc+.
9598+
*
9599+
* If the string is not in a Unicode Encoding, then an Exception is raised.
9600+
* In this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE,
9601+
* and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE.
9602+
* Anything other than UTF-8 is implemented by converting to UTF-8,
9603+
* which makes it slower than UTF-8.
9604+
*
9605+
* "a\u0300".unicode_normalize #=> 'à' (same as "\u00E0")
9606+
* "a\u0300".unicode_normalize(:nfc) #=> 'à' (same as "\u00E0")
9607+
* "\u00E0".unicode_normalize(:nfd) #=> 'à' (same as "a\u0300")
9608+
* "\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
9609+
* #=> Encoding::CompatibilityError raised
9610+
*/
9611+
static VALUE
9612+
rb_str_unicode_normalize(int argc, VALUE *argv, VALUE str)
9613+
{
9614+
if (!UnicodeNormalizeRequired) {
9615+
rb_require("unicode_normalize/normalize.rb");
9616+
UnicodeNormalizeRequired = 1;
9617+
}
9618+
/* return rb_funcall2(str, id_unicode_normalize, argc, argv); */
9619+
if (argc==0)
9620+
return rb_funcall(mUnicodeNormalize, id_normalize, 1, str);
9621+
else if (argc==1)
9622+
return rb_funcall(mUnicodeNormalize, id_normalize, 2, str, argv[0]);
9623+
else
9624+
rb_raise(rb_eArgError, "too many arguments to unicode_normalize");
9625+
}
9626+
95859627
/**********************************************************************
95869628
* Document-class: Symbol
95879629
*
@@ -10230,6 +10272,12 @@ Init_String(void)
1023010272
rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
1023110273
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
1023210274

10275+
/* define module here so that we don't have to look it up */
10276+
mUnicodeNormalize = rb_define_module("UnicodeNormalize");
10277+
id_normalize = rb_intern("normalize");
10278+
10279+
rb_define_method(rb_cString, "unicode_normalize", rb_str_unicode_normalize, -1);
10280+
1023310281
rb_fs = Qnil;
1023410282
rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
1023510283
rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);

0 commit comments

Comments
 (0)