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());
        }
        public TextAssetHelper(IEnumerable <string> rowSplitStrings = null, IEnumerable <string> colSplitStrings = null)
        {
            int comp(string a, string b) => b.Length.CompareTo(a.Length);

            List <string> tmpList = new List <string>();

            // row split strings
            tmpList.AddRange(rowSplitStrings?.ToArray() ?? new string[0]);
            tmpList.Sort(comp);
            RowSplitStrings = tmpList.ToArray();

            // col split strings
            tmpList.Clear();
            tmpList.AddRange(colSplitStrings?.ToArray() ?? new string[0]);
            tmpList.Sort(comp);
            ColSplitStrings = tmpList.ToArray();

            // invalid col strings
            tmpList.Clear();
            tmpList.AddRange(RowSplitStrings);
            tmpList.AddRange(ColSplitStrings);
            tmpList.Sort(comp);
            InvalidColStrings = tmpList.ToArray();

            Enabled = ColSplitStrings.Any() && RowSplitStrings.Any();
        }
Beispiel #3
0
        public TextAssetTableHelper(IEnumerable <string> rowSplitStrings = null,
                                    IEnumerable <string> colSplitStrings = null, Encoding encoding = null)
        {
            TextAssetEncoding = encoding ?? Encoding.UTF8;

            int Comp(string a, string b)
            {
                return(b.Length.CompareTo(a.Length));
            }

            var tmpList = new List <string>();

            // row split strings
            tmpList.AddRange(rowSplitStrings?.ToArray() ?? new string[0]);
            tmpList.Sort(Comp);
            _rowSplitStrings = tmpList.ToArray();

            // col split strings
            tmpList.Clear();
            tmpList.AddRange(colSplitStrings?.ToArray() ?? new string[0]);
            tmpList.Sort(Comp);
            _colSplitStrings = tmpList.ToArray();

            // invalid col strings
            tmpList.Clear();
            tmpList.AddRange(RowSplitStrings);
            tmpList.AddRange(ColSplitStrings);
            tmpList.Sort(Comp);
            _invalidColStrings = tmpList.ToArray();

            Enabled = ColSplitStrings.Any() && RowSplitStrings.Any();
        }
        public IEnumerable <string> SplitRowToCells(string row)
        {
#if DEBUG
            if (!IsTableRow(row))
            {
                throw new ArgumentException("row does not contain a table row)");
            }
#endif
            return(row.Split(ColSplitStrings.ToArray(), StringSplitOptions.None));
        }
Beispiel #5
0
        public bool IsTable(string table)
        {
            // possible table is only 1 row, but it needs to have at least 1 column break
            if (string.IsNullOrEmpty(table))
            {
                return(false);
            }

            var row = table.Split(_rowSplitStrings, StringSplitOptions.None).FirstOrDefault();

            return(row != null && ColSplitStrings.Any(row.Contains));
        }
Beispiel #6
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);
        }
Beispiel #7
0
 public bool IsTableRow(string row)
 {
     return(ColSplitStrings.Any(row.Contains));
 }
Beispiel #8
0
 public IEnumerable <string> SplitRowToCells(string row)
 {
     Debug.Assert(IsTableRow(row), "row does not contain a table row");
     return(row.Split(ColSplitStrings.ToArray(), StringSplitOptions.None));
 }