Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/bbannier/string-size'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 15, 2024
2 parents c06dca2 + c34daf6 commit 684ab5f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion hilti/runtime/include/types/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string_view>

#include <hilti/rt/extension-points.h>
#include <hilti/rt/safe-int.h>
#include <hilti/rt/util.h>

namespace hilti::rt {
Expand All @@ -27,7 +28,7 @@ HILTI_RT_ENUM_WITH_DEFAULT(DecodeErrorStrategy, IGNORE,
* @return the length of the input string
* @throws RuntimeError if the input is not a valid UTF8 string
*/
size_t size(const std::string& s, DecodeErrorStrategy errors = DecodeErrorStrategy::REPLACE);
integer::safe<uint64_t> size(const std::string& s, DecodeErrorStrategy errors = DecodeErrorStrategy::REPLACE);

/**
* Computes a lower-case version of an UTF8 string.
Expand Down
4 changes: 2 additions & 2 deletions hilti/runtime/src/types/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

using namespace hilti::rt;

size_t string::size(const std::string& s, DecodeErrorStrategy errors) {
integer::safe<uint64_t> string::size(const std::string& s, DecodeErrorStrategy errors) {
auto p = reinterpret_cast<const unsigned char*>(s.data());
auto e = p + s.size();

size_t len = 0;
uint64_t len = 0;

while ( p < e ) {
utf8proc_int32_t cp;
Expand Down
2 changes: 1 addition & 1 deletion hilti/toolchain/src/compiler/codegen/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ struct Visitor : hilti::visitor::PreOrder<cxx::Expression, Visitor> {

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::Size& n) { return fmt("::hilti::rt::string::size(%s)", op0(n)); }
result_t operator()(const operator_::string::Equal& n) { return binary(n, "=="); }
result_t operator()(const operator_::string::Unequal& n) { return binary(n, "!="); }

Expand Down
6 changes: 6 additions & 0 deletions tests/hilti/types/string/operators.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Foo {

import hilti;

global x1 = "abc";
x1 = x1 + "123";
assert x1 == "abc123";
Expand All @@ -18,4 +20,8 @@ assert !( "abc" == "123" );
assert !( "abc" != "abc" );
assert "abc" != "123";

assert |"𝔘𝔫𝔦𝔠𝔬𝔡𝔢"| == 7; # 7 codepoints but 28 bytes long.
assert |"abc"| == 3;
hilti::print(|"abc"|); # Validates that size operator returns a valid, printable runtime type.

}

0 comments on commit 684ab5f

Please sign in to comment.