Skip to content

Commit 6c734f9

Browse files
committed
Refactor prefix_in_list to return its result using optionalt
This avoids using an output parameter, which makes it more straight forward to read the data flow.
1 parent e8829e9 commit 6c734f9

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/goto-cc/armcc_cmdline.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Author: Daniel Kroening
1414
#include <cstring>
1515
#include <iostream>
1616

17+
#include "optional.h"
18+
1719
/// parses the command line options into a cmdlinet
1820
/// \par parameters: argument count, argument strings
1921
/// \return none
@@ -265,19 +267,18 @@ static const char *options_with_arg[]=
265267
nullptr
266268
};
267269

268-
static bool
269-
prefix_in_list(const char *option, const char **list, std::string &prefix)
270+
static optionalt<std::string>
271+
prefix_in_list(const char *option, const char **list)
270272
{
271273
for(std::size_t i = 0; list[i] != nullptr; i++)
272274
{
273275
if(strncmp(option, list[i], strlen(list[i])) == 0)
274276
{
275-
prefix = std::string(list[i]);
276-
return true;
277+
return {list[i]};
277278
}
278279
}
279280

280-
return false;
281+
return {};
281282
}
282283

283284
bool armcc_cmdlinet::parse(int argc, const char **argv)
@@ -292,35 +293,34 @@ bool armcc_cmdlinet::parse(int argc, const char **argv)
292293
}
293294

294295
// it starts with - and it isn't "-"
295-
296-
std::string prefix;
296+
optionalt<std::string> prefix;
297297

298298
if(in_list(argv[i], options_no_arg))
299299
{
300300
// options that don't have any arguments
301301
set(argv[i]);
302302
}
303-
else if(prefix_in_list(argv[i], options_with_arg, prefix))
303+
else if((prefix = prefix_in_list(argv[i], options_with_arg)))
304304
{
305305
// options that have a separated _or_ concatenated argument
306-
if(strlen(argv[i])>prefix.size()) // concatenated?
307-
set(prefix, std::string(argv[i], prefix.size(), std::string::npos));
306+
if(strlen(argv[i]) > prefix->size()) // Concatenated.
307+
set(*prefix, std::string(argv[i], prefix->size(), std::string::npos));
308308
else
309309
{
310310
// Separated.
311311
if(i!=argc-1) // Guard against end of command line.
312312
{
313-
set(prefix, argv[i+1]);
313+
set(*prefix, argv[i + 1]);
314314
i++;
315315
}
316316
else
317-
set(prefix, "");
317+
set(*prefix, "");
318318
}
319319
}
320-
else if(prefix_in_list(argv[i], options_with_prefix, prefix))
320+
else if((prefix = prefix_in_list(argv[i], options_with_prefix)))
321321
{
322322
// options that have a concatenated argument
323-
set(prefix, std::string(argv[i], prefix.size(), std::string::npos));
323+
set(*prefix, std::string(argv[i], prefix->size(), std::string::npos));
324324
}
325325
else
326326
{ // unrecognized option

0 commit comments

Comments
 (0)