Ejemplo n.º 1
0
        private static void WriteSerializedBDDs(StreamWriter sw)
        {
            int maxChar = 0xFFFF;
            var catMap  = new Dictionary <UnicodeCategory, Ranges>();

            foreach (UnicodeCategory c in Enum.GetValues <UnicodeCategory>())
            {
                catMap[c] = new Ranges();
            }

            Ranges whitespace      = new Ranges();
            Regex  whitespaceRegex = new(@"\s");

            for (int i = 0; i <= maxChar; i++)
            {
                char ch = (char)i;
                catMap[char.GetUnicodeCategory(ch)].Add(i);

                if (whitespaceRegex.IsMatch(ch.ToString()))
                {
                    whitespace.Add(i);
                }
            }

            //generate bdd reprs for each of the category ranges
            BDD[]         catBDDs = new BDD[catMap.Count];
            CharSetSolver bddb    = new CharSetSolver();

            for (int c = 0; c < catBDDs.Length; c++)
            {
                catBDDs[c] = bddb.CreateBddForIntRanges(catMap[(UnicodeCategory)c].ranges);
            }

            BDD whitespaceBdd = bddb.CreateBddForIntRanges(whitespace.ranges);

            sw.WriteLine("        /// <summary>Serialized BDD representations of all the Unicode categories.</summary>");
            sw.WriteLine("        public static readonly byte[][] AllCategoriesSerializedBDD = new byte[][]");
            sw.WriteLine("        {");
            for (int i = 0; i < catBDDs.Length; i++)
            {
                sw.WriteLine("            // {0}({1}):", (UnicodeCategory)i, i);
                sw.Write("            ");
                GeneratorHelper.WriteByteArrayInitSyntax(sw, catBDDs[i].SerializeToBytes());
                sw.WriteLine(",");
            }
            sw.WriteLine("        };");
            sw.WriteLine();

            sw.WriteLine("        /// <summary>Serialized BDD representation of the set of all whitespace characters.</summary>");
            sw.Write($"        public static readonly byte[] WhitespaceSerializedBDD = ");
            GeneratorHelper.WriteByteArrayInitSyntax(sw, whitespaceBdd.SerializeToBytes());
            sw.WriteLine(";");
        }