mirror of
https://github.com/ReneLergner/WPinternals.git
synced 2026-06-20 06:10:15 +10:00
Project Maintenance
This commit is contained in:
@@ -47,7 +47,7 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
public bool ThereIs;
|
||||
public bool WithMinus;
|
||||
public ArrayList PostStrings = new ArrayList();
|
||||
public ArrayList PostStrings = new();
|
||||
public int PostCharIndex;
|
||||
public SwitchResult()
|
||||
{
|
||||
@@ -57,28 +57,39 @@ namespace SevenZip.CommandLineParser
|
||||
|
||||
public class Parser
|
||||
{
|
||||
public ArrayList NonSwitchStrings = new ArrayList();
|
||||
SwitchResult[] _switches;
|
||||
public ArrayList NonSwitchStrings = new();
|
||||
private readonly SwitchResult[] _switches;
|
||||
|
||||
public Parser(int numSwitches)
|
||||
{
|
||||
_switches = new SwitchResult[numSwitches];
|
||||
for (int i = 0; i < numSwitches; i++)
|
||||
{
|
||||
_switches[i] = new SwitchResult();
|
||||
}
|
||||
}
|
||||
|
||||
bool ParseString(string srcString, SwitchForm[] switchForms)
|
||||
private bool ParseString(string srcString, SwitchForm[] switchForms)
|
||||
{
|
||||
int len = srcString.Length;
|
||||
if (len == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int pos = 0;
|
||||
if (!IsItSwitchChar(srcString[pos]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
while (pos < len)
|
||||
{
|
||||
if (IsItSwitchChar(srcString[pos]))
|
||||
{
|
||||
pos++;
|
||||
}
|
||||
|
||||
const int kNoLen = -1;
|
||||
int matchedSwitchIndex = 0;
|
||||
int maxLen = kNoLen;
|
||||
@@ -86,7 +97,10 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
int switchLen = switchForms[switchIndex].IDString.Length;
|
||||
if (switchLen <= maxLen || pos + switchLen > len)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (String.Compare(switchForms[switchIndex].IDString, 0,
|
||||
srcString, pos, switchLen, true) == 0)
|
||||
{
|
||||
@@ -95,11 +109,17 @@ namespace SevenZip.CommandLineParser
|
||||
}
|
||||
}
|
||||
if (maxLen == kNoLen)
|
||||
{
|
||||
throw new Exception("maxLen == kNoLen");
|
||||
}
|
||||
|
||||
SwitchResult matchedSwitch = _switches[matchedSwitchIndex];
|
||||
SwitchForm switchForm = switchForms[matchedSwitchIndex];
|
||||
if ((!switchForm.Multi) && matchedSwitch.ThereIs)
|
||||
{
|
||||
throw new Exception("switch must be single");
|
||||
}
|
||||
|
||||
matchedSwitch.ThereIs = true;
|
||||
pos += maxLen;
|
||||
int tailSize = len - pos;
|
||||
@@ -109,28 +129,39 @@ namespace SevenZip.CommandLineParser
|
||||
case SwitchType.PostMinus:
|
||||
{
|
||||
if (tailSize == 0)
|
||||
{
|
||||
matchedSwitch.WithMinus = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
matchedSwitch.WithMinus = (srcString[pos] == kSwitchMinus);
|
||||
if (matchedSwitch.WithMinus)
|
||||
{
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SwitchType.PostChar:
|
||||
{
|
||||
if (tailSize < switchForm.MinLen)
|
||||
{
|
||||
throw new Exception("switch is not full");
|
||||
}
|
||||
|
||||
string charSet = switchForm.PostCharSet;
|
||||
const int kEmptyCharValue = -1;
|
||||
if (tailSize == 0)
|
||||
{
|
||||
matchedSwitch.PostCharIndex = kEmptyCharValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
int index = charSet.IndexOf(srcString[pos]);
|
||||
if (index < 0)
|
||||
{
|
||||
matchedSwitch.PostCharIndex = kEmptyCharValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
matchedSwitch.PostCharIndex = index;
|
||||
@@ -144,10 +175,13 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
int minLen = switchForm.MinLen;
|
||||
if (tailSize < minLen)
|
||||
{
|
||||
throw new Exception("switch is not full");
|
||||
}
|
||||
|
||||
if (type == SwitchType.UnLimitedPostString)
|
||||
{
|
||||
matchedSwitch.PostStrings.Add(srcString.Substring(pos));
|
||||
matchedSwitch.PostStrings.Add(srcString[pos..]);
|
||||
return true;
|
||||
}
|
||||
String stringSwitch = srcString.Substring(pos, minLen);
|
||||
@@ -156,7 +190,10 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
char c = srcString[pos];
|
||||
if (IsItSwitchChar(c))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
stringSwitch += c;
|
||||
}
|
||||
matchedSwitch.PostStrings.Add(stringSwitch);
|
||||
@@ -165,7 +202,6 @@ namespace SevenZip.CommandLineParser
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void ParseStrings(SwitchForm[] switchForms, string[] commandStrings)
|
||||
@@ -176,13 +212,19 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
string s = commandStrings[i];
|
||||
if (stopSwitch)
|
||||
{
|
||||
NonSwitchStrings.Add(s);
|
||||
}
|
||||
else
|
||||
if (s == kStopSwitchParsing)
|
||||
{
|
||||
stopSwitch = true;
|
||||
}
|
||||
else
|
||||
if (!ParseString(s, switchForms))
|
||||
{
|
||||
NonSwitchStrings.Add(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +240,7 @@ namespace SevenZip.CommandLineParser
|
||||
{
|
||||
if (commandString.IndexOf(id) == 0)
|
||||
{
|
||||
postString = commandString.Substring(id.Length);
|
||||
postString = commandString[id.Length..];
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -213,7 +255,7 @@ namespace SevenZip.CommandLineParser
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool ParseSubCharsCommand(int numForms, CommandSubCharsSet[] forms,
|
||||
private static bool ParseSubCharsCommand(int numForms, CommandSubCharsSet[] forms,
|
||||
string commandString, ArrayList indices)
|
||||
{
|
||||
indices.Clear();
|
||||
@@ -230,28 +272,37 @@ namespace SevenZip.CommandLineParser
|
||||
if (newIndex >= 0)
|
||||
{
|
||||
if (currentIndex >= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (commandString.IndexOf(c, newIndex + 1) >= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
currentIndex = j;
|
||||
numUsedChars++;
|
||||
}
|
||||
}
|
||||
if (currentIndex == -1 && !charsSet.EmptyAllowed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
indices.Add(currentIndex);
|
||||
}
|
||||
return (numUsedChars == commandString.Length);
|
||||
return numUsedChars == commandString.Length;
|
||||
}
|
||||
const char kSwitchID1 = '-';
|
||||
const char kSwitchID2 = '/';
|
||||
private const char kSwitchID1 = '-';
|
||||
private const char kSwitchID2 = '/';
|
||||
|
||||
const char kSwitchMinus = '-';
|
||||
const string kStopSwitchParsing = "--";
|
||||
private const char kSwitchMinus = '-';
|
||||
private const string kStopSwitchParsing = "--";
|
||||
|
||||
static bool IsItSwitchChar(char c)
|
||||
private static bool IsItSwitchChar(char c)
|
||||
{
|
||||
return (c == kSwitchID1 || c == kSwitchID2);
|
||||
return c == kSwitchID1 || c == kSwitchID2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +317,7 @@ namespace SevenZip.CommandLineParser
|
||||
}
|
||||
}
|
||||
|
||||
class CommandSubCharsSet
|
||||
internal class CommandSubCharsSet
|
||||
{
|
||||
public string Chars = "";
|
||||
public bool EmptyAllowed = false;
|
||||
|
||||
Reference in New Issue
Block a user