Exemplo n.º 1
0
        /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
        public bool Equals(CompoundExpression other)
        {
            if (ReferenceEquals(_components, other._components))
            {
                return(true);
            }
            if (_components is null || other._components is null)
            {
                return(false);
            }
            if (_components.Length != other._components.Length)
            {
                return(false);
            }

            for (var i = 0; i < _components.Length; i++)
            {
                if (!((IEquatable <ExpressionComponent>)_components[i]).Equals(other._components[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the string representation of a expression to its structured equivalent.
        /// A return value indicates whether the conversion succeeded.
        /// </summary>
        /// <param name="s">A string containing a expression to convert.</param>
        /// <param name="result">
        /// When this method returns, contains the structured equivalent of the expression contained in <paramref name="s"/>,
        /// if the conversion succeeded, or default if the conversion failed. The conversion fails if the <paramref name="s"/> parameter
        /// is not in a format compliant with the Open API expression specification. This parameter is passed uninitialized;
        /// any value originally supplied in result will be overwritten.
        /// </param>
        /// <returns><c>true</c> if s was converted successfully; otherwise, <c>false</c>.</returns>
        public static bool TryParse(string s, out CompoundExpression result)
        {
            if (string.IsNullOrEmpty(s))
            {
                result = default;
                return(s != null);
            }

            var currentComponent = new StringBuilder();
            var components       = new List <ExpressionComponent>();
            var lastIndex        = s.Length - 1;

            for (var i = 0; i < s.Length; i++)
            {
                var c = s[i];
                if (c == '{' || c == '}')
                {
                    if (i == lastIndex)
                    {
                        result = default;
                        return(false);
                    }

                    i++;
                    if (s[i] == c)
                    {
                        currentComponent.Append(c);
                    }
                    else
                    {
                        if (c != '{' || !FieldExpression.TryParse(s, out var field, ref i, true) || i >= s.Length || s[i] != '}')
                        {
                            result = default;
                            return(false);
                        }

                        if (currentComponent.Length != 0)
                        {
                            components.Add(new LiteralExpression(currentComponent.ToString()));
                            currentComponent.Clear();
                        }

                        components.Add(field);
                    }
                }
                else
                {
                    currentComponent.Append(c);
                }
            }

            if (currentComponent.Length != 0)
            {
                components.Add(new LiteralExpression(currentComponent.ToString()));
                currentComponent.Clear();
            }

            result = new CompoundExpression(components.ToArray());
            return(true);
        }