Skip to content

Commit 56479b9

Browse files
committed
Store wire cursor position as pointers
1 parent b0a506f commit 56479b9

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

include/jsoncons_ext/cbor/cbor_view.hpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ namespace view {
720720
public:
721721

722722
explicit wire_cursor(span<const uint8_t> input) noexcept
723-
: input_(input), offset_(0)
723+
: begin_(input.data()), current_(input.data()), end_(input.data() + input.size())
724724
{
725725
}
726726

@@ -729,35 +729,31 @@ namespace view {
729729

730730
std::size_t position() const noexcept
731731
{
732-
return offset_;
732+
return static_cast<std::size_t>(current_ - begin_);
733733
}
734734

735735
span<const uint8_t> remaining() const noexcept
736736
{
737-
const uint8_t* data = offset_ == 0 ? input_.data() : input_.data() + offset_;
738-
return span<const uint8_t>(data, input_.size() - offset_);
737+
return span<const uint8_t>(current_, static_cast<std::size_t>(end_ - current_));
739738
}
740739

741740
// Reads one head and advances past that head only. Tags are returned
742741
// as their own heads. On failure, position() is the reported offset.
743742
expected<item_head, scan_error> read_head() noexcept
744743
{
745-
if (offset_ >= input_.size())
744+
if (current_ >= end_)
746745
{
747746
return expected<item_head, scan_error>(unexpect,
748-
scan_error{cbor_errc::unexpected_eof, offset_});
747+
scan_error{cbor_errc::unexpected_eof, position()});
749748
}
750749

751-
const uint8_t* p = input_.data() + offset_;
752-
const uint8_t* end = input_.data() + input_.size();
753750
detail_view::item_head h;
754751
std::error_code ec;
755-
const bool ok = detail_view::read_head(p, end, h, ec);
756-
offset_ = static_cast<std::size_t>(p - input_.data());
752+
const bool ok = detail_view::read_head(current_, end_, h, ec);
757753
if (!ok)
758754
{
759755
return expected<item_head, scan_error>(unexpect,
760-
scan_error{static_cast<cbor_errc>(ec.value()), offset_});
756+
scan_error{static_cast<cbor_errc>(ec.value()), position()});
761757
}
762758

763759
item_head head;
@@ -780,17 +776,18 @@ namespace view {
780776
// unchanged, when fewer bytes remain.
781777
bool skip(std::size_t count) noexcept
782778
{
783-
if (input_.size() - offset_ < count)
779+
if (static_cast<std::size_t>(end_ - current_) < count)
784780
{
785781
return false;
786782
}
787-
offset_ += count;
783+
current_ += count;
788784
return true;
789785
}
790786

791787
private:
792-
span<const uint8_t> input_;
793-
std::size_t offset_;
788+
const uint8_t* begin_;
789+
const uint8_t* current_;
790+
const uint8_t* end_;
794791
};
795792

796793
// Iterates the values of an item's leading semantic tags.
@@ -1387,38 +1384,33 @@ namespace view {
13871384

13881385
inline expected<item, scan_error> wire_cursor::read_item(scan_context& context)
13891386
{
1390-
const std::size_t start = offset_;
1387+
const std::size_t start = position();
13911388
auto scanned = scan_prefix(remaining(), context);
13921389
if (!scanned)
13931390
{
13941391
scan_error error = scanned.error();
1395-
const std::size_t available = input_.size() - offset_;
1396-
const std::size_t consumed = (std::min)(error.offset, available);
1397-
offset_ += consumed;
1392+
const std::size_t available = static_cast<std::size_t>(end_ - current_);
1393+
current_ += (std::min)(error.offset, available);
13981394
error.offset += start;
13991395
return expected<item, scan_error>(unexpect, error);
14001396
}
14011397

1402-
offset_ += scanned.value().first.encoded_bytes().size();
1398+
current_ += scanned.value().first.encoded_bytes().size();
14031399
return scanned.value().first;
14041400
}
14051401

14061402
inline expected<span<const uint8_t>, scan_error> wire_cursor::skip_item(scan_context& context)
14071403
{
1408-
const uint8_t* const base = input_.data();
1409-
const uint8_t* p = base + offset_;
1410-
const uint8_t* const end = base + input_.size();
1411-
const std::size_t start = offset_;
1404+
const uint8_t* const start = current_;
14121405
std::error_code ec;
1413-
const bool ok = detail_view::skip_item(p, end, context.max_nesting_depth(),
1406+
const bool ok = detail_view::skip_item(current_, end_, context.max_nesting_depth(),
14141407
detail_view::scan_access::workspace(context), ec);
1415-
offset_ = static_cast<std::size_t>(p - base);
14161408
if (!ok)
14171409
{
14181410
return expected<span<const uint8_t>, scan_error>(unexpect,
1419-
scan_error{detail_view::to_cbor_errc(ec), offset_});
1411+
scan_error{detail_view::to_cbor_errc(ec), position()});
14201412
}
1421-
return span<const uint8_t>(base + start, offset_ - start);
1413+
return span<const uint8_t>(start, static_cast<std::size_t>(current_ - start));
14221414
}
14231415

14241416

0 commit comments

Comments
 (0)