mapi32/tests: Fix typo.
[wine] / dlls / 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 static 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   if (!s) return;
87   FREE_OBJ(s->szData);
88   FREE_OBJ(s);
89 }
90
91 void ME_AppendString(ME_String *s1, const ME_String *s2)
92 {
93   if (s1->nLen+s2->nLen+1 <= s1->nBuffer) {
94     lstrcpyW(s1->szData+s1->nLen, s2->szData);
95     s1->nLen += s2->nLen;
96   }
97   else
98   {
99     WCHAR *buf;
100     s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
101
102     buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer); 
103     lstrcpyW(buf, s1->szData);
104     lstrcpyW(buf+s1->nLen, s2->szData);
105     FREE_OBJ(s1->szData);
106     s1->szData = buf;
107     s1->nLen += s2->nLen;
108   }
109 }
110
111 ME_String *ME_ConcatString(const ME_String *s1, const ME_String *s2)
112 {
113   ME_String *s = ALLOC_OBJ(ME_String);
114   s->nLen = s1->nLen+s2->nLen;
115   s->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
116   s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
117   lstrcpyW(s->szData, s1->szData);
118   lstrcpyW(s->szData+s1->nLen, s2->szData);
119   return s;  
120 }
121
122 ME_String *ME_VSplitString(ME_String *orig, int charidx)
123 {
124   ME_String *s;
125
126   /*if (charidx<0) charidx = 0;
127   if (charidx>orig->nLen) charidx = orig->nLen;
128   */
129   assert(charidx>=0);
130   assert(charidx<=orig->nLen);
131
132   s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
133   orig->nLen = charidx;
134   orig->szData[charidx] = '\0';
135   return s;
136 }
137
138 int ME_IsWhitespaces(const ME_String *s)
139 {
140   /* FIXME multibyte */
141   WCHAR *pos = s->szData;
142   while(ME_IsWSpace(*pos++))
143     ;
144   pos--;
145   if (*pos)
146     return 0;
147   else
148     return 1;
149 }
150
151 int ME_IsSplitable(const ME_String *s)
152 {
153   WCHAR *pos = s->szData;
154   WCHAR ch;
155   while(ME_IsWSpace(*pos++))
156     ;
157   pos--;
158   while((ch = *pos++) != 0)
159   {
160     if (ME_IsWSpace(ch))
161       return 1;
162   }
163   return 0;
164 }
165
166 /* FIXME multibyte */
167 /*
168 int ME_CalcSkipChars(ME_String *s)
169 {
170   int cnt = 0;
171   while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
172     cnt++;
173   return cnt;
174 }
175 */
176
177 int ME_StrLen(const ME_String *s) {
178   return s->nLen;
179 }
180
181 int ME_StrVLen(const ME_String *s) {
182   return s->nLen;
183 }
184
185 int ME_StrRelPos(const ME_String *s, int nVChar, int *pRelChars)
186 {
187   int nRelChars = *pRelChars;
188
189   TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars);
190
191   assert(*pRelChars);
192   if (!nRelChars)
193     return nVChar;
194   
195   if (nRelChars>0)
196     nRelChars = min(*pRelChars, s->nLen - nVChar);
197   else
198     nRelChars = max(*pRelChars, -nVChar);
199   nVChar += nRelChars;
200   *pRelChars -= nRelChars;
201   return nVChar;
202 }
203
204 int ME_StrRelPos2(const ME_String *s, int nVChar, int nRelChars)
205 {
206   return ME_StrRelPos(s, nVChar, &nRelChars);
207 }
208
209 int ME_VPosToPos(ME_String *s, int nVPos)
210 {
211   return nVPos;
212   /*
213   int i = 0, len = 0;
214   if (!nVPos)
215     return 0;
216   while (i < s->nLen)
217   {
218     if (i == nVPos)
219       return len;
220     if (s->szData[i]=='\\') i++;
221     i++;
222     len++;
223   }
224   return len;
225   */
226 }
227
228 int ME_PosToVPos(const ME_String *s, int nPos)
229 {
230   if (!nPos)
231     return 0;
232   return ME_StrRelPos2(s, 0, nPos);
233 }
234
235 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
236 {
237   int end_ofs;
238   
239   assert(nVChar >=0 && nVChar <= s->nLen);
240   assert(nChars >= 0);
241   assert(nVChar+nChars <= s->nLen);
242   
243   end_ofs = ME_StrRelPos2(s, nVChar, nChars);
244   assert(end_ofs <= s->nLen);
245   memmove(s->szData+nVChar, s->szData+end_ofs, 2*(s->nLen+1-end_ofs));
246   s->nLen -= (end_ofs - nVChar);
247 }
248
249 int ME_GetCharFwd(const ME_String *s, int nPos)
250 {
251   int nVPos = 0;
252
253   assert(nPos < ME_StrLen(s));
254   if (nPos)
255     nVPos = ME_StrRelPos2(s, nVPos, nPos);
256   
257   if (nVPos < s->nLen)
258     return s->szData[nVPos];
259   return -1;
260 }
261
262 int ME_GetCharBack(const ME_String *s, int nPos)
263 {
264   int nVPos = ME_StrVLen(s);
265
266   assert(nPos < ME_StrLen(s));
267   if (nPos)
268     nVPos = ME_StrRelPos2(s, nVPos, -nPos);
269   
270   if (nVPos < s->nLen)
271     return s->szData[nVPos];
272   return -1;
273 }
274
275 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
276   int i;
277   for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
278     ;
279     
280   return i;
281 }
282
283 /* note: returns offset of the first trailing whitespace */
284 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
285   int i;
286   for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
287     ;
288     
289   return i;
290 }
291
292 /* note: returns offset of the first trailing nonwhitespace */
293 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
294   int i;
295   for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
296     ;
297     
298   return i;
299 }
300
301
302 static int
303 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
304 {
305   /* FIXME: Native also knows about punctuation */
306   TRACE("s==%s, start==%d, len==%d, code==%d\n",
307         debugstr_wn(s, len), start, len, code);
308   /* convert number of bytes to number of characters. */
309   len /= sizeof(WCHAR);
310   switch (code)
311   {
312     case WB_ISDELIMITER:
313       return ME_IsWSpace(s[start]);
314     case WB_LEFT:
315     case WB_MOVEWORDLEFT:
316       while (start && ME_IsWSpace(s[start - 1]))
317         start--;
318       while (start && !ME_IsWSpace(s[start - 1]))
319         start--;
320       return start;
321     case WB_RIGHT:
322     case WB_MOVEWORDRIGHT:
323       while (start < len && !ME_IsWSpace(s[start]))
324         start++;
325       while (start < len && ME_IsWSpace(s[start]))
326         start++;
327       return start;
328   }
329   return 0;
330 }
331
332
333 int
334 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
335 {
336   if (!editor->pfnWordBreak) {
337     return ME_WordBreakProc(str->szData, start, str->nLen*sizeof(WCHAR), code);
338   } else if (!editor->bEmulateVersion10) {
339     /* MSDN lied about the third parameter for EditWordBreakProc being the number
340      * of characters, it is actually the number of bytes of the string. */
341     return editor->pfnWordBreak(str->szData, start, str->nLen*sizeof(WCHAR), code);
342   } else {
343     int result;
344     int buffer_size = WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
345                                           NULL, 0, NULL, NULL);
346     char *buffer = heap_alloc(buffer_size);
347     WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
348                         buffer, buffer_size, NULL, NULL);
349     result = editor->pfnWordBreak(str->szData, start, str->nLen, code);
350     heap_free(buffer);
351     return result;
352   }
353 }
354
355 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
356 {
357   assert(psz != NULL);
358
359   if (unicode)
360     return psz;
361   else {
362     WCHAR *tmp;
363     int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
364     if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
365       MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
366     return tmp;
367   }
368 }
369
370 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
371 {
372   if (!unicode)
373     FREE_OBJ(psz);
374 }