1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
25 using System.Collections.Generic;
26 using System.Globalization;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
35 internal static class Extensions
38 /// WebBrowserで選択中のテキストを取得します
40 public static string GetSelectedText(this WebBrowser webBrowser)
42 dynamic document = webBrowser.Document.DomDocument;
43 dynamic textRange = document.selection.createRange();
44 string selectedText = textRange.text;
49 public static ReadLockTransaction BeginReadTransaction(this ReaderWriterLockSlim lockObj)
50 => new ReadLockTransaction(lockObj);
52 public static WriteLockTransaction BeginWriteTransaction(this ReaderWriterLockSlim lockObj)
53 => new WriteLockTransaction(lockObj);
55 public static UpgradeableReadLockTransaction BeginUpgradeableReadTransaction(this ReaderWriterLockSlim lockObj)
56 => new UpgradeableReadLockTransaction(lockObj);
59 /// 一方のカルチャがもう一方のカルチャを内包するかを判断します
61 public static bool Contains(this CultureInfo @this, CultureInfo that)
63 if (@this.Equals(that))
66 // InvariantCulture の親カルチャは InvariantCulture 自身であるため、false になったら打ち切る
67 if (!that.Parent.Equals(that))
68 return Contains(@this, that.Parent);
73 public static int FindIndex<T>(this IEnumerable<T> enumerable, Predicate<T> finder)
75 if (enumerable is List<T> list)
76 return list.FindIndex(finder);
78 if (enumerable is T[] array)
79 return Array.FindIndex(array, finder);
83 foreach (var item in enumerable)
94 public static IEnumerable<(T Value, int Index)> WithIndex<T>(this IEnumerable<T> enumerable)
97 foreach (var value in enumerable)
98 yield return (value, i++);
101 public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value)
108 /// 文字列をコードポイント単位に分割して返します
110 public static IEnumerable<int> ToCodepoints(this string s)
113 throw new ArgumentNullException(nameof(s));
115 static IEnumerable<int> GetEnumerable(string input)
118 var length = input.Length;
121 if (char.IsSurrogatePair(input, i))
123 yield return char.ConvertToUtf32(input, i);
128 yield return input[i];
134 return GetEnumerable(s);
138 /// 指定された部分文字列のコードポイント単位での文字数を返す
140 /// <param name="s">文字列</param>
141 /// <param name="start">開始位置</param>
142 /// <param name="end">終了位置</param>
143 public static int GetCodepointCount(this string s, int start, int end)
146 throw new ArgumentNullException(nameof(s));
147 if (start < 0 || start > s.Length)
148 throw new ArgumentOutOfRangeException(nameof(start));
149 if (end < 0 || end > s.Length)
150 throw new ArgumentOutOfRangeException(nameof(end));
152 throw new ArgumentOutOfRangeException(nameof(start));
155 for (var i = start; i < end; i += char.IsSurrogatePair(s, i) ? 2 : 1)
161 public static Task ForEachAsync<T>(this IObservable<T> observable, Action<T> subscriber)
162 => ForEachAsync(observable, value => { subscriber(value); return Task.CompletedTask; });
164 public static Task ForEachAsync<T>(this IObservable<T> observable, Func<T, Task> subscriber)
165 => ForEachAsync(observable, subscriber, CancellationToken.None);
167 public static Task ForEachAsync<T>(this IObservable<T> observable, Action<T> subscriber, CancellationToken cancellationToken)
168 => ForEachAsync(observable, value => { subscriber(value); return Task.CompletedTask; }, cancellationToken);
170 public static async Task ForEachAsync<T>(this IObservable<T> observable, Func<T, Task> subscriber, CancellationToken cancellationToken)
172 var observer = new ForEachObserver<T>(subscriber);
173 using var unsubscriber = observable.Subscribe(observer);
175 using (cancellationToken.Register(() => unsubscriber.Dispose()))
176 await observer.Task.ConfigureAwait(false);
179 private class ForEachObserver<T> : IObserver<T>
181 private readonly Func<T, Task> subscriber;
182 private readonly TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
187 public ForEachObserver(Func<T, Task> subscriber)
188 => this.subscriber = subscriber;
190 public async void OnNext(T value)
194 await this.subscriber(value);
198 this.tcs.TrySetException(ex);
202 public void OnCompleted()
203 => this.tcs.TrySetResult(1);
205 public void OnError(Exception error)
206 => this.tcs.TrySetException(error);