Esempio n. 1
0
        private static bool ExtractValue(string asStringValue,
                                         string name,
                                         int position,
                                         out string value,
                                         out int newPosition,
                                         out string errorDescription)
        {
            errorDescription = "";
            bool result = false;

            int      asStringValueLength = asStringValue.Length;
            InQuotes inQuotes            = InQuotes.Out;

            StringBuilder valueBldr = new StringBuilder(40);

            int i = position;

            while (!result && errorDescription == "")
            {
                switch (inQuotes)
                {
                case InQuotes.Out:
                    if (i >= asStringValueLength)
                    {
                        errorDescription = string.Format(Properties.Resources.DeclaredParametersFormatter_ExtractValue_ParameterIsMissingValue, name, i);
                    }
                    else
                    {
                        if (!char.IsWhiteSpace(asStringValue[i]))
                        {
                            if (asStringValue[i] != QuoteChar)
                            {
                                errorDescription = string.Format(Properties.Resources.DeclaredParametersFormatter_ExtractValue_ValueForParameterIsNotQuoted, name, i);
                            }
                            else
                            {
                                inQuotes = InQuotes.In;
                            }
                        }
                    }
                    break;

                case InQuotes.In:
                    if (i >= asStringValueLength)
                    {
                        errorDescription = string.Format(Properties.Resources.DeclaredParametersFormatter_ExtractValue_ValueForParameterIsMissingClosingQuote, name, i);
                    }
                    else
                    {
                        if (asStringValue[i] != QuoteChar)
                        {
                            valueBldr.Append(asStringValue[i]);
                        }
                        else
                        {
                            inQuotes = InQuotes.InCheckingStuffed;
                        }
                    }
                    break;

                case InQuotes.InCheckingStuffed:
                    if (i >= asStringValueLength)
                    {
                        result = true;
                    }
                    else
                    {
                        if (asStringValue[i] != QuoteChar)
                        {
                            result = true;
                        }
                        else
                        {
                            valueBldr.Append(QuoteChar);
                            inQuotes = InQuotes.In;
                        }
                    }
                    break;

                default:
                    throw FtInternalException.Create(InternalError.DeclaredParametersFormatter_ExtractValue_UnknownInQuotes, inQuotes.ToString());
                }
            }

            value       = valueBldr.ToString();
            newPosition = i;

            return(result);
        }
Esempio n. 2
0
        public static bool TryParse(string value, bool strict, out string[] resultArray, out string errorDescription)
        {
            string[] strArray = null;
            errorDescription = "";
            int      count               = 0;
            InQuotes inQuotes            = InQuotes.NotIn;
            bool     waitingForDelimiter = false;
            int      startPos            = 0;
            int      valueLength         = value.Length;

            for (int i = 0; i < valueLength; i++)
            {
                char valueChar = value[i];
                if (waitingForDelimiter)
                {
                    if (valueChar == DelimiterChar)
                    {
                        waitingForDelimiter = false;
                        startPos            = i + 1;
                    }
                    else
                    {
                        if (strict && (!Char.IsWhiteSpace(valueChar)))
                        {
                            errorDescription = string.Format(Properties.Resources.FtCommaText_TryParse_UnexpectedCharAfterQuotedElement, i);
                            break;
                        }
                    }
                }
                else
                {
                    switch (inQuotes)
                    {
                    case InQuotes.NotIn:
                        switch (valueChar)
                        {
                        case DelimiterChar:
                            AddElement(value, ref strArray, ref count, startPos, i, false);
                            startPos = i + 1;
                            break;

                        case QuoteChar:
                            if (value.Substring(startPos, i - startPos).Trim() == "")
                            {
                                inQuotes = InQuotes.In;
                                startPos = i + 1;
                            }
                            break;
                        }
                        break;

                    case InQuotes.In:
                        if (valueChar == QuoteChar)
                        {
                            inQuotes = InQuotes.CheckingStuffed;
                        }
                        break;

                    case InQuotes.CheckingStuffed:
                        switch (valueChar)
                        {
                        case QuoteChar:
                            inQuotes = InQuotes.In;
                            break;

                        case DelimiterChar:
                            inQuotes = InQuotes.NotIn;
                            AddElement(value, ref strArray, ref count, startPos, i - 1, true);
                            startPos = i + 1;
                            break;

                        default:
                            inQuotes = InQuotes.NotIn;
                            AddElement(value, ref strArray, ref count, startPos, i - 1, true);
                            waitingForDelimiter = true;
                            if (strict && (!(Char.IsWhiteSpace(valueChar))))
                            {
                                errorDescription = string.Format(Properties.Resources.FtCommaText_TryParse_UnexpectedCharAfterQuotedElement, i);
                                break;
                            }
                            break;
                        }
                        break;

                    default:
                        throw FtInternalException.Create(InternalError.FtCommaText_TryParse_UnsupportedInQuotes1, inQuotes.ToString());
                    }
                }
            }

            if ((errorDescription == "") && (!waitingForDelimiter))
            {
                switch (inQuotes)
                {
                case InQuotes.NotIn:
                    if (startPos <= valueLength)
                    {
                        AddElement(value, ref strArray, ref count, startPos, valueLength, false);
                    }
                    break;

                case InQuotes.In:
                    if (strict)
                    {
                        errorDescription = Properties.Resources.FtCommaText_TryParse_QuotesNotClosedInLastElement;
                    }
                    else
                    {
                        AddElement(value, ref strArray, ref count, startPos, valueLength, true);
                    }
                    break;

                case InQuotes.CheckingStuffed:
                    AddElement(value, ref strArray, ref count, startPos, valueLength - 1, true);
                    break;

                default:
                    throw FtInternalException.Create(InternalError.FtCommaText_TryParse_UnsupportedInQuotes2, inQuotes.ToString());
                }
            }

            resultArray = new string[count];
            Array.Copy(strArray, resultArray, count);
            return(errorDescription == "");
        }