diff options
| author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2026-05-03 21:01:43 +0900 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu.nakada@gmail.com> | 2026-05-13 17:37:48 +0900 |
| commit | e887ffa35e32de39ca99cd34e16716fe7e61d6b9 (patch) | |
| tree | ffe51c28feae49309c16ac4f737d3b4a19e0112c /pathname.c | |
| parent | 516adc1f787a520d6331a3be91ffcded046e0d3d (diff) | |
pathname: Move add_trailing_separator to pathname.c
Diffstat (limited to 'pathname.c')
| -rw-r--r-- | pathname.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/pathname.c b/pathname.c index f68dea0372..c7a4bb8bcc 100644 --- a/pathname.c +++ b/pathname.c @@ -248,6 +248,23 @@ has_trailing_separator(VALUE self, VALUE path) return RBOOL(base + alllen < RSTRING_END(path)); } +/* :nodoc: */ +/* add_trailing_separator(path) -> path */ +static VALUE +add_trailing_separator(VALUE self, VALUE path) +{ + if (RSTRING_LEN(check_strpath(path)) <= 0) return path; + rb_encoding *enc = rb_enc_get(path); + const char *name = RSTRING_PTR(path); + const char *end = RSTRING_END(path); + const char *top = rb_enc_path_skip_prefix(name, end, enc); + if (top < end && isdirsep(end[-1])) { + if (end[-1] == '/' || rb_enc_prev_char(top, end, end, enc) == end - 1) + return path; + } + return rb_str_cat_cstr(rb_str_dup(path), "/"); +} + #include "pathname_builtin.rbinc" static void init_ids(void); @@ -276,6 +293,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, "has_trailing_separator?", has_trailing_separator, 1); + rb_define_private_method(rb_cPathname, "add_trailing_separator", add_trailing_separator, 1); rb_provide("pathname.so"); } |
