Example #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(";");
        }
        private static void WriteIgnoreCaseBDD(StreamWriter sw)
        {
            sw.WriteLine("        /// <summary>Serialized BDD for mapping characters to their case-ignoring equivalence classes in the default (en-US) culture.</summary>");

            var solver = new CharSetSolver();
            List <EquivalenceClass> ignoreCaseEquivalenceClasses = ComputeIgnoreCaseEquivalenceClasses(solver, new CultureInfo(DefaultCultureName));
            BDD ignorecase = solver.False;

            foreach (EquivalenceClass ec in ignoreCaseEquivalenceClasses)
            {
                // Create the Cartesian product of ec._set with itself
                BDD crossproduct = solver.And(solver.ShiftLeft(ec._set, 16), ec._set);
                // Add the product into the overall lookup table
                ignorecase = solver.Or(ignorecase, crossproduct);
            }

            sw.Write("        public static readonly byte[] IgnoreCaseEnUsSerializedBDD = ");
            GeneratorHelper.WriteByteArrayInitSyntax(sw, ignorecase.SerializeToBytes());
            sw.WriteLine(";");
        }