Skip to content

Commit

Permalink
GetQuotedString() uses correct windows cmdline notation
Browse files Browse the repository at this point in the history
- escaping of backslash and quotes
- fixes gh-312 for new cmdline parser algorithm (gh-307, gh-310)

Signed-off-by: Sergey G. Brester <info@sebres.de>
Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
  • Loading branch information
sebres authored and mcmilk committed Apr 14, 2023
1 parent f721c05 commit b9b0610
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CPP/7zip/UI/Explorer/ContextMenu.cpp
Expand Up @@ -408,7 +408,8 @@ static UString GetQuotedReducedString(const UString &s)
UString s2 = s;
ReduceString(s2);
s2.Replace(L"&", L"&&");
return GetQuotedString(s2);
s2.InsertAtFront(L'"'); s2 += L'"'; // quote without GetQuotedString (because it escapes now)
return s2;
}

static void MyFormatNew_ReducedName(UString &s, const UString &name)
Expand Down
38 changes: 36 additions & 2 deletions CPP/Common/MyString.cpp
Expand Up @@ -1806,10 +1806,44 @@ FString us2fs(const wchar_t *s)

// ----------------------------------------

UString GetQuotedString(const UString &s)
UString GetQuotedString(const UString &src)
{
UString s2 ('\"');
s2 += s;
unsigned bcount = 0;
wchar_t c; const wchar_t *f = src.Ptr(), *s = f, *b = f;
// add string considering backslashes before quote (escape them):
while (1)
{
c = *s++;
switch (c)
{
case L'\\':
// a backslash - save the position and count them up to quote-char or regular char
if (!bcount) b = s-1;
bcount++;
break;
case L'\0':
// end of string (it is always quoted, so need to escape backslashes too):
case L'"':
// add part before backslash (and unescaped backslashes if some are there):
s2.AddFrom(f, (unsigned)(s - f - 1));
f = s;
if (bcount) {
// escape backslashes before quote (same count of BS again):
s2.AddFrom(b, (unsigned)(s - b - 1));
}
// done if end of string
if (c == L'\0') goto done;
// escape this quote char:
s2 += L"\\\"";
break;
default:
// a regular character, reset backslash counter
bcount = 0;
}
}
s2.AddFrom(f, (unsigned)(s - f - 1));
done:
s2 += '\"';
return s2;
}

0 comments on commit b9b0610

Please sign in to comment.