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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 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_StrDup(ME_String *s)
55 return ME_MakeStringN(s->szData, s->nLen);
58 void ME_DestroyString(ME_String *s)
64 void ME_AppendString(ME_String *s1, ME_String *s2)
66 if (s1->nLen+s2->nLen+1 <= s1->nBuffer) {
67 lstrcpyW(s1->szData+s1->nLen, s2->szData);
73 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
75 buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
76 lstrcpyW(buf, s1->szData);
77 lstrcpyW(buf+s1->nLen, s2->szData);
84 ME_String *ME_ConcatString(ME_String *s1, ME_String *s2)
86 ME_String *s = ALLOC_OBJ(ME_String);
87 s->nLen = s1->nLen+s2->nLen;
88 s->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
89 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
90 lstrcpyW(s->szData, s1->szData);
91 lstrcpyW(s->szData+s1->nLen, s2->szData);
95 ME_String *ME_VSplitString(ME_String *orig, int charidx)
99 /*if (charidx<0) charidx = 0;
100 if (charidx>orig->nLen) charidx = orig->nLen;
103 assert(charidx<=orig->nLen);
105 s = ME_MakeString(orig->szData+charidx);
106 orig->nLen = charidx;
107 orig->szData[charidx] = L'\0';
111 int ME_IsWhitespaces(ME_String *s)
113 /* FIXME multibyte */
114 WCHAR *pos = s->szData;
115 while(ME_IsWSpace(*pos++))
124 int ME_IsSplitable(ME_String *s)
126 WCHAR *pos = s->szData;
128 while(ME_IsWSpace(*pos++))
131 while((ch = *pos++) != 0)
139 /* FIXME multibyte */
141 int ME_CalcSkipChars(ME_String *s)
144 while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
150 int ME_StrLen(ME_String *s) {
154 int ME_StrVLen(ME_String *s) {
158 int ME_StrRelPos(ME_String *s, int nVChar, int *pRelChars)
160 int nRelChars = *pRelChars;
162 TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars);
169 nRelChars = min(*pRelChars, s->nLen - nVChar);
171 nRelChars = max(*pRelChars, -nVChar);
173 *pRelChars -= nRelChars;
177 int ME_StrRelPos2(ME_String *s, int nVChar, int nRelChars)
179 return ME_StrRelPos(s, nVChar, &nRelChars);
182 int ME_VPosToPos(ME_String *s, int nVPos)
193 if (s->szData[i]=='\\') i++;
201 int ME_PosToVPos(ME_String *s, int nPos)
205 return ME_StrRelPos2(s, 0, nPos);
208 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
212 assert(nVChar >=0 && nVChar <= s->nLen);
214 assert(nVChar+nChars <= s->nLen);
216 end_ofs = ME_StrRelPos2(s, nVChar, nChars);
217 assert(end_ofs <= s->nLen);
218 memmove(s->szData+nVChar, s->szData+end_ofs, 2*(s->nLen+1-end_ofs));
219 s->nLen -= (end_ofs - nVChar);
222 int ME_GetCharFwd(ME_String *s, int nPos)
226 assert(nPos < ME_StrLen(s));
228 nVPos = ME_StrRelPos2(s, nVPos, nPos);
231 return s->szData[nVPos];
235 int ME_GetCharBack(ME_String *s, int nPos)
237 int nVPos = ME_StrVLen(s);
239 assert(nPos < ME_StrLen(s));
241 nVPos = ME_StrRelPos2(s, nVPos, -nPos);
244 return s->szData[nVPos];
248 int ME_FindNonWhitespaceV(ME_String *s, int nVChar) {
250 for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
256 /* note: returns offset of the first trailing whitespace */
257 int ME_ReverseFindNonWhitespaceV(ME_String *s, int nVChar) {
259 for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
265 /* note: returns offset of the first trailing nonwhitespace */
266 int ME_ReverseFindWhitespaceV(ME_String *s, int nVChar) {
268 for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
276 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
278 /* FIXME: Native also knows about punctuation */
279 TRACE("s==%s, start==%d, len==%d, code==%d\n",
280 debugstr_wn(s, len), start, len, code);
284 return ME_IsWSpace(s[start]);
286 case WB_MOVEWORDLEFT:
287 while (start && ME_IsWSpace(s[start - 1]))
289 while (start && !ME_IsWSpace(s[start - 1]))
293 case WB_MOVEWORDRIGHT:
294 if (start && ME_IsWSpace(s[start - 1]))
296 while (start < len && ME_IsWSpace(s[start]))
301 while (start < len && !ME_IsWSpace(s[start]))
303 while (start < len && ME_IsWSpace(s[start]))
313 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
315 /* FIXME: ANSIfy the string when bEmulateVersion10 is TRUE */
316 if (!editor->pfnWordBreak)
317 return ME_WordBreakProc(str->szData, start, str->nLen, code);
319 return editor->pfnWordBreak(str->szData, start, str->nLen, code);
322 LPWSTR ME_ToUnicode(HWND hWnd, LPVOID psz)
324 if (IsWindowUnicode(hWnd))
328 int nChars = MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, NULL, 0);
329 if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
330 MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, tmp, nChars);
335 void ME_EndToUnicode(HWND hWnd, LPVOID psz)
337 if (IsWindowUnicode(hWnd))
341 LPSTR ME_ToAnsi(HWND hWnd, LPVOID psz)
343 if (!IsWindowUnicode(hWnd))
347 int nChars = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)psz, -1, NULL, 0, NULL, NULL);
348 if((tmp = ALLOC_N_OBJ(char, nChars)) != NULL)
349 WideCharToMultiByte(CP_ACP, 0, (WCHAR *)psz, -1, tmp, nChars, NULL, NULL);
354 void ME_EndToAnsi(HWND hWnd, LPVOID psz)
356 if (!IsWindowUnicode(hWnd))