Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs (C#)

Code before:

        /// <summary>
        /// Adds paths to a 'combined path' string (like %Path% or %PSModulePath%) if they are not already there.
        /// </summary>
        /// <param name="basePath">Path string (like %Path% or %PSModulePath%).</param>
        /// <param name="pathToAdd">Collection of individual paths to add.</param>
        /// <param name="insertPosition">-1 to append to the end; 0 to insert in the beginning of the string; etc...</param>
        /// <returns>Result string.</returns>
        private static string UpdatePath(string basePath, string pathToAdd, ref int insertPosition)
        {
            // we don't support if any of the args are null - parent function should ensure this; empty values are ok
            Dbg.Assert(basePath != null, "basePath should not be null according to contract of the function");
            Dbg.Assert(pathToAdd != null, "pathToAdd should not be null according to contract of the function");

            // The 'pathToAdd' could be a 'combined path' (path-separator-separated).
            string[] newPaths = pathToAdd.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            if (newPaths.Length is 0)
            {
                // The 'pathToAdd' doesn't really contain any paths to add.
                return basePath;
            }

            var result = new StringBuilder(basePath);
            var addedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            string[] initialPaths = basePath.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            foreach (string p in initialPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                addedPaths.Add(NormalizePath(p));
            }

            foreach (string subPathToAdd in newPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                string normalizedPath = NormalizePath(subPathToAdd);
                if (addedPaths.Contains(normalizedPath))
                {
                    // The normalized sub path was already added - skip it.
                    continue;
                }

                // The normalized sub path was not found - add it.
                if (insertPosition is -1 || insertPosition >= result.Length)

Review comment (suggestion):
```suggestion
            var result = new StringBuilder(basePath, pathToAdd.Length + newPaths.Length);
```

Code after:

        /// <summary>
        /// Adds paths to a 'combined path' string (like %Path% or %PSModulePath%) if they are not already there.
        /// </summary>
        /// <param name="basePath">Path string (like %Path% or %PSModulePath%).</param>
        /// <param name="pathToAdd">An individual path to add, or multiple paths separated by the path separator character.</param>
        /// <param name="insertPosition">-1 to append to the end; 0 to insert in the beginning of the string; etc...</param>
        /// <returns>Result string.</returns>
        private static string UpdatePath(string basePath, string pathToAdd, ref int insertPosition)
        {
            // we don't support if any of the args are null - parent function should ensure this; empty values are ok
            Dbg.Assert(basePath != null, "basePath should not be null according to contract of the function");
            Dbg.Assert(pathToAdd != null, "pathToAdd should not be null according to contract of the function");

            // The 'pathToAdd' could be a 'combined path' (path-separator-separated).
            string[] newPaths = pathToAdd.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            if (newPaths.Length is 0)
            {
                // The 'pathToAdd' doesn't really contain any paths to add.
                return basePath;
            }

            var result = new StringBuilder(basePath, capacity: basePath.Length + pathToAdd.Length + newPaths.Length);
            var addedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            string[] initialPaths = basePath.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            foreach (string p in initialPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                addedPaths.Add(Path.TrimEndingDirectorySeparator(p));
            }

            foreach (string subPathToAdd in newPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                string normalizedPath = Path.TrimEndingDirectorySeparator(subPathToAdd);
                if (addedPaths.Contains(normalizedPath))
                {
                    // The normalized sub path was already added - skip it.
                    continue;
                }

                // The normalized sub path was not found - add it.
                if (insertPosition is -1 || insertPosition >= result.Length)