-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
228 lines (208 loc) · 6.57 KB
/
Util.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace FindInFiles {
internal static class Util {
public static string RemoveLineEnding(string? line) {
if (string.IsNullOrEmpty(line)) {
return string.Empty;
}
var length = line.Length;
var ch = line[length - 1];
if (ch == '\n' || ch == '\r') {
length -= 1;
if (ch == '\n' && length > 0 && line[length - 1] == '\r') {
length -= 1;
}
line = (length == 0) ? string.Empty : line[..length];
}
return line;
}
public static unsafe int GetLeadingAsciiCount(string text) {
var count = 0;
fixed (char* ptr = text) {
char* p = ptr;
char* end = p + text.Length;
while (p < end && *p < 0x80) {
++p;
++count;
}
}
return count;
}
public static unsafe int GetUTF8ByteCount(string text) {
var count = 0;
fixed (char* ptr = text) {
char* p = ptr;
char* end = p + text.Length;
while (p < end) {
var ch = *p++;
if (ch < 0x80) {
count += 1;
} else if (ch < 0x800) {
count += 2;
} else if (p < end && char.IsSurrogatePair(ch, *p)) {
++p;
count += 4;
} else {
count += 3;
}
}
}
return count;
}
public static unsafe int GetCharacterIndex(string text, int startIndex, ref int byteCount, int bytePos) {
var count = byteCount;
fixed (char* ptr = text) {
char* p = ptr + startIndex;
char* end = p + text.Length;
while (p < end && count < bytePos) {
var ch = *p++;
++startIndex;
if (ch < 0x80) {
count += 1;
} else if (ch < 0x800) {
count += 2;
} else if (p < end && char.IsSurrogatePair(ch, *p)) {
++p;
++startIndex;
count += 4;
} else {
count += 3;
}
}
}
byteCount = count;
return startIndex;
}
public static void StartEditor(string path, int line, int column) {
var exePath = FindExePath("Notepad4.exe");
if (!File.Exists(exePath)) {
return;
}
var startInfo = new ProcessStartInfo {
UseShellExecute = false,
FileName = exePath,
Arguments = $"/g {line},{column} \"{path}\"",
};
using var process = Process.Start(startInfo);
}
public static string FindExePath(string name) {
var path = Path.Combine(Application.StartupPath, name);
if (File.Exists(path)) {
return path;
}
char[] buffer = new char[260];
int length = SearchPathW(null, name, null, buffer.Length, buffer, IntPtr.Zero);
if (length > 0 && length < buffer.Length) {
name = new string(buffer, 0, length);
}
return name;
}
public static (bool exist, bool directory) PathExists(string? path) {
if (!string.IsNullOrEmpty(path)) {
var attr = GetFileAttributesW(path);
if (attr != INVALID_FILE_ATTRIBUTES) {
var directory = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
return (true, directory);
}
}
return (false, false);
}
private const int INVALID_FILE_ATTRIBUTES = -1;
private const int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
static extern int GetFileAttributesW(string lpFileName);
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
static extern int SearchPathW(string? lpPath, string lpFileName, string? lpExtension, int nBufferLength, char[] lpBuffer, IntPtr lpFilePart);
public static unsafe string GetTextRange(this TextBoxBase textBox, int start, int end) {
var buffer = new char[end - start];
fixed (char* ptr = buffer) {
TEXTRANGE range;
range.chrg.cpMin = start;
range.chrg.cpMax = end;
range.lpstrText = new IntPtr(ptr);
int length = SendMessage(textBox.Handle, EM_GETTEXTRANGE, IntPtr.Zero, range);
if (length > 0) {
return new string(buffer, 0, length);
}
}
return string.Empty;
}
public static float GetLineHeight(this TextBoxBase textBox) {
var font = textBox.Font;
var family = font.FontFamily;
var style = font.Style;
var emHeight = family.GetEmHeight(style);
var lineSpacing = family.GetLineSpacing(style);
var ascent = family.GetCellAscent(style);
var descent = family.GetCellDescent(style);
var height = font.Size * (lineSpacing + (ascent + descent) / 2) / emHeight;
return height;
}
public static void SetRedraw(this Control control, bool redraw) {
SendMessage(control.Handle, WM_SETREDRAW, new IntPtr(redraw ? 1 : 0), IntPtr.Zero);
}
private const int WM_SETREDRAW = 0x000B;
private const int WM_USER = 0x0400;
private const int EM_GETTEXTRANGE = WM_USER + 75;
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE {
public int cpMin;
public int cpMax;
}
[StructLayout(LayoutKind.Sequential)]
private struct TEXTRANGE {
public CHARRANGE chrg;
public IntPtr lpstrText;
}
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, TEXTRANGE lParam);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
public static void AddRange(this ComboBox comboBox, StringCollection? strings) {
if (strings != null) {
var count = strings.Count;
if (count != 0) {
var items = new string[count];
strings.CopyTo(items, 0);
comboBox.Items.AddRange(items);
}
}
}
public static void AddToTop(this ComboBox comboBox, string value) {
var collection = comboBox.Items;
var index = collection.IndexOf(value);
if (index != 0) {
collection.Insert(0, value);
if (index > 0) {
collection.RemoveAt(index + 1);
}
comboBox.SelectedIndex = 0;
}
}
public static bool AddHistory(ref StringCollection? collection, string? value, int maxCount) {
if (string.IsNullOrEmpty(value)) {
return false;
}
var created = false;
if (collection == null) {
created = true;
collection = new StringCollection { value };
} else {
var index = collection.IndexOf(value);
if (index != 0) {
collection.Insert(0, value);
if (index > 0) {
collection.RemoveAt(index + 1);
}
index = collection.Count;
if (index > maxCount) {
collection.RemoveAt(index - 1);
}
}
}
return created;
}
}
}