Exemple #1
0
        private static void ExtractParameters(string s, string[] delimiterStrings)
        {
            string startDelimiterString = delimiterStrings[0];
            string endDelimiterString   = delimiterStrings.Length > 1 ? delimiterStrings[1] : delimiterStrings[0];

            AddOrUpdateDelimiterInDictionary(s, endDelimiterString, startDelimiterString);
            _currentString = s;

            DelimiterIndices delimiterIndices = FindAllIndices(s, startDelimiterString, endDelimiterString);

            List <int> startCharIndices = delimiterIndices.StartDelimitersIndices;
            List <int> endCharIndices   = delimiterIndices.EndDelimitersIndices;

            if ((startCharIndices.Count + endCharIndices.Count) % 2 != 0)
            {
                throw new FormatException("The number of delimitersString should be even.");
            }

            string unescapedString = UnescapeDelimiters(s);

            List <Tuple <int, int> > indices = GetPairIndices(startCharIndices, endCharIndices).ToList();

            foreach (var va in indices)
            {
                string param = unescapedString.Substring(va.Item1,
                                                         va.Item2 - endDelimiterString.Length - va.Item1 +
                                                         startDelimiterString.Length);
                AddPair(s, new Pair <string, string> {
                    Key = param
                });
            }
        }
Exemple #2
0
        private static DelimiterIndices FindAllIndices(string stringToParse, string startDelimiterString,
                                                       string endDelimiterString)
        {
            var escapedDelimitersIndices = new List <int>();
            var delimiterIndices         = new List <List <int> >();

            int i; // current stringToParse index.
            int j;
            // delimiterString index : To compare chars of stringToParse and chars of delimiterString (char by char)
            int k; // escaped delimiter index : to check if a delimiterString is escaped (ex : $* -> $*$*)

            var delimiters = new List <string>();

            delimiterIndices.Add(new List <int>());
            delimiterIndices.Add(new List <int>());

            delimiters.Add(startDelimiterString);

            if (!String.IsNullOrWhiteSpace(endDelimiterString) && startDelimiterString != endDelimiterString)
            {
                delimiters.Add(endDelimiterString);
            }

            for (int m = 0; m < delimiters.Count; m++)
            {
                for (i = 0; i < stringToParse.Length; i++)
                {
                    // Check if current char equals to the first current char of delimiterString
                    if (stringToParse[i] == delimiters[m][0])
                    {
                        bool isAnEscapeDelimiter = true;

                        if (delimiters[m].Length == 1)
                        {
                            if (i + 1 < stringToParse.Length)
                            {
                                if (stringToParse[i + 1] == delimiters[m][0] && i > 0)
                                {
                                    i++;
                                }
                                else
                                {
                                    delimiterIndices[m].Add(i);
                                }
                            }
                            else
                            {
                                delimiterIndices[m].Add(i);
                            }
                        }
                        else
                        {
                            for (j = 1; j < delimiters[m].Length; j++)
                            {
                                // Check if all chars match
                                if (i + j > stringToParse.Length - 1)
                                {
                                    break;
                                }
                                if (stringToParse[i + j] == delimiters[m][j])
                                {
                                    if (j != delimiters[m].Length - 1)
                                    {
                                        continue;
                                    }

                                    // if i == 0, automatically consider as a delimiter.
                                    if (i == 0)
                                    {
                                        delimiterIndices[m].Add(i);
                                        break;
                                    }

                                    // Last iteration, Test if it is an escaped delimiter
                                    for (k = j; k <= delimiters[m].Length; k++)
                                    {
                                        int nextCharIndex      = i + k + 1;
                                        int pastFirstCharIndex = i + k - (delimiters[m].Length - 1);

                                        if (nextCharIndex < stringToParse.Length &&
                                            stringToParse[nextCharIndex] == stringToParse[pastFirstCharIndex])
                                        {
                                            continue;
                                        }

                                        // Not an escape delimiter
                                        isAnEscapeDelimiter = false;
                                        break;
                                    }

                                    if (!isAnEscapeDelimiter)
                                    {
                                        delimiterIndices[m].Add(i);
                                        break;
                                    }

                                    // Advancing index.
                                    escapedDelimitersIndices.Add(i);
                                    i += startDelimiterString.Length * 2 - 1; // -1 : to anticipate the loop increment
                                }
                            }
                        }
                    }
                }
            }
            _delimiterIndices = new DelimiterIndices
            {
                StartDelimitersIndices   = delimiterIndices[0],
                EndDelimitersIndices     = delimiterIndices[1],
                EscapedDelimitersIndices = escapedDelimitersIndices
            };

            return(_delimiterIndices);
        }
        private static DelimiterIndices FindAllIndices(string stringToParse, string startDelimiterString,
                                                       string endDelimiterString)
        {
            var escapedDelimitersIndices = new List<int>();
            var delimiterIndices = new List<List<int>>();

            int i; // current stringToParse index.
            int j;
                // delimiterString index : To compare chars of stringToParse and chars of delimiterString (char by char)
            int k; // escaped delimiter index : to check if a delimiterString is escaped (ex : $* -> $*$*)

            var delimiters = new List<string>();

            delimiterIndices.Add(new List<int>());
            delimiterIndices.Add(new List<int>());

            delimiters.Add(startDelimiterString);

            if (!String.IsNullOrWhiteSpace(endDelimiterString) && startDelimiterString != endDelimiterString)
                delimiters.Add(endDelimiterString);

            for (int m = 0; m < delimiters.Count; m++)
            {
                for (i = 0; i < stringToParse.Length; i++)
                {
                    // Check if current char equals to the first current char of delimiterString
                    if (stringToParse[i] == delimiters[m][0])
                    {
                        bool isAnEscapeDelimiter = true;

                        if (delimiters[m].Length == 1)
                        {
                            if (i + 1 < stringToParse.Length)
                            {
                                if (stringToParse[i + 1] == delimiters[m][0] && i > 0)
                                    i++;
                                else
                                    delimiterIndices[m].Add(i);
                            }
                            else
                            {
                                delimiterIndices[m].Add(i);
                            }
                        }
                        else
                        {
                            for (j = 1; j < delimiters[m].Length; j++)
                            {
                                // Check if all chars match
                                if (i + j > stringToParse.Length - 1) break;
                                if (stringToParse[i + j] == delimiters[m][j])
                                {
                                    if (j != delimiters[m].Length - 1)
                                        continue;

                                    // if i == 0, automatically consider as a delimiter.
                                    if (i == 0)
                                    {
                                        delimiterIndices[m].Add(i);
                                        break;
                                    }

                                    // Last iteration, Test if it is an escaped delimiter
                                    for (k = j; k <= delimiters[m].Length; k++)
                                    {
                                        int nextCharIndex = i + k + 1;
                                        int pastFirstCharIndex = i + k - (delimiters[m].Length - 1);

                                        if (nextCharIndex < stringToParse.Length &&
                                            stringToParse[nextCharIndex] == stringToParse[pastFirstCharIndex])
                                            continue;

                                        // Not an escape delimiter
                                        isAnEscapeDelimiter = false;
                                        break;
                                    }

                                    if (!isAnEscapeDelimiter)
                                    {
                                        delimiterIndices[m].Add(i);
                                        break;
                                    }

                                    // Advancing index.
                                    escapedDelimitersIndices.Add(i);
                                    i += startDelimiterString.Length*2 - 1; // -1 : to anticipate the loop increment
                                }
                            }
                        }
                    }
                }
            }
            _delimiterIndices = new DelimiterIndices
                                    {
                                        StartDelimitersIndices = delimiterIndices[0],
                                        EndDelimitersIndices = delimiterIndices[1],
                                        EscapedDelimitersIndices = escapedDelimitersIndices
                                    };

            return _delimiterIndices;
        }