Skip to content

Commit 7855bad

Browse files
committed
Refactor prefix_in_list to take vector of strings
Because this gives us compatability with modern C++ features such as ranged for loops and the algorithms in the standard template library. Because using a standard container rather than a null terminated array improves memory safety.
1 parent cc12dff commit 7855bad

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/goto-cc/armcc_cmdline.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ Author: Daniel Kroening
1212
#include "armcc_cmdline.h"
1313

1414
#include <util/optional.h>
15+
#include <util/prefix.h>
1516

17+
#include <algorithm>
1618
#include <cstring>
1719
#include <iostream>
20+
#include <string>
21+
#include <vector>
1822

1923
/// parses the command line options into a cmdlinet
2024
/// \par parameters: argument count, argument strings
@@ -197,7 +201,7 @@ static const char *options_no_arg[]=
197201
nullptr
198202
};
199203

200-
static const char *options_with_prefix[]=
204+
static const std::vector<std::string> options_with_prefix
201205
{
202206
"--project=",
203207
"--workdir=",
@@ -243,11 +247,10 @@ static const char *options_with_prefix[]=
243247
"--configure_sysroot=",
244248
"--configure_cpp_headers=",
245249
"--configure_extra_includes=",
246-
"--configure_extra_libraries=",
247-
nullptr
250+
"--configure_extra_libraries="
248251
};
249252

250-
static const char *options_with_arg[]=
253+
static const std::vector<std::string> options_with_arg
251254
{
252255
// goto-cc specific
253256
"--verbosity",
@@ -263,21 +266,19 @@ static const char *options_with_arg[]=
263266
"-Warmcc,",
264267
"-o",
265268
"--cpu",
266-
"--apcs",
267-
nullptr
269+
"--apcs"
268270
};
269271

270-
optionalt<std::string> prefix_in_list(const char *option, const char **list)
272+
optionalt<std::string>
273+
prefix_in_list(const std::string &option, const std::vector<std::string> &list)
271274
{
272-
for(std::size_t i = 0; list[i] != nullptr; i++)
273-
{
274-
if(strncmp(option, list[i], strlen(list[i])) == 0)
275-
{
276-
return {list[i]};
277-
}
278-
}
279-
280-
return {};
275+
const auto found =
276+
std::find_if(list.cbegin(), list.cend(), [&](const std::string &argument) {
277+
return has_prefix(argument, option);
278+
});
279+
if(found == list.cend())
280+
return {};
281+
return {*found};
281282
}
282283

283284
bool armcc_cmdlinet::parse(int argc, const char **argv)

0 commit comments

Comments
 (0)