From 70f25096c0c389888c06e0fea358c92153633b8e Mon Sep 17 00:00:00 2001
From: akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Wed, 13 Feb 2008 13:50:31 +0000
Subject: [PATCH] * lib/pathname.rb (Pathname#sub_ext): new method. 
 [ruby-list:44608]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15461 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 ChangeLog                      |  4 ++++
 lib/pathname.rb                | 13 ++++++++++++-
 test/pathname/test_pathname.rb | 11 +++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 7a4ae586a9..59587468af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Wed Feb 13 22:46:36 2008  Tanaka Akira  <akr@fsij.org>
+
+	* lib/pathname.rb (Pathname#sub_ext): new method.  [ruby-list:44608]
+
 Wed Feb 13 21:50:32 2008  Yusuke Endoh  <mame@tsg.ne.jp>
 
 	* proc.c (proc_curry): new method. [ruby-dev:33676]
diff --git a/lib/pathname.rb b/lib/pathname.rb
index e4ca5489ce..b2e119db11 100644
--- a/lib/pathname.rb
+++ b/lib/pathname.rb
@@ -255,11 +255,22 @@ class Pathname
   end
 
   if File::ALT_SEPARATOR
-    SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/
+    SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
+    SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
   else
+    SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
     SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
   end
 
+  # Return a pathname which the extention 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/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 246b69f0e3..11c1bb7b2b 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -381,6 +381,17 @@ class TestPathname < Test::Unit::TestCase
   def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
   defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
 
+  def pathsubext(path, repl) Pathname.new(path).sub_ext(repl).to_s end
+  defassert(:pathsubext, 'a.o', 'a.c', '.o')
+  defassert(:pathsubext, 'a.o', 'a.c++', '.o')
+  defassert(:pathsubext, 'a.png', 'a.gif', '.png')
+  defassert(:pathsubext, 'ruby.tar.bz2', 'ruby.tar.gz', '.bz2')
+  defassert(:pathsubext, 'd/a.o', 'd/a.c', '.o')
+  defassert(:pathsubext, 'foo', 'foo.exe', '')
+  defassert(:pathsubext, 'lex.yy.o', 'lex.yy.c', '.o')
+  defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
+  defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.o')
+
   def root?(path)
     Pathname.new(path).root?
   end