xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst (revision 508448280a4fc8d1e29c5ccf883836aac0f11ec9)
1.. title:: clang-tidy - modernize-use-starts-ends-with
2
3modernize-use-starts-ends-with
4==============================
5
6Checks for common roundabout ways to express ``starts_with`` and ``ends_with``
7and suggests replacing with the simpler method when it is available. Notably,
8this will work with ``std::string`` and ``std::string_view``.
9
10Covered scenarios:
11
12==================================================== =====================
13Expression                                           Replacement
14---------------------------------------------------- ---------------------
15``u.find(v) == 0``                                   ``u.starts_with(v)``
16``u.rfind(v, 0) != 0``                               ``!u.starts_with(v)``
17``u.compare(0, v.size(), v) == 0``                   ``u.starts_with(v)``
18``u.substr(0, v.size()) == v``                       ``u.starts_with(v)``
19``v != u.substr(0, v.size())``                       ``!u.starts_with(v)``
20``u.compare(u.size() - v.size(), v.size(), v) == 0`` ``u.ends_with(v)``
21``u.rfind(v) == u.size() - v.size()``                ``u.ends_with(v)``
22==================================================== =====================
23