public BrotliDictionary(IDictionaryFormat format, IReadOnlyList <WordTransform> transforms, IDictionarySource source) { this.Format = format; this.Transforms = transforms; this.source = source; this.index = new Lazy <BrotliDictionaryIndex>(() => new BrotliDictionaryIndex(this), isThreadSafe: true); }
private void backgroundWorkerLoading_DoWork(object?sender, DoWorkEventArgs e) { BrotliDictionary dict = (BrotliDictionary)e.Argument; IDictionaryFormat format = dict.Format; IReadOnlyList <WordTransform> transforms = dict.Transforms; DataTable table = new DataTable(); DataRowCollection rows = table.Rows; int totalProgressPoints = format.WordLengths.Sum(format.WordCount); int currentProgressPoints = 0; int lastPercentReported = -1; table.Columns.AddRange(new DataColumn[] { colLength, colIndex, colTransform, colText }); table.MinimumCapacity = totalProgressPoints * transforms.Count; table.BeginLoadData(); foreach (int length in format.WordLengths) { for (int word = 0, count = format.WordCount(length); word < count; word++) { if (backgroundWorkerLoading.CancellationPending) { e.Cancel = true; return; } byte[] bytes = dict.ReadRaw(length, word); for (int transform = 0; transform < transforms.Count; transform++) { DataRow row = table.NewRow(); row[colLength] = length; row[colIndex] = word; row[colTransform] = transform; row[colText] = Encoding.UTF8.GetString(transforms[transform].Process(bytes)).Replace(' ', FormatSpace).Replace("\r\n", FormatNewLine).Replace("\n", FormatNewLine).Replace("\r", ""); rows.Add(row); } ++currentProgressPoints; int percentProgress = (int)Math.Floor(100.0 * currentProgressPoints / totalProgressPoints); if (lastPercentReported != percentProgress) { lastPercentReported = percentProgress; backgroundWorkerLoading.ReportProgress(percentProgress, currentProgressPoints * transforms.Count); } } } table.EndLoadData(); e.Result = table.DefaultView; }