diff options
author | dotnet bot <dotnet-bot@dotnetfoundation.org> | 2018-12-20 20:48:59 +0300 |
---|---|---|
committer | Alexander Köplinger <alex.koeplinger@outlook.com> | 2018-12-20 20:51:17 +0300 |
commit | ccd7b52ca8a70d6aed6667e2d2f0d496f596a941 (patch) | |
tree | 349a7a7fda555563879e5f4ee5604c1d71573527 /netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs | |
parent | 53f091ea4f1fca6fc758a3f3a393898f22f93087 (diff) |
Add System.Private.CoreLib shared sources (#12162)
Taken from corefx commit 5956ad35864352b38c53aa8c2d1ce012c96b168d. Subsequent updates will happen via a mirroring bot.
Diffstat (limited to 'netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs')
-rw-r--r-- | netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs b/netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs new file mode 100644 index 00000000000..fae309be565 --- /dev/null +++ b/netcore/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs @@ -0,0 +1,102 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Text; +using System.Runtime.InteropServices; + +namespace System.IO +{ + /// <summary>Contains internal path helpers that are shared between many projects.</summary> + internal static partial class PathInternal + { + internal const char DirectorySeparatorChar = '/'; + internal const char AltDirectorySeparatorChar = '/'; + internal const char VolumeSeparatorChar = '/'; + internal const char PathSeparator = ':'; + + internal const string DirectorySeparatorCharAsString = "/"; + + // There is only one invalid path character in Unix + private const char InvalidPathChar = '\0'; + + internal const string ParentDirectoryPrefix = @"../"; + + internal static int GetRootLength(ReadOnlySpan<char> path) + { + return path.Length > 0 && IsDirectorySeparator(path[0]) ? 1 : 0; + } + + internal static bool IsDirectorySeparator(char c) + { + // The alternate directory separator char is the same as the directory separator, + // so we only need to check one. + Debug.Assert(DirectorySeparatorChar == AltDirectorySeparatorChar); + return c == DirectorySeparatorChar; + } + + /// <summary> + /// Normalize separators in the given path. Compresses forward slash runs. + /// </summary> + internal static string NormalizeDirectorySeparators(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + // Make a pass to see if we need to normalize so we can potentially skip allocating + bool normalized = true; + + for (int i = 0; i < path.Length; i++) + { + if (IsDirectorySeparator(path[i]) + && (i + 1 < path.Length && IsDirectorySeparator(path[i + 1]))) + { + normalized = false; + break; + } + } + + if (normalized) + return path; + + StringBuilder builder = new StringBuilder(path.Length); + + for (int i = 0; i < path.Length; i++) + { + char current = path[i]; + + // Skip if we have another separator following + if (IsDirectorySeparator(current) + && (i + 1 < path.Length && IsDirectorySeparator(path[i + 1]))) + continue; + + builder.Append(current); + } + + return builder.ToString(); + } + + internal static bool IsPartiallyQualified(ReadOnlySpan<char> path) + { + // This is much simpler than Windows where paths can be rooted, but not fully qualified (such as Drive Relative) + // As long as the path is rooted in Unix it doesn't use the current directory and therefore is fully qualified. + return !Path.IsPathRooted(path); + } + + /// <summary> + /// Returns true if the path is effectively empty for the current OS. + /// For unix, this is empty or null. For Windows, this is empty, null, or + /// just spaces ((char)32). + /// </summary> + internal static bool IsEffectivelyEmpty(string path) + { + return string.IsNullOrEmpty(path); + } + + internal static bool IsEffectivelyEmpty(ReadOnlySpan<char> path) + { + return path.IsEmpty; + } + } +} |