ruby-changes:16822
From: akr <ko1@a...>
Date: Sun, 1 Aug 2010 15:00:05 +0900 (JST)
Subject: [ruby-changes:16822] Ruby:r28817 (trunk): * ext/pathname/pathname.c (path_sub_ext): Pathname#sub_ext translated
akr 2010-08-01 14:59:49 +0900 (Sun, 01 Aug 2010) New Revision: 28817 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=28817 Log: * ext/pathname/pathname.c (path_sub_ext): Pathname#sub_ext translated from pathname.rb. Modified files: trunk/ChangeLog trunk/ext/pathname/lib/pathname.rb trunk/ext/pathname/pathname.c Index: ChangeLog =================================================================== --- ChangeLog (revision 28816) +++ ChangeLog (revision 28817) @@ -1,3 +1,8 @@ +Sun Aug 1 14:59:04 2010 Tanaka Akira <akr@f...> + + * ext/pathname/pathname.c (path_sub_ext): Pathname#sub_ext translated + from pathname.rb. + Sun Aug 1 10:23:48 2010 Yusuke Endoh <mame@t...> * lib/irb/init.rb (IRB.parse_opts): set VERBOSE to true when debug Index: ext/pathname/lib/pathname.rb =================================================================== --- ext/pathname/lib/pathname.rb (revision 28816) +++ ext/pathname/lib/pathname.rb (revision 28817) @@ -39,15 +39,6 @@ SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/ end - # Return a pathname which the extension of the basename is substituted by - # <i>repl</i>. - # - # If self has no extension part, <i>repl</i> is appended. - def sub_ext(repl) - ext = File.extname(@path) - self.class.new(@path.chomp(ext) + repl) - end - # chop_basename(path) -> [pre-basename, basename] or nil def chop_basename(path) base = File.basename(path) Index: ext/pathname/pathname.c =================================================================== --- ext/pathname/pathname.c (revision 28816) +++ ext/pathname/pathname.c (revision 28817) @@ -168,6 +168,37 @@ } /* + * Return a pathname which the extension of the basename is substituted by + * <i>repl</i>. + * + * If self has no extension part, <i>repl</i> is appended. + */ +static VALUE +path_sub_ext(VALUE self, VALUE repl) +{ + VALUE str = get_strpath(self); + VALUE str2; + long extlen; + const char *ext; + const char *p; + + StringValue(repl); + p = RSTRING_PTR(str); + ext = ruby_find_extname(p, &extlen); + if (ext == NULL) { + ext = p + RSTRING_LEN(str); + } + else if (extlen <= 1) { + ext += extlen; + } + str2 = rb_str_dup(str); + rb_str_set_len(str2, ext-p); + rb_str_append(str2, repl); + OBJ_INFECT(str2, str); + return rb_class_new_instance(1, &str2, rb_obj_class(self)); +} + +/* * == Pathname * * Pathname represents a pathname which locates a file in a filesystem. @@ -364,4 +395,5 @@ rb_define_method(rb_cPathname, "to_path", path_to_s, 0); rb_define_method(rb_cPathname, "inspect", path_inspect, 0); rb_define_method(rb_cPathname, "sub", path_sub, -1); + rb_define_method(rb_cPathname, "sub_ext", path_sub_ext, 1); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/