⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 277219 in webkit


Ignore:
Timestamp:
May 7, 2021, 6:05:02 PM (5 years ago)
Author:
Chris Dumez
Message:

Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
​https://bugs.webkit.org/show_bug.cgi?id=225524

Reviewed by Darin Adler.

Source/WTF:

Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem so
that we can get rid of platform-specific implementions.

  • wtf/FileSystem.cpp:

(WTF::FileSystemImpl::pathGetFileName):
(WTF::FileSystemImpl::directoryName):

  • wtf/glib/FileSystemGlib.cpp:

(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.

  • wtf/posix/FileSystemPOSIX.cpp:

(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.

  • wtf/win/FileSystemWin.cpp:

(WTF::FileSystemImpl::pathGetFileName): Deleted.
(WTF::FileSystemImpl::directoryName): Deleted.

Tools:

Add API test coverage.

  • TestWebKitAPI/Tests/WTF/FileSystem.cpp:

(TestWebKitAPI::TEST_F):

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r277210 r277219  
     12021-05-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
     4        https://bugs.webkit.org/show_bug.cgi?id=225524
     5
     6        Reviewed by Darin Adler.
     7
     8        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem so
     9        that we can get rid of platform-specific implementions.
     10
     11        * wtf/FileSystem.cpp:
     12        (WTF::FileSystemImpl::pathGetFileName):
     13        (WTF::FileSystemImpl::directoryName):
     14        * wtf/glib/FileSystemGlib.cpp:
     15        (WTF::FileSystemImpl::pathGetFileName): Deleted.
     16        (WTF::FileSystemImpl::directoryName): Deleted.
     17        * wtf/posix/FileSystemPOSIX.cpp:
     18        (WTF::FileSystemImpl::pathGetFileName): Deleted.
     19        (WTF::FileSystemImpl::directoryName): Deleted.
     20        * wtf/win/FileSystemWin.cpp:
     21        (WTF::FileSystemImpl::pathGetFileName): Deleted.
     22        (WTF::FileSystemImpl::directoryName): Deleted.
     23
    1242021-05-07  Tim Horton  <timothy_horton@apple.com>
    225
  • trunk/Source/WTF/wtf/FileSystem.cpp

    r277158 r277219  
    722722}
    723723
     724String pathGetFileName(const String& path)
     725{
     726    std::filesystem::path fsPath = fileSystemRepresentation(path).data();
     727    return String::fromUTF8(fsPath.filename().u8string().c_str());
     728}
     729
     730String directoryName(const String& path)
     731{
     732    std::filesystem::path fsPath = fileSystemRepresentation(path).data();
     733    return String::fromUTF8(fsPath.parent_path().u8string().c_str());
     734}
     735
    724736} // namespace FileSystemImpl
    725737} // namespace WTF
  • trunk/Source/WTF/wtf/glib/FileSystemGlib.cpp

    r277168 r277219  
    158158}
    159159
    160 String pathGetFileName(const String& path)
    161 {
    162     auto filename = fileSystemRepresentation(path);
    163     if (!validRepresentation(filename))
    164         return path;
    165 
    166     GUniquePtr<gchar> baseName(g_path_get_basename(filename.data()));
    167     return String::fromUTF8(baseName.get());
    168 }
    169 
    170 String directoryName(const String& path)
    171 {
    172     auto filename = fileSystemRepresentation(path);
    173     if (!validRepresentation(filename))
    174         return String();
    175 
    176     GUniquePtr<char> dirname(g_path_get_dirname(filename.data()));
    177     return String::fromUTF8(dirname.get());
    178 }
    179 
    180160Vector<String> listDirectory(const String& path, const String& filter)
    181161{
  • trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp

    r277158 r277219  
    203203}
    204204
    205 String pathGetFileName(const String& path)
    206 {
    207     return path.substring(path.reverseFind('/') + 1);
    208 }
    209 
    210 String directoryName(const String& path)
    211 {
    212     CString fsRep = fileSystemRepresentation(path);
    213 
    214     if (!fsRep.data() || fsRep.data()[0] == '\0')
    215         return String();
    216 
    217     return String::fromUTF8(dirname(fsRep.mutableData()));
    218 }
    219 
    220205Vector<String> listDirectory(const String& path, const String& filter)
    221206{
  • trunk/Source/WTF/wtf/win/FileSystemWin.cpp

    r277158 r277219  
    190190}
    191191
    192 String pathGetFileName(const String& path)
    193 {
    194     return String(::PathFindFileName(path.wideCharacters().data()));
    195 }
    196 
    197 String directoryName(const String& path)
    198 {
    199     String name = path.left(path.length() - pathGetFileName(path).length());
    200     if (name.characterStartingAt(name.length() - 1) == '\\'
    201         || name.characterStartingAt(name.length() - 1) == '/') {
    202         // Remove any trailing "\" or "/"
    203         name.truncate(name.length() - 1);
    204     }
    205     return name;
    206 }
    207 
    208192static String bundleName()
    209193{
  • trunk/Tools/ChangeLog

    r277209 r277219  
     12021-05-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Port FileSystem::pathGetFileName() & Filesystem::directoryName() to std::filesystem
     4        https://bugs.webkit.org/show_bug.cgi?id=225524
     5
     6        Reviewed by Darin Adler.
     7
     8        Add API test coverage.
     9
     10        * TestWebKitAPI/Tests/WTF/FileSystem.cpp:
     11        (TestWebKitAPI::TEST_F):
     12
    1132021-05-07  Brent Fulgham  <bfulgham@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp

    r277178 r277219  
    707707}
    708708
     709TEST_F(FileSystemTest, pathGetFileName)
     710{
     711    auto testPath = FileSystem::pathByAppendingComponents(tempEmptyFolderPath(), { "subfolder", "filename.txt" });
     712    EXPECT_STREQ("filename.txt", FileSystem::pathGetFileName(testPath).utf8().data());
     713
     714#if OS(UNIX)
     715    EXPECT_STREQ(".", FileSystem::pathGetFileName(".").utf8().data());
     716    EXPECT_STREQ("..", FileSystem::pathGetFileName("..").utf8().data());
     717    EXPECT_STREQ("", FileSystem::pathGetFileName("/").utf8().data());
     718    EXPECT_STREQ(".", FileSystem::pathGetFileName("/foo/.").utf8().data());
     719    EXPECT_STREQ("..", FileSystem::pathGetFileName("/foo/..").utf8().data());
     720    EXPECT_STREQ("", FileSystem::pathGetFileName("/foo/").utf8().data());
     721    EXPECT_STREQ("host", FileSystem::pathGetFileName("//host").utf8().data());
     722#endif
     723#if OS(WINDOWS)
     724    EXPECT_STREQ("", FileSystem::pathGetFileName("C:\\").utf8().data());
     725    EXPECT_STREQ("foo", FileSystem::pathGetFileName("C:\\foo").utf8().data());
     726    EXPECT_STREQ("", FileSystem::pathGetFileName("C:\\foo\\").utf8().data());
     727    EXPECT_STREQ("bar.txt", FileSystem::pathGetFileName("C:\\foo\\bar.txt").utf8().data());
     728#endif
     729}
     730
     731TEST_F(FileSystemTest, directoryName)
     732{
     733    auto testPath = FileSystem::pathByAppendingComponents(tempEmptyFolderPath(), { "subfolder", "filename.txt" });
     734    EXPECT_STREQ(FileSystem::pathByAppendingComponent(tempEmptyFolderPath(), "subfolder").utf8().data(), FileSystem::directoryName(testPath).utf8().data());
     735#if OS(UNIX)
     736    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/example.txt").utf8().data());
     737    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/").utf8().data());
     738    EXPECT_STREQ("/var/tmp", FileSystem::directoryName("/var/tmp/.").utf8().data());
     739    EXPECT_STREQ("/", FileSystem::directoryName("/").utf8().data());
     740#endif
     741#if OS(WINDOWS)
     742    EXPECT_STREQ("C:\\foo", FileSystem::directoryName("C:\\foo\\example.txt").utf8().data());
     743    EXPECT_STREQ("C:\\", FileSystem::directoryName("C:\\").utf8().data());
     744#endif
     745}
     746
    709747} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.