Skip to content

Commit

Permalink
[共通] FileSystem::Extension(), FileName(), BaseName() の一貫性改善 #1223
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Jul 2, 2024
1 parent fb0bb47 commit 236a48c
Showing 1 changed file with 20 additions and 31 deletions.
51 changes: 20 additions & 31 deletions Siv3D/src/Siv3D/FileSystem/SivFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,49 @@ namespace s3d
{
namespace FileSystem
{
String Extension(const FilePathView path)
String Extension(FilePathView path)
{
if (not path)
if (path.isEmpty())
{
return{};
}

if constexpr (Platform::HasEmbeddedResource)
while (path.starts_with(U'.'))
{
if (IsResourcePath(path))
{
return{};
}
path.remove_prefix(1);
}

const size_t dotPos = path.lastIndexOf(U'.');
const size_t lastDotPos = path.lastIndexOf(U'.');

if (dotPos == String::npos)
if (lastDotPos == String::npos)
{
return{};
}

const size_t sepPos = path.lastIndexOfAny(U"/\\");
const size_t lastSeparatorPos = path.lastIndexOfAny(U"/\\");

if ((sepPos != String::npos) && (dotPos < sepPos))
// aaa.bbb/ccc のようなケースを弾く
if ((lastSeparatorPos != String::npos)
&& (lastDotPos < lastSeparatorPos))
{
return{};
}

return String{ path.substr(dotPos + 1) }.lowercase();
return String{ path.substr(lastDotPos + 1) }.lowercase();
}

String FileName(const FilePathView path)
String FileName(const FilePathView path_)
{
if (not path)
if (path_.isEmpty())
{
return{};
}

if constexpr (Platform::HasEmbeddedResource)
{
if (IsResourcePath(path))
{
return{};
}
}
FilePath path = FilePath{ path_ }.replace(U'\\', U'/');

if (path.ends_with(U'/'))
{
const size_t sepPos = path.lastIndexOf(U'/', path.length() - 2);

if (sepPos == String::npos)
{
return String(path.begin(), path.end() - 1);
}
else
{
return String(path.begin() + sepPos + 1, path.end() - 1);
}
return{};
}
else
{
Expand Down Expand Up @@ -107,6 +91,11 @@ namespace s3d
return fileName;
}

if ((dotPos == 0) || (dotPos == (fileName.size() - 1)))
{
return fileName;
}

return String(fileName.begin(), fileName.begin() + dotPos);
}

Expand Down

0 comments on commit 236a48c

Please sign in to comment.