Exemple #1
0
        public void AppendString(StringBuilder builder, string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return;
            }

            for (int i = 0; i < str.Length; i++)
            {
                char ch = str[i];
                if (WordSplitterConsts.IsEmptyChar(ch) || DataConvertorConsts.IsWordChar(ch))
                {
                    builder.Append(WordSplitterConsts.EscapeChar);
                }
                builder.Append(ch);
            }
        }
        public static void Split(string source, string content, char[] wordChars, Action <Word> processor)
        {
            if (content == null)
            {
                return;
            }

            StringBuilder current        = new StringBuilder(1024);
            bool          currentIsEmpty = true;
            int           currentLine    = 0;
            int           currentColumn  = 0;

            Action <int, int> AddCurrent = (int lineZeroBase, int columnZeroBase) => {
                string word = current.ToString().Trim();
                if (!string.IsNullOrEmpty(word))
                {
                    processor(new Word(source, currentLine, currentColumn, word));
                }
                currentIsEmpty = true;
                currentLine    = lineZeroBase + 1;
                currentColumn  = columnZeroBase + 1;
                current.Length = 0;
            };

            Action <int, int, char> AppendToCurrent = (int lineZeroBase, int columnZeroBase, char ch) => {
                current.Append(ch);
                if (currentIsEmpty)
                {
                    if (!WordSplitterConsts.IsEmptyChar(ch))
                    {
                        currentIsEmpty = false;
                        currentLine    = lineZeroBase + 1;
                        currentColumn  = columnZeroBase + 1;
                    }
                }
            };

            bool escaped  = false;
            bool enclosed = false;

            string[] lines = content.Split(WordSplitterConsts.LineSeparators);
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                for (int j = 0; j < line.Length; j++)
                {
                    char ch = line[j];

                    if (escaped)
                    {
                        AppendToCurrent(i, j, ch);
                        escaped = false;
                    }
                    else
                    {
                        if (ch == WordSplitterConsts.EscapeChar)
                        {
                            escaped = true;
                        }
                        else if (enclosed)
                        {
                            if (ch == WordSplitterConsts.EncloseEndChar)
                            {
                                AddCurrent(i, j);
                                enclosed = false;
                            }
                            else
                            {
                                AppendToCurrent(i, j, ch);
                            }
                        }
                        else if (ch == WordSplitterConsts.EncloseBeginChar)
                        {
                            AddCurrent(i, j);
                            enclosed = true;
                        }
                        else if (WordSplitterConsts.IsWordSeparator(ch))
                        {
                            AddCurrent(i, j);
                        }
                        else if (wordChars != null && IsCharWord(wordChars, ch))
                        {
                            AddCurrent(i, j);
                            AppendToCurrent(i, j, ch);
                            AddCurrent(i, j);
                        }
                        else
                        {
                            AppendToCurrent(i, j, ch);
                        }
                    }
                }
                if (!enclosed)
                {
                    AddCurrent(i, -1);
                }
            }
            AddCurrent(-1, -1);
        }