From 77e9cb294a982964d71f6dc40730d27c36616166 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Thu, 4 Jan 2024 15:28:43 +0100 Subject: [PATCH] Add `+=` operator for `string`. This allows appending to a `string` without having to allocate a new string. This might perform better most of the time. Closes #1500. --- doc/autogen/types/string.rst | 4 ++++ .../toolchain/include/ast/operators/string.h | 2 ++ .../src/compiler/codegen/operators.cc | 1 + tests/hilti/types/string/operators.hlt | 21 +++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 tests/hilti/types/string/operators.hlt diff --git a/doc/autogen/types/string.rst b/doc/autogen/types/string.rst index 80aa223c7..a8485287e 100644 --- a/doc/autogen/types/string.rst +++ b/doc/autogen/types/string.rst @@ -23,6 +23,10 @@ Returns the concatenation of two strings. +.. spicy:operator:: string::SumAssign string t:string op:+= t:string + + Appends the second string to the first. + .. spicy:operator:: string::Unequal bool t:string op:!= t:string Compares two strings lexicographically. diff --git a/hilti/toolchain/include/ast/operators/string.h b/hilti/toolchain/include/ast/operators/string.h index 0b424dcac..d4159dd82 100644 --- a/hilti/toolchain/include/ast/operators/string.h +++ b/hilti/toolchain/include/ast/operators/string.h @@ -21,6 +21,8 @@ STANDARD_OPERATOR_1(string, Size, type::UnsignedInteger(64), type::String(), "Returns the number of characters the string contains."); STANDARD_OPERATOR_2(string, Sum, type::String(), type::String(), type::String(), "Returns the concatenation of two strings."); +STANDARD_OPERATOR_2(string, SumAssign, type::String(), type::String(), type::String(), + "Appends the second string to the first."); BEGIN_METHOD(string, Encode) const auto& signature() const { diff --git a/hilti/toolchain/src/compiler/codegen/operators.cc b/hilti/toolchain/src/compiler/codegen/operators.cc index 962e9c45e..4ae0a97c3 100644 --- a/hilti/toolchain/src/compiler/codegen/operators.cc +++ b/hilti/toolchain/src/compiler/codegen/operators.cc @@ -727,6 +727,7 @@ struct Visitor : hilti::visitor::PreOrder { // String result_t operator()(const operator_::string::Sum& n) { return binary(n, "+"); } + result_t operator()(const operator_::string::SumAssign& n) { return binary(n, "+="); } result_t operator()(const operator_::string::Size& n) { return fmt("%s.size()", op0(n)); } result_t operator()(const operator_::string::Equal& n) { return binary(n, "=="); } result_t operator()(const operator_::string::Unequal& n) { return binary(n, "!="); } diff --git a/tests/hilti/types/string/operators.hlt b/tests/hilti/types/string/operators.hlt new file mode 100644 index 000000000..d8fd2469d --- /dev/null +++ b/tests/hilti/types/string/operators.hlt @@ -0,0 +1,21 @@ +# @TEST-EXEC: ${HILTIC} -dj %INPUT + +module Foo { + +global x1 = "abc"; +x1 = x1 + "123"; +assert x1 == "abc123"; + +global x2 = "abc"; +x2 += "123"; +assert x2 == "abc123"; + +assert |"abc"| == 3; + +assert "abc" == "abc"; +assert !( "abc" == "123" ); + +assert !( "abc" != "abc" ); +assert "abc" != "123"; + +}