wined3d: CreateAdditionalSwapChain bugfix.
[wine/wine64.git] / dlls / riched20 / string.c
blob2ec0092fcab2b3ccfcae04ae4077164437756ff3
1 /*
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
21 #include "editor.h"
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);
37 return s;
40 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
42 ME_String *s = ALLOC_OBJ(ME_String);
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;
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);
58 s->nLen = nMaxChars;
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;
65 return s;
68 ME_String *ME_MakeStringB(int nMaxChars)
69 { /* Create a buffer (uninitialized string) of size nMaxChars */
70 ME_String *s = ALLOC_OBJ(ME_String);
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;
79 ME_String *ME_StrDup(const ME_String *s)
81 return ME_MakeStringN(s->szData, s->nLen);
84 void ME_DestroyString(ME_String *s)
86 FREE_OBJ(s->szData);
87 FREE_OBJ(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);
94 s1->nLen += s2->nLen;
96 else
98 WCHAR *buf;
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);
105 s1->szData = buf;
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);
118 return s;
121 ME_String *ME_VSplitString(ME_String *orig, int charidx)
123 ME_String *s;
125 /*if (charidx<0) charidx = 0;
126 if (charidx>orig->nLen) charidx = orig->nLen;
128 assert(charidx>=0);
129 assert(charidx<=orig->nLen);
131 s = ME_MakeString(orig->szData+charidx);
132 orig->nLen = charidx;
133 orig->szData[charidx] = '\0';
134 return s;
137 int ME_IsWhitespaces(const ME_String *s)
139 /* FIXME multibyte */
140 WCHAR *pos = s->szData;
141 while(ME_IsWSpace(*pos++))
143 pos--;
144 if (*pos)
145 return 0;
146 else
147 return 1;
150 int ME_IsSplitable(const ME_String *s)
152 WCHAR *pos = s->szData;
153 WCHAR ch;
154 while(ME_IsWSpace(*pos++))
156 pos--;
157 while((ch = *pos++) != 0)
159 if (ME_IsWSpace(ch))
160 return 1;
162 return 0;
165 /* FIXME multibyte */
167 int ME_CalcSkipChars(ME_String *s)
169 int cnt = 0;
170 while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ')
171 cnt++;
172 return cnt;
176 int ME_StrLen(const ME_String *s) {
177 return s->nLen;
180 int ME_StrVLen(const ME_String *s) {
181 return s->nLen;
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);
190 assert(*pRelChars);
191 if (!nRelChars)
192 return nVChar;
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;
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)
210 return nVPos;
212 int i = 0, len = 0;
213 if (!nVPos)
214 return 0;
215 while (i < s->nLen)
217 if (i == nVPos)
218 return len;
219 if (s->szData[i]=='\\') i++;
220 i++;
221 len++;
223 return len;
227 int ME_PosToVPos(const ME_String *s, int nPos)
229 if (!nPos)
230 return 0;
231 return ME_StrRelPos2(s, 0, nPos);
234 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
236 int end_ofs;
238 assert(nVChar >=0 && nVChar <= s->nLen);
239 assert(nChars >= 0);
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)
250 int nVPos = 0;
252 assert(nPos < ME_StrLen(s));
253 if (nPos)
254 nVPos = ME_StrRelPos2(s, nVPos, nPos);
256 if (nVPos < s->nLen)
257 return s->szData[nVPos];
258 return -1;
261 int ME_GetCharBack(const ME_String *s, int nPos)
263 int nVPos = ME_StrVLen(s);
265 assert(nPos < ME_StrLen(s));
266 if (nPos)
267 nVPos = ME_StrRelPos2(s, nVPos, -nPos);
269 if (nVPos < s->nLen)
270 return s->szData[nVPos];
271 return -1;
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++)
279 return i;
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--)
288 return i;
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--)
297 return i;
301 static int
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);
307 switch (code)
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 if (start && ME_IsWSpace(s[start - 1]))
322 while (start < len && ME_IsWSpace(s[start]))
323 start++;
325 else
327 while (start < len && !ME_IsWSpace(s[start]))
328 start++;
329 while (start < len && ME_IsWSpace(s[start]))
330 start++;
332 return start;
334 return 0;
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);
344 else
345 return editor->pfnWordBreak(str->szData, start, str->nLen, code);
348 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
350 assert(psz != NULL);
352 if (unicode)
353 return (LPWSTR)psz;
354 else {
355 WCHAR *tmp;
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);
359 return tmp;
363 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
365 if (!unicode)
366 FREE_OBJ(psz);