Bug 158237 - Use C++20 contains() instead of find() and end()
Summary: Use C++20 contains() instead of find() and end()
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
24.2.0.0 alpha0+
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:24.8.0
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2023-11-16 10:31 UTC by Hossein
Modified: 2024-03-29 13:59 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2023-11-16 10:31:53 UTC
Description:
Now that C++20 is the baseline for LibreOffice code, it is possible to use C++ features that are only available since C++20.

One of the new features of C++20 is the availability of new functions for working with containers more conveniently. This EasyHack is about using contains().

See this commit: 06e3877dc2cf22e00d04b48453e9ab0b948c3ca2

Other than the first part, which is the introduction of contains(), you see changes like this for some variables of type map:

-    CPPUNIT_ASSERT(aMap.find("LocalURL") != aMap.end());
+    CPPUNIT_ASSERT(aMap.contains("LocalURL"));

Instead of searching for a specific key, and when reaching .end() concluding that the value is not there, the new code is simply using contains() function to check if the key is present in the map, or not.

The actual map data type is SequenceAsHashMapBase, and is based on std::unordered_map.

You can read more about contains() here:
https://en.cppreference.com/w/cpp/container/map/contains

Finding instances:
You an use grep to find some instances. For example:

$ git grep -n -i "map.find(" | grep "!=" | grep "end("

Working with IDEs:
If your IDE is a few years old, there is a chance that it can not recognize contains(). Also, if you have built the project file before, you may have to rebuild the project again. This is needed to make sure that the project files contain the C++20 compatibility option, and not for example C++17.
https://wiki.documentfoundation.org/Development/IDE
In any case, you need to make sure that LibreOffice can be built correctly.
Comment 1 Commit Notification 2024-01-01 11:06:24 UTC
Luv Sharma committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a058fa6eb87dcc4a01dac14674e15ff3ba611ced

tdf#158237 chart2: use c++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 2 Commit Notification 2024-02-13 13:05:00 UTC
Steven Casey (SMCode) committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a3df4852b8cc1a1445fdc1fe0057b94a5405376d

tdf#158237 comphelper: Use C++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 3 Commit Notification 2024-02-13 18:54:48 UTC
AkshayWarrier committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/488cd78f3b30f8b43007a3250d0c11fd0416dde9

tdf#158237 comphelper: Use C++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 4 Commit Notification 2024-03-09 11:28:31 UTC
Sujatro Bhadra committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/ddd5017410f4ec2f4860a27f4946e05458686afd

tdf#158237 Use C++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 5 Commit Notification 2024-03-23 19:16:43 UTC
Beeram_12 committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7174403b03a7effe8bedf78368c09840b4fdf7a9

tdf#158237 use c++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 6 Commit Notification 2024-03-26 07:20:16 UTC
RMZeroFour committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/74299624ec810f9683326608ad398211cf51787b

tdf#158237 Use contains instead of find-end

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Commit Notification 2024-03-29 13:59:14 UTC
Aaron Bourdeaux committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6c358c28c5cc2d1d8d8bb9d1238e3546a9bf3ce8

tdf#158237 Use C++20 contains() instead of find() and end()

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.