diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-06-29 17:06:21 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-30 11:13:54 +0200 |
commit | 7405536a1abcc23d6a54565687800b14bde3db53 (patch) | |
tree | bb3e3affb873a170eebfb6d08a3abae9b08d4ffc /AK/LexicalPath.h | |
parent | fc6d051dfdddc13835548537d025e760ba186b5b (diff) | |
download | serenity-7405536a1abcc23d6a54565687800b14bde3db53.zip |
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.
Diffstat (limited to 'AK/LexicalPath.h')
-rw-r--r-- | AK/LexicalPath.h | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index 257b39963b..4c425c4ed3 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -17,15 +17,16 @@ public: LexicalPath() = default; explicit LexicalPath(String); - bool is_absolute() const { return m_is_absolute; } + bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; } String const& string() const { return m_string; } - String const& dirname() const { return m_dirname; } - String const& basename() const { return m_basename; } - String const& title() const { return m_title; } - String const& extension() const { return m_extension; } + 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; } - Vector<String> const& parts() const { return m_parts; } + Vector<StringView> const& parts_view() const { return m_parts; } + Vector<String> parts() const; bool has_extension(StringView const&) const; @@ -71,13 +72,12 @@ public: private: void canonicalize(); - Vector<String> m_parts; + Vector<StringView> m_parts; String m_string; - String m_dirname; - String m_basename; - String m_title; - String m_extension; - bool m_is_absolute { false }; + StringView m_dirname; + StringView m_basename; + StringView m_title; + StringView m_extension; }; template<> |