forked from organicmaps/organicmaps
Do not strip non-ASCII characters from exported bookmarks
Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
parent
f8dec32d8f
commit
38af9ddb06
3 changed files with 21 additions and 15 deletions
|
@ -211,11 +211,13 @@ std::string GetFileExt(std::string const & filePath)
|
|||
return strings::MakeLowerCase(base::GetFileExtension(filePath));
|
||||
}
|
||||
|
||||
bool IsBadCharForPath(char c)
|
||||
bool IsBadCharForPath(strings::UniChar c)
|
||||
{
|
||||
for (char illegalChar : {':', '/', '\\', '<', '>', '\"', '|', '?', '*'})
|
||||
if (c < ' ')
|
||||
return true;
|
||||
for (strings::UniChar const illegalChar : {':', '/', '\\', '<', '>', '\"', '|', '?', '*'})
|
||||
{
|
||||
if (c < ' ' || illegalChar == c)
|
||||
if (illegalChar == c)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -228,14 +230,18 @@ std::string GetBookmarksDirectory()
|
|||
return base::JoinPath(GetPlatform().SettingsDir(), "bookmarks");
|
||||
}
|
||||
|
||||
std::string RemoveInvalidSymbols(std::string name)
|
||||
std::string RemoveInvalidSymbols(std::string const & name)
|
||||
{
|
||||
// Remove not allowed symbols.
|
||||
name.erase(std::remove_if(name.begin(), name.end(), [](char c)
|
||||
strings::UniString filtered;
|
||||
filtered.reserve(name.size());
|
||||
auto it = name.begin();
|
||||
while (it != name.end())
|
||||
{
|
||||
return IsBadCharForPath(c);
|
||||
}), name.end());
|
||||
return name;
|
||||
auto const c = ::utf8::unchecked::next(it);
|
||||
if (!IsBadCharForPath(c))
|
||||
filtered.push_back(c);
|
||||
}
|
||||
return strings::ToUtf8(filtered);
|
||||
}
|
||||
|
||||
std::string GenerateUniqueFileName(const std::string & path, std::string name, std::string const & ext)
|
||||
|
@ -251,9 +257,9 @@ std::string GenerateUniqueFileName(const std::string & path, std::string name, s
|
|||
return base::JoinPath(path, name + suffix + ext);
|
||||
}
|
||||
|
||||
std::string GenerateValidAndUniqueFilePathForKML(std::string fileName)
|
||||
std::string GenerateValidAndUniqueFilePathForKML(std::string const & fileName)
|
||||
{
|
||||
std::string filePath = RemoveInvalidSymbols(std::move(fileName));
|
||||
std::string filePath = RemoveInvalidSymbols(fileName);
|
||||
if (filePath.empty())
|
||||
filePath = kDefaultBookmarksFileName;
|
||||
|
||||
|
|
|
@ -89,9 +89,9 @@ inline std::string DebugPrint(KmlFileType fileType)
|
|||
/// @name File name/path helpers.
|
||||
/// @{
|
||||
std::string GetBookmarksDirectory();
|
||||
std::string RemoveInvalidSymbols(std::string name);
|
||||
std::string RemoveInvalidSymbols(std::string const & name);
|
||||
std::string GenerateUniqueFileName(const std::string & path, std::string name, std::string const & ext = kKmlExtension);
|
||||
std::string GenerateValidAndUniqueFilePathForKML(std::string fileName);
|
||||
std::string GenerateValidAndUniqueFilePathForKML(std::string const & fileName);
|
||||
/// @}
|
||||
|
||||
/// @name SerDes helpers.
|
||||
|
|
|
@ -482,8 +482,8 @@ UNIT_TEST(Bookmarks_AddressInfo)
|
|||
|
||||
UNIT_TEST(Bookmarks_IllegalFileName)
|
||||
{
|
||||
vector<string> const arrIllegal = {"?", "?|", "\"x", "|x:", "x<>y", "xy*"};
|
||||
vector<string> const arrLegal = {"", "", "x", "x", "xy", "xy"};
|
||||
vector<string> const arrIllegal = {"?", "?|", "ч\"x", "|x:", "x<>y", "xy*地圖"};
|
||||
vector<string> const arrLegal = {"", "", "чx", "x", "xy", "xy地圖"};
|
||||
|
||||
for (size_t i = 0; i < arrIllegal.size(); ++i)
|
||||
TEST_EQUAL(arrLegal[i], RemoveInvalidSymbols(arrIllegal[i]), ());
|
||||
|
|
Loading…
Add table
Reference in a new issue