Skip to content

Commit 8552ef5

Browse files
authored
Utils: Expand functionality and refactor join behavior (#12)
Adds two new utilities: * isCommandArg: takes a string and a command option and determines if the string is the command option * normalArg: Normalizes a command line argument for easier parsing (lowercase, strip leading \ and - characters, etc) Also refactors join's behavior, instead of stripping a trailing space, we remove the dangling separator, no matter what it is.
1 parent 091b1a7 commit 8552ef5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/utils.cxx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ std::string join(const StrList &args, const std::string &join_char)
141141
for(std::string arg : args) {
142142
joined_path += arg + join_char;
143143
}
144-
// Remove trailing space
145-
joined_path.pop_back();
144+
// Remove trailing token
145+
const size_t last_token_pos = joined_path.rfind(join_char);
146+
if(last_token_pos != std::string::npos)
147+
{
148+
joined_path.erase(last_token_pos, joined_path.length());
149+
}
146150
return joined_path;
147151
}
148152

@@ -403,6 +407,22 @@ void debug(char * dbgStmt, int len) {
403407
debug(std::string(dbgStmt, len));
404408
}
405409

410+
bool isCommandArg(const std::string &arg, const std::string &command)
411+
{
412+
const std::string slash_opt = "/"+command;
413+
const std::string dash_opt = "-"+command;
414+
return startswith(arg, slash_opt) || startswith(arg, dash_opt);
415+
}
416+
417+
void normalArg(std::string &arg)
418+
{
419+
// first normalize capitalization
420+
lower(arg);
421+
// strip leading / and -
422+
arg = strip(strip(arg, "-"), "/");
423+
}
424+
425+
406426
std::string reportLastError()
407427
{
408428
DWORD error = GetLastError();

src/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ void debug(std::string dbgStmt);
160160

161161
void debug(char * dbgStmt, int len);
162162

163+
bool isCommandArg(const std::string &arg, const std::string &opt);
164+
165+
void normalArg(std::string &arg);
166+
163167
/**
164168
* Library Searching utility class
165169
* Collection of heuristics and logic surrounding library

0 commit comments

Comments
 (0)