ClickHouse/src/Common/escapeForFileName.cpp

57 lines
1001 B
C++
Raw Normal View History

2017-06-01 13:41:58 +00:00
#include <Common/hex.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/escapeForFileName.h>
namespace DB
{
std::string escapeForFileName(const std::string & s)
{
std::string res;
const char * pos = s.data();
const char * end = pos + s.size();
while (pos != end)
{
2017-08-07 08:03:23 +00:00
unsigned char c = *pos;
if (isWordCharASCII(c))
res += c;
else
{
res += '%';
res += hexDigitUppercase(c / 16);
res += hexDigitUppercase(c % 16);
}
++pos;
}
return res;
}
std::string unescapeForFileName(const std::string & s)
{
std::string res;
const char * pos = s.data();
const char * end = pos + s.size();
while (pos != end)
{
2017-08-09 01:34:01 +00:00
if (!(*pos == '%' && pos + 2 < end))
{
res += *pos;
++pos;
}
else
{
2017-08-09 01:34:01 +00:00
++pos;
res += unhex2(pos);
pos += 2;
}
}
return res;
}
}