Пример #1
0
        public static bool IsAbsolute([NotNullWhen(true)] string?path)
        {
            if (RoslynString.IsNullOrEmpty(path))
            {
                return(false);
            }

            if (IsUnixLikePlatform)
            {
                return(path[0] == DirectorySeparatorChar);
            }

            // "C:\"
            if (IsDriveRootedAbsolutePath(path))
            {
                // Including invalid paths (e.g. "*:\")
                return(true);
            }

            // "\\machine\share"
            // Including invalid/incomplete UNC paths (e.g. "\\goo")
            return(path.Length >= 2 &&
                   IsDirectorySeparator(path[0]) &&
                   IsDirectorySeparator(path[1]));
        }
Пример #2
0
        private static bool IsGeneratedCodeFile([NotNullWhen(returnValue: true)] string?filePath)
        {
            if (!RoslynString.IsNullOrEmpty(filePath))
            {
                var fileName = PathUtilities.GetFileName(filePath);
                if (fileName.StartsWith("TemporaryGeneratedFile_", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                var extension = PathUtilities.GetExtension(fileName);
                if (!string.IsNullOrEmpty(extension))
                {
                    var fileNameWithoutExtension = PathUtilities.GetFileName(filePath, includeExtension: false);
                    if (fileNameWithoutExtension.EndsWith(".designer", StringComparison.OrdinalIgnoreCase) ||
                        fileNameWithoutExtension.EndsWith(".generated", StringComparison.OrdinalIgnoreCase) ||
                        fileNameWithoutExtension.EndsWith(".g", StringComparison.OrdinalIgnoreCase) ||
                        fileNameWithoutExtension.EndsWith(".g.i", StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #3
0
        private static string?ConvertCase(
            this string?shortName,
            bool trimLeadingTypePrefix,
            Func <char, char> convert
            )
        {
            // Special case the common .NET pattern of "IGoo" as a type name.  In this case we
            // want to generate "goo" as the parameter name.
            if (!RoslynString.IsNullOrEmpty(shortName))
            {
                if (
                    trimLeadingTypePrefix &&
                    (
                        shortName.LooksLikeInterfaceName() || shortName.LooksLikeTypeParameterName()
                    )
                    )
                {
                    return(convert(shortName[1]) + shortName.Substring(2));
                }

                if (convert(shortName[0]) != shortName[0])
                {
                    return(convert(shortName[0]) + shortName.Substring(1));
                }
            }

            return(shortName);
        }
        public static IList <string> ParseFeatureFromMSBuild(string?features)
        {
            if (RoslynString.IsNullOrEmpty(features))
            {
                return(new List <string>(capacity: 0));
            }

            return(features.Split(new[] { ';', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
        }
Пример #5
0
        public static PathKind GetPathKind(string?path)
        {
            if (RoslynString.IsNullOrWhiteSpace(path))
            {
                return(PathKind.Empty);
            }

            // "C:\"
            // "\\machine" (UNC)
            // "/etc"      (Unix)
            if (IsAbsolute(path))
            {
                return(PathKind.Absolute);
            }

            // "."
            // ".."
            // ".\"
            // "..\"
            if (path.Length > 0 && path[0] == '.')
            {
                if (path.Length == 1 || IsDirectorySeparator(path[1]))
                {
                    return(PathKind.RelativeToCurrentDirectory);
                }

                if (path[1] == '.')
                {
                    if (path.Length == 2 || IsDirectorySeparator(path[2]))
                    {
                        return(PathKind.RelativeToCurrentParent);
                    }
                }
            }

            if (!IsUnixLikePlatform)
            {
                // "\"
                // "\goo"
                if (path.Length >= 1 && IsDirectorySeparator(path[0]))
                {
                    return(PathKind.RelativeToCurrentRoot);
                }

                // "C:goo"

                if (path.Length >= 2 && path[1] == VolumeSeparatorChar && (path.Length <= 2 || !IsDirectorySeparator(path[2])))
                {
                    return(PathKind.RelativeToDriveDirectory);
                }
            }

            // "goo.dll"
            return(PathKind.Relative);
        }
Пример #6
0
        public static string CombinePathsUnchecked(string root, string?relativePath)
        {
            RoslynDebug.Assert(!RoslynString.IsNullOrEmpty(root));

            char c = root[root.Length - 1];

            if (!IsDirectorySeparator(c) && c != VolumeSeparatorChar)
            {
                return(root + DirectorySeparatorStr + relativePath);
            }

            return(root + relativePath);
        }
Пример #7
0
        public static string?CombinePossiblyRelativeAndRelativePaths(string?rootOpt, string?relativePath)
        {
            if (RoslynString.IsNullOrEmpty(rootOpt))
            {
                return(null);
            }

            switch (GetPathKind(relativePath))
            {
            case PathKind.Empty:
                return(rootOpt);

            case PathKind.Absolute:
            case PathKind.RelativeToCurrentRoot:
            case PathKind.RelativeToDriveDirectory:
                return(null);
            }

            return(CombinePathsUnchecked(rootOpt, relativePath));
        }
Пример #8
0
        /// <summary>
        /// Checks if the given name is a sequence of valid CLR names separated by a dot.
        /// </summary>
        internal static bool IsValidClrNamespaceName([NotNullWhen(returnValue: true)] this string?name)
        {
            if (RoslynString.IsNullOrEmpty(name))
            {
                return(false);
            }

            char lastChar = '.';

            foreach (char c in name)
            {
                if (c == '\0' || (c == '.' && lastChar == '.'))
                {
                    return(false);
                }

                lastChar = c;
            }

            return(lastChar != '.');
        }
Пример #9
0
        public static bool IsValidFilePath([NotNullWhen(true)] string?fullPath)
        {
            try
            {
                if (RoslynString.IsNullOrEmpty(fullPath))
                {
                    return(false);
                }

                // Uncomment when this is fixed: https://github.com/dotnet/roslyn/issues/19592
                // Debug.Assert(IsAbsolute(fullPath));

                var fileInfo = new FileInfo(fullPath);
                return(!string.IsNullOrEmpty(fileInfo.Name));
            }
            catch (Exception ex) when(
                ex is ArgumentException ||          // The file name is empty, contains only white spaces, or contains invalid characters.
                ex is PathTooLongException ||       // The specified path, file name, or both exceed the system-defined maximum length.
                ex is NotSupportedException)        // fileName contains a colon (:) in the middle of the string.
            {
                return(false);
            }
        }
Пример #10
0
        /// <summary>
        /// Check that the name is a valid Unicode identifier.
        /// </summary>
        public static bool IsValidIdentifier([NotNullWhen(returnValue: true)] string?name)
        {
            if (RoslynString.IsNullOrEmpty(name))
            {
                return(false);
            }

            if (!IsIdentifierStartCharacter(name[0]))
            {
                return(false);
            }

            int nameLength = name.Length;

            for (int i = 1; i < nameLength; i++) //NB: start at 1
            {
                if (!IsIdentifierPartCharacter(name[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #11
0
        private static string EscapeString(string?value)
        {
            PooledStringBuilder?pooledBuilder = null;
            StringBuilder?      b             = null;

            if (RoslynString.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            int startIndex = 0;
            int count      = 0;

            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];

                if (c == '\"' || c == '\\' || ShouldAppendAsUnicode(c))
                {
                    if (b == null)
                    {
                        RoslynDebug.Assert(pooledBuilder == null);
                        pooledBuilder = PooledStringBuilder.GetInstance();
                        b             = pooledBuilder.Builder;
                    }

                    if (count > 0)
                    {
                        b.Append(value, startIndex, count);
                    }

                    startIndex = i + 1;
                    count      = 0;

                    switch (c)
                    {
                    case '\"':
                        b.Append("\\\"");
                        break;

                    case '\\':
                        b.Append("\\\\");
                        break;

                    default:
                        Debug.Assert(ShouldAppendAsUnicode(c));
                        AppendCharAsUnicode(b, c);
                        break;
                    }
                }
                else
                {
                    count++;
                }
            }

            if (b == null)
            {
                return(value);
            }
            else
            {
                RoslynDebug.Assert(pooledBuilder is object);
            }

            if (count > 0)
            {
                b.Append(value, startIndex, count);
            }

            return(pooledBuilder.ToStringAndFree());
        }
Пример #12
0
 internal static bool IsValidClrTypeName([NotNullWhen(returnValue: true)] this string?name)
 {
     return(!RoslynString.IsNullOrEmpty(name) && name.IndexOf('\0') == -1);
 }