forked from ForNeVeR/Cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(ForNeVeR#80) SDK: self-written code for process argument passing
- Loading branch information
Showing
5 changed files
with
113 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Cesium.Sdk.Tests; | ||
|
||
public class ArgumentUtilTests | ||
{ | ||
[Fact] | ||
public void PerformTests() | ||
{ | ||
Assert.Equal("\"\"", ArgumentUtil.ToCommandLineString([""])); | ||
Assert.Equal("a b c", ArgumentUtil.ToCommandLineString(["a", "b", "c"])); | ||
Assert.Equal("\"a b\" c", ArgumentUtil.ToCommandLineString(["a b", "c"])); | ||
Assert.Equal("a\\b c", ArgumentUtil.ToCommandLineString([@"a\b", "c"])); | ||
Assert.Equal("\"\\\"\"", ArgumentUtil.ToCommandLineString(["\""])); | ||
Assert.Equal("\"a \\\"b\\\"\"", ArgumentUtil.ToCommandLineString(["a \"b\""])); | ||
Assert.Equal("\"\\\\\\\"\"", ArgumentUtil.ToCommandLineString(["\\\""])); | ||
Assert.Equal("\"a\\ \\\\\\\"b\\\"\"", ArgumentUtil.ToCommandLineString(["a\\ \\\"b\""])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Cesium.Sdk; | ||
|
||
public static class ArgumentUtil | ||
{ | ||
public static string ToCommandLineString(IEnumerable<string> args) | ||
{ | ||
var result = new StringBuilder(); | ||
var first = true; | ||
foreach (var a in args) | ||
{ | ||
if (first) | ||
{ | ||
first = false; | ||
} | ||
else | ||
{ | ||
result.Append(' '); | ||
} | ||
if (a.Length == 0 || a.Any(c => char.IsWhiteSpace(c) || c == '"')) | ||
{ | ||
result.Append(Quoted(a)); | ||
} | ||
else | ||
{ | ||
result.Append(a); | ||
} | ||
} | ||
return result.ToString(); | ||
} | ||
|
||
private static string Quoted(string arg) | ||
{ | ||
// The simplified rules: | ||
// 1. Every quote should be escaped by \ | ||
// 2. Slashes preceding the quotes should be escaped by \ | ||
// | ||
// Meaning any amount of slashes following a quote should be converted to twice as many slashes + slash + quote. | ||
// The final quote (not part of the argument per se) also counts as quote for this purpose. | ||
var result = new StringBuilder(arg.Length + 2); | ||
result.Append('"'); | ||
var slashes = 0; | ||
foreach (var c in arg) | ||
{ | ||
switch (c) | ||
{ | ||
case '\\': | ||
slashes++; | ||
break; | ||
case '"': | ||
result.Append('\\', slashes * 2); | ||
slashes = 0; | ||
|
||
result.Append("\\\""); | ||
break; | ||
default: | ||
result.Append('\\', slashes); | ||
slashes = 0; | ||
|
||
result.Append(c); | ||
break; | ||
} | ||
} | ||
|
||
result.Append('\\', slashes * 2); | ||
result.Append('"'); | ||
|
||
return result.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.