Project

General

Profile

Feature #6587 ยป 0001-File.rootname.patch

nobu (Nobuyoshi Nakada), 06/14/2012 06:04 PM

View differences:

file.c
3766 3766
}
3767 3767

  
3768 3768
/*
3769
 *  call-seq:
3770
 *     File.rootname(file_name )  ->  root_name
3771
 *
3772
 *  Returns root part of the filename given in <i>file_name</i>.
3773
 *  Returns an empty string if <i>file_name</i> is not an absolute
3774
 *  path.
3775
 *
3776
 *  * On Unix
3777
 *     File.rootname("/home/gumby/work/ruby.rb")        #=> "/"
3778
 *     File.rootname("home/gumby/work/ruby.rb")         #=> ""
3779
 *
3780
 *  * On Windows
3781
 *     File.rootname("c:/Users/gumby/work/ruby.rb")     #=> "c:/"
3782
 *     File.rootname("//host/share/gumby/work/ruby.rb") #=> "//host/share/"
3783
 *     File.rootname("home/gumby/work/ruby.rb")         #=> ""
3784
 */
3785

  
3786
static VALUE
3787
rb_file_s_rootname(VALUE klass, VALUE fname)
3788
{
3789
    const char *name, *root, *end;
3790
    VALUE rootname;
3791
    rb_encoding *enc;
3792

  
3793
    FilePathStringValue(fname);
3794
    name = StringValueCStr(fname);
3795
    end = name + RSTRING_LEN(fname);
3796
    enc = rb_enc_get(fname);
3797
    root = skipprefix(name, end, enc);
3798
    if (root < end && isdirsep(*root)) root++;
3799
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3800
    if (root == name + 1 || (root == name + 2 && name[1] == ':')) {
3801
	root = name;
3802
    }
3803
#endif
3804
    rootname = rb_enc_str_new(name, root - name, enc);
3805
    OBJ_INFECT(rootname, fname);
3806
    return rootname;
3807
}
3808

  
3809
/*
3769 3810
 * accept a String, and return the pointer of the extension.
3770 3811
 * if len is passed, set the length of extension to it.
3771 3812
 * returned pointer is in ``name'' or NULL.
......
5460 5501
    rb_define_singleton_method(rb_cFile, "basename", rb_file_s_basename, -1);
5461 5502
    rb_define_singleton_method(rb_cFile, "dirname", rb_file_s_dirname, 1);
5462 5503
    rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1);
5504
    rb_define_singleton_method(rb_cFile, "rootname", rb_file_s_rootname, 1);
5463 5505
    rb_define_singleton_method(rb_cFile, "path", rb_file_s_path, 1);
5464 5506

  
5465 5507
    separator = rb_obj_freeze(rb_usascii_str_new2("/"));
test/ruby/test_file_exhaustive.rb
522 522
    assert_incompatible_encoding {|d| File.extname(d)}
523 523
  end
524 524

  
525
  def test_rootname
526
    assert_equal("", File.rootname("foo/bar"))
527
    if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
528
      assert_equal("c:/", File.rootname("c:/Users/gumby/work/ruby.rb"))
529
      assert_equal("//host/share/", File.rootname("//host/share/gumby/work/ruby.rb"))
530
      assert_equal("", File.rootname("/Users/gumby/work/ruby.rb"))
531
      assert_equal("", File.rootname("c:gumby/work/ruby.rb"))
532
    else
533
      assert_equal("/", File.rootname("/home/gumby/work/ruby.rb"))
534
    end
535
    assert_equal("", File.rootname("home/gumby/work/ruby.rb"))
536
  end
537

  
525 538
  def test_split
526 539
    d, b = File.split(@file)
527 540
    assert_equal(File.dirname(@file), d)
528
-