From 7dad6c043e3bb88b9e013cc471d62b162d8c352e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 3 May 2026 21:01:43 +0900 Subject: pathname: Move split_names to pathname.c --- pathname.c | 21 +++++++++++++++++++++ pathname_builtin.rb | 11 ----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pathname.c b/pathname.c index 21b3940841..b2f1da8f75 100644 --- a/pathname.c +++ b/pathname.c @@ -233,6 +233,26 @@ chop_basename(VALUE self, VALUE path) return rb_assoc_new(dir, basename); } +/* :nodoc: */ +/* split_names(path) -> prefix, [name, ...] */ +static VALUE +split_names(VALUE self, VALUE path) +{ + rb_encoding *enc = rb_enc_get(check_strpath(path)); + const char *beg = RSTRING_PTR(path), *ptr = beg; + const char *end = RSTRING_END(path); + const char *root = rb_enc_path_skip_prefix_root(ptr, end, enc); + VALUE pre = rb_str_subseq(path, 0, root - ptr); + VALUE names = rb_ary_new(); + while (ptr < end) { + const char *next = rb_enc_path_next(ptr, end, enc); + if (next > ptr) rb_ary_push(names, rb_str_subseq(path, ptr - beg, next - ptr)); + ptr = next; + while (ptr < end && isdirsep(*ptr)) ++ptr; + } + return rb_assoc_new(pre, names); +} + /* :nodoc: */ /* has_trailing_separator?(path) -> bool */ static VALUE @@ -321,6 +341,7 @@ InitVM_pathname(void) rb_define_private_method(rb_cPathname, "has_separator?", has_separator_p, 1); rb_define_private_method(rb_cPathname, "chop_basename", chop_basename, 1); + rb_define_private_method(rb_cPathname, "split_names", split_names, 1); rb_define_private_method(rb_cPathname, "has_trailing_separator?", has_trailing_separator, 1); rb_define_private_method(rb_cPathname, "add_trailing_separator", add_trailing_separator, 1); rb_define_private_method(rb_cPathname, "del_trailing_separator", del_trailing_separator, 1); diff --git a/pathname_builtin.rb b/pathname_builtin.rb index e8186c9e1d..ea302c594b 100644 --- a/pathname_builtin.rb +++ b/pathname_builtin.rb @@ -326,17 +326,6 @@ class Pathname self end - # split_names(path) -> prefix, [name, ...] - def split_names(path) # :nodoc: - names = [] - while r = chop_basename(path) - path, basename = r - names.unshift basename - end - return path, names - end - private :split_names - def prepend_prefix(prefix, relpath) # :nodoc: if relpath.empty? File.dirname(prefix) -- cgit v1.2.3