TryMatchText() public method

Matches a text without setting an error if match fails.
public TryMatchText ( string text, System.StringComparison comparisonType = StringComparison.OrdinalIgnoreCase ) : bool
text string The string that must match. Can not be null nor empty.
comparisonType System.StringComparison Specifies the culture, case, and sort rules.
return bool
コード例 #1
0
        /// <summary>
        /// Matches a JSON value: a "string", null, a number (double value), true or false.
        /// Not error is set if match fails.
        /// </summary>
        /// <param name="this">This <see cref="StringMatcher"/>.</param>
        /// <param name="value">The parsed value. Can be null.</param>
        /// <returns>True if a JSON value has been matched, false otherwise.</returns>
        public static bool TryMatchJSONValue(this StringMatcher @this, out object value)
        {
            string s;

            if (@this.TryMatchJSONQuotedString(out s, true))
            {
                value = s;
                return(true);
            }
            double d;

            if (@this.TryMatchDoubleValue(out d))
            {
                value = d;
                return(true);
            }
            if (@this.TryMatchText("true"))
            {
                value = true;
                return(true);
            }
            if (@this.TryMatchText("false"))
            {
                value = false;
                return(true);
            }
            value = null;
            return(false);
        }
コード例 #2
0
 /// <summary>
 /// Matches a JSON value: a "string", null, a number (double value), true or false.
 /// This method ignores the actual value and does not set any error if match fails.
 /// </summary>
 /// <param name="this">This <see cref="StringMatcher"/>.</param>
 /// <returns>True if a JSON value has been matched, false otherwise.</returns>
 public static bool TryMatchJSONTerminalValue(this StringMatcher @this)
 {
     return(@this.TryMatchJSONQuotedString(true) ||
            @this.TryMatchDoubleValue() ||
            @this.TryMatchText("true") ||
            @this.TryMatchText("false"));
 }
コード例 #3
0
        /// <summary>
        /// Matches a quoted string without extracting its content.
        /// </summary>
        /// <param name="this">This <see cref="StringMatcher"/>.</param>
        /// <param name="allowNull">True to allow 'null'.</param>
        /// <returns><c>true</c> when matched, <c>false</c> otherwise.</returns>
        public static bool TryMatchJSONQuotedString(this StringMatcher @this, bool allowNull = false)
        {
            if (@this.IsEnd)
            {
                return(false);
            }
            int i = @this.StartIndex;

            if (@this.Text[i++] != '"')
            {
                return(allowNull && @this.TryMatchText("null"));
            }
            int len = @this.Length - 1;

            while (len >= 0)
            {
                if (len == 0)
                {
                    return(false);
                }
                char c = @this.Text[i++];
                --len;
                if (c == '"')
                {
                    break;
                }
                if (c == '\\')
                {
                    i++;
                    --len;
                }
            }
            return(@this.UncheckedMove(i - @this.StartIndex));
        }
コード例 #4
0
        /// <summary>
        /// Matches a JSON quoted string without setting an error if match fails.
        /// </summary>
        /// <param name="this">This <see cref="StringMatcher"/>.</param>
        /// <param name="content">Extracted content.</param>
        /// <param name="allowNull">True to allow 'null'.</param>
        /// <returns><c>true</c> when matched, <c>false</c> otherwise.</returns>
        public static bool TryMatchJSONQuotedString(this StringMatcher @this, out string content, bool allowNull = false)
        {
            content = null;
            if (@this.IsEnd)
            {
                return(false);
            }
            int i = @this.StartIndex;

            if (@this.Text[i++] != '"')
            {
                return(allowNull && @this.TryMatchText("null"));
            }
            int           len = @this.Length - 1;
            StringBuilder b   = null;

            while (len >= 0)
            {
                if (len == 0)
                {
                    return(false);
                }
                char c = @this.Text[i++];
                --len;
                if (c == '"')
                {
                    break;
                }
                if (c == '\\')
                {
                    if (len == 0)
                    {
                        return(false);
                    }
                    if (b == null)
                    {
                        b = new StringBuilder(@this.Text, @this.StartIndex + 1, i - @this.StartIndex - 2, 1024);
                    }
                    switch ((c = @this.Text[i++]))
                    {
                    case 'r': c = '\r'; break;

                    case 'n': c = '\n'; break;

                    case 'b': c = '\b'; break;

                    case 't': c = '\t'; break;

                    case 'f': c = '\f'; break;

                    case 'u':
                    {
                        if (--len == 0)
                        {
                            return(false);
                        }
                        int cN;
                        cN = ReadHexDigit(@this.Text[i++]);
                        if (cN < 0 || cN > 15)
                        {
                            return(false);
                        }
                        int val = cN << 12;
                        if (--len == 0)
                        {
                            return(false);
                        }
                        cN = ReadHexDigit(@this.Text[i++]);
                        if (cN < 0 || cN > 15)
                        {
                            return(false);
                        }
                        val |= cN << 8;
                        if (--len == 0)
                        {
                            return(false);
                        }
                        cN = ReadHexDigit(@this.Text[i++]);
                        if (cN < 0 || cN > 15)
                        {
                            return(false);
                        }
                        val |= cN << 4;
                        if (--len == 0)
                        {
                            return(false);
                        }
                        cN = ReadHexDigit(@this.Text[i++]);
                        if (cN < 0 || cN > 15)
                        {
                            return(false);
                        }
                        val |= cN;
                        c    = (char)val;
                        break;
                    }
                    }
                }
                if (b != null)
                {
                    b.Append(c);
                }
            }
            int lenS = i - @this.StartIndex;

            if (b != null)
            {
                content = b.ToString();
            }
            else
            {
                content = @this.Text.Substring(@this.StartIndex + 1, lenS - 2);
            }
            return(@this.UncheckedMove(lenS));
        }