ReadOnlyCollection<string> ParsePathTemplate (string template, int index, int end)
{
int widx = template.IndexOf ('*', index, end);
if (widx >= 0 && widx != end - 1)
throw new FormatException (String.Format ("Wildcard in UriTemplate is valid only if it is placed at the last part of the path: '{0}'", template));
List<string> list = null;
int prevEnd = -2;
for (int i = index; i <= end; ) {
i = template.IndexOf ('{', i);
if (i < 0 || i > end)
break;
if (i == prevEnd + 1)
throw new ArgumentException (String.Format ("The UriTemplate '{0}' contains adjacent templated segments, which is invalid.", template));
int e = template.IndexOf ('}', i + 1);
if (e < 0 || i > end)
throw new FormatException (String.Format ("Missing '}' in URI template '{0}'", template));
prevEnd = e;
if (list == null)
list = new List<string> ();
i++;
string name = template.Substring (i, e - i);
string uname = name.ToUpper (CultureInfo.InvariantCulture);
if (list.Contains (uname) || (path != null && path.Contains (uname)))
throw new InvalidOperationException (String.Format ("The URI template string contains duplicate template item {{'{0}'}}", name));
list.Add (uname);
i = e + 1;
}
return list != null ? new ReadOnlyCollection<string> (list) : empty_strings;
}