convert StringData to std::string_view (SERVER-32422)

[SERVER-82604] Add missing std::string_view members to StringData Created: 31/Oct/23  Updated: 04/Jan/24  Resolved: 15/Nov/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.3.0-rc0

Type: Sub-task Priority: Major - P3
Reporter: Billy Donahue Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
causes SERVER-84563 The pretty printer for StringData is ... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Comments   
Comment by Billy Donahue [ 15/Nov/23 ]

[Announcement to #server copied here for durability...]

Howdy Serverinos!

Quick announcement about the ubiquitous mongo::StringData class.
(https://github.com/mongodb/mongo/blob/master/src/mongo/base/string_data.h#L75)

SERVER-82604 makes some changes to the StringData API, mostly good, and a few that could be frustrating. The idea is that StringData is eventually going to become std::string_view, which has performance, correctness, and maintenance benefits for us.

We've taken the step of giving it a single std::string_view data member and forwarding all member functions to that data member. You might think of it as simulating private inheritance. Mostly everything string_view could do (at least the large subset supported by all platforms) can now be done with StringData. That includes:

  • Added .data() (to be preferred over .rawData()).
  • Conversion (explicit) to std::string (to be preferred over .toString()).
  • Conversion (explicit) to std::string_view (for APIs that need std::string_view).
  • Added npos.
  • begin/end are now extended with cbegin/cend, rbegin/rend, crbegin/crend .
  • back, front, at
  • Added ~20 new overloads of find/rfind/find_first_of/find_last_of.
  • Added remove_prefix, remove_suffix.
  • Added member swap.
  • All typedefs, including size_type.
  • Debug-mode instrumented iterators on Windows (already caught some bugs!).
  • Added constexpr and noexcept attributes.
  • StringData::StringData(std::nullptr_t) = delete; (per std::string_view).

Caveats:
Iterator is an unspecified type, and not necessarily const char*. Yes, this makes some existing usage more annoying, but has safety advantages. You can't directly use a StringData iterator in an expression that takes a const char* anymore. We also cannot "cheat" and peek beyond a StringData's range. Use .data() to work around this.
The const char*, const char* range constructor is removed[1]. Ranges must be specified as const char*, size_t constructor arguments.

All uses of StringData in the codebase have been adapted to deal with the API change. But hopefully this announcement will reduce surprise.


[1] This is unfortunate, but it's a C++20 feature and the string_view in XCode13.2.1 doesn't have it, and we don't want complete conversion to std::string_view to be blocked on an XCode upgrade. It wasn't hard to work around this in practice. This is also why it doesn't have a contains member.

Comment by Githook User [ 14/Nov/23 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-82604 implement StringData with a std::string_view member
Branch: master
https://github.com/mongodb/mongo/commit/5b9380b8be11fcb1cfb4eba3a13331312d30b4da

Generated at Thu Feb 08 06:49:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.