diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-08-01 05:59:49 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-08-01 05:59:49 +0000 |
commit | caa05197d34667fea40a4637b899669dd65f536f (patch) | |
tree | 1b78ee8e0ff5c6dcc7a76e15d979ed0b60714c79 /ext/pathname | |
parent | ffe49186c971e751e2082db342dd4b04f1520b49 (diff) | |
download | ruby-caa05197d34667fea40a4637b899669dd65f536f.tar.gz |
* ext/pathname/pathname.c (path_sub_ext): Pathname#sub_ext translated
from pathname.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28817 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/pathname')
-rw-r--r-- | ext/pathname/lib/pathname.rb | 9 | ||||
-rw-r--r-- | ext/pathname/pathname.c | 32 |
2 files changed, 32 insertions, 9 deletions
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index b0639a4079..c30be4c948 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -39,15 +39,6 @@ class Pathname 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) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index 37fc87b500..ecfe11cfc8 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -168,6 +168,37 @@ path_sub(int argc, VALUE *argv, VALUE self) } /* + * 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 @@ Init_pathname() 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); } |