-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathExtensions.cs
More file actions
113 lines (97 loc) · 3.89 KB
/
Extensions.cs
File metadata and controls
113 lines (97 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ISBoxerEVELauncher
{
public static class Extensions
{
/// <summary>
/// Check if a process name matches, ignoring any ".vshost" extension from Visual Studio debugging...
/// </summary>
/// <param name="processA"></param>
/// <param name="processB"></param>
/// <returns></returns>
public static bool NameMatches(this System.Diagnostics.Process processA, System.Diagnostics.Process processB)
{
string cleanA = processA.ProcessName.ToLowerInvariant().Replace(".vshost", string.Empty);
string cleanB = processB.ProcessName.ToLowerInvariant().Replace(".vshost", string.Empty);
return cleanA == cleanB;
}
/// <summary>
/// Check if a process MainModule FileName matches, ignoring any ".vshost" extension from Visual Studio debugging...
/// </summary>
/// <param name="processA"></param>
/// <param name="processB"></param>
/// <returns></returns>
public static bool MainModuleNameMatches(this System.Diagnostics.Process processA, System.Diagnostics.Process processB)
{
string cleanA = processA.MainModule.FileName.ToLowerInvariant().Replace(".vshost", string.Empty);
string cleanB = processB.MainModule.FileName.ToLowerInvariant().Replace(".vshost", string.Empty);
return cleanA == cleanB;
}
/// <summary>
/// Trim matching quotes if the input begins and ends with the given quote character
/// </summary>
/// <param name="input"></param>
/// <param name="quote"></param>
/// <returns></returns>
public static string TrimMatchingQuotes(this string input, char quote)
{
if ((input.Length >= 2) &&
(input[0] == quote) && (input[input.Length - 1] == quote))
return input.Substring(1, input.Length - 2);
return input;
}
/// <summary>
/// Split a string with command-line rules (spaces, quotes, escaping with \)
/// </summary>
/// <param name="commandLine"></param>
/// <param name="splitter"></param>
/// <returns></returns>
public static IEnumerable<string> SplitCommandLine(this string commandLine, char splitter = ' ')
{
bool inQuotes = false;
char lastC = '\0';
IEnumerable<string> split = commandLine.Split(c =>
{
if (c == '\"' && lastC != '\\')
inQuotes = !inQuotes;
lastC = c;
return !inQuotes && c == splitter;
})
.Select(arg => arg.Trim().TrimMatchingQuotes('\"'))
.Where(arg => !string.IsNullOrEmpty(arg));
// return System.Text.RegularExpressions.Regex.Unescape(split);
List<string> unEscaped = new List<string>();
foreach (string s in split)
{
string newS = s;
try
{
newS = System.Text.RegularExpressions.Regex.Unescape(s);
}
catch
{
}
unEscaped.Add(newS);
}
return unEscaped;
}
public static IEnumerable<string> Split(this string str,
Func<char, bool> controller)
{
int nextPiece = 0;
for (int c = 0; c < str.Length; c++)
{
if (controller(str[c]))
{
yield return str.Substring(nextPiece, c - nextPiece);
nextPiece = c + 1;
}
}
yield return str.Substring(nextPiece);
}
}
}