Add function isValidIdentifier().

This commit is contained in:
Vitaly Baranov 2020-08-08 19:34:35 +03:00
parent 5401694138
commit 2c5a2b2f43
2 changed files with 8 additions and 15 deletions

View File

@ -122,6 +122,11 @@ inline bool isPrintableASCII(char c)
return uc >= 32 && uc <= 126; /// 127 is ASCII DEL.
}
inline bool isValidIdentifier(const std::string_view & str)
{
return !str.empty() && isValidIdentifierBegin(str[0]) && std::all_of(str.begin() + 1, str.end(), isWordCharASCII);
}
/// Works assuming isAlphaASCII.
inline char toLowerIfAlphaASCII(char c)
{

View File

@ -68,22 +68,10 @@ void writeException(const Exception & e, WriteBuffer & buf, bool with_stack_trac
template <typename F>
static inline void writeProbablyQuotedStringImpl(const StringRef & s, WriteBuffer & buf, F && write_quoted_string)
{
if (!s.size || !isValidIdentifierBegin(s.data[0]))
{
write_quoted_string(s, buf);
}
else
{
const char * pos = s.data + 1;
const char * end = s.data + s.size;
for (; pos < end; ++pos)
if (!isWordCharASCII(*pos))
break;
if (pos != end)
write_quoted_string(s, buf);
else
if (isValidIdentifier(std::string_view{s}))
writeString(s, buf);
}
else
write_quoted_string(s, buf);
}
void writeProbablyBackQuotedString(const StringRef & s, WriteBuffer & buf)