示例#1
0
        void tb_TextChangedDelayed(object sender, TextChangedEventArgs e)
        {
            FastColoredTextBox tb   = (sender as FastColoredTextBox);
            string             text = tb.Text;

            //recalculate words, characters, lines
            ThreadPool.QueueUserWorkItem(
                (o) =>
            {
                TextStatistics result = TextProcessor.CountTextStatistics(text);
                lbStatusStrip.Text    = $"Lines: { result.Lines}, Words: { result.Words }, "
                                        + $"Keys: {result.Keys}, Values: {result.Values}, "
                                        + $"All symbols: { text.Length }";
            }
                );
        }
示例#2
0
        public static TextStatistics CountTextStatistics(string text)
        {
            char[] separators = { ' ', ',', '.', '!', '?', ':',  '{',  '}',
                                  ';', ')', '(', '[', ']', '\n', '\r', '"' };

            TextStatistics result = new TextStatistics();

            result.Lines = 0;  result.Words = 0;
            result.Keys  = 0; result.Values = 0;
            bool wasChar = false;

            for (int i = 0; i < text.Length; ++i)
            {
                char c = text[i];
                if (System.Array.IndexOf(separators, c) == -1)
                {
                    wasChar = true;
                }
                else
                {
                    if ('\n' == c)
                    {
                        ++result.Lines;
                    }
                    else if ('{' == c || ',' == c)
                    {
                        int temp = i;
                        if (isSomethingLikeKey(ref temp, text))
                        {
                            temp += 2;
                            int endOfKeyPosition = text.IndexOf('"', temp);
                            if (endOfKeyPosition != -1)
                            {
                                int j = omitWhiteSpaces(endOfKeyPosition, text);
                                if (':' == text[j])
                                {
                                    ++result.Keys;
                                }
                            }
                        }
                    }
                    else if (':' == c)
                    {
                        int j = i;
                        j = omitWhiteSpaces(j, text);
                        c = text[j];

                        if (('"' == c && text.IndexOf('"', j + 1) != -1) ||
                            ('{' == c && text.IndexOf('}', j + 1) != -1) ||
                            ('[' == c && text.IndexOf(']', j + 1) != -1))
                        {
                            ++result.Values;
                        }
                        else if (Char.IsDigit(c))
                        {
                            double dTemp;
                            if (Double.TryParse(
                                    text.Substring(j, text.IndexOf(' ', j) - j),
                                    out dTemp))
                            {
                                ++result.Values;
                            }
                        }
                        else
                        {
                            bool condition = false;
                            try
                            {
                                condition = text.Substring(j, 4) == "null" ||
                                            text.Substring(j, 4) == "true" ||
                                            text.Substring(j, 5) == "false";
                            } catch (ArgumentOutOfRangeException ex)
                            {
                                Console.WriteLine(
                                    "Everything is okay! Just there is no value after :"
                                    );
                                Console.WriteLine(ex.Message);
                            }
                            if (condition)
                            {
                                ++result.Values;
                            }
                        }
                    }

                    if (wasChar)
                    {
                        ++result.Words;
                        wasChar = false;
                    }
                }
            }
            if (wasChar)
            {
                ++result.Words;
            }
            ++result.Lines;
            return(result);
        }