ClickHouse/dbms/src/Common/StringUtils/StringUtils.h

137 lines
2.9 KiB
C++
Raw Normal View History

#pragma once
#include <string>
#include <cstring>
2017-07-27 21:05:33 +00:00
#include <cstddef>
2018-05-05 12:31:47 +00:00
#include <type_traits>
namespace detail
{
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);
}
inline bool startsWith(const std::string & s, const std::string & prefix)
{
return detail::startsWith(s, prefix.data(), prefix.size());
}
inline bool endsWith(const std::string & s, const std::string & suffix)
{
return detail::endsWith(s, suffix.data(), suffix.size());
}
/// With GCC, strlen is evaluated compile time if we pass it a constant
/// string that is known at compile time.
inline bool startsWith(const std::string & s, const char * prefix)
{
return detail::startsWith(s, prefix, strlen(prefix));
}
inline bool endsWith(const std::string & s, const char * suffix)
{
return detail::endsWith(s, suffix, strlen(suffix));
}
/// Given an integer, return the adequate suffix for
/// printing an ordinal number.
template <typename T>
std::string getOrdinalSuffix(T n)
{
2017-12-25 04:01:46 +00:00
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>,
"Unsigned integer value required");
2017-07-27 21:05:33 +00:00
const auto last_digit = n % 10;
2017-07-27 21:05:33 +00:00
if ((last_digit < 1 || last_digit > 3)
|| ((n > 10) && (((n / 10) % 10) == 1)))
return "th";
2017-07-27 21:05:33 +00:00
switch (last_digit)
{
2017-07-27 21:05:33 +00:00
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
};
}
/// More efficient than libc, because doesn't respect locale. But for some functions table implementation could be better.
inline bool isASCII(char c)
{
return static_cast<unsigned char>(c) < 0x80;
}
inline bool isAlphaASCII(char c)
{
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z');
}
inline bool isNumericASCII(char c)
{
return (c >= '0' && c <= '9');
}
inline bool isHexDigit(char c)
{
return isNumericASCII(c)
|| (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F');
}
inline bool isAlphaNumericASCII(char c)
{
return isAlphaASCII(c)
|| isNumericASCII(c);
}
inline bool isWordCharASCII(char c)
{
return isAlphaNumericASCII(c)
|| c == '_';
}
inline bool isValidIdentifierBegin(char c)
{
return isAlphaASCII(c)
|| c == '_';
}
inline bool isWhitespaceASCII(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
}
inline bool isControlASCII(char c)
{
return c >= 0 && c <= 31;
}
/// Works assuming isAlphaASCII.
inline char toLowerIfAlphaASCII(char c)
{
return c | 0x20;
}
inline char toUpperIfAlphaASCII(char c)
{
return c & (~0x20);
}
inline char alternateCaseIfAlphaASCII(char c)
{
return c ^ 0x20;
}
inline bool equalsCaseInsensitive(char a, char b)
{
return a == b || (isAlphaASCII(a) && alternateCaseIfAlphaASCII(a) == b);
}
std::string trim(const std::string & str, const std::function<bool(char)> & predicate);