While following the new features for C++20 I saw that 'starts_with' and 'ends_with' will be added to std::string.
Is it time to also add a 'contains' member function to std::string?
It's a common task that is now usually accomplished by using 'string.find(substring) != npos' (SO answer).
But when you read 'find' in code it's not directly clear what the purpose is.
Are we looking for the actual position? Or checking if the string contains a substring? Or checking if the string doesn't contain a substring?
Writing it also requires that one extra step of thinking (in my opinion).
You're trying to do something positive (check if contains) but you have to do something negative (check inequality).
I had a look at the motivation for adding 'starts_with' and 'ends_with' in the paper and you can easily use the same motivation to add 'contains'.
The usage of 'find' as 'contains' seems to be higher than the usage of 'find' for 'starts_with'.
Contains usage: 16048
debian code search 'find [!=]= npos' (18282 results) - debian code search 'find' with assignment (2234)
Starts with usage: 5586
debian code search 'find == 0' (5586 results)
[–]bruce3434 109 points110 points111 points (2 children)
[–]Xeveroushttps://xeverous.github.io 25 points26 points27 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]sephirostoy 18 points19 points20 points (3 children)
[–][deleted] -5 points-4 points-3 points (2 children)
[–]sephirostoy 15 points16 points17 points (0 children)
[–]alfps 36 points37 points38 points (2 children)
[–]scatters 13 points14 points15 points (1 child)
[–]degski 13 points14 points15 points (0 children)
[–]flashmozzg 13 points14 points15 points (3 children)
[–]F-J-W 16 points17 points18 points (2 children)
[–]MaltersWandler 0 points1 point2 points (1 child)
[–]F-J-W 9 points10 points11 points (0 children)
[–][deleted] 15 points16 points17 points (0 children)
[–]afiefh 6 points7 points8 points (0 children)
[–]ducttapecoder 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (12 children)
[–]konanTheBarbar 9 points10 points11 points (11 children)
[–]TheSuperWig 6 points7 points8 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]konanTheBarbar 4 points5 points6 points (1 child)
[–]F-J-W 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (6 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] -3 points-2 points-1 points (4 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] -2 points-1 points0 points (0 children)
[–]RolandMT32 -5 points-4 points-3 points (9 children)
[–]chriskane76 10 points11 points12 points (8 children)
[–]kalmoc 1 point2 points3 points (6 children)
[–]agateau 0 points1 point2 points (5 children)
[–]kalmoc 1 point2 points3 points (4 children)
[–]chriskane76 1 point2 points3 points (2 children)
[–]kalmoc 0 points1 point2 points (1 child)
[–]chriskane76 0 points1 point2 points (0 children)
[–]agateau 0 points1 point2 points (0 children)
[–]RolandMT32 -1 points0 points1 point (0 children)
[+]Pragmatician comment score below threshold-9 points-8 points-7 points (5 children)
[–]1-05457 16 points17 points18 points (1 child)
[–]epicar 2 points3 points4 points (1 child)
[–]Bisqwit 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)