canonical/mir

Replace `str(n)?cmp` with `std::string_view::operator==`

Open

#4567 aperta il 16 dic 2025

Vedi su GitHub
 (5 commenti) (0 reazioni) (1 assegnatario)C++ (137 fork)auto 404
Good first issue

Metriche repository

Star
 (775 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Lots of places in ourcodebase read strings from the environment or some external source and compare them to other strings. The use of str(n)?cmp can sometimes be confusing, and while std::string_view{string1} == std::string_view{string2} is a bit more verbose, it's a lot easier to reason about at a glance than strcmp(string1,string2) != 0.

Additionally, strncmp can be usually replaced with std::string_view::starts_with.

Examples:

https://github.com/canonical/mir/blob/7a9370befcace079fcc6d2103d5a33218462f86e/examples/miral-shell/shell_main.cpp#L59-L61

https://github.com/canonical/mir/blob/7a9370befcace079fcc6d2103d5a33218462f86e/tests/mir_test_framework/test_wlcs_display_server.cpp#L485

If std::string_view{...} == std::string_view{...} is too verbose, maybe wrapping that in a utility function would be a good middle ground?

Guida contributor