Exemplo n.º 1
0
        string _trim(int startIndex, int endIndex, bool trimStart, bool trimEnd)
        {
            if (startIndex == endIndex)
            {
                return(string.Empty);
            }

            if (trimStart)
            {
                while (UnderlyingString[startIndex].IsWhiteSpace())
                {
                    ++startIndex;
                    if (startIndex >= endIndex)
                    {
                        return(string.Empty);
                    }
                }
            }

            if (trimEnd)
            {
                while (UnderlyingString[endIndex - 1].IsWhiteSpace())
                {
                    --endIndex;
                    if (endIndex <= startIndex)
                    {
                        return(string.Empty);
                    }
                }
            }

            return(UnderlyingString.Substring(startIndex, endIndex - startIndex));
        }
Exemplo n.º 2
0
        private StoragePath?GetParent()
        {
            if (IsRootPathOnly())
            {
                return(null);
            }

            // Find the index of the first separator from behind and then (to fulfill spec) continue
            // walking down until the last consecutive char has been passed.
            var rootLength = GetRootLength();
            var endIndex   = Length;

            while (endIndex > rootLength && !IsDirectorySeparator(UnderlyingString[endIndex - 1]))
            {
                endIndex--;
            }

            while (endIndex > rootLength && IsDirectorySeparator(UnderlyingString[endIndex - 1]))
            {
                endIndex--;
            }

            // If there has been no directory separator there is no parent (can happen with relative paths).
            if (endIndex <= 0)
            {
                return(null);
            }

            return(FileSystem.GetPath(UnderlyingString.Substring(0, endIndex)));
        }
        /// <summary>
        /// Reads a string consisting of ASCII letters and digits, plus characters specified in <paramref name="additionalChars"/> at the beginning of the reading scope.
        /// </summary>
        /// <param name="additionalChars">Specifies additional characters to read.</param>
        /// <returns>A string at the beginning of the current reading scope, consisting of ASCII letters and digits plus <paramref name="additionalChars"/>.</returns>
        public string ReadASCIILettersAndDigits(params char[] additionalChars)
        {
            var pos = CurrentPosition;

            while (!EOF && (First.IsASCIILetterOrDigit() || First.In(additionalChars)))
            {
                Advance();
            }
            return(UnderlyingString.Substring(pos, CurrentPosition - pos));
        }
 /// <summary>
 /// Advances this reader to the end of the search scope and returns a substring starting from the current position to the end of the search scope. NOTE that after executing this method, this reader is marked <see cref="EOF"/> and is no longer readable.
 /// </summary>
 /// <returns>A substring of the underlying string of this reader, starting from the current position to the end of the reading scope, if the current reader is not marked EOF; otherwise, <see cref="String.Empty"/>.</returns>
 public string ReadToEnd()
 {
     if (CurrentPosition == EndPosition)
     {
         return(string.Empty);
     }
     else
     {
         var output = UnderlyingString.Substring(CurrentPosition, EndPosition - CurrentPosition);
         CurrentPosition = EndPosition;
         return(output);
     }
 }
        /// <summary>
        /// For internal use only. The same as <see cref="SubstringBeforeCurrentPosition"/> except that the white spaces at the end of returned string are trimmed.
        /// </summary>
        /// <param name="prevPos">Provides a position prior to the reader's <see cref="CurrentPosition"/>.</param>
        /// <returns>A substring starting from the specified <paramref name="prevPos" /> to the character previous to the <see cref="CurrentPosition" />. Note the first character of the current reading scope is not contained in the returned substring. <c>null</c> is returned if <paramref name="prevPos" /> equals <see cref="CurrentPosition" />.</returns>
        internal string InnerSubstringBeforeCurrentPositionWithTrimEnd(int prevPos)
        {
            if (prevPos == CurrentPosition)
            {
                return(null);
            }
            var pos = CurrentPosition - 1;

            while (UnderlyingString[pos].IsWhiteSpace())
            {
                --pos;
                if (pos < prevPos)
                {
                    return(null);
                }
            }
            return(UnderlyingString.Substring(prevPos, pos + 1 - prevPos));
        }
        /// <summary>
        /// Reads a variable name from this reader. The valid first character of the variable name is defined by the <paramref name="isValidVariableFirstChr"/>, and a valid variable name character (except the first character) is defined by <paramref name="isValidVariableChr"/>.
        /// </summary>
        /// <returns>A variable name read from this reader.</returns>
        public string ReadVariableName(Func <char, bool> isValidVariableFirstChr, Func <char, bool> isValidVariableChr)
        {
            var pos = CurrentPosition; //! DO NOT use MarkPosition; it is for external use.

            if (isValidVariableFirstChr(First))
            {
                Advance();
            }
            else
            {
                return(null);
            }

            while (!EOF && isValidVariableChr(First))
            {
                Advance();
            }
            return(UnderlyingString.Substring(pos, CurrentPosition - pos));
        }
        /// <summary>
        /// Reads a WAS reference name from this reader. The valid first character of the variable name can either be a ASCII letter, or '_'; and a valid variable name character (except the first character) can either be a ASCII letter or ASCII digit or '_' or '-'.
        /// </summary>
        /// <returns>A variable name read from this reader.</returns>
        public string ReadWASReferenceName()
        {
            var pos = CurrentPosition; //! DO NOT use MarkPosition; it is for external use.

            if (First.IsASCIILetter() || First == '_')
            {
                Advance();
            }
            else
            {
                return(null);
            }

            while (!EOF && First.IsASCIILetterOrDigit() || First == '_' || First == '-')
            {
                Advance();
            }
            return(UnderlyingString.Substring(pos, CurrentPosition - pos));
        }
Exemplo n.º 8
0
        private StoragePath TrimEndingDirectorySeparatorImpl()
        {
            if (!EndsWithDirectorySeparator)
            {
                return(this);
            }

            if (Length == 1)
            {
                throw new InvalidOperationException(ExceptionStrings.StoragePath.TrimmingResultsInEmptyPath());
            }

            var trimmedPath = UnderlyingString.Substring(0, UnderlyingString.Length - 1);

            try
            {
                return(FileSystem.GetPath(trimmedPath));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(ExceptionStrings.StoragePath.TrimmingResultsInInvalidPath(), ex);
            }
        }
Exemplo n.º 9
0
        private string GetName()
        {
            if (IsRootPathOnly())
            {
                return(string.Empty);
            }

            // Specification requires us to ignore exactly one trailing directory separator character.
            // The name starts at the position of the last directory separator character except for
            // that trailing one.
            var startIndex = 0;
            var endIndex   = EndsWithDirectorySeparator ? Length - 2 : Length - 1;

            for (var i = endIndex; i >= 0; i--)
            {
                if (IsDirectorySeparator(UnderlyingString[i]))
                {
                    startIndex = i + 1;
                    break;
                }
            }

            return(UnderlyingString.Substring(startIndex, endIndex - startIndex + 1));
        }
Exemplo n.º 10
0
 public override string ToString()
 {
     return(UnderlyingString?.Substring(Index, Length) ?? String.Empty);
 }
Exemplo n.º 11
0
 public override string ToString()
 {
     return(UnderlyingString.Substring(Index, Length));
 }
 /// <summary>
 /// For internal use only. The internal version of <see cref="SubstringBeforeCurrentPosition"/> without argument check.
 /// </summary>
 /// <param name="prevPos">Provides a position prior to the reader's <see cref="CurrentPosition"/>.</param>
 /// <returns>A substring starting from the specified <paramref name="prevPos" /> to the character previous to the <see cref="CurrentPosition" />. Note the first character of the current reading scope is not contained in the returned substring. <c>null</c> is returned if <paramref name="prevPos" /> equals <see cref="CurrentPosition" />.</returns>
 internal string InnerSubstringBeforeCurrentPosition(int prevPos)
 {
     return(CurrentPosition == prevPos ? null : UnderlyingString.Substring(prevPos, CurrentPosition - prevPos));
 }
 /// <summary>
 /// Returns a substring of the underlying string instance of this reader, starting from the current position to the limit of this reader.
 /// </summary>
 /// <returns>
 /// A <see cref="System.String" /> that is a substring of the underlying string instance of this reader.
 /// </returns>
 public override string ToString()
 {
     return(UnderlyingString.Substring(CurrentPosition, EndPosition - CurrentPosition));
 }