private static void ConvertPatternInto(string glob, StringBuilder buf)
{
for (int i = 0; i < glob.Length; ++i)
{
char c = glob[i];
switch (c)
{
case '.':
case '$':
case '^':
case '{':
case '[':
case '(':
case '|':
case ')':
case '+':
// escape regex operators
buf.Append('\\');
buf.Append(c);
break;
case '/':
case '\\':
// accept either directory separator
buf.Append(@"[/\\]");
break;
case '*':
if (i + 1 < glob.Length && glob[i + 1] == '*')
{
// match any path
buf.Append(".*");
++i;
}
else
{
// match any name
buf.Append(@"[^/\\]*");
}
break;
case '?':
// match any name char
buf.Append(@"[^/\\]");
break;
default:
// passthrough char
buf.Append(c);
break;
}
}
buf.Append('$');
}