/// <summary> /// The parse string. /// </summary> /// <param name="srcString"> /// The src string. /// </param> /// <param name="switchForms"> /// The switch forms. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> /// <exception cref="ApplicationException"> /// </exception> 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; for (int switchIndex = 0; switchIndex < this._switches.Length; switchIndex++) { 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) { matchedSwitchIndex = switchIndex; maxLen = switchLen; } } if (maxLen == kNoLen) { throw new ApplicationException("maxLen == kNoLen"); } SwitchResult matchedSwitch = this._switches[matchedSwitchIndex]; SwitchForm switchForm = switchForms[matchedSwitchIndex]; if ((!switchForm.Multi) && matchedSwitch.ThereIs) { throw new ApplicationException("switch must be single"); } matchedSwitch.ThereIs = true; pos += maxLen; int tailSize = len - pos; SwitchType type = switchForm.Type; switch (type) { 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 ApplicationException("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; pos++; } } break; } case SwitchType.LimitedPostString: case SwitchType.UnLimitedPostString: { int minLen = switchForm.MinLen; if (tailSize < minLen) { throw new ApplicationException("switch is not full"); } if (type == SwitchType.UnLimitedPostString) { matchedSwitch.PostStrings.Add(srcString.Substring(pos).TrimMatchingQuotes('\"')); return true; } string stringSwitch = srcString.Substring(pos, minLen); pos += minLen; for (int i = minLen; i < switchForm.MaxLen && pos < len; i++, pos++) { char c = srcString[pos]; if (IsItSwitchChar(c)) { break; } stringSwitch += c; } matchedSwitch.PostStrings.Add(stringSwitch.TrimMatchingQuotes('\"')); break; } } } return true; }
/// <summary> /// The parse strings. /// </summary> /// <param name="switchForms"> /// The switch forms. /// </param> /// <param name="commandStrings"> /// The command strings. /// </param> public void ParseStrings(SwitchForm[] switchForms, string[] commandStrings) { int numCommandStrings = commandStrings.Length; bool stopSwitch = false; for (int i = 0; i < numCommandStrings; i++) { string s = commandStrings[i]; if (stopSwitch) { this.NonSwitchStrings.Add(s); } else if (s == kStopSwitchParsing) { stopSwitch = true; } else if (!this.ParseString(s, switchForms)) { this.NonSwitchStrings.Add(s); } } }