2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-29 11:11:03 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Vector.h>
|
2018-10-28 07:54:20 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
class LexicalPath {
|
2018-10-28 07:54:20 +00:00
|
|
|
public:
|
2021-04-16 21:44:06 +00:00
|
|
|
explicit LexicalPath(String);
|
2018-10-28 07:54:20 +00:00
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 15:06:21 +00:00
|
|
|
bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; }
|
2021-06-29 11:01:56 +00:00
|
|
|
String const& string() const { return m_string; }
|
2018-10-28 07:54:20 +00:00
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 15:06:21 +00:00
|
|
|
StringView const& dirname() const { return m_dirname; }
|
|
|
|
StringView const& basename() const { return m_basename; }
|
|
|
|
StringView const& title() const { return m_title; }
|
|
|
|
StringView const& extension() const { return m_extension; }
|
2018-11-18 13:57:41 +00:00
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 15:06:21 +00:00
|
|
|
Vector<StringView> const& parts_view() const { return m_parts; }
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] Vector<String> parts() const;
|
2019-03-30 02:27:25 +00:00
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
bool has_extension(StringView const&) const;
|
2019-05-26 20:33:30 +00:00
|
|
|
|
2021-06-29 15:55:12 +00:00
|
|
|
[[nodiscard]] LexicalPath append(StringView const&) const;
|
|
|
|
[[nodiscard]] LexicalPath parent() const;
|
2021-05-12 19:17:39 +00:00
|
|
|
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static String canonicalized_path(String);
|
2021-06-29 18:59:38 +00:00
|
|
|
[[nodiscard]] static String relative_path(StringView const& absolute_path, StringView const& prefix);
|
2020-05-26 11:52:44 +00:00
|
|
|
|
2021-05-12 19:17:39 +00:00
|
|
|
template<typename... S>
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static LexicalPath join(String const& first, S&&... rest)
|
2021-05-12 19:17:39 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(first);
|
|
|
|
((builder.append('/'), builder.append(forward<S>(rest))), ...);
|
|
|
|
|
|
|
|
return LexicalPath { builder.to_string() };
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static String dirname(String path)
|
2021-06-29 14:46:16 +00:00
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.dirname();
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static String basename(String path)
|
2021-06-29 14:46:16 +00:00
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.basename();
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static String title(String path)
|
2021-06-29 14:46:16 +00:00
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.title();
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:30:45 +00:00
|
|
|
[[nodiscard]] static String extension(String path)
|
2021-06-29 14:46:16 +00:00
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.extension();
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
private:
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 15:06:21 +00:00
|
|
|
Vector<StringView> m_parts;
|
2018-10-28 07:54:20 +00:00
|
|
|
String m_string;
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 15:06:21 +00:00
|
|
|
StringView m_dirname;
|
|
|
|
StringView m_basename;
|
|
|
|
StringView m_title;
|
|
|
|
StringView m_extension;
|
2018-10-28 07:54:20 +00:00
|
|
|
};
|
|
|
|
|
2020-10-08 12:09:38 +00:00
|
|
|
template<>
|
|
|
|
struct Formatter<LexicalPath> : Formatter<StringView> {
|
2021-06-29 11:01:56 +00:00
|
|
|
void format(FormatBuilder& builder, LexicalPath const& value)
|
2020-10-08 12:09:38 +00:00
|
|
|
{
|
2020-12-30 11:14:15 +00:00
|
|
|
Formatter<StringView>::format(builder, value.string());
|
2020-10-08 12:09:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
};
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
using AK::LexicalPath;
|