public static @NonNull String quoteArgument(@NonNull String argument) {
        if (!NEEDS_QUOTING.matcher(argument).find()) return argument;
        StringBuilder sb = new StringBuilder();
        sb.append('"');
        int end = argument.length();
        for (int i = 0; i < end; i++) {
            int nrBackslashes = 0;
            while (i < end && argument.charAt(i) == '\\') {
                i++;
                nrBackslashes++;
            }

            if (i == end) {
                // backslashes at the end of the argument must be escaped so the terminate quote isn't
                nrBackslashes = nrBackslashes * 2;
            } else if (argument.charAt(i) == '"') {
                // backslashes preceding a quote all need to be escaped along with the quote
                nrBackslashes = nrBackslashes * 2 + 1;
            }
            // else backslashes have no special meaning and don't need to be escaped here

            sb.append("\\".repeat(Math.max(0, nrBackslashes)));

            if (i < end) {
                sb.append(argument.charAt(i));
            }
        }
        return sb.append('"').toString();
    }
@Test
    public void testQuoteArgument_OnlyQuotesWhenNecessary() {
        for (String arg : Arrays.asList("", "foo", "foo-bar", "C:\\test\\path", "http://www.example.com/")) {
            assertEquals(arg, WindowsUtil.quoteArgument(arg));
        }
    }