blob: db45d928d64316cb1a4069c84bcdacc13d989fa1 [file] [log] [blame]
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +00001/*
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000019 */
20
Dylan Smith4b7e8f12009-02-07 13:20:55 -050021#include "editor.h"
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000022
23WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
Andrew Talbotb53d7d32009-01-17 16:27:08 +000025static int ME_GetOptimalBuffer(int nLen)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000026{
Dylan Smith4b7e8f12009-02-07 13:20:55 -050027 /* FIXME: This seems wasteful for tabs and end of lines strings,
28 * since they have a small fixed length. */
29 return ((sizeof(WCHAR) * nLen) + 128) & ~63;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000030}
31
Dylan Smith4b7e8f12009-02-07 13:20:55 -050032/* Create a buffer (uninitialized string) of size nMaxChars */
33static ME_String *ME_MakeStringB(int nMaxChars)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000034{
35 ME_String *s = ALLOC_OBJ(ME_String);
Dylan Smith4b7e8f12009-02-07 13:20:55 -050036
37 s->nLen = nMaxChars;
38 s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000039 s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
Dylan Smith4b7e8f12009-02-07 13:20:55 -050040 s->szData[s->nLen] = 0;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000041 return s;
42}
43
44ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
45{
Dylan Smith4b7e8f12009-02-07 13:20:55 -050046 ME_String *s = ME_MakeStringB(nMaxChars);
47 /* Native allows NULL chars */
48 memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000049 return s;
50}
51
Dylan Smith4b7e8f12009-02-07 13:20:55 -050052/* Make a string by repeating a char nMaxChars times */
Matt Finnicum2b92bf72006-08-04 15:47:44 -040053ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
Dylan Smith4b7e8f12009-02-07 13:20:55 -050054{
Matt Finnicum2b92bf72006-08-04 15:47:44 -040055 int i;
Dylan Smith4b7e8f12009-02-07 13:20:55 -050056 ME_String *s = ME_MakeStringB(nMaxChars);
57 for (i = 0; i < nMaxChars; i++)
Matt Finnicum2b92bf72006-08-04 15:47:44 -040058 s->szData[i] = cRepeat;
Matt Finnicum2b92bf72006-08-04 15:47:44 -040059 return s;
60}
61
Andrew Talbot291dd7a2007-08-15 21:35:51 +010062ME_String *ME_StrDup(const ME_String *s)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000063{
64 return ME_MakeStringN(s->szData, s->nLen);
65}
66
67void ME_DestroyString(ME_String *s)
68{
Dylan Smith5d74f582009-01-28 01:34:56 -050069 if (!s) return;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000070 FREE_OBJ(s->szData);
71 FREE_OBJ(s);
72}
73
Andrew Talbot291dd7a2007-08-15 21:35:51 +010074void ME_AppendString(ME_String *s1, const ME_String *s2)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000075{
Dylan Smithf53f40b2009-02-07 13:20:46 -050076 if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000077 {
Dylan Smithf53f40b2009-02-07 13:20:46 -050078 memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
79 s1->nLen += s2->nLen;
80 s1->szData[s1->nLen] = 0;
81 } else {
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000082 WCHAR *buf;
83 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
84
Dylan Smithf53f40b2009-02-07 13:20:46 -050085 buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
86 memcpy(buf, s1->szData, s1->nLen * sizeof(WCHAR));
87 memcpy(buf + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000088 FREE_OBJ(s1->szData);
89 s1->szData = buf;
90 s1->nLen += s2->nLen;
Dylan Smithf53f40b2009-02-07 13:20:46 -050091 s1->szData[s1->nLen] = 0;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000092 }
93}
94
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +000095ME_String *ME_VSplitString(ME_String *orig, int charidx)
96{
97 ME_String *s;
98
99 /*if (charidx<0) charidx = 0;
100 if (charidx>orig->nLen) charidx = orig->nLen;
101 */
102 assert(charidx>=0);
103 assert(charidx<=orig->nLen);
104
Dylan Smith6d76d432008-06-25 11:33:26 -0400105 s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000106 orig->nLen = charidx;
Michael Stefaniucdf01f672007-06-21 22:56:17 +0200107 orig->szData[charidx] = '\0';
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000108 return s;
109}
110
Andrew Talbot291dd7a2007-08-15 21:35:51 +0100111int ME_IsWhitespaces(const ME_String *s)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000112{
113 /* FIXME multibyte */
114 WCHAR *pos = s->szData;
Krzysztof Foltmanf089de12005-03-17 10:23:40 +0000115 while(ME_IsWSpace(*pos++))
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000116 ;
117 pos--;
118 if (*pos)
119 return 0;
120 else
121 return 1;
122}
123
Andrew Talbot291dd7a2007-08-15 21:35:51 +0100124int ME_IsSplitable(const ME_String *s)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000125{
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000126 WCHAR *pos = s->szData;
127 WCHAR ch;
Krzysztof Foltmanf089de12005-03-17 10:23:40 +0000128 while(ME_IsWSpace(*pos++))
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000129 ;
130 pos--;
131 while((ch = *pos++) != 0)
132 {
Krzysztof Foltman810b2612005-03-19 17:06:17 +0000133 if (ME_IsWSpace(ch))
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000134 return 1;
135 }
136 return 0;
137}
138
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000139void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
140{
Dylan Smithc8b44552009-02-07 13:21:29 -0500141 int end_ofs = nVChar + nChars;
142
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000143 assert(nChars >= 0);
Dylan Smithc8b44552009-02-07 13:21:29 -0500144 assert(nVChar >= 0);
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000145 assert(end_ofs <= s->nLen);
Dylan Smithc8b44552009-02-07 13:21:29 -0500146
147 memmove(s->szData + nVChar, s->szData + end_ofs,
148 (s->nLen - end_ofs + 1) * sizeof(WCHAR));
149 s->nLen -= nChars;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000150}
151
Andrew Talbot291dd7a2007-08-15 21:35:51 +0100152int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000153 int i;
Krzysztof Foltman810b2612005-03-19 17:06:17 +0000154 for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000155 ;
156
157 return i;
158}
159
160/* note: returns offset of the first trailing whitespace */
Andrew Talbot291dd7a2007-08-15 21:35:51 +0100161int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000162 int i;
Krzysztof Foltman810b2612005-03-19 17:06:17 +0000163 for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000164 ;
165
166 return i;
167}
168
169/* note: returns offset of the first trailing nonwhitespace */
Andrew Talbot291dd7a2007-08-15 21:35:51 +0100170int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000171 int i;
Krzysztof Foltman810b2612005-03-19 17:06:17 +0000172 for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000173 ;
174
175 return i;
176}
177
Phil Krylov49eecf52006-01-12 11:54:57 +0100178
179static int
180ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
181{
182 /* FIXME: Native also knows about punctuation */
183 TRACE("s==%s, start==%d, len==%d, code==%d\n",
184 debugstr_wn(s, len), start, len, code);
Dylan Smith50397292008-10-23 01:07:31 -0400185 /* convert number of bytes to number of characters. */
186 len /= sizeof(WCHAR);
Phil Krylov49eecf52006-01-12 11:54:57 +0100187 switch (code)
188 {
189 case WB_ISDELIMITER:
190 return ME_IsWSpace(s[start]);
191 case WB_LEFT:
192 case WB_MOVEWORDLEFT:
193 while (start && ME_IsWSpace(s[start - 1]))
194 start--;
195 while (start && !ME_IsWSpace(s[start - 1]))
196 start--;
197 return start;
198 case WB_RIGHT:
199 case WB_MOVEWORDRIGHT:
Dylan Smithba747f42008-06-25 11:33:19 -0400200 while (start < len && !ME_IsWSpace(s[start]))
201 start++;
202 while (start < len && ME_IsWSpace(s[start]))
203 start++;
Phil Krylov49eecf52006-01-12 11:54:57 +0100204 return start;
205 }
206 return 0;
207}
208
209
210int
211ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
212{
Dylan Smith50397292008-10-23 01:07:31 -0400213 if (!editor->pfnWordBreak) {
214 return ME_WordBreakProc(str->szData, start, str->nLen*sizeof(WCHAR), code);
215 } else if (!editor->bEmulateVersion10) {
216 /* MSDN lied about the third parameter for EditWordBreakProc being the number
217 * of characters, it is actually the number of bytes of the string. */
218 return editor->pfnWordBreak(str->szData, start, str->nLen*sizeof(WCHAR), code);
219 } else {
220 int result;
221 int buffer_size = WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
222 NULL, 0, NULL, NULL);
Michael Stefaniuc90024d02008-11-03 22:36:03 +0100223 char *buffer = heap_alloc(buffer_size);
Dylan Smith50397292008-10-23 01:07:31 -0400224 WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
225 buffer, buffer_size, NULL, NULL);
226 result = editor->pfnWordBreak(str->szData, start, str->nLen, code);
227 heap_free(buffer);
228 return result;
229 }
Phil Krylov49eecf52006-01-12 11:54:57 +0100230}
231
Dmitry Timoshkov592b53d2007-03-22 19:09:43 +0800232LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000233{
Dmitry Timoshkov9bde4112007-05-02 17:59:21 +0900234 assert(psz != NULL);
235
Dmitry Timoshkov592b53d2007-03-22 19:09:43 +0800236 if (unicode)
Michael Stefaniucd1a7e412009-01-30 10:40:02 +0100237 return psz;
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000238 else {
239 WCHAR *tmp;
Michael Stefaniucd1a7e412009-01-30 10:40:02 +0100240 int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000241 if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
Michael Stefaniucd1a7e412009-01-30 10:40:02 +0100242 MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000243 return tmp;
244 }
245}
246
Dmitry Timoshkov592b53d2007-03-22 19:09:43 +0800247void ME_EndToUnicode(BOOL unicode, LPVOID psz)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000248{
Dmitry Timoshkov592b53d2007-03-22 19:09:43 +0800249 if (!unicode)
Krzysztof Foltmand488f3f2005-03-05 11:19:14 +0000250 FREE_OBJ(psz);
251}