C#:サロゲートペアを考慮した文字の列挙に拡張メソッドも使えるようにする †
昨日の記事の続き…というか改良版。
前回作った TextElementEnumerable
クラスは IEnumerable
インタフェースを実装していましたが、 System.Linq.Enumerable
静的クラスにある豊富な拡張メソッドを利用するためには IEnumerable<T>
ジェネリックインタフェースをこそ実装すべきです。
そして IEnumerable<T>.GetEnumerator
メソッドの実装には yield return
文を使うのが簡単です。
というわけで、改良した TextElementEnumerable
クラスはこちら。
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
42
43
44
45
46
47
48
49
|
-
-
|
|
!
-
-
|
|
|
!
-
|
!
|
-
|
|
!
|
-
|
|
|
!
-
|
|
-
|
!
!
|
|
|
|
-
|
!
|
|
!
!
| using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
namespace ruche.util
{
public class TextElementEnumerable : IEnumerable<string>
{
public TextElementEnumerable(string source)
{
this.Source = source;
}
public string Source { get; private set; }
public IEnumerator<string> GetEnumerator()
{
var e = StringInfo.GetTextElementEnumerator(this.Source);
while (e.MoveNext())
{
yield return e.GetTextElement();
}
}
#region IEnumerable の明示的実装
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}
|
これで System.Linq.Enumerable
静的クラスの拡張メソッドも利用可能になります。
1
2
3
4
5
6
7
8
9
10
|
-
!
-
!
-
-
!
| string text = "あいうえお";
int unicodeLen = (new TextElementEnumerable(text)).Count();
foreach (var s in new TextElementEnumerable(text))
{
}
|
ちなみに、利用する場所が限られているのならば、わざわざ TextElementEnumerable
クラスを作らずとも下記のようなメソッドを作るだけでも事足ります。
1
2
3
4
5
6
7
8
9
10
11
12
13
| -
|
|
|
|
!
-
|
|
-
|
!
!
| private IEnumerable<string> GetTextElementEnumerable(string source)
{
var e = StringInfo.GetTextElementEnumerator(source);
while (e.MoveNext())
{
yield return e.GetTextElement();
}
}
|
これで new TextElementEnumerable(xxx)
の代わりに GetTextElementEnumerable(xxx)
と書くことができます。
Category: [
プログラミング][
C#] - 2013-03-15 00:05:35