[go: nahoru, domu]

Skip to content

Commit

Permalink
Export of internal doc changes to Abseil OSS:
Browse files Browse the repository at this point in the history
rpc://team/absl-team/Abseil-Docs

Included changes:

529858419(jdennett):	Internal change.
527274040(Abseil Team):	Internal change.

PiperOrigin-RevId: 529858419
Change-Id: Id7e51a03cb144d560433e5aa660ff55cc94fed65
  • Loading branch information
Abseil Team authored and manshreck committed May 8, 2023
1 parent 3a75894 commit 310d595
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions docs/cpp/guides/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ A `string_view` is also suitable for local variables if you know that the
lifetime of the underlying object is longer than the lifetime of your
`string_view` variable. However, beware of binding it to a temporary value:

```cpp {.bad}
```cpp
// BAD use of string_view: lifetime problem
absl::string_view sv = obj.ReturnAString();
```

```cpp {.good}
```cpp
// GOOD use of string_view: str outlives sv
std::string str = obj.ReturnAString();
absl::string_view sv = str;
Expand Down Expand Up @@ -294,7 +294,7 @@ overhead. Always look for ways to reduce creation of such temporaries.
For example, the following code is inefficient:
```cpp {.bad}
```cpp
// Inefficient code
std::string s1 = "A string";
s1 = s1 + " another string";
Expand All @@ -304,7 +304,7 @@ The assignment operator above creates a temporary string, copies `s1` into that
temporary string, concatenates that temporary string, and then assigns it back
to `s1`. Instead use the optimized `+=` operator for such concatenation:

```cpp {.good}
```cpp
// Efficient code
s1 += " another string";
```
Expand All @@ -313,7 +313,7 @@ Good compilers may be able to optimize the preceding inefficient code. However,
operations that involve more than one concatenation cannot normally avoid
temporaries:

```cpp {.bad}
```cpp
// Inefficient code
std::string s1 = "A string";
std::string another = " and another string";
Expand All @@ -326,14 +326,14 @@ and `absl::StrAppend()` are often more efficient than operators such as `+=`,
since they don't require the creation of temporary `std::string` objects, and
their memory is preallocated during string construction.

```cpp {.bad}
```cpp
// Inefficient code
std::string s1 = "A string";
std::string another = " and another string";
s1 += " and some other string" + another;
```

```cpp {.good}
```cpp
// Efficient code
std::string s1 = "A string";
std::string another = " and another string";
Expand Down Expand Up @@ -391,7 +391,7 @@ For clarity and performance, don't use `absl::StrCat()` when appending to a
string. Use `absl::StrAppend()` instead. In particular, avoid using any of these
(anti-)patterns:

```cpp {.bad}
```cpp
str.append(absl::StrCat(...))
str += absl::StrCat(...)
str = absl::StrCat(str, ...)
Expand Down Expand Up @@ -506,7 +506,7 @@ Traditionally, most C++ code used built-in functions such as `sprintf()` and
`snprintf()`; these functions have some problems in that they don't support
`absl::string_view` and the memory of the formatted buffer must be managed.
```cpp {.bad}
```cpp
// Bad. Need to worry about buffer size and NUL-terminations.
std::string GetErrorMessage(char *op, char *user, int id) {
Expand All @@ -523,7 +523,7 @@ std::string GetErrorMessage(absl::string_view op, absl::string_view user, int id
}
```
```cpp {.good}
```cpp
// Best. Using absl::Substitute() is easier to read and to understand.
std::string GetErrorMessage(absl::string_view op, absl::string_view user, int id) {
return absl::Substitute("Error in $0 for user $1 ($2)", op, user, id);
Expand Down

0 comments on commit 310d595

Please sign in to comment.