diff options
| -rw-r--r-- | depend | 5 | ||||
| -rw-r--r-- | pathname.c | 41 | ||||
| -rw-r--r-- | pathname_builtin.rb | 17 |
3 files changed, 46 insertions, 17 deletions
@@ -11143,6 +11143,10 @@ parser_st.$(OBJEXT): {$(VPATH)}st.c pathname.$(OBJEXT): $(hdrdir)/ruby.h pathname.$(OBJEXT): $(hdrdir)/ruby/ruby.h pathname.$(OBJEXT): $(top_srcdir)/internal/compilers.h +pathname.$(OBJEXT): $(top_srcdir)/internal/file.h +pathname.$(OBJEXT): $(top_srcdir)/internal/serial.h +pathname.$(OBJEXT): $(top_srcdir)/internal/static_assert.h +pathname.$(OBJEXT): $(top_srcdir)/internal/vm.h pathname.$(OBJEXT): $(top_srcdir)/internal/warnings.h pathname.$(OBJEXT): {$(VPATH)}assert.h pathname.$(OBJEXT): {$(VPATH)}backward.h @@ -11160,6 +11164,7 @@ pathname.$(OBJEXT): {$(VPATH)}config.h pathname.$(OBJEXT): {$(VPATH)}defines.h pathname.$(OBJEXT): {$(VPATH)}encoding.h pathname.$(OBJEXT): {$(VPATH)}intern.h +pathname.$(OBJEXT): {$(VPATH)}internal.h pathname.$(OBJEXT): {$(VPATH)}internal/abi.h pathname.$(OBJEXT): {$(VPATH)}internal/anyargs.h pathname.$(OBJEXT): {$(VPATH)}internal/arithmetic.h diff --git a/pathname.c b/pathname.c index 28e25512a1..87382dd804 100644 --- a/pathname.c +++ b/pathname.c @@ -1,4 +1,17 @@ #include "ruby.h" +#include "internal.h" +#include "internal/file.h" +#include "internal/vm.h" + +#if defined __CYGWIN__ || defined DOSISH +# define drive_letter 1 +# define alt_separator 1 +# define isdirsep(x) ((x) == '/' || (x) == '\\') +#else +# define drive_letter 0 +# define alt_separator 0 +# define isdirsep(x) ((x) == '/') +#endif static VALUE rb_cPathname; static ID id_at_path; @@ -98,6 +111,33 @@ path_sub(int argc, VALUE *argv, VALUE self) return rb_class_new_instance(1, &str, rb_obj_class(self)); } +/* + * call-seq: + * absolute? -> true or false + * + * Returns whether +self+ contains an absolute path: + * + * Pathname.new('/home').absolute? # => true + * Pathname.new('lib').absolute? # => false + * + * OS-dependent for some paths: + * + * Pathname.new('C:/').absolute? # => true # On Windows. + * Pathname.new('C:/').absolute? # => false # Elsewhere. + */ +static VALUE +path_absolute_p(VALUE self) +{ + VALUE path = get_strpath(self); + const char *ptr = RSTRING_PTR(path); + long len = RSTRING_LEN(path); + if (len < 1) return Qfalse; + if (drive_letter) { + if (len >= 2 && ISALPHA(ptr[0]) && (ptr[1] == ':')) return Qtrue; + } + return RBOOL(isdirsep(ptr[0])); +} + #include "pathname_builtin.rbinc" static void init_ids(void); @@ -119,6 +159,7 @@ InitVM_pathname(void) rb_cPathname = rb_define_class("Pathname", rb_cObject); rb_define_method(rb_cPathname, "<=>", path_cmp, 1); rb_define_method(rb_cPathname, "sub", path_sub, -1); + rb_define_method(rb_cPathname, "absolute?", path_absolute_p, 0); rb_provide("pathname.so"); } diff --git a/pathname_builtin.rb b/pathname_builtin.rb index 5965cb2830..b9cba0260f 100644 --- a/pathname_builtin.rb +++ b/pathname_builtin.rb @@ -632,23 +632,6 @@ class Pathname chop_basename(@path) == nil && SEPARATOR_PAT.match?(@path) end - # call-seq: - # absolute? -> true or false - # - # Returns whether +self+ contains an absolute path: - # - # Pathname.new('/home').absolute? # => true - # Pathname.new('lib').absolute? # => false - # - # OS-dependent for some paths: - # - # Pathname.new('C:/').absolute? # => true # On Windows. - # Pathname.new('C:/').absolute? # => false # Elsewhere. - # - def absolute? - ABSOLUTE_PATH.match? @path - end - # The opposite of Pathname#absolute? # # It returns +false+ if the pathname begins with a slash. |
