Spicy
string.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <vector>
7 
8 #include <hilti/ast/builder/expression.h>
9 #include <hilti/ast/operators/common.h>
10 #include <hilti/ast/types/bool.h>
11 #include <hilti/ast/types/bytes.h>
12 #include <hilti/ast/types/string.h>
13 
14 namespace hilti::operator_ {
15 
16 STANDARD_OPERATOR_2(string, Equal, type::Bool(), type::String(), type::String(),
17  "Compares two strings lexicographically.")
18 STANDARD_OPERATOR_2(string, Unequal, type::Bool(), type::String(), type::String(),
19  "Compares two strings lexicographically.")
20 STANDARD_OPERATOR_1(string, Size, type::UnsignedInteger(64), type::String(),
21  "Returns the number of characters the string contains.");
22 STANDARD_OPERATOR_2(string, Sum, type::String(), type::String(), type::String(),
23  "Returns the concatenation of two strings.");
24 
25 BEGIN_METHOD(string, Encode)
26  const auto& signature() const {
27  static auto _signature =
28  Signature{.self = type::constant(type::String()),
29  .result = type::Bytes(),
30  .id = "encode",
31  .args = {{"charset", type::Enum(type::Wildcard()), false, builder::id("hilti::Charset::UTF8")}},
32  .doc =
33  R"(
34 Converts the string into a binary representation encoded with the given character set.
35 )"};
36  return _signature;
37  }
38 END_METHOD
39 
40 BEGIN_OPERATOR_CUSTOM(string, Modulo)
41  Type result(const hilti::node::Range<Expression>& /* ops */) const { return type::String(); }
42 
43  bool isLhs() const { return false; }
44  auto priority() const { return hilti::operator_::Priority::Normal; }
45 
46  const std::vector<Operand>& operands() const {
47  static std::vector<Operand> _operands = {{{}, type::String()}, {{}, type::Any()}};
48  return _operands;
49  }
50 
51  void validate(const expression::ResolvedOperator& /* i */, operator_::position_t /* p */) const {
52  // TODO(robin): Not sure if we need this restriction. Let's try without.
53  //
54  // if ( i.op1().type().isA<type::Tuple>() && ! i.op1().isA<expression::Ctor>() )
55  // p.node.addError("tuple argument to '%' must a be constant");
56  }
57 
58  std::string doc() const { return "Renders a printf-style format string."; }
59 END_OPERATOR_CUSTOM
60 
61 } // namespace hilti::operator_
Definition: node.h:37
Definition: operator-registry.h:15