OSDN Git Service

バージョン v3.13.1-dev 開発開始
[opentween/open-tween.git] / OpenTween / Extensions.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.Globalization;
27 using System.Linq;
28 using System.Text;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
32
33 namespace OpenTween
34 {
35     internal static class Extensions
36     {
37         /// <summary>
38         /// WebBrowserで選択中のテキストを取得します
39         /// </summary>
40         public static string GetSelectedText(this WebBrowser webBrowser)
41         {
42             dynamic document = webBrowser.Document.DomDocument;
43             dynamic textRange = document.selection.createRange();
44             string selectedText = textRange.text;
45
46             return selectedText;
47         }
48
49         public static ReadLockTransaction BeginReadTransaction(this ReaderWriterLockSlim lockObj)
50             => new(lockObj);
51
52         public static WriteLockTransaction BeginWriteTransaction(this ReaderWriterLockSlim lockObj)
53             => new(lockObj);
54
55         public static UpgradeableReadLockTransaction BeginUpgradeableReadTransaction(this ReaderWriterLockSlim lockObj)
56             => new(lockObj);
57
58         /// <summary>
59         /// 一方のカルチャがもう一方のカルチャを内包するかを判断します
60         /// </summary>
61         public static bool Contains(this CultureInfo @this, CultureInfo that)
62         {
63             if (@this.Equals(that))
64                 return true;
65
66             // InvariantCulture の親カルチャは InvariantCulture 自身であるため、false になったら打ち切る
67             if (!that.Parent.Equals(that))
68                 return Contains(@this, that.Parent);
69
70             return false;
71         }
72
73         public static int FindIndex<T>(this IEnumerable<T> enumerable, Predicate<T> finder)
74         {
75             if (enumerable is List<T> list)
76                 return list.FindIndex(finder);
77
78             if (enumerable is T[] array)
79                 return Array.FindIndex(array, finder);
80
81             var index = 0;
82
83             foreach (var item in enumerable)
84             {
85                 if (finder(item))
86                     return index;
87
88                 index++;
89             }
90
91             return -1;
92         }
93
94         public static IEnumerable<(T Value, int Index)> WithIndex<T>(this IEnumerable<T> enumerable)
95         {
96             var i = 0;
97             foreach (var value in enumerable)
98                 yield return (value, i++);
99         }
100
101         public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value)
102         {
103             key = kvp.Key;
104             value = kvp.Value;
105         }
106
107         /// <summary>
108         /// 文字列をコードポイント単位に分割して返します
109         /// </summary>
110         public static IEnumerable<int> ToCodepoints(this string s)
111         {
112             if (s == null)
113                 throw new ArgumentNullException(nameof(s));
114
115             static IEnumerable<int> GetEnumerable(string input)
116             {
117                 var i = 0;
118                 var length = input.Length;
119                 while (i < length)
120                 {
121                     if (char.IsSurrogatePair(input, i))
122                     {
123                         yield return char.ConvertToUtf32(input, i);
124                         i += 2;
125                     }
126                     else
127                     {
128                         yield return input[i];
129                         i++;
130                     }
131                 }
132             }
133
134             return GetEnumerable(s);
135         }
136
137         /// <summary>
138         /// 指定された部分文字列のコードポイント単位での文字数を返す
139         /// </summary>
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)
144         {
145             if (s == null)
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));
151             if (start > end)
152                 throw new ArgumentOutOfRangeException(nameof(start));
153
154             var count = 0;
155             for (var i = start; i < end; i += char.IsSurrogatePair(s, i) ? 2 : 1)
156                 count++;
157
158             return count;
159         }
160
161         public static bool TryInvoke(this Control control, Action action)
162         {
163             if (control.IsDisposed)
164                 return false;
165
166             if (control.InvokeRequired)
167                 control.Invoke(action);
168             else
169                 action();
170
171             return true;
172         }
173
174         public static Task ForEachAsync<T>(this IObservable<T> observable, Action<T> subscriber)
175         {
176             return ForEachAsync(observable, value =>
177             {
178                 subscriber(value);
179                 return Task.CompletedTask;
180             });
181         }
182
183         public static Task ForEachAsync<T>(this IObservable<T> observable, Func<T, Task> subscriber)
184             => ForEachAsync(observable, subscriber, CancellationToken.None);
185
186         public static Task ForEachAsync<T>(this IObservable<T> observable, Action<T> subscriber, CancellationToken cancellationToken)
187         {
188             return ForEachAsync(
189                 observable,
190                 value =>
191                 {
192                     subscriber(value);
193                     return Task.CompletedTask;
194                 },
195                 cancellationToken
196             );
197         }
198
199         public static async Task ForEachAsync<T>(this IObservable<T> observable, Func<T, Task> subscriber, CancellationToken cancellationToken)
200         {
201             var observer = new ForEachObserver<T>(subscriber);
202             using var unsubscriber = observable.Subscribe(observer);
203
204             using (cancellationToken.Register(() => unsubscriber.Dispose()))
205                 await observer.Task.ConfigureAwait(false);
206         }
207
208         private class ForEachObserver<T> : IObserver<T>
209         {
210             private readonly Func<T, Task> subscriber;
211             private readonly TaskCompletionSource<int> tcs = new();
212
213             public Task Task
214                 => this.tcs.Task;
215
216             public ForEachObserver(Func<T, Task> subscriber)
217                 => this.subscriber = subscriber;
218
219             public async void OnNext(T value)
220             {
221                 try
222                 {
223                     await this.subscriber(value);
224                 }
225                 catch (Exception ex)
226                 {
227                     this.tcs.TrySetException(ex);
228                 }
229             }
230
231             public void OnCompleted()
232                 => this.tcs.TrySetResult(1);
233
234             public void OnError(Exception error)
235                 => this.tcs.TrySetException(error);
236         }
237     }
238 }