| 1 |
.. title:: clang-tidy - modernize-use-starts-ends-with |
| 2 |
|
| 3 |
modernize-use-starts-ends-with |
| 4 |
============================== |
| 5 |
|
| 6 |
Checks for common roundabout ways to express ``starts_with`` and ``ends_with`` |
| 7 |
and suggests replacing with ``starts_with`` when the method is available. |
| 8 |
Notably, this will work with ``std::string`` and ``std::string_view``. |
| 9 |
|
| 10 |
.. code-block:: c++ |
| 11 |
|
| 12 |
std::string s = "..."; |
| 13 |
if (s.find("prefix") == 0) { /* do something */ } |
| 14 |
if (s.rfind("prefix", 0) == 0) { /* do something */ } |
| 15 |
if (s.compare(0, strlen("prefix"), "prefix") == 0) { /* do something */ } |
| 16 |
|
| 17 |
becomes |
| 18 |
|
| 19 |
.. code-block:: c++ |
| 20 |
|
| 21 |
std::string s = "..."; |
| 22 |
if (s.starts_with("prefix")) { /* do something */ } |
| 23 |
if (s.starts_with("prefix")) { /* do something */ } |
| 24 |
if (s.starts_with("prefix")) { /* do something */ } |