FileSystem: Fix zeros getting stripped in path

This commit is contained in:
Connor McLaughlin 2022-07-31 17:37:21 +10:00
parent 3505ca26e0
commit 97d5d659d3
2 changed files with 9 additions and 3 deletions

View File

@ -230,6 +230,8 @@ TEST(FileSystem, SanitizeFileName)
ASSERT_EQ(Path::SanitizeFileName(u8"foo/bar"), u8"foo_bar");
ASSERT_EQ(Path::SanitizeFileName(u8"f🙃o"), u8"f🙃o");
ASSERT_EQ(Path::SanitizeFileName(u8"ŻąłóРстуぬねのはen🍪⟑η∏☉ⴤ∩₲ ₱⟑♰⫳🐱"), u8"ŻąłóРстуぬねのはen🍪⟑η∏☉ⴤ∩₲ ₱⟑♰⫳🐱");
ASSERT_EQ(Path::SanitizeFileName(u8"abcdefghijlkmnopqrstuvwxyz-0123456789+&=_[]{}"), u8"abcdefghijlkmnopqrstuvwxyz-0123456789+&=_[]{}");
ASSERT_EQ(Path::SanitizeFileName(u8"some*path**with*asterisks"), u8"some_path__with_asterisks");
#ifdef _WIN32
ASSERT_EQ(Path::SanitizeFileName(u8"foo:"), u8"foo_");
ASSERT_EQ(Path::SanitizeFileName(u8"foo:bar."), u8"foo_bar_");

View File

@ -60,14 +60,14 @@ static std::time_t ConvertFileTimeToUnixTime(const FILETIME& ft)
}
#endif
static inline bool FileSystemCharacterIsSane(char32_t c, bool strip_slashes)
__declspec(noinline) static inline bool FileSystemCharacterIsSane(char32_t c, bool strip_slashes)
{
#ifdef _WIN32
// https://docs.microsoft.com/en-gb/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#naming-conventions
if ((c == U'/' || c == U'\\') && strip_slashes)
return false;
if (c == U'<' || c == U'>' || c == U':' || c == U'"' || c == U'|' || c == U'?' || c == U'*' || c == U'0' ||
if (c == U'<' || c == U'>' || c == U':' || c == U'"' || c == U'|' || c == U'?' || c == U'*' || c == 0 ||
c <= static_cast<char32_t>(31))
{
return false;
@ -76,7 +76,11 @@ static inline bool FileSystemCharacterIsSane(char32_t c, bool strip_slashes)
if (c == '/' && strip_slashes)
return false;
// macos doesn't allow colons, apparently
// drop asterisks too, they make globbing annoying
if (c == '*')
return false;
// macos doesn't allow colons, apparently
#ifdef __APPLE__
if (c == U':')
return false;