1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:07:46 +00:00

AK: Rename FileSystemPath -> LexicalPath

And move canonicalized_path() to a static method on LexicalPath.

This is to make it clear that FileSystemPath/canonicalized_path() only
perform *lexical* canonicalization.
This commit is contained in:
Sergey Bugaev 2020-05-26 14:52:44 +03:00 committed by Andreas Kling
parent f746bbda17
commit 602c3fdb3a
44 changed files with 174 additions and 181 deletions

View file

@ -24,21 +24,21 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FileSystemPath.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
namespace AK {
FileSystemPath::FileSystemPath(const StringView& s)
LexicalPath::LexicalPath(const StringView& s)
: m_string(s)
{
canonicalize();
m_is_valid = true;
}
void FileSystemPath::canonicalize()
void LexicalPath::canonicalize()
{
if (m_string.is_empty()) {
m_parts.clear();
@ -105,14 +105,14 @@ void FileSystemPath::canonicalize()
m_string = builder.to_string();
}
bool FileSystemPath::has_extension(const StringView& extension) const
bool LexicalPath::has_extension(const StringView& extension) const
{
return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive);
}
String canonicalized_path(const StringView& path)
String LexicalPath::canonicalized_path(const StringView& path)
{
return FileSystemPath(path).string();
return LexicalPath(path).string();
}
}

View file

@ -31,10 +31,10 @@
namespace AK {
class FileSystemPath {
class LexicalPath {
public:
FileSystemPath() {}
explicit FileSystemPath(const StringView&);
LexicalPath() { }
explicit LexicalPath(const StringView&);
bool is_valid() const { return m_is_valid; }
bool is_absolute() const { return m_is_absolute; }
@ -49,6 +49,8 @@ public:
bool has_extension(const StringView&) const;
static String canonicalized_path(const StringView&);
private:
void canonicalize();
@ -62,9 +64,6 @@ private:
bool m_is_absolute { false };
};
String canonicalized_path(const StringView&);
};
using AK::canonicalized_path;
using AK::FileSystemPath;
using AK::LexicalPath;

View file

@ -26,17 +26,17 @@
#include <AK/TestSuite.h>
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <AK/FileSystemPath.h>
TEST_CASE(construct)
{
EXPECT_EQ(FileSystemPath().is_valid(), false);
EXPECT_EQ(LexicalPath().is_valid(), false);
}
TEST_CASE(basic)
{
FileSystemPath path("/abc/def/ghi.txt");
LexicalPath path("/abc/def/ghi.txt");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.basename(), "ghi.txt");
EXPECT_EQ(path.title(), "ghi");
@ -48,8 +48,8 @@ TEST_CASE(basic)
TEST_CASE(dotdot_coalescing)
{
EXPECT_EQ(FileSystemPath("/home/user/../../not/home").string(), "/not/home");
EXPECT_EQ(FileSystemPath("/../../../../").string(), "/");
EXPECT_EQ(LexicalPath("/home/user/../../not/home").string(), "/not/home");
EXPECT_EQ(LexicalPath("/../../../../").string(), "/");
}
// Temporarily disabled, as they were broken by commit a3e4dfdf9859a9b955bf4728328f740a47de5851
@ -59,21 +59,21 @@ TEST_CASE(dotdot_coalescing)
TEST_CASE(relative_paths)
{
{
FileSystemPath path("simple");
LexicalPath path("simple");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.string(), "./simple");
EXPECT_EQ(path.parts().size(), 2u);
EXPECT_EQ(path.basename(), "simple");
}
{
FileSystemPath path("a/relative/path");
LexicalPath path("a/relative/path");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.string(), "./a/relative/path");
EXPECT_EQ(path.parts().size(), 4u);
EXPECT_EQ(path.basename(), "path");
}
{
FileSystemPath path("./././foo");
LexicalPath path("./././foo");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.string(), "./foo");
EXPECT_EQ(path.parts().size(), 2u);
@ -81,7 +81,7 @@ TEST_CASE(relative_paths)
}
{
FileSystemPath path(".");
LexicalPath path(".");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.string(), ".");
EXPECT_EQ(path.parts().size(), 1u);
@ -123,4 +123,4 @@ TEST_CASE(has_extension)
#endif
TEST_MAIN(FileSystemPath)
TEST_MAIN(LexicalPath)

View file

@ -24,7 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FileSystemPath.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
#include <AK/URL.h>
@ -314,22 +314,22 @@ URL URL::complete_url(const String& string) const
}
StringBuilder builder;
FileSystemPath fspath(path());
LexicalPath lexical_path(path());
builder.append('/');
bool document_url_ends_in_slash = path()[path().length() - 1] == '/';
for (size_t i = 0; i < fspath.parts().size(); ++i) {
if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash)
for (size_t i = 0; i < lexical_path.parts().size(); ++i) {
if (i == lexical_path.parts().size() - 1 && !document_url_ends_in_slash)
break;
builder.append(fspath.parts()[i]);
builder.append(lexical_path.parts()[i]);
builder.append('/');
}
builder.append(string);
auto built = builder.to_string();
fspath = FileSystemPath(built);
lexical_path = LexicalPath(built);
built = fspath.string();
built = lexical_path.string();
if (string.ends_with('/') && !built.ends_with('/')) {
builder.clear();
builder.append(built);
@ -399,7 +399,7 @@ URL URL::create_with_url_or_path(const String& url_or_path)
if (url.is_valid())
return url;
String path = canonicalized_path(url_or_path);
String path = LexicalPath::canonicalized_path(url_or_path);
return URL::create_with_file_protocol(path);
}
@ -407,7 +407,7 @@ String URL::basename() const
{
if (!m_valid)
return {};
return FileSystemPath(m_path).basename();
return LexicalPath(m_path).basename();
}
}