From d230bb16d4724faa237005bd4dfb7e0e4ff7b21c Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 20 Jul 2025 21:28:35 +0900 Subject: [PATCH] Use end pointers instead of lengths For processing simplicity. --- mrbgems/mruby-io/src/file.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/mrbgems/mruby-io/src/file.c b/mrbgems/mruby-io/src/file.c index 973a68425..e842c482f 100644 --- a/mrbgems/mruby-io/src/file.c +++ b/mrbgems/mruby-io/src/file.c @@ -246,50 +246,44 @@ mrb_file_basename(mrb_state *mrb, mrb_value klass) mrb_get_args(mrb, "z|z", &path, &suffix); - // Copy path to a local buffer to avoid modifying the original string - size_t len = strlen(path); - if (len == 0) { + const char *endp = path + strlen(path); + if (path == endp) { return mrb_str_new_lit(mrb, "."); } #ifdef _WIN32 if (UNC_PATH_P(path)) { - const char *p = path + 2; - SKIP_DIRSEP(p); - NEXT_DIRSEP(p); // skip server name - SKIP_DIRSEP(p); - NEXT_DIRSEP(p); // skip share name - len -= p - path; - path = p; + path += 2; + SKIP_DIRSEP(path); + NEXT_DIRSEP(path); // skip server name + SKIP_DIRSEP(path); + NEXT_DIRSEP(path); // skip share name } else if (DRIVE_LETTER_P(path)) { - const char *p = path + 2; - if (p == path + len) { + path += 2; + if (path == endp) { return mrb_str_new_lit(mrb, ""); } - len -= p - path; - path = p; } #endif // _WIN32 // Remove trailing slashes (except when path is only "/") - while (len > 1 && DIRSEP_P(path[len - 1])) { - len--; + while (path < endp && DIRSEP_P(endp[-1])) { + endp--; } // Find the last path separator - ssize_t base = len - 1; - while (base >= 0 && !DIRSEP_P(path[base])) { + const char *base = endp; + while (path < base && !DIRSEP_P(base[-1])) { base--; } - base++; // move to the first character after '/' // If path is all slashes, return "/" - if ((size_t)base == len) { + if (base == endp) { return mrb_str_new_lit(mrb, "/"); } - mrb_value result = mrb_str_new(mrb, path + base, len - base); + mrb_value result = mrb_str_new(mrb, base, endp - base); // Suffix removal (CRuby compatible) if (suffix && *suffix) {