forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualPathExtension.cs
More file actions
44 lines (35 loc) · 1.51 KB
/
VirtualPathExtension.cs
File metadata and controls
44 lines (35 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack.Razor.VirtualPath
{
public static class VirtualPathExtension
{
public static Stack<string> TokenizeVirtualPath(this string str, IVirtualPathProvider pathProvider)
{
if (pathProvider == null)
throw new ArgumentNullException("pathProvider");
return TokenizeVirtualPath(str, pathProvider.VirtualPathSeparator);
}
public static Stack<string> TokenizeVirtualPath(this string str, string virtualPathSeparator)
{
if (string.IsNullOrEmpty(str))
return new Stack<string>();
var tokens = str.Split(new[] { virtualPathSeparator }, StringSplitOptions.RemoveEmptyEntries);
return new Stack<string>(tokens.Reverse());
}
public static Stack<string> TokenizeResourcePath(this string str, char pathSeparator = '.')
{
if (string.IsNullOrEmpty(str))
return new Stack<string>();
var n = str.Count(c => c == pathSeparator);
var tokens = str.Split(new [] { pathSeparator }, n);
return new Stack<string>(tokens.Reverse());
}
public static IEnumerable<IGrouping<string, string[]>> GroupByFirstToken(this IEnumerable<string> resourceNames, char pathSeparator = '.')
{
return resourceNames.Select(n => n.Split(new[] { pathSeparator }, 2))
.GroupBy(t => t[0]);
}
}
}