Skip to content

Commit

Permalink
* file.c (rb_file_s_basename): ignore non-ascii extension in
Browse files Browse the repository at this point in the history
  different encoding, which cannot match.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jan 25, 2012
1 parent b094ab0 commit 97f0b0f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Wed Jan 25 11:40:26 2012 Nobuyoshi Nakada <[email protected]>
Wed Jan 25 13:27:42 2012 Nobuyoshi Nakada <[email protected]>

* file.c (rb_file_s_basename): ignore non-ascii extension in
different encoding, which cannot match.

* file.c (rmext): no extension to strip if empty string.

Expand Down
11 changes: 4 additions & 7 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3658,8 +3658,10 @@ rb_file_s_basename(int argc, VALUE *argv)
}
}
FilePathStringValue(fname);
if (!NIL_P(fext)) enc = rb_enc_check(fname, fext);
else enc = rb_enc_get(fname);
if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
enc = rb_enc_get(fname);
fext = Qnil;
}
if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
return rb_str_new_shared(fname);

Expand All @@ -3669,12 +3671,7 @@ rb_file_s_basename(int argc, VALUE *argv)
f = n;
}
else {
rb_encoding *fenc = rb_enc_get(fext);
const char *fp;
if (enc != fenc &&
rb_enc_str_coderange(fext) != ENC_CODERANGE_7BIT) {
fext = rb_str_conv_enc(fext, fenc, enc);
}
fp = StringValueCStr(fext);
if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
f = n;
Expand Down
3 changes: 3 additions & 0 deletions test/ruby/test_file_exhaustive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ def test_basename

s = "foo\x93_a".force_encoding("cp932")
assert_equal(s, File.basename(s, "_a"))

s = "\u4032.\u3024"
assert_equal(s, File.basename(s, ".\x95\\".force_encoding("cp932")))
end

def test_dirname
Expand Down

0 comments on commit 97f0b0f

Please sign in to comment.