Commit e1ab01a5 authored by Joe Loser's avatar Joe Loser Committed by Facebook Github Bot

Add non-const data() method to FBString (#1116)

Summary:
- As of C++17, `std::string` contains a non-const `data()` member function.
  This is useful when working with C APIs that have `char*` output parameters.
- Implements P0272 for `FBString`:
  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0272r1.html

Example of workaround without this:

```
  fbstring s{};
  some_api(&s.front());
```

- The expression `&s.front()` above causes undefined behavior since the string
  is empty.

- Add non-const public member function `data()` to `fbstring` which
  delegates to `fbstring_core`.
Pull Request resolved: https://github.com/facebook/folly/pull/1116

Reviewed By: Orvid

Differential Revision: D15213884

Pulled By: yfeldblum

fbshipit-source-id: f533542db0a7e34c09a622e5257d7c1e66ab75e2
parent b6b647e6
......@@ -383,6 +383,10 @@ class fbstring_core {
return c_str();
}
Char* data() {
return c_str();
}
Char* mutableData() {
switch (category()) {
case Category::isSmall:
......@@ -482,6 +486,13 @@ class fbstring_core {
// Disabled
fbstring_core& operator=(const fbstring_core& rhs);
Char* c_str() {
Char* ptr = ml_.data_;
// With this syntax, GCC and Clang generate a CMOV instead of a branch.
ptr = (category() == Category::isSmall) ? small_ : ptr;
return ptr;
}
void reset() {
setSmallSize(0);
}
......@@ -1669,6 +1680,10 @@ class basic_fbstring {
return c_str();
}
value_type* data() {
return store_.data();
}
allocator_type get_allocator() const {
return allocator_type();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment