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 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)
90 void ME_AppendString(ME_String *s1, const ME_String *s2)
92 if (s1->nLen+s2->nLen+1 <= s1->nBuffer) {
93 lstrcpyW(s1->szData+s1->nLen, s2->szData);
99 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
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);
106 s1->nLen += s2->nLen;
110 ME_String *ME_ConcatString(const ME_String *s1, const ME_String *s2)
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);
121 ME_String *ME_VSplitString(ME_String *orig, int charidx)
125 /*if (charidx<0) charidx = 0;
126 if (charidx>orig->nLen) charidx = orig->nLen;
129 assert(charidx<=orig->nLen);
131 s = ME_MakeString(orig->szData+charidx);
132 orig->nLen = charidx;
133 orig->szData[charidx] = '\0';
137 int ME_IsWhitespaces(const ME_String *s)
139 /* FIXME multibyte */
140 WCHAR *pos = s->szData;
141 while(ME_IsWSpace(*pos++))
150 int ME_IsSplitable(const ME_String *s)
152 WCHAR *pos = s->szData;
154 while(ME_IsWSpace(*pos++))
157 while((ch = *pos++) != 0)
165 /* FIXME multibyte */
167 int ME_CalcSkipChars(ME_String *s)
170 while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
176 int ME_StrLen(const ME_String *s) {
180 int ME_StrVLen(const ME_String *s) {
184 int ME_StrRelPos(const ME_String *s, int nVChar, int *pRelChars)
186 int nRelChars = *pRelChars;
188 TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars);
195 nRelChars = min(*pRelChars, s->nLen - nVChar);
197 nRelChars = max(*pRelChars, -nVChar);
199 *pRelChars -= nRelChars;
203 int ME_StrRelPos2(const ME_String *s, int nVChar, int nRelChars)
205 return ME_StrRelPos(s, nVChar, &nRelChars);
208 int ME_VPosToPos(ME_String *s, int nVPos)
219 if (s->szData[i]=='\\') i++;
227 int ME_PosToVPos(const ME_String *s, int nPos)
231 return ME_StrRelPos2(s, 0, nPos);
234 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
238 assert(nVChar >=0 && nVChar <= s->nLen);
240 assert(nVChar+nChars <= s->nLen);
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);
248 int ME_GetCharFwd(const ME_String *s, int nPos)
252 assert(nPos < ME_StrLen(s));
254 nVPos = ME_StrRelPos2(s, nVPos, nPos);
257 return s->szData[nVPos];
261 int ME_GetCharBack(const ME_String *s, int nPos)
263 int nVPos = ME_StrVLen(s);
265 assert(nPos < ME_StrLen(s));
267 nVPos = ME_StrRelPos2(s, nVPos, -nPos);
270 return s->szData[nVPos];
274 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
276 for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
282 /* note: returns offset of the first trailing whitespace */
283 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
285 for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
291 /* note: returns offset of the first trailing nonwhitespace */
292 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
294 for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
302 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
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);
310 return ME_IsWSpace(s[start]);
312 case WB_MOVEWORDLEFT:
313 while (start && ME_IsWSpace(s[start - 1]))
315 while (start && !ME_IsWSpace(s[start - 1]))
319 case WB_MOVEWORDRIGHT:
320 if (start && ME_IsWSpace(s[start - 1]))
322 while (start < len && ME_IsWSpace(s[start]))
327 while (start < len && !ME_IsWSpace(s[start]))
329 while (start < len && ME_IsWSpace(s[start]))
339 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
341 /* FIXME: ANSIfy the string when bEmulateVersion10 is TRUE */
342 if (!editor->pfnWordBreak)
343 return ME_WordBreakProc(str->szData, start, str->nLen, code);
345 return editor->pfnWordBreak(str->szData, start, str->nLen, code);
348 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
356 int nChars = MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, NULL, 0);
357 if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
358 MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, tmp, nChars);
363 void ME_EndToUnicode(BOOL unicode, LPVOID psz)