@@ -98,25 +98,26 @@ ByteString::ByteString(Allocator<> allocator, const absl::Cord& cord) {
9898 }
9999}
100100
101- ByteString ByteString::Borrowed (Owner owner , absl::string_view string) {
102- ABSL_DCHECK (owner != Owner ::None ()) << " Borrowing from Owner::None()" ;
103- auto * arena = owner .arena ();
101+ ByteString ByteString::Borrowed (Borrower borrower , absl::string_view string) {
102+ ABSL_DCHECK (borrower != Borrower ::None ()) << " Borrowing from Owner::None()" ;
103+ auto * arena = borrower .arena ();
104104 if (string.size () <= kSmallByteStringCapacity || arena != nullptr ) {
105105 return ByteString (arena, string);
106106 }
107- const auto * refcount = OwnerRelease ( std::move (owner) );
107+ const auto * refcount = BorrowerRelease (borrower );
108108 // A nullptr refcount indicates somebody called us to borrow something that
109109 // has no owner. If this is the case, we fallback to assuming operator
110110 // new/delete and convert it to a reference count.
111111 if (refcount == nullptr ) {
112112 std::tie (refcount, string) = MakeReferenceCountedString (string);
113113 }
114+ StrongRef (*refcount);
114115 return ByteString (refcount, string);
115116}
116117
117- ByteString ByteString::Borrowed (const Owner& owner , const absl::Cord& cord) {
118- ABSL_DCHECK (owner != Owner ::None ()) << " Borrowing from Owner::None()" ;
119- return ByteString (owner .arena (), cord);
118+ ByteString ByteString::Borrowed (Borrower borrower , const absl::Cord& cord) {
119+ ABSL_DCHECK (borrower != Borrower ::None ()) << " Borrowing from Owner::None()" ;
120+ return ByteString (borrower .arena (), cord);
120121}
121122
122123ByteString::ByteString (absl::Nonnull<const ReferenceCount*> refcount,
@@ -126,7 +127,7 @@ ByteString::ByteString(absl::Nonnull<const ReferenceCount*> refcount,
126127 kMetadataOwnerReferenceCountBit );
127128}
128129
129- absl::Nullable<google::protobuf::Arena*> ByteString::GetArena () const noexcept {
130+ absl::Nullable<google::protobuf::Arena*> ByteString::GetArena () const {
130131 switch (GetKind ()) {
131132 case ByteStringKind::kSmall :
132133 return GetSmallArena ();
@@ -137,7 +138,7 @@ absl::Nullable<google::protobuf::Arena*> ByteString::GetArena() const noexcept {
137138 }
138139}
139140
140- bool ByteString::empty () const noexcept {
141+ bool ByteString::empty () const {
141142 switch (GetKind ()) {
142143 case ByteStringKind::kSmall :
143144 return rep_.small .size == 0 ;
@@ -148,7 +149,7 @@ bool ByteString::empty() const noexcept {
148149 }
149150}
150151
151- size_t ByteString::size () const noexcept {
152+ size_t ByteString::size () const {
152153 switch (GetKind ()) {
153154 case ByteStringKind::kSmall :
154155 return rep_.small .size ;
@@ -170,7 +171,7 @@ absl::string_view ByteString::Flatten() {
170171 }
171172}
172173
173- absl::optional<absl::string_view> ByteString::TryFlat () const noexcept {
174+ absl::optional<absl::string_view> ByteString::TryFlat () const {
174175 switch (GetKind ()) {
175176 case ByteStringKind::kSmall :
176177 return GetSmall ();
@@ -181,23 +182,6 @@ absl::optional<absl::string_view> ByteString::TryFlat() const noexcept {
181182 }
182183}
183184
184- absl::string_view ByteString::GetFlat (std::string& scratch) const {
185- switch (GetKind ()) {
186- case ByteStringKind::kSmall :
187- return GetSmall ();
188- case ByteStringKind::kMedium :
189- return GetMedium ();
190- case ByteStringKind::kLarge : {
191- const auto & large = GetLarge ();
192- if (auto flat = large.TryFlat (); flat) {
193- return *flat;
194- }
195- scratch = static_cast <std::string>(large);
196- return scratch;
197- }
198- }
199- }
200-
201185void ByteString::RemovePrefix (size_t n) {
202186 ABSL_DCHECK_LE (n, size ());
203187 if (n == 0 ) {
@@ -275,12 +259,44 @@ std::string ByteString::ToString() const {
275259 }
276260}
277261
262+ void ByteString::CopyToString (absl::Nonnull<std::string*> out) const {
263+ ABSL_DCHECK (out != nullptr );
264+
265+ switch (GetKind ()) {
266+ case ByteStringKind::kSmall :
267+ out->assign (GetSmall ());
268+ break ;
269+ case ByteStringKind::kMedium :
270+ out->assign (GetMedium ());
271+ break ;
272+ case ByteStringKind::kLarge :
273+ absl::CopyCordToString (GetLarge (), out);
274+ break ;
275+ }
276+ }
277+
278+ void ByteString::AppendToString (absl::Nonnull<std::string*> out) const {
279+ ABSL_DCHECK (out != nullptr );
280+
281+ switch (GetKind ()) {
282+ case ByteStringKind::kSmall :
283+ out->append (GetSmall ());
284+ break ;
285+ case ByteStringKind::kMedium :
286+ out->append (GetMedium ());
287+ break ;
288+ case ByteStringKind::kLarge :
289+ absl::AppendCordToString (GetLarge (), out);
290+ break ;
291+ }
292+ }
293+
278294namespace {
279295
280296struct ReferenceCountReleaser {
281297 absl::Nonnull<const ReferenceCount*> refcount;
282298
283- void operator ()() const noexcept { StrongUnref (*refcount); }
299+ void operator ()() const { StrongUnref (*refcount); }
284300};
285301
286302} // namespace
@@ -322,8 +338,54 @@ absl::Cord ByteString::ToCord() && {
322338 }
323339}
324340
341+ void ByteString::CopyToCord (absl::Nonnull<absl::Cord*> out) const {
342+ ABSL_DCHECK (out != nullptr );
343+
344+ switch (GetKind ()) {
345+ case ByteStringKind::kSmall :
346+ *out = absl::Cord (GetSmall ());
347+ break ;
348+ case ByteStringKind::kMedium : {
349+ const auto * refcount = GetMediumReferenceCount ();
350+ if (refcount != nullptr ) {
351+ StrongRef (*refcount);
352+ *out = absl::MakeCordFromExternal (GetMedium (),
353+ ReferenceCountReleaser{refcount});
354+ } else {
355+ *out = absl::Cord (GetMedium ());
356+ }
357+ } break ;
358+ case ByteStringKind::kLarge :
359+ *out = GetLarge ();
360+ break ;
361+ }
362+ }
363+
364+ void ByteString::AppendToCord (absl::Nonnull<absl::Cord*> out) const {
365+ ABSL_DCHECK (out != nullptr );
366+
367+ switch (GetKind ()) {
368+ case ByteStringKind::kSmall :
369+ out->Append (GetSmall ());
370+ break ;
371+ case ByteStringKind::kMedium : {
372+ const auto * refcount = GetMediumReferenceCount ();
373+ if (refcount != nullptr ) {
374+ StrongRef (*refcount);
375+ out->Append (absl::MakeCordFromExternal (
376+ GetMedium (), ReferenceCountReleaser{refcount}));
377+ } else {
378+ out->Append (GetMedium ());
379+ }
380+ } break ;
381+ case ByteStringKind::kLarge :
382+ out->Append (GetLarge ());
383+ break ;
384+ }
385+ }
386+
325387absl::Nullable<google::protobuf::Arena*> ByteString::GetMediumArena (
326- const MediumByteStringRep& rep) noexcept {
388+ const MediumByteStringRep& rep) {
327389 if ((rep.owner & kMetadataOwnerBits ) == kMetadataOwnerArenaBit ) {
328390 return reinterpret_cast <google::protobuf::Arena*>(rep.owner &
329391 kMetadataOwnerPointerMask );
@@ -332,7 +394,7 @@ absl::Nullable<google::protobuf::Arena*> ByteString::GetMediumArena(
332394}
333395
334396absl::Nullable<const ReferenceCount*> ByteString::GetMediumReferenceCount (
335- const MediumByteStringRep& rep) noexcept {
397+ const MediumByteStringRep& rep) {
336398 if ((rep.owner & kMetadataOwnerBits ) == kMetadataOwnerReferenceCountBit ) {
337399 return reinterpret_cast <const ReferenceCount*>(rep.owner &
338400 kMetadataOwnerPointerMask );
@@ -814,7 +876,7 @@ void ByteString::Swap(ByteString& other) {
814876 }
815877}
816878
817- void ByteString::Destroy () noexcept {
879+ void ByteString::Destroy () {
818880 switch (GetKind ()) {
819881 case ByteStringKind::kSmall :
820882 break ;
@@ -1022,7 +1084,7 @@ void ByteString::SwapLargeLarge(ByteString& lhs, ByteString& rhs) {
10221084 swap (lhs.GetLarge (), rhs.GetLarge ());
10231085}
10241086
1025- ByteStringView::ByteStringView (const ByteString& other) noexcept {
1087+ ByteStringView::ByteStringView (const ByteString& other) {
10261088 switch (other.GetKind ()) {
10271089 case ByteStringKind::kSmall : {
10281090 auto * other_arena = other.GetSmallArena ();
@@ -1054,7 +1116,7 @@ ByteStringView::ByteStringView(const ByteString& other) noexcept {
10541116 }
10551117}
10561118
1057- bool ByteStringView::empty () const noexcept {
1119+ bool ByteStringView::empty () const {
10581120 switch (GetKind ()) {
10591121 case ByteStringViewKind::kString :
10601122 return rep_.string .size == 0 ;
@@ -1063,7 +1125,7 @@ bool ByteStringView::empty() const noexcept {
10631125 }
10641126}
10651127
1066- size_t ByteStringView::size () const noexcept {
1128+ size_t ByteStringView::size () const {
10671129 switch (GetKind ()) {
10681130 case ByteStringViewKind::kString :
10691131 return rep_.string .size ;
@@ -1072,7 +1134,7 @@ size_t ByteStringView::size() const noexcept {
10721134 }
10731135}
10741136
1075- absl::optional<absl::string_view> ByteStringView::TryFlat () const noexcept {
1137+ absl::optional<absl::string_view> ByteStringView::TryFlat () const {
10761138 switch (GetKind ()) {
10771139 case ByteStringViewKind::kString :
10781140 return GetString ();
@@ -1084,21 +1146,7 @@ absl::optional<absl::string_view> ByteStringView::TryFlat() const noexcept {
10841146 }
10851147}
10861148
1087- absl::string_view ByteStringView::GetFlat (std::string& scratch) const {
1088- switch (GetKind ()) {
1089- case ByteStringViewKind::kString :
1090- return GetString ();
1091- case ByteStringViewKind::kCord : {
1092- if (auto flat = GetCord ().TryFlat (); flat) {
1093- return flat->substr (rep_.cord .pos , rep_.cord .size );
1094- }
1095- scratch = static_cast <std::string>(GetSubcord ());
1096- return scratch;
1097- }
1098- }
1099- }
1100-
1101- bool ByteStringView::Equals (ByteStringView rhs) const noexcept {
1149+ bool ByteStringView::Equals (ByteStringView rhs) const {
11021150 switch (GetKind ()) {
11031151 case ByteStringViewKind::kString :
11041152 switch (rhs.GetKind ()) {
@@ -1117,7 +1165,7 @@ bool ByteStringView::Equals(ByteStringView rhs) const noexcept {
11171165 }
11181166}
11191167
1120- int ByteStringView::Compare (ByteStringView rhs) const noexcept {
1168+ int ByteStringView::Compare (ByteStringView rhs) const {
11211169 switch (GetKind ()) {
11221170 case ByteStringViewKind::kString :
11231171 switch (rhs.GetKind ()) {
@@ -1136,7 +1184,7 @@ int ByteStringView::Compare(ByteStringView rhs) const noexcept {
11361184 }
11371185}
11381186
1139- bool ByteStringView::StartsWith (ByteStringView rhs) const noexcept {
1187+ bool ByteStringView::StartsWith (ByteStringView rhs) const {
11401188 switch (GetKind ()) {
11411189 case ByteStringViewKind::kString :
11421190 switch (rhs.GetKind ()) {
@@ -1160,7 +1208,7 @@ bool ByteStringView::StartsWith(ByteStringView rhs) const noexcept {
11601208 }
11611209}
11621210
1163- bool ByteStringView::EndsWith (ByteStringView rhs) const noexcept {
1211+ bool ByteStringView::EndsWith (ByteStringView rhs) const {
11641212 switch (GetKind ()) {
11651213 case ByteStringViewKind::kString :
11661214 switch (rhs.GetKind ()) {
@@ -1212,6 +1260,28 @@ std::string ByteStringView::ToString() const {
12121260 }
12131261}
12141262
1263+ void ByteStringView::CopyToString (absl::Nonnull<std::string*> out) const {
1264+ switch (GetKind ()) {
1265+ case ByteStringViewKind::kString :
1266+ out->assign (GetString ());
1267+ break ;
1268+ case ByteStringViewKind::kCord :
1269+ absl::CopyCordToString (GetSubcord (), out);
1270+ break ;
1271+ }
1272+ }
1273+
1274+ void ByteStringView::AppendToString (absl::Nonnull<std::string*> out) const {
1275+ switch (GetKind ()) {
1276+ case ByteStringViewKind::kString :
1277+ out->append (GetString ());
1278+ break ;
1279+ case ByteStringViewKind::kCord :
1280+ absl::AppendCordToString (GetSubcord (), out);
1281+ break ;
1282+ }
1283+ }
1284+
12151285absl::Cord ByteStringView::ToCord () const {
12161286 switch (GetKind ()) {
12171287 case ByteStringViewKind::kString : {
@@ -1228,7 +1298,43 @@ absl::Cord ByteStringView::ToCord() const {
12281298 }
12291299}
12301300
1231- absl::Nullable<google::protobuf::Arena*> ByteStringView::GetArena () const noexcept {
1301+ void ByteStringView::CopyToCord (absl::Nonnull<absl::Cord*> out) const {
1302+ switch (GetKind ()) {
1303+ case ByteStringViewKind::kString : {
1304+ const auto * refcount = GetStringReferenceCount ();
1305+ if (refcount != nullptr ) {
1306+ StrongRef (*refcount);
1307+ *out = absl::MakeCordFromExternal (GetString (),
1308+ ReferenceCountReleaser{refcount});
1309+ } else {
1310+ *out = absl::Cord (GetString ());
1311+ }
1312+ } break ;
1313+ case ByteStringViewKind::kCord :
1314+ *out = GetSubcord ();
1315+ break ;
1316+ }
1317+ }
1318+
1319+ void ByteStringView::AppendToCord (absl::Nonnull<absl::Cord*> out) const {
1320+ switch (GetKind ()) {
1321+ case ByteStringViewKind::kString : {
1322+ const auto * refcount = GetStringReferenceCount ();
1323+ if (refcount != nullptr ) {
1324+ StrongRef (*refcount);
1325+ out->Append (absl::MakeCordFromExternal (
1326+ GetString (), ReferenceCountReleaser{refcount}));
1327+ } else {
1328+ out->Append (GetString ());
1329+ }
1330+ } break ;
1331+ case ByteStringViewKind::kCord :
1332+ out->Append (GetSubcord ());
1333+ break ;
1334+ }
1335+ }
1336+
1337+ absl::Nullable<google::protobuf::Arena*> ByteStringView::GetArena () const {
12321338 switch (GetKind ()) {
12331339 case ByteStringViewKind::kString :
12341340 return GetStringArena ();
0 commit comments