@@ -24,54 +24,34 @@ namespace windows
2424 if (ws.empty ())
2525 return 2u ; // just quotes
2626
27- constexpr static auto space = L' ' ;
2827 constexpr static auto quote = L' "' ;
29-
30- const auto has_space = ws.find (space) != basic_string_view<wchar_t >::npos;
31- const auto quoted = (ws.front () == quote) && (ws.back () == quote);
32- const auto has_inner_quote = std::find (std::next (ws.begin ()), std::prev (ws.end ()), quote) != std::prev (ws.end ());
33- const auto needs_escape = (has_space && !quoted) || has_inner_quote;
34-
35- if (!needs_escape)
36- return ws.size ();
37- else
38- return ws.size () + std::count (ws.begin (), ws.end (), quote) + (quoted ? 2u : 0u );
28+ const auto needs_quotes = ws.find_first_of (L" \t " ) != std::basic_string_view<wchar_t >::npos;
29+ return ws.size () + std::count (ws.begin (), ws.end (), quote) + (needs_quotes ? 2u : 0u );
3930 }
4031
4132
4233 std::size_t default_launcher::escape_argv_string (wchar_t * itr, std::size_t max_size,
4334 basic_string_view<wchar_t > ws)
4435 {
45- constexpr static auto space = L' ' ;
4636 constexpr static auto quote = L' "' ;
47-
48- const auto sz = escaped_argv_length (ws);
37+ const auto needs_quotes = ws.find_first_of (L" \t " ) != basic_string_view<wchar_t >::npos;
38+ const auto need_escapes = std::count (ws.begin (), ws.end (), quote);
39+
40+ const auto sz = ws.size () + needed_escapes + (needs_quotes ? 2u : 0u );
41+
4942 if (sz > max_size)
5043 return 0u ;
44+
5145 if (ws.empty ())
5246 {
5347 itr[0 ] = quote;
5448 itr[1 ] = quote;
5549 return 2u ;
5650 }
5751
58-
59- const auto has_space = ws.find (space) != basic_string_view<wchar_t >::npos;
60- const auto quoted = (ws.front () == quote) && (ws.back () == quote);
61- const auto has_inner_quote = std::find (std::next (ws.begin ()), std::prev (ws.end ()), quote) != std::prev (ws.end ());
62- const auto needs_escape = (has_space && !quoted) || has_inner_quotes;
63-
64- if (!needs_escape)
65- return std::copy (ws.begin (), ws.end (), itr) - itr;
66-
67- if (sz < (2u + ws.size ()))
68- return 0u ;
69-
70-
71-
7252 const auto end = itr + sz;
7353 const auto begin = itr;
74- if (!quoted )
54+ if (needs_quotes )
7555 *(itr++) = quote;
7656
7757 for (auto wc : ws)
@@ -80,7 +60,8 @@ namespace windows
8060 *(itr++) = L' \\ ' ;
8161 *(itr++) = wc;
8262 }
83- if (!quoted)
63+
64+ if (needs_quotes)
8465 *(itr++) = quote;
8566 return itr - begin;
8667 }
0 commit comments