ClickHouse/src/Functions/initcap.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.7 KiB
C++
Raw Normal View History

2023-06-30 13:11:06 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringToString.h>
2023-06-30 13:53:20 +00:00
#include <Common/StringUtils/StringUtils.h>
2023-06-30 13:11:06 +00:00
namespace DB
{
namespace
{
struct InitcapImpl
{
static void vector(const ColumnString::Chars & data,
const ColumnString::Offsets & offsets,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets)
{
2023-07-03 18:09:08 +00:00
if (data.empty())
return;
2023-06-30 13:11:06 +00:00
res_data.resize(data.size());
res_offsets.assign(offsets);
array(data.data(), data.data() + data.size(), res_data.data());
}
static void vectorFixed(const ColumnString::Chars & data, size_t /*n*/, ColumnString::Chars & res_data)
{
res_data.resize(data.size());
array(data.data(), data.data() + data.size(), res_data.data());
}
private:
static void array(const UInt8 * src, const UInt8 * src_end, UInt8 * dst)
{
2023-06-30 13:53:20 +00:00
bool prev_alphanum = false;
2023-06-30 13:11:06 +00:00
for (; src < src_end; ++src, ++dst)
{
2023-06-30 13:53:20 +00:00
char c = *src;
bool alphanum = isAlphaNumericASCII(c);
if (alphanum && !prev_alphanum)
if (isAlphaASCII(c))
*dst = toUpperIfAlphaASCII(c);
2023-06-30 13:11:06 +00:00
else
2023-06-30 13:53:20 +00:00
*dst = c;
else if (isAlphaASCII(c))
*dst = toLowerIfAlphaASCII(c);
2023-06-30 13:11:06 +00:00
else
2023-06-30 13:53:20 +00:00
*dst = c;
prev_alphanum = alphanum;
2023-06-30 13:11:06 +00:00
}
}
};
struct NameInitcap
{
static constexpr auto name = "initcap";
};
using FunctionInitcap = FunctionStringToString<InitcapImpl, NameInitcap>;
}
REGISTER_FUNCTION(Initcap)
{
factory.registerFunction<FunctionInitcap>({}, FunctionFactory::CaseInsensitive);
}
}