summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2026-05-03 21:01:43 +0900
committerNobuyoshi Nakada <nobu.nakada@gmail.com>2026-05-13 17:37:48 +0900
commitce5a0999012980f8b83908e407590c4e0ae494fa (patch)
tree333521ec79002ac4ec29c597e0906bff525f0691
parente887ffa35e32de39ca99cd34e16716fe7e61d6b9 (diff)
pathname: Move del_trailing_separator to pathname.c
-rw-r--r--pathname.c30
-rw-r--r--pathname_builtin.rb12
2 files changed, 30 insertions, 12 deletions
diff --git a/pathname.c b/pathname.c
index c7a4bb8bcc..21b3940841 100644
--- a/pathname.c
+++ b/pathname.c
@@ -265,6 +265,35 @@ add_trailing_separator(VALUE self, VALUE path)
return rb_str_cat_cstr(rb_str_dup(path), "/");
}
+/* :nodoc: */
+static VALUE
+del_trailing_separator(VALUE self, VALUE path)
+{
+ long len = RSTRING_LEN(check_strpath(path));
+ if (len <= 0) return path;
+ rb_encoding *enc = rb_enc_get(path);
+ const char *name = RSTRING_PTR(path);
+ const char *end = name + len, *tail = end;
+ const char *top = rb_enc_path_skip_prefix(name, end, enc);
+ if (tail > top && isdirsep(tail[-1])) {
+ while (--tail > top && isdirsep(tail[-1]));
+ if (tail > top &&
+ tail[0] != '/' &&
+ !rb_str_enc_fastpath(path) &&
+ rb_enc_left_char_head(top, tail, end, enc) != tail) {
+ /* trailing byte, not a directory separator */
+ ++tail;
+ }
+ if (tail < end) {
+ if (tail == name || (drive_letter && tail == top && top[-1] == ':')) {
+ ++tail;
+ }
+ }
+ }
+ if (tail == end) return path;
+ return rb_str_subseq(path, 0, tail - name);
+}
+
#include "pathname_builtin.rbinc"
static void init_ids(void);
@@ -294,6 +323,7 @@ InitVM_pathname(void)
rb_define_private_method(rb_cPathname, "chop_basename", chop_basename, 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);
rb_provide("pathname.so");
}
diff --git a/pathname_builtin.rb b/pathname_builtin.rb
index 246d14ee3f..f353e027b6 100644
--- a/pathname_builtin.rb
+++ b/pathname_builtin.rb
@@ -500,18 +500,6 @@ class Pathname
end
private :cleanpath_aggressive
- def del_trailing_separator(path) # :nodoc:
- if r = chop_basename(path)
- pre, basename = r
- pre + basename
- elsif /#{SEPARATOR_PAT}+\z/o =~ path
- $` + File.dirname(path)[/#{SEPARATOR_PAT}*\z/o]
- else
- path
- end
- end
- private :del_trailing_separator
-
def cleanpath_conservative # :nodoc:
path = @path
names = []