Bug 1632277 - Part 2: Remove assembleSingleArgument helper r=tkikuchi

Differential Revision: https://phabricator.services.mozilla.com/D75205
This commit is contained in:
Mark Striemer 2020-05-29 18:41:03 +00:00
Родитель 3e55277036
Коммит e01be58994
2 изменённых файлов: 0 добавлений и 106 удалений

Просмотреть файл

@ -129,98 +129,6 @@ inline int assembleCmdLine(const char* const* aArgv, wchar_t** aWideCmdLine,
return 0;
}
# ifdef MOZILLA_INTERNAL_API
inline UniquePtr<wchar_t[]> assembleSingleArgument(const nsString& aArg) {
static_assert(sizeof(char16_t) == sizeof(wchar_t),
"char16_t and wchar_t sizes differ");
/*
* \ and " need to be escaped by a \. In the worst case,
* every character is a \ or ", so the string of length
* may double. If we quote an argument, that needs two ".
* Finally, we need a space between arguments, and
* a null byte at the end of command line.
*/
int cmdLineSize = 2 * aArg.Length() /* \ and " need to be escaped */
+ 2 /* we quote every argument */
+ 1; /* space in between, or final null */
auto assembledArg = MakeUnique<wchar_t[]>(cmdLineSize);
if (!assembledArg) {
return nullptr;
}
/* If the argument contains white space, it needs to be quoted. */
int argNeedQuotes = 0;
if (aArg.FindCharInSet(u" \f\n\r\t\v") != kNotFound) {
argNeedQuotes = 1;
}
wchar_t* p = assembledArg.get();
if (argNeedQuotes) {
*p++ = '"';
}
const char16_t* q = aArg.get();
int numBackslashes = 0;
while (*q) {
if (*q == '\\') {
numBackslashes++;
q++;
} else if (*q == '"') {
if (numBackslashes) {
/*
* Double the backslashes since they are followed
* by a quote
*/
for (int i = 0; i < 2 * numBackslashes; i++) {
*p++ = '\\';
}
numBackslashes = 0;
}
/* To escape the quote */
*p++ = '\\';
*p++ = *q++;
} else {
if (numBackslashes) {
/*
* Backslashes are not followed by a quote, so
* don't need to double the backslashes.
*/
for (int i = 0; i < numBackslashes; i++) {
*p++ = '\\';
}
numBackslashes = 0;
}
*p++ = *q++;
}
}
/* Now we are at the end of this argument */
if (numBackslashes) {
/*
* Double the backslashes if we have a quote string
* delimiter at the end.
*/
if (argNeedQuotes) {
numBackslashes *= 2;
}
for (int i = 0; i < numBackslashes; i++) {
*p++ = '\\';
}
}
if (argNeedQuotes) {
*p++ = '"';
}
*p = '\0';
return assembledArg;
}
# endif // MOZILLA_INTERNAL_API
} // namespace mozilla
#endif // defined(XP_WIN)

Просмотреть файл

@ -80,17 +80,3 @@ TEST(AssembleCommandLineWin, assembleCmdLine)
EXPECT_STREQ(assembled.get(), testCase.mExpected);
}
}
TEST(AssembleCommandLineWin, assembleSingleArgument)
{
for (const auto& testCase : testCases) {
if (testCase.mArgs[1]) {
// Skip a case having more than one argument.
continue;
}
auto assembled =
assembleSingleArgument(NS_ConvertUTF8toUTF16(testCase.mArgs[0]));
EXPECT_STREQ(assembled.get(), testCase.mExpected);
}
}