コード例 #1
0
        public string ProcessTable(TextAsset textAsset, Func <string, string> columnTransform, out TableResult tableResult)
        {
            tableResult = new TableResult();
            string        colJoin = ColSplitStrings.First();
            StringBuilder result  = new StringBuilder(textAsset.text.Length * 2);
            //foreach (string row in EnumerateRows(textAsset))
            var rows = SplitTableToRows(textAsset);

            if (rows == null)
            {
                return(null);
            }

            foreach (string row in rows)
            {
                tableResult.Rows++;
                int colCount = 0;

                bool rowUpdated = false;
                //foreach (string col in EnumerateCols(row))
                foreach (string col in SplitRowToCells(row))
                {
                    colCount++;
                    string newCol = columnTransform(col);
                    if (newCol != null && col != newCol)
                    {
                        tableResult.CellsUpdated++;
                        rowUpdated = true;
                        foreach (string invalid in InvalidColStrings)
                        {
                            newCol = newCol.Replace(invalid, " ");
                        }
                        result.Append(newCol);
                    }
                    else
                    {
                        result.Append(col);
                    }
                    result.Append(colJoin);
                }
                // row complete
                // remove trailing colSplit
                result.Length -= colJoin.Length;
                result.Append(Environment.NewLine);
                if (rowUpdated)
                {
                    tableResult.RowsUpdated++;
                }
                tableResult.Cols = Math.Max(tableResult.Cols, colCount);
            }
            // table complete
            // remove last newline
            result.Length -= Environment.NewLine.Length;

            if (!tableResult.Updated)
            {
                return(textAsset.text);
            }
            return(result.ToString());
        }
コード例 #2
0
        public string ProcessTable(TextAsset textAsset, CellTransform columnTransform, out TextAssetTableResult tableResult)
        {
            tableResult = new TextAssetTableResult();
            var colJoin    = ColSplitStrings.First();
            var result     = new StringBuilder(textAsset.text.Length * 2);
            var colBuilder = new StringBuilder();

            bool ColumnTransformWrapper(int rowIndex, int colIndex, string col, out string newCol)
            {
                if (!columnTransform(rowIndex, colIndex, col, out newCol))
                {
                    return(false);
                }

                colBuilder.Length = 0;
                colBuilder.Append(newCol);
                colBuilder = InvalidColStrings.Aggregate(colBuilder,
                                                         (current, invalid) => current.Replace(invalid, " "));

                newCol = colBuilder.ToString();
                return(true);
            }

            var table = SplitTable(textAsset);

            tableResult.Rows = table.Length;
            tableResult.Cols = 0;
            for (var r = 0; r < table.Length; r++)
            {
                var rowUpdated = false;
                tableResult.Cols = Math.Max(tableResult.Cols, table[r].Length);

                for (var c = 0; c < table[r].Length; c++)
                {
                    var col = table[r][c];
                    tableResult.Cols = Math.Max(tableResult.Cols, col.Length);
                    if (ColumnTransformWrapper(r, c, col, out var newCol))
                    {
                        tableResult.CellsUpdated++;
                        rowUpdated = true;
                        result.Append(newCol);
                    }
                    else
                    {
                        result.Append(col);
                    }
                    result.Append(colJoin);
                }

                // row complete
                // remove trailing colSplit
                result.Length -= colJoin.Length;
                result.Append(Environment.NewLine);
                if (rowUpdated)
                {
                    tableResult.RowsUpdated++;
                }
            }

            // table complete
            // remove last newline
            result.Length -= Environment.NewLine.Length;

            return(tableResult.Updated ? result.ToString() : textAsset.text);
        }