public static bool TryWriteNewEndToken(ModelDelta token, ref TableStreamRun tableStream)
 {
     if (!(tableStream.endStream is EndCodeStreamStrategy strategy))
     {
         return(false);
     }
     for (int i = 0; i < strategy.EndCode.Count; i++)
     {
         token.ChangeData(tableStream.model, tableStream.Start + i, strategy.EndCode[i]);
     }
     tableStream = (TableStreamRun)tableStream.Clone(tableStream.PointerSources);
     return(true);
 }
 public static bool DataMatches(IDataModel model, TableStreamRun tableStream, int elementsCount)
 {
     for (int i = 0; i < elementsCount; i++)
     {
         int subStart = tableStream.Start + tableStream.ElementLength * i;
         for (int j = 0; j < tableStream.ElementContent.Count; j++)
         {
             if (!ArrayRun.DataMatchesSegmentFormat(model, subStart, tableStream.ElementContent[j], default, tableStream.ElementContent, i))
             {
                 return(false);
             }
             subStart += tableStream.ElementContent[j].Length;
         }
     }
     return(tableStream.ElementCount > 0 || tableStream.AllowsZeroElements);
 }
        public static bool TryParseTableStream(IDataModel model, int start, SortedSpan <int> sources, string fieldName, string content, IReadOnlyList <ArrayRunElementSegment> sourceSegments, out TableStreamRun tableStream)
        {
            tableStream = null;

            if (content.Length < 4 || content[0] != '[')
            {
                return(false);
            }
            var close = content.LastIndexOf(']');

            if (close == -1)
            {
                return(false);
            }
            try {
                var segmentContent = content.Substring(1, close - 1);
                var segments       = ArrayRun.ParseSegments(segmentContent, model);
                var endStream      = ParseEndStream(model, fieldName, content.Substring(close + 1), segments, sourceSegments);
                if (endStream == null)
                {
                    return(false);
                }
                if (segments.Count == 0)
                {
                    return(false);
                }
                tableStream = new TableStreamRun(model, start, sources, content, segments, endStream);
            } catch (ArrayRunParseException) {
                return(false);
            }

            if (start < 0)
            {
                return(false);        // not a valid data location, so the data can't possibly be valid
            }
            if (model.GetUnmappedSourcesToAnchor(fieldName).Count > 0)
            {
                // we're pasting this format and something else is expecting it. Don't expect the content to match yet.
                return(tableStream.ElementCount > 0);
            }

            // if the first 90% matches, we don't need to check the last 10%
            var mostElementsCount = (int)Math.Ceiling(tableStream.ElementCount * .85);

            return(DataMatches(model, tableStream, mostElementsCount));
        }