예제 #1
0
        /// <summary>
        /// Returns true if the path begins with a directory separator.
        /// </summary>
        public static bool BeginsWithDirectorySeparator(StringBuilder path)
        {
            if (path == null || path.Length == 0)
            {
                return(false);
            }

            return(Paths.IsDirectorySeparator(path[0]));
        }
예제 #2
0
        /// <summary>
        /// Returns true if the path begins with a directory separator.
        /// </summary>
        public static bool BeginsWithDirectorySeparator(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return(false);
            }

            return(Paths.IsDirectorySeparator(path[0]));
        }
예제 #3
0
        /// <summary>
        /// Returns true if the path ends in a directory separator.
        /// </summary>
        public static bool EndsInDirectorySeparator(StringBuilder path)
        {
            if (path == null || path.Length == 0)
            {
                return(false);
            }

            char lastChar = path[path.Length - 1];

            return(Paths.IsDirectorySeparator(lastChar));
        }
예제 #4
0
        /// <summary>
        /// Returns true if the path ends in a directory separator.
        /// </summary>
        public static bool EndsInDirectorySeparator(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return(false);
            }

            char lastChar = path[path.Length - 1];

            return(Paths.IsDirectorySeparator(lastChar));
        }
예제 #5
0
        private static StringBuffer InternalNormalizeDirectorySeparators(string path)
        {
            // Check to see if we need to normalize
            bool normalized = true;
            char current;

            for (int i = 0; i < path.Length; i++)
            {
                current = path[i];

                // If we have a separator
                if (Paths.IsDirectorySeparator(current))
                {
                    if (
                        // And it isn't the default
                        current != Paths.DirectorySeparator
                        // or it isn't the first char and the next is also a separator (to allow for UNC & extended syntax which begin with \\)
                        || (i > 0 && i < path.Length - 1 && Paths.IsDirectorySeparator(path[i + 1])))
                    {
                        normalized = false;
                        break;
                    }
                }
            }

            // Already normalized, don't allocate another string
            if (normalized)
            {
                return(null);
            }

            // Normalize
            var builder = StringBufferCache.Instance.Acquire((ulong)path.Length);

            // Keep an initial separator if we start with separators
            int startSeparators = 0;

            while (startSeparators < path.Length && Paths.IsDirectorySeparator(path[startSeparators]))
            {
                startSeparators++;
            }
            if (startSeparators > 0)
            {
                builder.Append(Paths.DirectorySeparator);
            }

            // This is a special case- we want to keep *two* if we have *just* two to allow for UNCs and extended paths
            if (startSeparators == 2)
            {
                builder.Append(Paths.DirectorySeparator);
            }

            for (int i = startSeparators; i < path.Length; i++)
            {
                current = path[i];

                // If we have a separator
                if (Paths.IsDirectorySeparator(current))
                {
                    // If the next is a separator, skip adding this
                    if (i < path.Length - 1 && Paths.IsDirectorySeparator(path[i + 1]))
                    {
                        continue;
                    }

                    // Ensure it is the primary separator
                    current = Paths.DirectorySeparator;
                }

                builder.Append(current);
            }

            return(builder);
        }