Skip to content

Commit cc12dff

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 0313558 commit cc12dff

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/goto-cc/armcc_cmdline.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening
1111

1212
#include "armcc_cmdline.h"
1313

14+
#include <util/optional.h>
15+
1416
#include <cstring>
1517
#include <iostream>
1618

@@ -265,18 +267,17 @@ static const char *options_with_arg[]=
265267
nullptr
266268
};
267269

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

279-
return false;
280+
return {};
280281
}
281282

282283
bool armcc_cmdlinet::parse(int argc, const char **argv)
@@ -291,35 +292,34 @@ bool armcc_cmdlinet::parse(int argc, const char **argv)
291292
}
292293

293294
// it starts with - and it isn't "-"
294-
295-
std::string prefix;
295+
optionalt<std::string> prefix;
296296

297297
if(in_list(argv[i], options_no_arg))
298298
{
299299
// options that don't have any arguments
300300
set(argv[i]);
301301
}
302-
else if(prefix_in_list(argv[i], options_with_arg, prefix))
302+
else if((prefix = prefix_in_list(argv[i], options_with_arg)))
303303
{
304304
// options that have a separated _or_ concatenated argument
305-
if(strlen(argv[i])>prefix.size()) // concatenated?
306-
set(prefix, std::string(argv[i], prefix.size(), std::string::npos));
305+
if(strlen(argv[i]) > prefix->size()) // Concatenated.
306+
set(*prefix, std::string(argv[i], prefix->size(), std::string::npos));
307307
else
308308
{
309309
// Separated.
310310
if(i!=argc-1) // Guard against end of command line.
311311
{
312-
set(prefix, argv[i+1]);
312+
set(*prefix, argv[i + 1]);
313313
i++;
314314
}
315315
else
316-
set(prefix, "");
316+
set(*prefix, "");
317317
}
318318
}
319-
else if(prefix_in_list(argv[i], options_with_prefix, prefix))
319+
else if((prefix = prefix_in_list(argv[i], options_with_prefix)))
320320
{
321321
// options that have a concatenated argument
322-
set(prefix, std::string(argv[i], prefix.size(), std::string::npos));
322+
set(*prefix, std::string(argv[i], prefix->size(), std::string::npos));
323323
}
324324
else
325325
{ // unrecognized option

0 commit comments

Comments
 (0)