-
Notifications
You must be signed in to change notification settings - Fork 3
/
Text.h
278 lines (240 loc) · 6.98 KB
/
Text.h
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#pragma once
// TODO Consistently use DWORD for char pos
int TextLength(HLOCAL hText);
int NewLineNextLength(PCWSTR buffer, int nIndex)
{
if (nIndex < 0)
return -1;
else if (buffer[nIndex] == L'\0')
return -1;
else if (buffer[nIndex] == L'\r' && buffer[nIndex + 1] == L'\n')
return 2;
else if (buffer[nIndex] == L'\r')
return 1;
else if (buffer[nIndex] == L'\n')
return 1;
else
return -1;
}
int NewLinePrevLength(PCWSTR buffer, int nIndex)
{
if (nIndex < 1)
return -1;
else if (nIndex >= 2 && buffer[nIndex - 2] == L'\r' && buffer[nIndex - 1] == L'\n')
return 2;
else if (buffer[nIndex - 1] == L'\r')
return 1;
else if (buffer[nIndex - 1] == L'\n')
return 1;
else
return -1;
}
int GetNextChar(PCWSTR buffer, int nIndex)
{
if (buffer[nIndex] == L'\0')
return -1; // TODO Or return nIndex
else if (NewLineNextLength(buffer, nIndex) > 0)
return nIndex + NewLineNextLength(buffer, nIndex);
else
return (int) (CharNext(buffer + nIndex) - buffer);
}
int GetNextChar(HLOCAL hText, int nIndex)
{
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
nIndex = GetNextChar(buffer, nIndex);
LocalUnlock(hText);
return nIndex;
}
int GetPrevChar(PCWSTR buffer, int nIndex)
{
if (nIndex <= 0)
return -1; // TODO Or return nIndex
else if (NewLinePrevLength(buffer, nIndex) > 0)
return nIndex - NewLinePrevLength(buffer, nIndex);
else
return (int) (CharPrev(buffer, buffer + nIndex) - buffer);
}
int GetPrevChar(HLOCAL hText, int nIndex)
{
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
nIndex = GetPrevChar(buffer, nIndex);
LocalUnlock(hText);
return nIndex;
}
int CALLBACK DefaultEditWordBreakProc(LPWSTR lpch, int ichCurrent, int cch, int code)
{
switch (code)
{
case WB_ISDELIMITER:
{
WCHAR c = lpch[ichCurrent];
ichCurrent = iswspace(c);
}
return ichCurrent;
break;
case WB_LEFT:
if (iswblank(lpch[ichCurrent - 1]))
{
while (ichCurrent > 0 && iswblank(lpch[ichCurrent - 1]))
--ichCurrent;
}
else if (ichCurrent >= 2 && NewLinePrevLength(lpch, ichCurrent) == 2)
{
ichCurrent -= 2;
}
else //if (!iswblank(lpch[ichCurrent - 1]))
{
while (ichCurrent > 0 && !iswspace(lpch[ichCurrent - 1]))
ichCurrent = (int) (CharPrev(lpch, lpch + ichCurrent) - lpch);
}
return ichCurrent;
break;
case WB_RIGHT:
if (iswblank(lpch[ichCurrent]))
{
while (ichCurrent < (cch - 1) && iswblank(lpch[ichCurrent]))
++ichCurrent;
}
else if (ichCurrent <= (cch - 2) && NewLineNextLength(lpch, ichCurrent) == 2)
{
ichCurrent += 2;
}
else //if (!iswblank(lpch[ichCurrent]))
{
while (ichCurrent < (cch - 1) && !iswspace(lpch[ichCurrent]))
ichCurrent = (int) (CharNext(lpch + ichCurrent) - lpch);
}
return ichCurrent;
break;
default:
ASSERT(FALSE);
return -1;
}
}
int MoveWord(EDITWORDBREAKPROC ewb, int code, HLOCAL hText, int nIndex)
{
ASSERT(ewb != nullptr);
int iTextLength = TextLength(hText);
PWSTR const buffer = (PWSTR) LocalLock(hText);
nIndex = ewb(buffer, nIndex, iTextLength, code);
LocalUnlock(hText);
return nIndex;
}
int GetNextLine(PCWSTR buffer, int nIndex)
{
ASSERT(buffer != NULL);
PCWSTR end = StrChr(buffer + nIndex, L'\n'); // TODO Handle different line endings
return end != nullptr ? static_cast<int>(end - buffer + 1) : -1;
}
int GetLineStart(PCWSTR buffer, int nIndex)
{
PCWSTR const end = StrRChr(buffer, buffer + nIndex, L'\n'); // TODO Handle different line endings
return end != nullptr ? static_cast<int>(end - buffer + 1) : 0;
}
int GetLineEnd(PCWSTR buffer, int nIndex)
{
PCWSTR end = StrChr(buffer + nIndex, L'\n'); // TODO Handle different line endings
return end != nullptr ? static_cast<int>(end - buffer) : nIndex + (int) wcslen(buffer + nIndex);
}
HLOCAL TextCreate(PCWSTR const pText)
{
int nLength = (int) wcslen(pText) + 1;
HLOCAL hText = LocalAlloc(LMEM_MOVEABLE, nLength * sizeof(WCHAR));
PWSTR const buffer = (PWSTR) LocalLock(hText);
memcpy(buffer, pText, nLength * sizeof(WCHAR));
LocalUnlock(hText);
return hText;
}
int TextLength(HLOCAL hText)
{
ASSERT(hText != NULL);
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
int nLength = (int) wcslen(buffer);
LocalUnlock(hText);
return nLength;
}
int TextLineCount(HLOCAL hText)
{
ASSERT(hText != NULL);
int nLine = 0;
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
int nIndex = 0;
while (nIndex >= 0)
{
nIndex = GetNextLine(buffer, nIndex);
++nLine;
}
LocalUnlock(hText);
return nLine;
}
int TextLineStart(HLOCAL hText, int nIndex)
{
ASSERT(hText != NULL);
ASSERT(nIndex >= 0);
ASSERT(nIndex <= TextLength(hText));
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
nIndex = GetLineStart(buffer, nIndex);
LocalUnlock(hText);
return nIndex;
}
int TextLineEnd(HLOCAL hText, int nIndex)
{
ASSERT(hText != NULL);
ASSERT(nIndex >= 0);
ASSERT(nIndex <= TextLength(hText));
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
nIndex = GetLineEnd(buffer, nIndex);
LocalUnlock(hText);
return nIndex;
}
int TextLineFromChar(HLOCAL hText, int nChar)
{
ASSERT(hText != NULL);
ASSERT(nChar >= 0);
int nLine = 0;
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
int nIndex = 0;
while ((nIndex = GetNextLine(buffer, nIndex)) <= nChar)
{
if (nIndex == -1)
break;
++nLine;
}
LocalUnlock(hText);
return nLine;
}
int TextLineIndex(HLOCAL hText, int nLine)
{
ASSERT(hText != NULL);
ASSERT(nLine >= 0);
PCWSTR const buffer = (PCWSTR) LocalLock(hText);
int nIndex = 0;
while (nIndex >= 0 && nLine > 0)
{
nIndex = GetNextLine(buffer, nIndex);
--nLine;
}
LocalUnlock(hText);
return nIndex;
}
HLOCAL TextReplace(HLOCAL hText, const int nStart, DWORD& nEnd, PCWSTR const pText)
{
ASSERT(hText != NULL);
ASSERT(nStart <= (int) nEnd);
ASSERT(pText != nullptr);
const int nTextLen = (int) wcslen(pText);
const int nDiff = nTextLen - (nEnd - nStart);
const int nLen = TextLength(hText) + 1;
ASSERT(nStart >= 0 && nStart <= nLen);
ASSERT(nEnd >= 0 && (int) nEnd <= nLen);
HLOCAL hTextNew = LocalReAlloc(hText, (nLen + nDiff) * sizeof(WCHAR), LMEM_MOVEABLE);
if (hTextNew == NULL)
return NULL;
hText = hTextNew;
PWSTR const buffer = (PWSTR) LocalLock(hText);
memmove(buffer + nEnd + nDiff, buffer + nEnd, (nLen - nEnd) * sizeof(WCHAR));
memcpy(buffer + nStart, pText, nTextLen * sizeof(WCHAR));
LocalUnlock(hText);
nEnd += nDiff;
return hText;
}