예제 #1
0
        public SegmentedDocument Deserialise(string input)
        {
            var    segments          = new List <DocumentSegment>();
            string outputString      = "";
            int    lastMatchPosition = 0;
            int    charactersAdded   = 0;

            var doc = new SegmentedDocument(new TextDocument());

            foreach (Match match in segmentRegex.Matches(input))
            {
                outputString += input.Substring(lastMatchPosition, match.Index - lastMatchPosition);
                string className = match.Groups["class"].Value.ToLower();

                if (!constructorDictionary.ContainsKey(className))
                {
                    outputString     += match.Value;
                    lastMatchPosition = match.Index + match.Length;
                    continue;
                }

                DocumentSegment segment = constructorDictionary[className](match.Index + charactersAdded, match.Groups["text"].Value);

                charactersAdded += segment.Text.Length - match.Length;

                segments.Add(segment);

                outputString     += segment.Text;
                lastMatchPosition = match.Index + match.Length;
            }

            outputString += input.Substring(lastMatchPosition);
            doc.Load(outputString, segments);
            return(doc);
        }
예제 #2
0
 protected virtual DocumentSegment Clone(DocumentSegment segment)
 {
     segment.EndOffset   = EndOffset;
     segment.StartOffset = StartOffset;
     segment.Length      = Length;
     segment.Text        = Text;
     return(segment);
 }
예제 #3
0
        public void Replace(ref int offset, ref int replacementLength, SegmentedDocument text)
        {
            InAutomaticChange = true;

            var affectedSegments = new ReadOnlyCollection <DocumentSegment>(new List <DocumentSegment>());

            affectedSegments = Segments.FindOverlappingSegments(offset, replacementLength);

            string textString = text.Text;

            Replace(affectedSegments, ref offset, ref replacementLength, ref textString);

            foreach (DocumentSegment segment in text.Segments)
            {
                DocumentSegment newsegment = segment.Clone();
                newsegment.StartOffset = newsegment.StartOffset + offset;
                newsegment.EndOffset   = newsegment.StartOffset + newsegment.Length;
                Segments.Add(newsegment);
            }
            InAutomaticChange = false;
            lastChange        = null;
        }