diff options
| author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2026-05-06 20:08:02 +0900 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu.nakada@gmail.com> | 2026-05-13 17:37:48 +0900 |
| commit | 488b8e58bc90b29655bd9f85611abcebc58bb11f (patch) | |
| tree | 92b688a274f367edc028a249bb2d11ec02da9bcb /pathname.c | |
| parent | 9021799650021684e5586215f00f0b701d6160ab (diff) | |
pathname: Move root? to pathname.c
Diffstat (limited to 'pathname.c')
| -rw-r--r-- | pathname.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/pathname.c b/pathname.c index 87382dd804..e8959bb976 100644 --- a/pathname.c +++ b/pathname.c @@ -112,6 +112,24 @@ path_sub(int argc, VALUE *argv, VALUE self) } /* + * Predicate method for root directories. Returns +true+ if the + * pathname consists of consecutive slashes. + * + * It doesn't access the filesystem. So it may return +false+ for some + * pathnames which points to roots such as <tt>/usr/..</tt>. + */ +static VALUE +path_root_p(VALUE self) +{ + VALUE path = get_strpath(self); + if (RSTRING_LEN(path) == 0) return Qfalse; + const char *ptr = RSTRING_PTR(path), *end = RSTRING_END(path); + rb_encoding *enc = rb_enc_get(path); + const char *base = rb_enc_path_skip_prefix_root(ptr, end, enc); + return RBOOL(base == end); +} + +/* * call-seq: * absolute? -> true or false * @@ -159,6 +177,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, "root?", path_root_p, 0); rb_define_method(rb_cPathname, "absolute?", path_absolute_p, 0); rb_provide("pathname.so"); |
