From a4e4df979d285863479d345046eb271f9a1f4412 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 6 Feb 2022 11:12:31 +0100 Subject: [PATCH] Added a new path_get_prev_dir method to String. --- core/string.cpp | 38 ++++++++++++++++++++++++++++++++++++++ core/string.h | 1 + 2 files changed, 39 insertions(+) diff --git a/core/string.cpp b/core/string.cpp index ed07c34..7de71e3 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -1112,6 +1112,44 @@ String String::path_get_last_segment() const { return substr_index(ssind, seind); } +String String::path_get_prev_dir() const { + if (_size == 0) { + return String(); + } + + int seind = _size - 1; + while (seind > 0 && (_data[seind] == '/' || _data[seind] == '\\')) { + --seind; + } + + if (seind == 0) { + // ///////// + // or + // a/////// + // no prev dir + + return String("/"); + } + + // fol/fol2/fol3// + // ^ (seind) + + while (seind > 0 && (_data[seind] != '/' || _data[seind] != '\\')) { + --seind; + } + + // fol/fol2/fol3// + // ^ (seind) + + --seind; + + if (seind <= 0) { + return String("/"); + } + + return substr_index(0, seind); +} + void String::to_html_special_chars() { replace("&", "&"); replace("\"", """); diff --git a/core/string.h b/core/string.h index 4797859..5eb57ba 100644 --- a/core/string.h +++ b/core/string.h @@ -113,6 +113,7 @@ public: void path_ensure_end_slash(); String path_get_basename() const; String path_get_last_segment() const; + String path_get_prev_dir() const; void to_html_special_chars(); void from_html_special_chars();