Skip to content

Commit

Permalink
let Cursor::peek return span
Browse files Browse the repository at this point in the history
Summary:
Currently, `CursorBase::peek()` returns `pair<uint8_t const*, size_t>` and `CursorBase::peekView()` returns `basic_string_view<uint8_t>`. Consolidate them into `CursorBase::peek()` returning `span<uint8_t const>`.

As one motivation, libc++ v19 removes `basic_string_view<uint8_t>`.

Reviewed By: thevinster

Differential Revision: D62460851

fbshipit-source-id: 72d567817faf778287594c10dfd7f6f379bf2446
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Sep 13, 2024
1 parent c4e5844 commit 3f21ed6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
1 change: 1 addition & 0 deletions folly/io/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cpp_library(
"//folly:portability",
"//folly:range",
"//folly:scope_guard",
"//folly/container:span",
"//folly/detail:iterators",
"//folly/lang:bits",
"//folly/lang:checked_math",
Expand Down
19 changes: 3 additions & 16 deletions folly/io/Cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string_view>
#include <type_traits>

#include <folly/Likely.h>
#include <folly/Memory.h>
#include <folly/Portability.h>
#include <folly/Range.h>
#include <folly/container/span.h>
#include <folly/io/IOBuf.h>
#include <folly/io/IOBufQueue.h>
#include <folly/lang/Bits.h>
Expand Down Expand Up @@ -678,29 +678,16 @@ class CursorBase {
}

/**
* Alternate version of peekBytes() that returns a std::basic_string_view
* Alternate version of peekBytes() that returns a std::span
* instead of a ByteRage.
*
* @methodset Accessors
*/
std::basic_string_view<uint8_t> peekView() {
span<uint8_t const> peek() {
auto bytes = peekBytes();
return {bytes.data(), bytes.size()};
}

/**
* Alternate version of peekBytes() that returns a std::pair
* instead of a ByteRange.
*
* @methodset Accessors
*
* This function will eventually be deprecated.
*/
std::pair<const uint8_t*, size_t> peek() {
auto bytes = peekBytes();
return std::make_pair(bytes.data(), bytes.size());
}

/**
* Clone len bytes from this cursor into an IOBuf.
*
Expand Down
28 changes: 14 additions & 14 deletions folly/io/test/IOBufCursorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,18 @@ TEST(IOBuf, PullAndPeek) {
{
RWPrivateCursor cursor(iobuf1.get());
auto b = cursor.peekBytes();
EXPECT_EQ(b.data(), cursor.peekView().data());
EXPECT_EQ(b.size(), cursor.peekView().size());
EXPECT_EQ(b.data(), cursor.peek().data());
EXPECT_EQ(b.size(), cursor.peek().size());
EXPECT_EQ("he", StringPiece(b));
cursor.skip(b.size());
b = cursor.peekBytes();
EXPECT_EQ(b.data(), cursor.peekView().data());
EXPECT_EQ(b.size(), cursor.peekView().size());
EXPECT_EQ(b.data(), cursor.peek().data());
EXPECT_EQ(b.size(), cursor.peek().size());
EXPECT_EQ("llo ", StringPiece(b));
cursor.skip(b.size());
b = cursor.peekBytes();
EXPECT_EQ(b.data(), cursor.peekView().data());
EXPECT_EQ(b.size(), cursor.peekView().size());
EXPECT_EQ(b.data(), cursor.peek().data());
EXPECT_EQ(b.size(), cursor.peek().size());
EXPECT_EQ("world", StringPiece(b));
cursor.skip(b.size());
EXPECT_EQ(3, iobuf1->countChainElements());
Expand All @@ -240,8 +240,8 @@ TEST(IOBuf, PullAndPeek) {
RWPrivateCursor cursor(iobuf1.get());
cursor.gather(11);
auto b = cursor.peekBytes();
EXPECT_EQ(b.data(), cursor.peekView().data());
EXPECT_EQ(b.size(), cursor.peekView().size());
EXPECT_EQ(b.data(), cursor.peek().data());
EXPECT_EQ(b.size(), cursor.peek().size());
EXPECT_EQ("hello world", StringPiece(b));
EXPECT_EQ(1, iobuf1->countChainElements());
EXPECT_EQ(11, iobuf1->computeChainDataLength());
Expand Down Expand Up @@ -1568,18 +1568,18 @@ TEST(IOBuf, BoundedCursorPullAndPeek) {
{
Cursor subCursor(iobuf1.get(), 13);
auto b = subCursor.peekBytes();
EXPECT_EQ(b.data(), subCursor.peekView().data());
EXPECT_EQ(b.size(), subCursor.peekView().size());
EXPECT_EQ(b.data(), subCursor.peek().data());
EXPECT_EQ(b.size(), subCursor.peek().size());
EXPECT_EQ("he", StringPiece(b));
subCursor.skip(b.size());
b = subCursor.peekBytes();
EXPECT_EQ(b.data(), subCursor.peekView().data());
EXPECT_EQ(b.size(), subCursor.peekView().size());
EXPECT_EQ(b.data(), subCursor.peek().data());
EXPECT_EQ(b.size(), subCursor.peek().size());
EXPECT_EQ("llo ", StringPiece(b));
subCursor.skip(b.size());
b = subCursor.peekBytes();
EXPECT_EQ(b.data(), subCursor.peekView().data());
EXPECT_EQ(b.size(), subCursor.peekView().size());
EXPECT_EQ(b.data(), subCursor.peek().data());
EXPECT_EQ(b.size(), subCursor.peek().size());
EXPECT_EQ("world a", StringPiece(b));
subCursor.skip(b.size());
EXPECT_EQ(3, iobuf1->countChainElements());
Expand Down
3 changes: 2 additions & 1 deletion folly/somerge_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Generated by xplat/cross_plat_devx/somerge_maps/compute_merge_maps.py
@generated SignedSource<<4d6c648a98484ebd6d13a39719f413d2>>
@generated SignedSource<<be29d3bd36c7d732d1042df61b88bf15>>
"""

# Entry Points:
Expand Down Expand Up @@ -132,6 +132,7 @@ FOLLY_BASE_NATIVE_LIBRARY_MERGE_MAP = [
"fbsource//xplat/folly/container:reserveAndroid",
"fbsource//xplat/folly/container:small_vectorAndroid",
"fbsource//xplat/folly/container:sorted_vector_typesAndroid",
"fbsource//xplat/folly/container:spanAndroid",
"fbsource//xplat/folly/container:viewAndroid",
"fbsource//xplat/folly/coro:traitsAndroid",
"fbsource//xplat/folly/detail:async_traceAndroid",
Expand Down

0 comments on commit 3f21ed6

Please sign in to comment.