| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | internal static class PathUtilities |
| | | 10 | | { |
| | | 11 | | private const char DirectorySeparatorChar = '\\'; |
| | | 12 | | private const char AltDirectorySeparatorChar = '/'; |
| | | 13 | | private const char VolumeSeparatorChar = ':'; |
| | | 14 | | |
| | | 15 | | private static string? s_platformSpecificDirectorySeparator; |
| | | 16 | | |
| | | 17 | | private static string PlatformSpecificDirectorySeparator => |
| | | 18 | | // '*' is a valid char on Unix-based FS |
| | 0 | 19 | | s_platformSpecificDirectorySeparator ??= |
| | 0 | 20 | | (Array.IndexOf(Path.GetInvalidFileNameChars(), '*') >= 0 ? DirectorySeparatorChar : AltDirectorySeparato |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Returns the position in given path where the file name starts. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <returns>-1 if path is null.</returns> |
| | | 26 | | internal static int IndexOfFileName(string path) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (path == null) |
| | 0 | 29 | | { |
| | 0 | 30 | | return -1; |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | for (int i = path.Length - 1; i >= 0; i--) |
| | 0 | 34 | | { |
| | 0 | 35 | | char ch = path[i]; |
| | 0 | 36 | | if (ch == DirectorySeparatorChar || ch == AltDirectorySeparatorChar || ch == VolumeSeparatorChar) |
| | 0 | 37 | | { |
| | 0 | 38 | | return i + 1; |
| | | 39 | | } |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return 0; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Get file name from path. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <remarks>Unlike <see cref="System.IO.Path.GetFileName(string)"/> this method doesn't check for invalid path |
| | | 49 | | internal static string GetFileName(string path) |
| | 0 | 50 | | { |
| | 0 | 51 | | int fileNameStart = IndexOfFileName(path); |
| | 0 | 52 | | return (fileNameStart <= 0) ? path : path.Substring(fileNameStart); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static string CombinePathWithRelativePath(string root, string relativePath) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (root.Length == 0) |
| | 0 | 58 | | { |
| | 0 | 59 | | return relativePath; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | char c = root[root.Length - 1]; |
| | 0 | 63 | | if (c == DirectorySeparatorChar || c == AltDirectorySeparatorChar || c == VolumeSeparatorChar) |
| | 0 | 64 | | { |
| | 0 | 65 | | return root + relativePath; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return root + PlatformSpecificDirectorySeparator + relativePath; |
| | 0 | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |