Пример #1
0
        // --------------------------- Dequote ------------------------------
        public static string Dequote(string InText, QuoteEncapsulation InQem)
        {
            int           Lx        = InText.Length + 2; // 2=quote chars.
            char          QuoteChar = InText[0];
            StringBuilder sb        = new StringBuilder(Lx);
            int           Ix        = 0;
            int           EndIx     = InText.Length - 2;

            while (true)
            {
                ++Ix;
                if (Ix > EndIx)
                {
                    break;
                }
                char ch1 = InText[Ix];

                // using the escape method to enclose quote chars. This means the escape char
                // is used to encapsulate other special characters in the quoted string.
                // todo: dequote using "QuotedStringTraits" rules.
                if ((ch1 == '\\') &&
                    (InQem == QuoteEncapsulation.Escape) &&
                    (Ix < (EndIx - 1)))
                {
                    sb.Append(MaterializeEscapeChar(InText, Ix));
                    ++Ix;
                }

                // quote char enquoted using the "double the char" method.
                else if ((ch1 == QuoteChar) &&
                         (InQem == QuoteEncapsulation.Double) &&
                         (AcCommon.PullChar(InText, Ix + 1) == QuoteChar))
                {
                    sb.Append(ch1);
                    ++Ix;
                }

                // any other character.  append to result string.
                else
                {
                    sb.Append(ch1);
                }
            }
            return(sb.ToString( ));
        }
Пример #2
0
        // --------------------------- ScanCloseQuote ------------------------------
        public static int ScanCloseQuote(
            string InString, int InBx, QuoteEncapsulation InQem)
        {
            char QuoteChar = InString[InBx];
            int  cloqIx    = -1;

            for (int Ix = InBx + 1; Ix < InString.Length; ++Ix)
            {
                char ch1 = InString[Ix];

                // using the escape method to enclose quote chars. This means the escape char
                // is used to encapsulate other special characters in the quoted string.
                // todo: dequote using "QuotedStringTraits" rules.
                if ((ch1 == '\\') &&
                    (InQem == QuoteEncapsulation.Escape) &&
                    (Ix < (InString.Length - 1)))
                {
                    ++Ix;
                }

                // quote char enquoted using the "double the char" method.
                else if ((ch1 == QuoteChar) &&
                         (InQem == QuoteEncapsulation.Double) &&
                         (AcCommon.PullChar(InString, Ix + 1) == QuoteChar))
                {
                    ++Ix;
                }

                // found the closing quote char.
                else if (ch1 == QuoteChar)
                {
                    cloqIx = Ix;
                    break;
                }
            }

            return(cloqIx);
        }
Пример #3
0
        /// <summary>
        /// Examine the string for its WordClassification content.
        /// </summary>
        /// <param name="InWord"></param>
        /// <param name="InTraits"></param>
        /// <returns></returns>
        public static CharObjectPair CalcWordClassification(
            string InWord, TextTraits InTraits)
        {
            int Fx = 0, Ix = 0;
            WordClassification wc = WordClassification.None;
            char braceChar        = ' ';
            char ch1              = AcCommon.PullChar(InWord, 0);
            int  Ex               = InWord.Length - 1;

            // is quoted. the word runs to the closing quote.
            if (Scanner.IsOpenQuoteChar(ch1) == true)
            {
                Ix = Scanner.ScanCloseQuote(InWord, 0, InTraits.QuoteEncapsulation);
                if (Ix == Ex)
                {
                    wc = WordClassification.Quoted;
                }
                else
                {
                    wc = WordClassification.MixedText;
                }
            }

            // check if the string is a Braced or NameBraced word.
            if (wc == WordClassification.None)
            {
                char[] combo = AcCommon.Concat(InTraits.DelimChars, InTraits.BraceChars);
                Scanner.ScanCharResults results = Scanner.ScanEqual(InWord, 0, combo);
                Fx  = results.ResultPos;
                ch1 = results.ResultChar;

                // found a brace char
                if ((InTraits.IsOpenBraceChar(ch1) == true) &&
                    (InTraits.IsDelimChar(ch1) == false))
                {
                    Ix = Scanner.ScanCloseBrace(InWord, Fx);
                    if (Ix == Ex)
                    {
                        braceChar = ch1;
                        if (Fx == 0)
                        {
                            wc = WordClassification.Braced;
                        }
                        else
                        {
                            wc = WordClassification.NameBraced;
                        }
                    }
                }
            }

            // word is all delimeter.
            if (wc == WordClassification.None)
            {
                Fx = Scanner.ScanNotEqual(InWord, 0, InTraits.DelimChars).a;
                if (Fx >= 0)
                {
                    wc = WordClassification.Delimeter;
                }
            }

            // check if a numeric string.
            if (wc == WordClassification.None)
            {
                char[] digitChars =
                    new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-' };
                Fx = Scanner.ScanNotEqual(InWord, 0, digitChars).a;
                if (Fx == -1)
                {
                    wc = WordClassification.Numeric;
                    try
                    {
                        double vx = double.Parse(InWord);
                    }
                    catch (Exception)
                    {
                        wc = WordClassification.None;
                    }
                }
            }

            // any delim chars in the string.  if not, the string is a name.  otherwise, it is
            // mixed.
            if (wc == WordClassification.None)
            {
                Fx = Scanner.ScanEqual(InWord, 0, InTraits.DelimChars).a;
                if (Fx == -1)
                {
                    wc = WordClassification.Name;
                }
                else
                {
                    wc = WordClassification.MixedText;
                }
            }

            return(new CharObjectPair(braceChar, wc));
        }