4
4
//
5
5
using NStack ;
6
6
7
- namespace System
8
- {
9
- public partial struct Rune
10
- {
11
- static uint [ , ] combining = new uint [ , ] {
7
+ namespace System {
8
+ public partial struct Rune {
9
+ static uint [ , ] combining = new uint [ , ] {
12
10
{ 0x0300 , 0x036F } , { 0x0483 , 0x0486 } , { 0x0488 , 0x0489 } ,
13
11
{ 0x0591 , 0x05BD } , { 0x05BF , 0x05BF } , { 0x05C1 , 0x05C2 } ,
14
12
{ 0x05C4 , 0x05C5 } , { 0x05C7 , 0x05C7 } , { 0x0600 , 0x0603 } ,
@@ -48,16 +46,16 @@ public partial struct Rune
48
46
{ 0x1B36 , 0x1B3A } , { 0x1B3C , 0x1B3C } , { 0x1B42 , 0x1B42 } ,
49
47
{ 0x1B6B , 0x1B73 } , { 0x1DC0 , 0x1DCA } , { 0x1DFE , 0x1DFF } ,
50
48
{ 0x200B , 0x200F } , { 0x202A , 0x202E } , { 0x2060 , 0x2063 } ,
51
- { 0x206A , 0x206F } , { 0x20D0 , 0x20EF } , { 0x2e9a , 0x2e9a } ,
52
- { 0x2ef4 , 0x2eff } , { 0x2fd6 , 0x2fef } , { 0x2ffc , 0x2fff } ,
53
- { 0x31e4 , 0x31ef } , { 0x321f , 0x321f } , { 0xA48D , 0xA48F } ,
49
+ { 0x206A , 0x206F } , { 0x20D0 , 0x20EF } , { 0x2E9A , 0x2E9A } ,
50
+ { 0x2EF4 , 0x2EFF } , { 0x2FD6 , 0x2FEF } , { 0x2FFC , 0x2FFF } ,
51
+ { 0x31E4 , 0x31EF } , { 0x321F , 0x321F } , { 0xA48D , 0xA48F } ,
54
52
{ 0xA806 , 0xA806 } , { 0xA80B , 0xA80B } , { 0xA825 , 0xA826 } ,
55
53
{ 0xFB1E , 0xFB1E } , { 0xFE00 , 0xFE0F } , { 0xFE1A , 0xFE1F } ,
56
54
{ 0xFE20 , 0xFE23 } , { 0xFE53 , 0xFE53 } , { 0xFE67 , 0xFE67 } ,
57
55
{ 0xFEFF , 0xFEFF } , { 0xFFF9 , 0xFFFB } ,
58
56
} ;
59
57
60
- static uint [ , ] combiningWideChars = new uint [ , ] {
58
+ static uint [ , ] combiningWideChars = new uint [ , ] {
61
59
/* Hangul Jamo init. consonants - 0x1100, 0x11ff */
62
60
/* Miscellaneous Technical - 0x2300, 0x23ff */
63
61
/* Hangul Syllables - 0x11a8, 0x11c2 */
@@ -84,22 +82,21 @@ public partial struct Rune
84
82
{ 0x3131 , 0x318e } , { 0x3190 , 0x3247 } , { 0x3250 , 0x4dbf } ,
85
83
{ 0x4e00 , 0xa4c6 } , { 0xa960 , 0xa97c } , { 0xac00 , 0xd7a3 } ,
86
84
{ 0xf900 , 0xfaff } , { 0xfe10 , 0xfe1f } , { 0xfe30 , 0xfe6b } ,
87
- { 0xff01 , 0xff60 } , { 0xffe0 , 0xffe6 }
85
+ { 0xff01 , 0xff60 } , { 0xffe0 , 0xffe6 } , { 0x10000 , 0x10ffff }
88
86
} ;
89
87
90
- static int bisearch ( uint rune , uint [ , ] table , int max )
88
+ static int bisearch ( uint rune , uint [ , ] table , int max )
91
89
{
92
90
int min = 0 ;
93
91
int mid ;
94
92
95
- if ( rune < table [ 0 , 0 ] || rune > table [ max , 1 ] )
93
+ if ( rune < table [ 0 , 0 ] || rune > table [ max , 1 ] )
96
94
return 0 ;
97
- while ( max >= min )
98
- {
95
+ while ( max >= min ) {
99
96
mid = ( min + max ) / 2 ;
100
- if ( rune > table [ mid , 1 ] )
97
+ if ( rune > table [ mid , 1 ] )
101
98
min = mid + 1 ;
102
- else if ( rune < table [ mid , 0 ] )
99
+ else if ( rune < table [ mid , 0 ] )
103
100
max = mid - 1 ;
104
101
else
105
102
return 1 ;
@@ -127,82 +124,44 @@ static int bisearch(uint rune, uint[,] table, int max)
127
124
// return false;
128
125
//}
129
126
130
- static uint gethexaformat ( uint rune , int length )
131
- {
132
- var hex = rune . ToString ( $ "x{ length } ") ;
133
- var hexstr = hex . Substring ( hex . Length - length , length ) ;
134
- return ( uint ) int . Parse ( hexstr , System . Globalization . NumberStyles . HexNumber ) ;
135
- }
136
-
137
127
/// <summary>
138
128
/// Check if the rune is a non-spacing character.
139
129
/// </summary>
140
130
/// <param name="rune">The rune.</param>
141
131
/// <returns>True if is a non-spacing character, false otherwise.</returns>
142
- public static bool IsNonSpacingChar ( uint rune )
132
+ public static bool IsNonSpacingChar ( uint rune )
143
133
{
144
- return bisearch ( rune , combining , combining . GetLength ( 0 ) - 1 ) != 0 ;
134
+ return bisearch ( rune , combining , combining . GetLength ( 0 ) - 1 ) != 0 ;
145
135
}
146
136
147
137
/// <summary>
148
138
/// Check if the rune is a wide character.
149
139
/// </summary>
150
140
/// <param name="rune">The rune.</param>
151
141
/// <returns>True if is a wide character, false otherwise.</returns>
152
- public static bool IsWideChar ( uint rune )
142
+ public static bool IsWideChar ( uint rune )
153
143
{
154
- return bisearch ( gethexaformat ( rune , 4 ) , combiningWideChars , combiningWideChars . GetLength ( 0 ) - 1 ) != 0 ;
144
+ return bisearch ( rune , combiningWideChars , combiningWideChars . GetLength ( 0 ) - 1 ) != 0 ;
155
145
}
156
146
157
- static char firstSurrogatePairChar = '\0 ' ;
158
-
159
147
/// <summary>
160
148
/// Number of column positions of a wide-character code. This is used to measure runes as displayed by text-based terminals.
161
149
/// </summary>
162
150
/// <returns>The width in columns, 0 if the argument is the null character, -1 if the value is not printable, otherwise the number of columns that the rune occupies.</returns>
163
151
/// <param name="rune">The rune.</param>
164
- public static int ColumnWidth ( Rune rune )
152
+ public static int ColumnWidth ( Rune rune )
165
153
{
166
- if ( firstSurrogatePairChar != '\0 ' )
167
- firstSurrogatePairChar = '\0 ' ;
168
154
uint irune = ( uint ) rune ;
169
155
if ( irune < 0x20 || ( irune >= 0x7f && irune < 0xa0 ) )
170
156
return - 1 ;
171
157
if ( irune < 0x7f )
172
158
return 1 ;
173
159
/* binary search in table of non-spacing characters */
174
- if ( bisearch ( gethexaformat ( irune , 4 ) , combining , combining . GetLength ( 0 ) - 1 ) != 0 )
160
+ if ( bisearch ( irune , combining , combining . GetLength ( 0 ) - 1 ) != 0 )
175
161
return 0 ;
176
162
/* if we arrive here, ucs is not a combining or C0/C1 control character */
177
163
return 1 +
178
- ( bisearch ( gethexaformat ( irune , 4 ) , combiningWideChars , combiningWideChars . GetLength ( 0 ) - 1 ) != 0 ? 1 : 0 ) ;
179
- }
180
-
181
- /// <summary>
182
- /// Number of column positions of a wide-character code. This is used to measure runes as displayed by text-based terminals.
183
- /// </summary>
184
- /// <returns>The width in columns, 0 if the argument is the null character, -1 if the value is not printable, otherwise the number of columns that the rune occupies.</returns>
185
- /// <param name="c">The char.</param>
186
- public static int ColumnWidth ( char c )
187
- {
188
- if ( ! ( ( Rune ) c ) . IsValid )
189
- {
190
- if ( firstSurrogatePairChar == '\0 ' )
191
- {
192
- firstSurrogatePairChar = c ;
193
- return 0 ;
194
- }
195
- else if ( firstSurrogatePairChar != '\0 ' )
196
- {
197
- var r = new Rune ( firstSurrogatePairChar , c ) ;
198
- firstSurrogatePairChar = '\0 ' ;
199
- return ColumnWidth ( r ) ;
200
- }
201
- }
202
- if ( firstSurrogatePairChar != '\0 ' )
203
- firstSurrogatePairChar = '\0 ' ;
204
-
205
- return ColumnWidth ( ( Rune ) c ) ;
164
+ ( bisearch ( irune , combiningWideChars , combiningWideChars . GetLength ( 0 ) - 1 ) != 0 ? 1 : 0 ) ;
206
165
}
207
166
}
208
167
}
0 commit comments