PathInternal.Unix.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. namespace System.IO
  8. {
  9. /// <summary>Contains internal path helpers that are shared between many projects.</summary>
  10. internal static partial class PathInternal
  11. {
  12. internal const char DirectorySeparatorChar = '/';
  13. internal const char AltDirectorySeparatorChar = '/';
  14. internal const char VolumeSeparatorChar = '/';
  15. internal const char PathSeparator = ':';
  16. internal const string DirectorySeparatorCharAsString = "/";
  17. // There is only one invalid path character in Unix
  18. private const char InvalidPathChar = '\0';
  19. internal const string ParentDirectoryPrefix = @"../";
  20. internal static int GetRootLength(ReadOnlySpan<char> path)
  21. {
  22. return path.Length > 0 && IsDirectorySeparator(path[0]) ? 1 : 0;
  23. }
  24. internal static bool IsDirectorySeparator(char c)
  25. {
  26. // The alternate directory separator char is the same as the directory separator,
  27. // so we only need to check one.
  28. Debug.Assert(DirectorySeparatorChar == AltDirectorySeparatorChar);
  29. return c == DirectorySeparatorChar;
  30. }
  31. /// <summary>
  32. /// Normalize separators in the given path. Compresses forward slash runs.
  33. /// </summary>
  34. internal static string NormalizeDirectorySeparators(string path)
  35. {
  36. if (string.IsNullOrEmpty(path))
  37. return path;
  38. // Make a pass to see if we need to normalize so we can potentially skip allocating
  39. bool normalized = true;
  40. for (int i = 0; i < path.Length; i++)
  41. {
  42. if (IsDirectorySeparator(path[i])
  43. && (i + 1 < path.Length && IsDirectorySeparator(path[i + 1])))
  44. {
  45. normalized = false;
  46. break;
  47. }
  48. }
  49. if (normalized)
  50. return path;
  51. StringBuilder builder = new StringBuilder(path.Length);
  52. for (int i = 0; i < path.Length; i++)
  53. {
  54. char current = path[i];
  55. // Skip if we have another separator following
  56. if (IsDirectorySeparator(current)
  57. && (i + 1 < path.Length && IsDirectorySeparator(path[i + 1])))
  58. continue;
  59. builder.Append(current);
  60. }
  61. return builder.ToString();
  62. }
  63. internal static bool IsPartiallyQualified(ReadOnlySpan<char> path)
  64. {
  65. // This is much simpler than Windows where paths can be rooted, but not fully qualified (such as Drive Relative)
  66. // As long as the path is rooted in Unix it doesn't use the current directory and therefore is fully qualified.
  67. return !Path.IsPathRooted(path);
  68. }
  69. /// <summary>
  70. /// Returns true if the path is effectively empty for the current OS.
  71. /// For unix, this is empty or null. For Windows, this is empty, null, or
  72. /// just spaces ((char)32).
  73. /// </summary>
  74. internal static bool IsEffectivelyEmpty(string path)
  75. {
  76. return string.IsNullOrEmpty(path);
  77. }
  78. internal static bool IsEffectivelyEmpty(ReadOnlySpan<char> path)
  79. {
  80. return path.IsEmpty;
  81. }
  82. }
  83. }