COMMON: Add Path::numComponents() method

This commit is contained in:
Eugene Sandulenko 2024-11-12 01:08:29 +01:00
parent 43a90ba922
commit c6daf24573
No known key found for this signature in database
GPG Key ID: 014D387312D34F08
2 changed files with 26 additions and 0 deletions

View File

@ -405,6 +405,27 @@ String Path::baseName() const {
return unescape(kNoSeparator, begin, end);
}
int Path::numComponents() const {
if (_str.empty())
return 0;
const char *str = _str.c_str();
if (isEscaped())
str++;
int num = 1;
const char *sep = strchr(str, SEPARATOR);
while (sep) {
str = sep + 1;
sep = strchr(str, SEPARATOR);
num++;
}
return num;
}
Path &Path::appendInPlace(const Path &x) {
if (x._str.empty()) {
return *this;

View File

@ -304,6 +304,11 @@ public:
*/
String baseName() const;
/**
* Returns number of components in this path,
*/
int numComponents() const;
/** Check whether this path is identical to path @p x. */
bool operator==(const Path &x) const {
return _str == x._str;