Example #1
0
        public static int LastIndexOf(this CharSpan chars, char ch)
        {
            int i = chars.Length - 1;

            while (i >= 0 && chars[i] != ch)
            {
                i--;
            }

            return(i);
        }
Example #2
0
        public static CharSpan TrimLeft(this CharSpan chars)
        {
            int skip = 0;

            while (skip < chars.Length && char.IsWhiteSpace(chars[skip]))
            {
                skip++;
            }

            return(chars.Substring(skip));
        }
Example #3
0
        public static CharSpan TrimRight(this CharSpan chars)
        {
            int length = chars.Length;

            while (length != 0 && char.IsWhiteSpace(chars[length - 1]))
            {
                length--;
            }

            return(chars.Substring(0, length));
        }
Example #4
0
        /// <summary>
        /// Finds last whitespace character and returns substring that follows.
        /// </summary>
        public static CharSpan LastWord(this CharSpan chars)
        {
            for (int i = chars.Length - 1; i >= 0; i--)
            {
                if (char.IsWhiteSpace(chars[i]))
                {
                    return(Substring(chars, i + 1));
                }
            }

            return(chars);
        }
Example #5
0
        public static CharSpan Substring(this CharSpan chars, int index, int length)
        {
            if (length == 0)
            {
                return(CharSpan.Empty);
            }
            if (length > chars.Length - index)
            {
                throw new ArgumentOutOfRangeException();
            }

            return(new CharSpan(chars.Buffer, chars.Start + index, length));
        }
Example #6
0
        public static bool StartsWith(this CharSpan chars, string text)
        {
            if (text.Length > chars.Length)
            {
                return(false);
            }

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] != chars[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #7
0
        public bool Equals(CharSpan other)
        {
            if (other != null && other.Length == Length)
            {
                for (int i = 0; i < Length; i++)
                {
                    if (other[i] != this[i])
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
Example #8
0
        public static bool EndsWith(this CharSpan chars, CharSpan text)
        {
            if (text.Length > chars.Length)
            {
                return(false);
            }

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] != chars[chars.Length - text.Length + i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #9
0
 public static CharSpan Substring(this CharSpan chars, int index)
 => index == 0
     ? chars
     : index >= chars.Length
         ? CharSpan.Empty
         : new CharSpan(chars.Buffer, chars.Start + index, chars.Length - index);
Example #10
0
 /// <summary>
 /// Gets last character of the span or '\0' if span is empty.
 /// </summary>
 public static char LastChar(this CharSpan chars)
 {
     return(chars.Length != 0 ? chars[chars.Length - 1] : '\0');
 }
Example #11
0
 public static CharSpan Trim(this CharSpan chars) => chars.TrimLeft().TrimRight();