2016-07-14 05:22:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2016-07-14 05:22:09 +00:00
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-09-15 21:27:34 +00:00
|
|
|
namespace DB { namespace ErrorCodes {
|
|
|
|
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
|
|
|
|
}}
|
2016-07-14 05:22:09 +00:00
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool startsWith(const std::string & s, const char * prefix, size_t prefix_size);
|
|
|
|
bool endsWith(const std::string & s, const char * suffix, size_t suffix_size);
|
2016-07-14 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool startsWith(const std::string & s, const std::string & prefix)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return detail::startsWith(s, prefix.data(), prefix.size());
|
2016-07-14 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool endsWith(const std::string & s, const std::string & suffix)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return detail::endsWith(s, suffix.data(), suffix.size());
|
2016-07-14 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 21:27:34 +00:00
|
|
|
/// With GCC, strlen is evaluated compile time if we pass it a constant
|
|
|
|
/// string that is known at compile time.
|
2016-07-14 05:22:09 +00:00
|
|
|
inline bool startsWith(const std::string & s, const char * prefix)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return detail::startsWith(s, prefix, strlen(prefix));
|
2016-07-14 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool endsWith(const std::string & s, const char * suffix)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return detail::endsWith(s, suffix, strlen(suffix));
|
2016-07-14 05:22:09 +00:00
|
|
|
}
|
2016-09-15 21:27:34 +00:00
|
|
|
|
|
|
|
/// Given an integer, return the adequate suffix for
|
|
|
|
/// printing an ordinal number.
|
|
|
|
template <typename T>
|
|
|
|
std::string getOrdinalSuffix(T n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
|
|
|
|
"Unsigned integer value required");
|
2016-09-15 21:27:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto val = n % 10;
|
2016-09-15 21:27:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_th;
|
|
|
|
if ((val >= 1) && (val <= 3))
|
|
|
|
is_th = (n > 10) && (((n / 10) % 10) == 1);
|
|
|
|
else
|
|
|
|
is_th = true;
|
2016-09-15 21:27:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (is_th)
|
|
|
|
return "th";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 1: return "st";
|
|
|
|
case 2: return "nd";
|
|
|
|
case 3: return "rd";
|
|
|
|
default: throw DB::Exception{"getOrdinalSuffix: internal error",
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR};
|
|
|
|
};
|
|
|
|
}
|
2016-09-15 21:27:34 +00:00
|
|
|
}
|
2016-08-16 21:23:53 +00:00
|
|
|
|
|
|
|
/// More efficient than libc, because doesn't respect locale.
|
|
|
|
|
|
|
|
inline bool isASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<unsigned char>(c) < 0x80;
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isAlphaASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (c >= 'a' && c <= 'z')
|
|
|
|
|| (c >= 'A' && c <= 'Z');
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isNumericASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (c >= '0' && c <= '9');
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isAlphaNumericASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return isAlphaASCII(c)
|
|
|
|
|| isNumericASCII(c);
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isWordCharASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return isAlphaNumericASCII(c)
|
|
|
|
|| c == '_';
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:58:07 +00:00
|
|
|
inline bool isValidIdentifierBegin(char c)
|
|
|
|
{
|
|
|
|
return isAlphaASCII(c)
|
|
|
|
|| c == '_';
|
|
|
|
}
|
|
|
|
|
2016-08-17 05:38:51 +00:00
|
|
|
inline bool isWhitespaceASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
|
2016-08-17 05:38:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:23:53 +00:00
|
|
|
/// Works assuming isAlphaASCII.
|
|
|
|
inline char toLowerIfAlphaASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return c | 0x20;
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline char toUpperIfAlphaASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return c & (~0x20);
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline char alternateCaseIfAlphaASCII(char c)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return c ^ 0x20;
|
2016-08-16 21:23:53 +00:00
|
|
|
}
|
2016-12-30 05:13:14 +00:00
|
|
|
|
|
|
|
inline bool equalsCaseInsensitive(char a, char b)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return a == b || (isAlphaASCII(a) && alternateCaseIfAlphaASCII(a) == b);
|
2016-12-30 05:13:14 +00:00
|
|
|
}
|
|
|
|
|