The real, definitive, Visual C++ support branch. Accept no substitutes
[reactos.git] / dll / win32 / riched20 / string.c
1 /*
2 * RichEdit - string operations
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 int ME_GetOptimalBuffer(int nLen)
26 {
27 return ((2*nLen+1)+128)&~63;
28 }
29
30 ME_String *ME_MakeString(LPCWSTR szText)
31 {
32 ME_String *s = ALLOC_OBJ(ME_String);
33 s->nLen = lstrlenW(szText);
34 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
35 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
36 lstrcpyW(s->szData, szText);
37 return s;
38 }
39
40 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
41 {
42 ME_String *s = ALLOC_OBJ(ME_String);
43
44 s->nLen = nMaxChars;
45 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
46 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
47 /* Native allows NUL chars */
48 memmove(s->szData, szText, s->nLen * sizeof(WCHAR));
49 s->szData[s->nLen] = 0;
50 return s;
51 }
52
53 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
54 { /* Make a string by repeating a char nMaxChars times */
55 int i;
56 ME_String *s = ALLOC_OBJ(ME_String);
57
58 s->nLen = nMaxChars;
59 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
60 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
61
62 for (i = 0;i<nMaxChars;i++)
63 s->szData[i] = cRepeat;
64 s->szData[s->nLen] = 0;
65 return s;
66 }
67
68 ME_String *ME_MakeStringB(int nMaxChars)
69 { /* Create a buffer (uninitialized string) of size nMaxChars */
70 ME_String *s = ALLOC_OBJ(ME_String);
71
72 s->nLen = nMaxChars;
73 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
74 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
75 s->szData[s->nLen] = 0;
76 return s;
77 }
78
79 ME_String *ME_StrDup(const ME_String *s)
80 {
81 return ME_MakeStringN(s->szData, s->nLen);
82 }
83
84 void ME_DestroyString(ME_String *s)
85 {
86 FREE_OBJ(s->szData);
87 FREE_OBJ(s);
88 }
89
90 void ME_AppendString(ME_String *s1, const ME_String *s2)
91 {
92 if (s1->nLen+s2->nLen+1 <= s1->nBuffer) {
93 lstrcpyW(s1->szData+s1->nLen, s2->szData);
94 s1->nLen += s2->nLen;
95 }
96 else
97 {
98 WCHAR *buf;
99 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
100
101 buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
102 lstrcpyW(buf, s1->szData);
103 lstrcpyW(buf+s1->nLen, s2->szData);
104 FREE_OBJ(s1->szData);
105 s1->szData = buf;
106 s1->nLen += s2->nLen;
107 }
108 }
109
110 ME_String *ME_ConcatString(const ME_String *s1, const ME_String *s2)
111 {
112 ME_String *s = ALLOC_OBJ(ME_String);
113 s->nLen = s1->nLen+s2->nLen;
114 s->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
115 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
116 lstrcpyW(s->szData, s1->szData);
117 lstrcpyW(s->szData+s1->nLen, s2->szData);
118 return s;
119 }
120
121 ME_String *ME_VSplitString(ME_String *orig, int charidx)
122 {
123 ME_String *s;
124
125 /*if (charidx<0) charidx = 0;
126 if (charidx>orig->nLen) charidx = orig->nLen;
127 */
128 assert(charidx>=0);
129 assert(charidx<=orig->nLen);
130
131 s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
132 orig->nLen = charidx;
133 orig->szData[charidx] = '\0';
134 return s;
135 }
136
137 int ME_IsWhitespaces(const ME_String *s)
138 {
139 /* FIXME multibyte */
140 WCHAR *pos = s->szData;
141 while(ME_IsWSpace(*pos++))
142 ;
143 pos--;
144 if (*pos)
145 return 0;
146 else
147 return 1;
148 }
149
150 int ME_IsSplitable(const ME_String *s)
151 {
152 WCHAR *pos = s->szData;
153 WCHAR ch;
154 while(ME_IsWSpace(*pos++))
155 ;
156 pos--;
157 while((ch = *pos++) != 0)
158 {
159 if (ME_IsWSpace(ch))
160 return 1;
161 }
162 return 0;
163 }
164
165 /* FIXME multibyte */
166 /*
167 int ME_CalcSkipChars(ME_String *s)
168 {
169 int cnt = 0;
170 while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
171 cnt++;
172 return cnt;
173 }
174 */
175
176 int ME_StrLen(const ME_String *s) {
177 return s->nLen;
178 }
179
180 int ME_StrVLen(const ME_String *s) {
181 return s->nLen;
182 }
183
184 int ME_StrRelPos(const ME_String *s, int nVChar, int *pRelChars)
185 {
186 int nRelChars = *pRelChars;
187
188 TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars);
189
190 assert(*pRelChars);
191 if (!nRelChars)
192 return nVChar;
193
194 if (nRelChars>0)
195 nRelChars = min(*pRelChars, s->nLen - nVChar);
196 else
197 nRelChars = max(*pRelChars, -nVChar);
198 nVChar += nRelChars;
199 *pRelChars -= nRelChars;
200 return nVChar;
201 }
202
203 int ME_StrRelPos2(const ME_String *s, int nVChar, int nRelChars)
204 {
205 return ME_StrRelPos(s, nVChar, &nRelChars);
206 }
207
208 int ME_VPosToPos(ME_String *s, int nVPos)
209 {
210 return nVPos;
211 /*
212 int i = 0, len = 0;
213 if (!nVPos)
214 return 0;
215 while (i < s->nLen)
216 {
217 if (i == nVPos)
218 return len;
219 if (s->szData[i]=='\\') i++;
220 i++;
221 len++;
222 }
223 return len;
224 */
225 }
226
227 int ME_PosToVPos(const ME_String *s, int nPos)
228 {
229 if (!nPos)
230 return 0;
231 return ME_StrRelPos2(s, 0, nPos);
232 }
233
234 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
235 {
236 int end_ofs;
237
238 assert(nVChar >=0 && nVChar <= s->nLen);
239 assert(nChars >= 0);
240 assert(nVChar+nChars <= s->nLen);
241
242 end_ofs = ME_StrRelPos2(s, nVChar, nChars);
243 assert(end_ofs <= s->nLen);
244 memmove(s->szData+nVChar, s->szData+end_ofs, 2*(s->nLen+1-end_ofs));
245 s->nLen -= (end_ofs - nVChar);
246 }
247
248 int ME_GetCharFwd(const ME_String *s, int nPos)
249 {
250 int nVPos = 0;
251
252 assert(nPos < ME_StrLen(s));
253 if (nPos)
254 nVPos = ME_StrRelPos2(s, nVPos, nPos);
255
256 if (nVPos < s->nLen)
257 return s->szData[nVPos];
258 return -1;
259 }
260
261 int ME_GetCharBack(const ME_String *s, int nPos)
262 {
263 int nVPos = ME_StrVLen(s);
264
265 assert(nPos < ME_StrLen(s));
266 if (nPos)
267 nVPos = ME_StrRelPos2(s, nVPos, -nPos);
268
269 if (nVPos < s->nLen)
270 return s->szData[nVPos];
271 return -1;
272 }
273
274 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
275 int i;
276 for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
277 ;
278
279 return i;
280 }
281
282 /* note: returns offset of the first trailing whitespace */
283 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
284 int i;
285 for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
286 ;
287
288 return i;
289 }
290
291 /* note: returns offset of the first trailing nonwhitespace */
292 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
293 int i;
294 for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
295 ;
296
297 return i;
298 }
299
300
301 static int
302 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
303 {
304 /* FIXME: Native also knows about punctuation */
305 TRACE("s==%s, start==%d, len==%d, code==%d\n",
306 debugstr_wn(s, len), start, len, code);
307 switch (code)
308 {
309 case WB_ISDELIMITER:
310 return ME_IsWSpace(s[start]);
311 case WB_LEFT:
312 case WB_MOVEWORDLEFT:
313 while (start && ME_IsWSpace(s[start - 1]))
314 start--;
315 while (start && !ME_IsWSpace(s[start - 1]))
316 start--;
317 return start;
318 case WB_RIGHT:
319 case WB_MOVEWORDRIGHT:
320 while (start < len && !ME_IsWSpace(s[start]))
321 start++;
322 while (start < len && ME_IsWSpace(s[start]))
323 start++;
324 return start;
325 }
326 return 0;
327 }
328
329
330 int
331 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
332 {
333 /* FIXME: ANSIfy the string when bEmulateVersion10 is TRUE */
334 if (!editor->pfnWordBreak)
335 return ME_WordBreakProc(str->szData, start, str->nLen, code);
336 else
337 return editor->pfnWordBreak(str->szData, start, str->nLen, code);
338 }
339
340 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
341 {
342 assert(psz != NULL);
343
344 if (unicode)
345 return (LPWSTR)psz;
346 else {
347 WCHAR *tmp;
348 int nChars = MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, NULL, 0);
349 if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
350 MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, tmp, nChars);
351 return tmp;
352 }
353 }
354
355 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
356 {
357 if (!unicode)
358 FREE_OBJ(psz);
359 }