2 * RichEdit - string operations
4 * Copyright 2004 by Krzysztof Foltman
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.
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.
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
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 static int ME_GetOptimalBuffer(int nLen)
27 return ((2*nLen+1)+128)&~63;
30 ME_String *ME_MakeString(LPCWSTR szText)
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);
40 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
42 ME_String *s = ALLOC_OBJ(ME_String);
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;
53 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
54 { /* Make a string by repeating a char nMaxChars times */
56 ME_String *s = ALLOC_OBJ(ME_String);
59 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
60 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
62 for (i = 0;i<nMaxChars;i++)
63 s->szData[i] = cRepeat;
64 s->szData[s->nLen] = 0;
68 ME_String *ME_MakeStringB(int nMaxChars)
69 { /* Create a buffer (uninitialized string) of size nMaxChars */
70 ME_String *s = ALLOC_OBJ(ME_String);
73 s->nBuffer = ME_GetOptimalBuffer(s->nLen+1);
74 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
75 s->szData[s->nLen] = 0;
79 ME_String *ME_StrDup(const ME_String *s)
81 return ME_MakeStringN(s->szData, s->nLen);
84 void ME_DestroyString(ME_String *s)
91 void ME_AppendString(ME_String *s1, const ME_String *s2)
93 if (s1->nLen+s2->nLen+1 <= s1->nBuffer) {
94 lstrcpyW(s1->szData+s1->nLen, s2->szData);
100 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
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);
107 s1->nLen += s2->nLen;
111 ME_String *ME_ConcatString(const ME_String *s1, const ME_String *s2)
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);
122 ME_String *ME_VSplitString(ME_String *orig, int charidx)
126 /*if (charidx<0) charidx = 0;
127 if (charidx>orig->nLen) charidx = orig->nLen;
130 assert(charidx<=orig->nLen);
132 s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
133 orig->nLen = charidx;
134 orig->szData[charidx] = '\0';
138 int ME_IsWhitespaces(const ME_String *s)
140 /* FIXME multibyte */
141 WCHAR *pos = s->szData;
142 while(ME_IsWSpace(*pos++))
151 int ME_IsSplitable(const ME_String *s)
153 WCHAR *pos = s->szData;
155 while(ME_IsWSpace(*pos++))
158 while((ch = *pos++) != 0)
166 /* FIXME multibyte */
168 int ME_CalcSkipChars(ME_String *s)
171 while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
177 int ME_StrLen(const ME_String *s) {
181 int ME_StrVLen(const ME_String *s) {
185 int ME_StrRelPos(const ME_String *s, int nVChar, int *pRelChars)
187 int nRelChars = *pRelChars;
189 TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars);
196 nRelChars = min(*pRelChars, s->nLen - nVChar);
198 nRelChars = max(*pRelChars, -nVChar);
200 *pRelChars -= nRelChars;
204 int ME_StrRelPos2(const ME_String *s, int nVChar, int nRelChars)
206 return ME_StrRelPos(s, nVChar, &nRelChars);
209 int ME_VPosToPos(ME_String *s, int nVPos)
220 if (s->szData[i]=='\\') i++;
228 int ME_PosToVPos(const ME_String *s, int nPos)
232 return ME_StrRelPos2(s, 0, nPos);
235 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
239 assert(nVChar >=0 && nVChar <= s->nLen);
241 assert(nVChar+nChars <= s->nLen);
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);
249 int ME_GetCharFwd(const ME_String *s, int nPos)
253 assert(nPos < ME_StrLen(s));
255 nVPos = ME_StrRelPos2(s, nVPos, nPos);
258 return s->szData[nVPos];
262 int ME_GetCharBack(const ME_String *s, int nPos)
264 int nVPos = ME_StrVLen(s);
266 assert(nPos < ME_StrLen(s));
268 nVPos = ME_StrRelPos2(s, nVPos, -nPos);
271 return s->szData[nVPos];
275 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
277 for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
283 /* note: returns offset of the first trailing whitespace */
284 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
286 for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
292 /* note: returns offset of the first trailing nonwhitespace */
293 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
295 for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
303 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
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);
313 return ME_IsWSpace(s[start]);
315 case WB_MOVEWORDLEFT:
316 while (start && ME_IsWSpace(s[start - 1]))
318 while (start && !ME_IsWSpace(s[start - 1]))
322 case WB_MOVEWORDRIGHT:
323 while (start < len && !ME_IsWSpace(s[start]))
325 while (start < len && ME_IsWSpace(s[start]))
334 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
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);
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);
355 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
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);
370 void ME_EndToUnicode(BOOL unicode, LPVOID psz)