From c38a9138ba8294ab1243cf03ad0c8b0df4901967 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Thu, 2 Oct 2025 00:51:20 +0530 Subject: [PATCH] src/common: add helper to prepend "..." to trimmed paths Signed-off-by: Rishabh Dave --- src/common/strescape.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/common/strescape.h b/src/common/strescape.h index 9bf27fc3494..412fd1f6f7e 100644 --- a/src/common/strescape.h +++ b/src/common/strescape.h @@ -34,4 +34,21 @@ inline std::string binstrprint(std::string_view sv, size_t maxlen=0) return s; } +inline std::string get_trimmed_path_str(const std::string& path) +{ + // index of '/' before 10th component (count from end of the path). + size_t n = 0; + + for (int i = 1; i <= 10; ++i) { + n = path.rfind("/", n - 1); + if (n == std::string::npos) { + // path doesn't contain 10 components, return path as it is. + return path; + break; + } + } + + return std::string("..." + path.substr(n, -1)); +} + #endif -- 2.39.5