Exemplo n.º 1
0
            public string Generate()
            {
                if (subClass != null)
                {
                    subClass.PrepareGeneration();
                    for (int i = 0; i < table.Length; i++)
                    {
                        if (subClass.table [i] != 0)
                        {
                            table [i] = subClass.table [i];
                        }
                    }
                }
                PrepareGeneration();
                var result = new StringBuilder();

                result.Append('[');
                if (negativeGroup)
                {
                    result.Append('^');
                }

                bool hasAllPrintable = true;

                for (int i = 0; i < table.Length; i++)
                {
                    var ch = (char)i;
                    if (ch == ' ' || ch == '\t')
                    {
                        continue;
                    }
                    var cat = char.GetUnicodeCategory(ch);
                    if (cat == UnicodeCategory.Control || cat == UnicodeCategory.LineSeparator)
                    {
                        continue;
                    }
                    if (table [i] == 0)
                    {
                        hasAllPrintable = false;
                    }
                }

                if (hasAllPrintable)
                {
                    for (int i = 0; i < table.Length; i++)
                    {
                        var ch = (char)i;
                        if (ch == ' ' || ch == '\t')
                        {
                            continue;
                        }
                        var cat = char.GetUnicodeCategory(ch);
                        if (cat == UnicodeCategory.Control || cat == UnicodeCategory.LineSeparator)
                        {
                            continue;
                        }
                        table [i] = 0;
                    }
                    result.Append("\\S");
                }

                if (HasRange('a', 'z') && HasRange('A', 'Z'))
                {
                    RemoveRange('a', 'z');
                    RemoveRange('A', 'Z');
                    result.Append("\\w");
                }

                if (HasRange('0', '9'))
                {
                    RemoveRange('0', '9');
                    result.Append("\\d");
                }

                if (Has(" \t\r\n"))
                {
                    result.Append("\\s");
                    table [' ']  = 0;
                    table ['\t'] = 0;
                    table ['\r'] = 0;
                    table ['\n'] = 0;
                    table ['\f'] = 0;
                }

                for (int i = 0; i < table.Length; i++)
                {
                    int cur = table [i];
                    if (cur != 0)
                    {
                        AddChar(result, (char)i);
                        int j = i + 1;
                        for (; j < table.Length; j++)
                        {
                            if (table [j] == 0)
                            {
                                break;
                            }
                        }
                        if (j - i > 3)
                        {
                            result.Append("-");
                            AddChar(result, (char)(j - 1));
                            i = j - 1;
                        }
                    }
                }
                foreach (var grp in unicodeGroups)
                {
                    result.Append("\\p{");
                    result.Append(grp);
                    result.Append("}");
                }
                result.Append(']');
                return(result.ToString());
            }