| 1 |
.. title:: clang-tidy - abseil-string-find-startswith |
| 2 |
|
| 3 |
abseil-string-find-startswith |
| 4 |
============================= |
| 5 |
|
| 6 |
Checks whether a ``std::string::find()`` or ``std::string::rfind()`` (and |
| 7 |
corresponding ``std::string_view`` methods) result is compared with 0, and |
| 8 |
suggests replacing with ``absl::StartsWith()``. This is both a readability and |
| 9 |
performance issue. |
| 10 |
|
| 11 |
``starts_with`` was added as a built-in function on those types in C++20. If |
| 12 |
available, prefer enabling :doc:`modernize-use-starts-ends-with |
| 13 |
<../modernize/use-starts-ends-with>` instead of this check. |
| 14 |
|
| 15 |
.. code-block:: c++ |
| 16 |
|
| 17 |
string s = "..."; |
| 18 |
if (s.find("Hello World") == 0) { /* do something */ } |
| 19 |
if (s.rfind("Hello World", 0) == 0) { /* do something */ } |
| 20 |
|
| 21 |
becomes |
| 22 |
|
| 23 |
|
| 24 |
.. code-block:: c++ |
| 25 |
|
| 26 |
string s = "..."; |
| 27 |
if (absl::StartsWith(s, "Hello World")) { /* do something */ } |
| 28 |
if (absl::StartsWith(s, "Hello World")) { /* do something */ } |
| 29 |
|
| 30 |
|
| 31 |
Options |
| 32 |
------- |
| 33 |
|
| 34 |
.. option:: StringLikeClasses |
| 35 |
|
| 36 |
Semicolon-separated list of names of string-like classes. By default both |
| 37 |
``std::basic_string`` and ``std::basic_string_view`` are considered. The list |
| 38 |
of methods to be considered is fixed. |
| 39 |
|
| 40 |
.. option:: IncludeStyle |
| 41 |
|
| 42 |
A string specifying which include-style is used, `llvm` or `google`. Default |
| 43 |
is `llvm`. |
| 44 |
|
| 45 |
.. option:: AbseilStringsMatchHeader |
| 46 |
|
| 47 |
The location of Abseil's ``strings/match.h``. Defaults to |
| 48 |
``absl/strings/match.h``. |