Bug 145538 - Use range based for loops
Summary: Use range based for loops
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:7.3.0 target:7.4.0 reviewed:20...
Keywords: difficultyBeginner, easyHack, skillCpp
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2021-11-04 08:54 UTC by Hossein
Modified: 2024-02-12 12:30 UTC (History)
3 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 2021-11-04 08:54:10 UTC
Since C++11, range based loops are available for iterating over a known
range of values. For example, when there is a need to process elements
of a container, range based loops are usually a good choice. They are
easy to write, read and understand.

This EasyHack is about using range based loops when it is more appropriate. This is an example where range based loop was better suited:

Use range based for loops
https://git.libreoffice.org/core/+/47728bb925b33a8fe536fed8c913a2669bf101b6

Make sure that you understand where to use reference or const reference if needed. A suggested reading:
https://blog.knatten.org/2013/10/13/prefer-using-references-with-range-based-for-loops/
Comment 1 Commit Notification 2021-11-23 12:57:39 UTC
Henrik Palomäki committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 Use range based for loops

It will be available in 7.3.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 2021-11-23 13:43:41 UTC
Harjot committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/34a0f7023aad59bc054385f0593606d5d602e052

tdf#145538 Use range based for loops in EnhancedShapeDumper.cxx

It will be available in 7.3.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 2021-12-30 13:51:15 UTC
Ramreiso Kashung committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7886b6a56a51cb699fb2a18d8a8dff0e224bdfb5

tdf#145538 : Using range-based for loops

It will be available in 7.4.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 2021-12-30 20:44:37 UTC
Ankur Khandelwal committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/96882cdec23c64974737c4833f157d54b85a129e

tdf#145538 Use range based loops

It will be available in 7.4.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 2022-01-12 19:14:34 UTC
dtm committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/582bf1a27c5adc6684e8d9af3805cfe40fa051d5

tdf#145538 - Use range based for loops

It will be available in 7.4.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 2022-01-17 19:50:41 UTC
VaibhavMalik4187 committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 - Use range based for loops

It will be available in 7.4.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 2022-03-01 12:03:38 UTC
Deep17 committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 - Use range based for loops

It will be available in 7.4.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 8 Hossein 2022-06-21 13:53:42 UTC
Re-evaluating the EasyHack in 2022

This issue is still relevant, as there are many places in the code that range based for loops can be used.
Comment 9 Commit Notification 2022-08-09 05:51:55 UTC
Stanislav Lopatin committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/815851d1cbc427bbd85da1638f3b121b253d0926

tdf#145538 Change common loops to range based in ScanlineToolsTest.cxx

It will be available in 7.5.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 10 Commit Notification 2022-08-26 20:38:10 UTC
PoonamShokeen committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/08a63a88543b0da68380090b655e777bd2b980be

tdf#145538 Use range based for loops

It will be available in 7.5.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 11 Commit Notification 2022-08-30 04:36:54 UTC
Stanislav Lopatin committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/91f91815124c00727eefdd92fbed2d927faaf60b

tdf#145538 Change common loops to range based.

It will be available in 7.5.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 12 Commit Notification 2022-10-07 12:42:34 UTC
Andrea Rosetti committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 use range based for loops

It will be available in 7.5.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 13 Commit Notification 2022-10-17 20:17:13 UTC
OmkarAcharekar committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7b63242ea72885d05faa5f0879395fa93a3446d1

tdf#145538 - Use range based for loops

It will be available in 7.5.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 14 Commit Notification 2022-11-06 00:26:35 UTC
jsala committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/64469bb1a7383da6e7ff1150d693b3e08f54bc91

tdf#145538 Use range based for loops

It will be available in 7.5.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 15 Commit Notification 2022-11-07 07:21:58 UTC
Hossein committed a patch related to this issue.
It has been pushed to "master":

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

Revert "tdf#145538 Use range based for loops"

It will be available in 7.5.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 16 Commit Notification 2022-12-19 16:40:15 UTC
Leonid Ryzhov committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 Use range based for loops

It will be available in 7.6.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 17 Commit Notification 2023-03-02 12:02:18 UTC
mostafa-elsharnoby committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/5d2be45a788c450e6f914eea8c3b74d214edaccd

tdf#145538 Use range based for loops

It will be available in 7.6.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 18 Commit Notification 2023-03-11 11:26:04 UTC
gokulakrishnan-shankar committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/20a5e38ee279022b7e6fbc32d2d259738244c0fd

tdf#145538 Refactor to use range-based for-loops

It will be available in 7.6.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 19 Commit Notification 2023-03-17 14:26:15 UTC
anfanite396 committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/34f7c8cce9c16861940f403f9a633fe8b26ba4ad

tdf#145538 Use range based for loops

It will be available in 7.6.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 20 Commit Notification 2023-04-06 10:10:07 UTC
ektagoel12 committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/87f52dbf4faf8dcd6fcc1b070e070b9ca8584ac4

tdf#145538 Use range based for loops

It will be available in 7.6.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 21 Commit Notification 2023-04-28 19:52:17 UTC
Radhey Parekh committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 Use range based for loops

It will be available in 7.6.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 22 Commit Notification 2023-05-12 22:54:12 UTC
Supriyo Paul committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538: traditional for loops to range-based for loops

It will be available in 7.6.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 23 Commit Notification 2023-05-30 13:38:32 UTC
Dr. David Alan Gilbert committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/4998370216bbea3bcaff7fac2d62cbb4ac978c5d

tdf#145538,tdf#147021: sw/source: range based loops

It will be available in 7.6.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 24 Commit Notification 2023-06-02 14:59:47 UTC
Dr. David Alan Gilbert committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7bec1689fcbe9102aa220739b6113118d9a8dd1f

tdf#145538,tdf#147021: qa: range based loops

It will be available in 7.6.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 25 Commit Notification 2023-06-12 08:30:42 UTC
buldi committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 Refactor to use range-based for-loop

It will be available in 24.2.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 26 Commit Notification 2023-06-13 07:24:24 UTC
buldi committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/65a20a85c20ddc87d66445657404a46041bcf6e1

tdf#145538 Refactor to use range-based for-loop

It will be available in 24.2.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 27 Commit Notification 2023-09-14 14:48:02 UTC
Adam Seskunas committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/046cee10e62993238534c68e33d97ed63067b290

tdf#145538 Change traditional for loop to range based for loop

It will be available in 24.2.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 28 Commit Notification 2023-10-03 20:09:02 UTC
Yli875 committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/67594344631c362d43b9d6a0cfadefbcfa5046d8

tdf#145538 Use range based for loops in idl:object

It will be available in 24.2.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 29 Buovjaga 2023-10-26 14:10:13 UTC
Note: don't apply this task to odk/examples. They conform to the SDK C++ baseline, which is still C++98. (Thanks, Stephan Bergmann)
Comment 30 Commit Notification 2024-01-02 21:35:45 UTC
Luv Sharma committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/54f2bd45346c0e4a6331ddce5c23aec0c37b0dba

tdf#145538 Use range based for loops

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 31 Commit Notification 2024-01-11 23:39:38 UTC
Kira Tubo committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 Use range based for loops

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 32 Commit Notification 2024-01-17 07:27:10 UTC
achintya committed a patch related to this issue.
It has been pushed to "master":

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

tdf#145538 use std::equal in place of for loop

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 33 Devansh Varshney 2024-02-04 16:13:07 UTC
should I do something this in this libreoffice/basic/source/classes/image.cxx file at line 429-

        for( size_t i = 0; i < mvStringOffsets.size(); i++ )
        {
            sal_uInt16 nOff = static_cast<sal_uInt16>(mvStringOffsets[ i ]);
            OString aStr(OUStringToOString(std::u16string_view(pStrings.get() + nOff), eCharSet));
            memcpy( pByteStrings.get() + nOff, aStr.getStr(), (aStr.getLength() + 1) * sizeof( char ) );
        }

to something like -

for (const sal_uInt32& offset : mvStringOffsets) {
    sal_uInt16 nOff = static_cast<sal_uInt16>(offset);
    OString aStr(OUStringToOString(std::u16string_view(pStrings.get() + nOff), eCharSet));
    memcpy(pByteStrings.get() + nOff, aStr.getStr(), (aStr.getLength() + 1) * sizeof(char));
}
Comment 34 Buovjaga 2024-02-04 17:40:58 UTC
(In reply to Devansh Varshney from comment #33)
> should I do something this in this
> libreoffice/basic/source/classes/image.cxx file at line 429-
> 
>         for( size_t i = 0; i < mvStringOffsets.size(); i++ )
>         {
>             sal_uInt16 nOff = static_cast<sal_uInt16>(mvStringOffsets[ i ]);
>             OString
> aStr(OUStringToOString(std::u16string_view(pStrings.get() + nOff),
> eCharSet));
>             memcpy( pByteStrings.get() + nOff, aStr.getStr(),
> (aStr.getLength() + 1) * sizeof( char ) );
>         }
> 
> to something like -
> 
> for (const sal_uInt32& offset : mvStringOffsets) {
>     sal_uInt16 nOff = static_cast<sal_uInt16>(offset);
>     OString aStr(OUStringToOString(std::u16string_view(pStrings.get() +
> nOff), eCharSet));
>     memcpy(pByteStrings.get() + nOff, aStr.getStr(), (aStr.getLength() + 1)
> * sizeof(char));
> }

Please discuss such in a Gerrit patch you send. It is usually the best place.
Comment 35 Commit Notification 2024-02-12 12:30:50 UTC
Keldin Maldonado (KNM) committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/5dadcd1ea9e63bcbc0a5d4c4cd95d7d8b37edef9

tdf#145538 use range based for loops

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.