public PathMatchResult Match(string path) { var result = new PathMatchResult { Parameters = new string[ArgumentCount], Success = false }; if (MatchSingleLiteral(path, _literal0)) { result.Success = MatchRemainder(result, 0, path.Substring(_literal0.Length)); } return result; }
private bool MatchRemainder(PathMatchResult result, int argNumber, string remainder) { if (argNumber == ArgumentCount) return remainder.Length == 0; var literal = _literals[argNumber]; if (literal.Length == 0) // no trailing literal, all is argument; only allowed at end { if (remainder.Length == 0) // no arg is not allowed return false; result.Parameters[argNumber] = remainder; return true; } // Start at pos 1, not 0, because the arg should always match at least one char for (int p = 0; p < remainder.Length;) { var literalPos = remainder.IndexOf(literal, p); if (literalPos < 0) return false; else if (literalPos == 0 && p < remainder.Length) // found immediately, so try stepping forward just one char literalPos = remainder.IndexOf(literal, p + 1); result.Parameters[argNumber] = remainder.Substring(p, literalPos - p); p = literalPos + literal.Length; if (MatchRemainder(result, argNumber + 1, remainder.Substring(p))) return true; } return false; }