diff options
Diffstat (limited to 'src/fs')
-rw-r--r-- | src/fs/Path.cxx | 21 | ||||
-rw-r--r-- | src/fs/Path.hxx | 29 |
2 files changed, 48 insertions, 2 deletions
diff --git a/src/fs/Path.cxx b/src/fs/Path.cxx index 1a1f133d0..8f31f45f7 100644 --- a/src/fs/Path.cxx +++ b/src/fs/Path.cxx @@ -194,15 +194,32 @@ Path::RelativeFS(const char *other_fs) const other_fs += l; if (*other_fs != 0) { - if (!G_IS_DIR_SEPARATOR(*other_fs)) + if (!IsSeparatorFS(*other_fs)) /* mismatch */ return nullptr; /* skip remaining path separators */ do { ++other_fs; - } while (G_IS_DIR_SEPARATOR(*other_fs)); + } while (IsSeparatorFS(*other_fs)); } return other_fs; } + +void +Path::ChopSeparators() +{ + size_t l = length(); + const char *p = data(); + + while (l >= 2 && IsSeparatorFS(p[l - 1])) { + --l; + +#if GCC_CHECK_VERSION(4,7) && !defined(__clang__) + value.pop_back(); +#else + value.erase(value.end() - 1, value.end()); +#endif + } +} diff --git a/src/fs/Path.hxx b/src/fs/Path.hxx index 52a62ae76..bd3f3a94e 100644 --- a/src/fs/Path.hxx +++ b/src/fs/Path.hxx @@ -57,6 +57,14 @@ public: typedef string::pointer pointer; typedef string::const_pointer const_pointer; +#ifdef WIN32 + static constexpr value_type SEPARATOR_FS = '\\'; + static constexpr char SEPARATOR_UTF8 = '/'; +#else + static constexpr value_type SEPARATOR_FS = '/'; + static constexpr char SEPARATOR_UTF8 = '/'; +#endif + private: string value; @@ -232,6 +240,27 @@ public: */ gcc_pure const char *RelativeFS(const char *other_fs) const; + + /** + * Chop trailing directory separators. + */ + void ChopSeparators(); + + static constexpr bool IsSeparatorFS(value_type ch) { + return +#ifdef WIN32 + ch == '/' || +#endif + ch == SEPARATOR_FS; + } + + static constexpr bool IsSeparatorUTF8(char ch) { + return +#ifdef WIN32 + ch == '/' || +#endif + ch == SEPARATOR_UTF8; + } }; #endif |