@@ -10,10 +10,12 @@ public class CharSet: IEnumerable<CharRange>
10
10
{
11
11
private readonly List < CharRange > list = new List < CharRange > ( ) ;
12
12
13
+ public int Length { get { return list . Sum ( r => r . Count ( ) ) ; } }
14
+
13
15
public bool Contains ( char c )
14
16
{
15
17
var index = 0 ;
16
- while ( index < list . Count && list [ index ] . From < c )
18
+ while ( index < list . Count && c > list [ index ] . To )
17
19
index ++ ;
18
20
if ( index >= list . Count )
19
21
return false ;
@@ -24,7 +26,7 @@ public bool Contains(char c)
24
26
public bool Contains ( char from , char to )
25
27
{
26
28
var index = 0 ;
27
- while ( index < list . Count && list [ index ] . From < from )
29
+ while ( index < list . Count && from > list [ index ] . To )
28
30
index ++ ;
29
31
if ( index >= list . Count )
30
32
return false ;
@@ -35,7 +37,7 @@ public bool Contains(char from, char to)
35
37
public void Add ( char c )
36
38
{
37
39
var index = 0 ;
38
- while ( index < list . Count && list [ index ] . From < c )
40
+ while ( index < list . Count && c > list [ index ] . To )
39
41
index ++ ;
40
42
if ( index >= list . Count )
41
43
{
@@ -47,16 +49,23 @@ public void Add(char c)
47
49
var range = list [ index ] ;
48
50
if ( c >= range . From && c <= range . To )
49
51
return ; //already included
50
- //c must be greater than range.To
51
- list . Insert ( index + 1 , new CharRange ( c ) ) ;
52
- TryMergeRange ( index + 1 ) ;
52
+ if ( c > range . To )
53
+ {
54
+ list . Insert ( index + 1 , new CharRange ( c ) ) ;
55
+ TryMergeRange ( index + 1 ) ;
56
+ }
57
+ else // c < range.From
58
+ {
59
+ list . Insert ( index , new CharRange ( c ) ) ;
60
+ TryMergeRange ( index ) ;
61
+ }
53
62
}
54
63
}
55
64
56
65
public void Add ( char from , char to )
57
66
{
58
67
var index = 0 ;
59
- while ( index < list . Count && list [ index ] . From < from )
68
+ while ( index < list . Count && from > list [ index ] . To )
60
69
index ++ ;
61
70
if ( index >= list . Count )
62
71
{
@@ -66,14 +75,71 @@ public void Add(char from, char to)
66
75
else
67
76
{
68
77
var fromIndex = index ;
69
- while ( index < list . Count && list [ index ] . To < to )
78
+ while ( index < list . Count && from > list [ index ] . To )
70
79
index ++ ;
71
- list . RemoveRange ( fromIndex , index - fromIndex + 1 ) ;
80
+ if ( index < list . Count )
81
+ {
82
+ if ( from > list [ index ] . From )
83
+ {
84
+ from = list [ index ] . From ;
85
+ list . RemoveRange ( fromIndex , index - fromIndex + 1 ) ;
86
+ }
87
+ }
72
88
list . Insert ( fromIndex , new CharRange ( from , to ) ) ;
73
89
TryMergeRange ( fromIndex ) ;
74
90
}
75
91
}
76
-
92
+
93
+ public void Remove ( char c )
94
+ {
95
+ var index = 0 ;
96
+ while ( index < list . Count && c > list [ index ] . To )
97
+ index ++ ;
98
+ if ( index >= list . Count )
99
+ return ;
100
+ var range = list [ index ] ;
101
+ if ( c < range . From || c > range . To )
102
+ return ;
103
+ list . RemoveAt ( index ) ;
104
+ if ( range . From == range . To )
105
+ ;
106
+ else if ( range . From == c )
107
+ list . Insert ( index , new CharRange ( ( char ) ( c + 1 ) , range . To ) ) ;
108
+ else if ( range . To == c )
109
+ list . Insert ( index , new CharRange ( range . From , ( char ) ( c - 1 ) ) ) ;
110
+ else
111
+ {
112
+ list . Insert ( index , new CharRange ( range . From , ( char ) ( c - 1 ) ) ) ;
113
+ list . Insert ( index + 1 , new CharRange ( ( char ) ( c + 1 ) , range . To ) ) ;
114
+ }
115
+ }
116
+
117
+ public void Remove ( char from , char to )
118
+ {
119
+ var index = 0 ;
120
+ while ( index < list . Count && from > list [ index ] . To )
121
+ index ++ ;
122
+ if ( index >= list . Count )
123
+ return ;
124
+ var range = list [ index ] ;
125
+ if ( from > range . To )
126
+ return ; //nothing to do
127
+ list . RemoveAt ( index ) ;
128
+ if ( range . From <= from - 1 )
129
+ {
130
+ list . Insert ( index , new CharRange ( range . From , ( char ) ( from - 1 ) ) ) ;
131
+ index ++ ;
132
+ while ( index < list . Count && to < list [ index ] . From )
133
+ list . RemoveAt ( index ) ;
134
+ if ( index < list . Count )
135
+ {
136
+ range = list [ index ] ;
137
+ list . RemoveAt ( index ) ;
138
+ list . Insert ( index , new CharRange ( ( char ) ( to + 1 ) , range . To ) ) ;
139
+ }
140
+ }
141
+ }
142
+
77
143
private void TryMergeRange ( int index )
78
144
{
79
145
var current = list [ index ] ;
@@ -98,7 +164,7 @@ private void TryMergeRange(int index)
98
164
}
99
165
}
100
166
}
101
-
167
+
102
168
public IEnumerator < CharRange > GetEnumerator ( )
103
169
{
104
170
return list . GetEnumerator ( ) ;
0 commit comments