Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/autogen/types/bytes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
Returns the subsequence from the value's beginning to (but not
including) *end*.

.. spicy:method:: bytes::sub bytes sub False bytes (end: uint<64>)

Returns the subsequence from the value's beginning to (but not
including) offset *end*.

.. spicy:method:: bytes::to_int bytes to_int False int<64> ([ base: uint<64> ])

Interprets the data as representing an ASCII-encoded number and
Expand Down
6 changes: 6 additions & 0 deletions doc/autogen/types/stream-view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
Returns a new view of the subsequence from the beginning of the stream
up to (but not including) *end*.

.. spicy:method:: stream::view::sub view<stream> sub False view<stream> (end: uint<64>)

Returns a new view of the subsequence from the beginning of the view
to (but not including) offset *end*. The offset is relative to the
beginning of the view.

.. rubric:: View Operators

.. spicy:operator:: stream::view::Equal bool t:view<stream> <sp> op:== <sp> t:bytes $commutative$
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class Strip;
class SubIterators;
class SubIterator;
class SubOffsets;
class SubOffset;
class Join;
class ToIntAscii;
class ToUIntAscii;
Expand Down Expand Up @@ -463,6 +464,7 @@ class StartsWith;
class SubIterators;
class SubIterator;
class SubOffsets;
class SubOffset;
} // namespace view

class Ctor;
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/node-tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ constexpr Tag ToUIntAscii = 930;
constexpr Tag ToUIntBinary = 931;
constexpr Tag Unequal = 932;
constexpr Tag UpperCase = 933;
constexpr Tag SubOffset = 934;

namespace iterator {
constexpr Tag Deref = 1000;
Expand Down Expand Up @@ -476,6 +477,7 @@ constexpr Tag SubIterators = 3314;
constexpr Tag SubOffsets = 3315;
constexpr Tag UnequalBytes = 3316;
constexpr Tag UnequalView = 3317;
constexpr Tag SubOffset = 3318;
} // namespace view

} // namespace stream
Expand Down
1 change: 1 addition & 0 deletions hilti/toolchain/include/ast/operators/bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ HILTI_NODE_OPERATOR(bytes, Strip)
HILTI_NODE_OPERATOR(bytes, SubIterators)
HILTI_NODE_OPERATOR(bytes, SubIterator)
HILTI_NODE_OPERATOR(bytes, SubOffsets)
HILTI_NODE_OPERATOR(bytes, SubOffset)
HILTI_NODE_OPERATOR(bytes, Join)
HILTI_NODE_OPERATOR(bytes, ToIntAscii)
HILTI_NODE_OPERATOR(bytes, ToUIntAscii)
Expand Down
1 change: 1 addition & 0 deletions hilti/toolchain/include/ast/operators/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ HILTI_NODE_OPERATOR(stream::view, StartsWith)
HILTI_NODE_OPERATOR(stream::view, SubIterators)
HILTI_NODE_OPERATOR(stream::view, SubIterator)
HILTI_NODE_OPERATOR(stream::view, SubOffsets)
HILTI_NODE_OPERATOR(stream::view, SubOffset)
HILTI_NODE_OPERATOR(stream, Ctor)
HILTI_NODE_OPERATOR(stream, Size)
HILTI_NODE_OPERATOR(stream, Unequal)
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/visitor-dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Dispatcher {
virtual void operator()(hilti::operator_::bytes::SubIterators*) {}
virtual void operator()(hilti::operator_::bytes::SubIterator*) {}
virtual void operator()(hilti::operator_::bytes::SubOffsets*) {}
virtual void operator()(hilti::operator_::bytes::SubOffset*) {}
virtual void operator()(hilti::operator_::bytes::Join*) {}
virtual void operator()(hilti::operator_::bytes::ToIntAscii*) {}
virtual void operator()(hilti::operator_::bytes::ToUIntAscii*) {}
Expand Down Expand Up @@ -359,6 +360,7 @@ class Dispatcher {
virtual void operator()(hilti::operator_::stream::view::SubIterators*) {}
virtual void operator()(hilti::operator_::stream::view::SubIterator*) {}
virtual void operator()(hilti::operator_::stream::view::SubOffsets*) {}
virtual void operator()(hilti::operator_::stream::view::SubOffset*) {}
virtual void operator()(hilti::operator_::stream::Ctor*) {}
virtual void operator()(hilti::operator_::stream::Size*) {}
virtual void operator()(hilti::operator_::stream::Unequal*) {}
Expand Down
25 changes: 25 additions & 0 deletions hilti/toolchain/src/ast/operators/bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,31 @@ Returns the subsequence from offset *begin* to (but not including) offset *end*.
};
HILTI_OPERATOR_IMPLEMENTATION(SubOffsets);

class SubOffset : public BuiltInMemberCall {
public:
Signature signature(Builder* builder) const final {
return Signature{
.kind = Kind::MemberCall,
.self = {.kind = parameter::Kind::In, .type = builder->typeBytes()},
.member = "sub",
.param0 =
{
.name = "end",
.type = {.kind = parameter::Kind::In, .type = builder->typeUnsignedInteger(64)},
},
.result = {.constness = Constness::Const, .type = builder->typeBytes()},
.ns = "bytes",
.doc = R"(
Returns the subsequence from the value's beginning to (but not including)
offset *end*.
)",
};
}

HILTI_OPERATOR(hilti, bytes::SubOffset);
};
HILTI_OPERATOR_IMPLEMENTATION(SubOffset);

class Join : public BuiltInMemberCall {
public:
Signature signature(Builder* builder) const final {
Expand Down
26 changes: 26 additions & 0 deletions hilti/toolchain/src/ast/operators/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,32 @@ offset *end*. The offsets are relative to the beginning of the view.
};
HILTI_OPERATOR_IMPLEMENTATION(SubOffsets);

class SubOffset : public BuiltInMemberCall {
public:
Signature signature(Builder* builder) const final {
return Signature{
.kind = Kind::MemberCall,
.self = {.kind = parameter::Kind::In, .type = builder->typeStreamView()},
.member = "sub",
.param0 =
{
.name = "end",
.type = {.kind = parameter::Kind::In, .type = builder->typeUnsignedInteger(64)},
},
.result = {.constness = Constness::Const, .type = builder->typeStreamView()},
.ns = "stream::view",
.doc = R"(
Returns a new view of the subsequence from the beginning of the view to (but
not including) offset *end*. The offset is relative to the beginning of the
view.
)",
};
}

HILTI_OPERATOR(hilti, stream::view::SubOffset);
};
HILTI_OPERATOR_IMPLEMENTATION(SubOffset);

} // namespace view

class Ctor : public Operator {
Expand Down
10 changes: 10 additions & 0 deletions hilti/toolchain/src/compiler/codegen/operators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ struct Visitor : hilti::visitor::PreOrder {
result = fmt("%s.sub(%s, %s)", self, args[0], args[1]);
}

void operator()(operator_::bytes::SubOffset* n) final {
auto [self, args] = methodArguments(n);
result = fmt("%s.sub(%s)", self, args[0]);
}

void operator()(operator_::bytes::Join* n) final {
auto [self, args] = methodArguments(n);
result = fmt("%s.join(%s)", self, args[0]);
Expand Down Expand Up @@ -765,6 +770,11 @@ struct Visitor : hilti::visitor::PreOrder {
result = fmt("%s.sub(%s, %s)", self, args[0], args[1]);
}

void operator()(operator_::stream::view::SubOffset* n) final {
auto [self, args] = methodArguments(n);
result = fmt("%s.sub(%s)", self, args[0]);
}

// Stream

void operator()(operator_::stream::Size* n) final { result = fmt("%s.size()", op0(n)); }
Expand Down
4 changes: 4 additions & 0 deletions tests/hilti/types/bytes/ops.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ assert y.find(b"890") == (False, y.at(8));

global auto y2 = b"abcdedede";
assert y2.sub(y2.find(b"def")[1], end(y2)) == b"de";
assert y.sub(3) == b"012";
assert y.sub(0) == b"";
assert y.sub(|y|) == y;
assert y.find(b"XYZ") == (False, end(y));
assert (b"456" in y);
assert ! (b"xyz" in y);
Expand Down Expand Up @@ -107,6 +110,7 @@ function void p(bytes x) {
assert x <= b"bbc";
assert (b"123" + x) == b"123abc";
assert begin(x) == x.at(0);
assert x.sub(2) == b"ab";
assert x.find(b"b") == (True, x.at(1));
assert (b"b" in x);
assert ! (b"X" in x);
Expand Down
5 changes: 5 additions & 0 deletions tests/hilti/types/stream/view.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ assert ! v.starts_with(b"XYZ");

assert |v| == 9;

assert v.sub(3) == b"123";
assert v.sub(0) == b"";
assert v.sub(|v|) == b"123456789";
assert all.sub(5) == b"01234";

global stream x2 = stream(b"AbCdEf");
v2 = cast<view<stream>>(x2).sub(x2.at(1), x2.at(5));

Expand Down
2 changes: 2 additions & 0 deletions tests/spicy/types/bytes/operators.spicy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ assert b"".split1() == (b"", b"");
assert b"12".split1() == (b"12", b"");
assert b"12 34 56 78".split1() == (b"12", b"34 56 78");
assert b"12XX34XX56XX78".split1(b"XX") == (b"12", b"34XX56XX78");
assert b"12345".sub(3) == b"123";
assert b"12345".sub(0) == b"";
assert b"xyz".starts_with(b"xy");
assert ! b"xyz".starts_with(b"XY");
assert b" xyz ".strip() == b"xyz";
Expand Down
Loading