aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/TextChange.cs
blob: 6533a9b4606c5aa1ed2d29c94adc716df267f854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2013-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Collections.Generic;
using System.Text;

namespace Crow.Text
{
    public struct TextChange
    {
        public readonly int Start;
        public readonly int Length;
        public string ChangedText;

        public int End => Start + Length;
        public int End2 => End + CharDiff;

		public int CharDiff => string.IsNullOrEmpty (ChangedText) ? - Length : ChangedText.Length - Length;
        public bool IsEmpty => string.IsNullOrEmpty (ChangedText) && Length == 0;
        public bool HasNewText => !string.IsNullOrEmpty (ChangedText);
        
        public bool HasNoEffect(ReadOnlySpan<char> src)
            => src.Slice(Start, Length).Equals(ChangedText, StringComparison.Ordinal);

        public TextChange (int position, int length, string changedText = null) {
            Start = position;
            Length = length;
            ChangedText = changedText;
        }
        public TextChange (int position, int length, ReadOnlySpan<char> changedText) {
            Start = position;
            Length = length;
            ChangedText = changedText.ToString();
        }
        public TextChange Inverse (ReadOnlySpan<char> src)
            => new TextChange (Start, string.IsNullOrEmpty (ChangedText) ? 0 : ChangedText.Length,
                Length == 0 ? "" : src.Slice (Start, Length).ToString ());
        public override string ToString() => $"{Start},{ChangedText}";
    }
}