2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using System . Runtime . InteropServices ;
5
6
using System . Text . RegularExpressions ;
6
7
7
8
// ReSharper disable UseFileSystem
8
9
9
10
namespace KY . Core . DataAccess
10
11
{
11
- public class PathHelper // TODO: to internal
12
+ public class PathHelper
12
13
{
13
- private const string CurrentSymbol = "." ;
14
- private const string ParentSymbol = ".." ;
15
- private const string DriveSymbol = ":" ;
16
- public static readonly Regex AbsolutePathRegex = new Regex ( @"^(([A-z]:)|(file:\\\\)|(\\\\))" ) ;
14
+ private const string currentSymbol = "." ;
15
+ private const string parentSymbol = ".." ;
16
+ private const string driveSymbol = ":" ;
17
+
18
+ private static readonly Regex absolutePathRegex = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
19
+ ? new Regex ( @"^(([A-z]:)|(file:\\\\)|(\\\\))" )
20
+ : new Regex ( "^/" ) ;
17
21
public string Root { get ; }
18
22
19
23
public PathHelper ( string root = null )
@@ -24,7 +28,7 @@ public PathHelper(string root = null)
24
28
25
29
public bool IsAbsolute ( string path )
26
30
{
27
- return AbsolutePathRegex . IsMatch ( path ) ;
31
+ return absolutePathRegex . IsMatch ( path ) ;
28
32
}
29
33
30
34
public string ToAbsolute ( params string [ ] pathChunks )
@@ -47,8 +51,10 @@ public string ToRelative(string path, bool useRelativeChar = true)
47
51
48
52
public string Format ( string path )
49
53
{
50
- path = path ? . Replace ( '/' , Path . DirectorySeparatorChar ) ;
51
- if ( path != null && path . StartsWith ( Path . DirectorySeparatorChar . ToString ( ) + Path . DirectorySeparatorChar ) )
54
+ path = path ? . Replace ( '/' , Path . DirectorySeparatorChar ) . Replace ( '\\ ' , Path . DirectorySeparatorChar ) ;
55
+ bool isNetworkPath = path ? . StartsWith ( Path . DirectorySeparatorChar . ToString ( ) + Path . DirectorySeparatorChar ) ?? false ;
56
+ bool isDrive = ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ) && ( path ? . StartsWith ( Path . DirectorySeparatorChar . ToString ( ) ) ?? false ) ;
57
+ if ( isNetworkPath || isDrive )
52
58
{
53
59
return path . TrimEnd ( Path . DirectorySeparatorChar ) ;
54
60
}
@@ -69,24 +75,24 @@ public string Combine(params string[] pathChunks)
69
75
{
70
76
return pathChunks . FirstOrDefault ( ) ;
71
77
}
72
- bool isDrive = safePathChunks . First ( ) . Contains ( DriveSymbol ) ;
78
+ bool isDrive = safePathChunks . First ( ) . Contains ( driveSymbol ) ;
73
79
List < string > parts = new List < string > ( ) ;
74
80
foreach ( string pathChunk in safePathChunks . Select ( this . Format ) )
75
81
{
76
82
string [ ] chunks = pathChunk . Split ( Path . DirectorySeparatorChar ) ;
77
83
foreach ( string chunk in chunks )
78
84
{
79
85
string last = parts . LastOrDefault ( ) ;
80
- if ( chunk == CurrentSymbol )
86
+ if ( chunk == currentSymbol )
81
87
{
82
88
if ( parts . Count == 0 )
83
89
{
84
90
parts . Add ( chunk ) ;
85
91
}
86
92
}
87
- else if ( chunk == ParentSymbol )
93
+ else if ( chunk == parentSymbol )
88
94
{
89
- if ( parts . Count == 0 || last == ParentSymbol )
95
+ if ( parts . Count == 0 || last == parentSymbol )
90
96
{
91
97
parts . Add ( chunk ) ;
92
98
}
@@ -95,7 +101,7 @@ public string Combine(params string[] pathChunks)
95
101
parts . Remove ( last ) ;
96
102
}
97
103
}
98
- else if ( parts . Count == 0 || last == ParentSymbol )
104
+ else if ( parts . Count == 0 || last == parentSymbol )
99
105
{
100
106
parts . Add ( chunk ) ;
101
107
}
@@ -108,26 +114,6 @@ public string Combine(params string[] pathChunks)
108
114
return string . Join ( Path . DirectorySeparatorChar . ToString ( ) , parts ) ;
109
115
}
110
116
111
- //public static string Get(string relative, string absolute)
112
- //{
113
- // int goToParent = 0;
114
- // while (relative.StartsWith(@"..\"))
115
- // {
116
- // ++goToParent;
117
- // relative = relative.Substring(3);
118
- // }
119
-
120
- // DirectoryInfo info = new DirectoryInfo(absolute);
121
- // for (int i = 0; i < goToParent; ++i)
122
- // {
123
- // if (info.Parent == null)
124
- // break;
125
-
126
- // info = info.Parent;
127
- // }
128
- // return Path.Combine(info.FullName, relative);
129
- //}
130
-
131
117
public string Parent ( string path )
132
118
{
133
119
return string . IsNullOrEmpty ( path ) ? path : Path . GetDirectoryName ( path ) ;
0 commit comments