mkql_builtins_with.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "mkql_builtins_compare.h"
  2. namespace NKikimr {
  3. namespace NMiniKQL {
  4. namespace {
  5. template<bool Reverse>
  6. NUdf::TUnboxedValuePod StringWith(const NUdf::TUnboxedValuePod full, const NUdf::TUnboxedValuePod part) {
  7. const std::string_view one = full.AsStringRef();
  8. const std::string_view two = part.AsStringRef();
  9. return NUdf::TUnboxedValuePod(Reverse ? one.ends_with(two) : one.starts_with(two));
  10. }
  11. NUdf::TUnboxedValuePod StringContains(const NUdf::TUnboxedValuePod full, const NUdf::TUnboxedValuePod part) {
  12. const std::string_view one = full.AsStringRef();
  13. const std::string_view two = part.AsStringRef();
  14. return NUdf::TUnboxedValuePod(one.contains(two));
  15. }
  16. template <NUdf::TUnboxedValuePod (*StringFunc)(const NUdf::TUnboxedValuePod full, const NUdf::TUnboxedValuePod part)>
  17. struct TStringWith {
  18. static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod string, NUdf::TUnboxedValuePod sub)
  19. {
  20. return StringFunc(string, sub);
  21. }
  22. #ifndef MKQL_DISABLE_CODEGEN
  23. static Value* Generate(Value* string, Value* sub, const TCodegenContext& ctx, BasicBlock*& block)
  24. {
  25. auto& context = ctx.Codegen.GetContext();
  26. const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(StringFunc));
  27. const auto funType = FunctionType::get(string->getType(), {string->getType(), sub->getType()}, false);
  28. const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "func", block);
  29. const auto result = CallInst::Create(funType, funcPtr, {string, sub}, "has", block);
  30. return result;
  31. }
  32. #endif
  33. };
  34. template<NUdf::EDataSlot> using TStartsWith = TStringWith<&StringWith<false>>;
  35. template<NUdf::EDataSlot> using TEndsWith = TStringWith<&StringWith<true>>;
  36. template<NUdf::EDataSlot> using TContains = TStringWith<&StringContains>;
  37. }
  38. void RegisterWith(IBuiltinFunctionRegistry& registry) {
  39. RegisterCompareStrings<TStartsWith, TCompareArgsOpt, false>(registry, "StartsWith");
  40. RegisterCompareStrings<TEndsWith, TCompareArgsOpt, false>(registry, "EndsWith");
  41. RegisterCompareStrings<TContains, TCompareArgsOpt, false>(registry, "StringContains");
  42. }
  43. } // namespace NMiniKQL
  44. } // namespace NKikimr