wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[wine] / dlls / riched20 / string.c
1 /*
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
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 static int ME_GetOptimalBuffer(int nLen)
26 {
27   /* 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;
30 }
31
32 /* Create a buffer (uninitialized string) of size nMaxChars */
33 static ME_String *ME_MakeStringB(int nMaxChars)
34 {
35   ME_String *s = ALLOC_OBJ(ME_String);
36
37   s->nLen = nMaxChars;
38   s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
39   s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
40   s->szData[s->nLen] = 0;
41   return s;
42 }
43
44 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
45 {
46   ME_String *s = ME_MakeStringB(nMaxChars);
47   /* Native allows NULL chars */
48   memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
49   return s;
50 }
51
52 ME_String *ME_MakeString(LPCWSTR szText)
53 {
54   return ME_MakeStringN(szText, lstrlenW(szText));
55 }
56
57 /* Make a string by repeating a char nMaxChars times */
58 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
59 {
60   int i;
61   ME_String *s = ME_MakeStringB(nMaxChars);
62   for (i = 0; i < nMaxChars; i++)
63     s->szData[i] = cRepeat;
64   return s;
65 }
66
67 ME_String *ME_StrDup(const ME_String *s)
68 {
69   return ME_MakeStringN(s->szData, s->nLen);
70 }
71
72 void ME_DestroyString(ME_String *s)
73 {
74   if (!s) return;
75   FREE_OBJ(s->szData);
76   FREE_OBJ(s);
77 }
78
79 void ME_AppendString(ME_String *s1, const ME_String *s2)
80 {
81   if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
82   {
83     memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
84     s1->nLen += s2->nLen;
85     s1->szData[s1->nLen] = 0;
86   } else {
87     WCHAR *buf;
88     s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
89
90     buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
91     memcpy(buf, s1->szData, s1->nLen * sizeof(WCHAR));
92     memcpy(buf + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
93     FREE_OBJ(s1->szData);
94     s1->szData = buf;
95     s1->nLen += s2->nLen;
96     s1->szData[s1->nLen] = 0;
97   }
98 }
99
100 ME_String *ME_VSplitString(ME_String *orig, int charidx)
101 {
102   ME_String *s;
103
104   /*if (charidx<0) charidx = 0;
105   if (charidx>orig->nLen) charidx = orig->nLen;
106   */
107   assert(charidx>=0);
108   assert(charidx<=orig->nLen);
109
110   s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
111   orig->nLen = charidx;
112   orig->szData[charidx] = '\0';
113   return s;
114 }
115
116 int ME_IsWhitespaces(const ME_String *s)
117 {
118   /* FIXME multibyte */
119   WCHAR *pos = s->szData;
120   while(ME_IsWSpace(*pos++))
121     ;
122   pos--;
123   if (*pos)
124     return 0;
125   else
126     return 1;
127 }
128
129 int ME_IsSplitable(const ME_String *s)
130 {
131   WCHAR *pos = s->szData;
132   WCHAR ch;
133   while(ME_IsWSpace(*pos++))
134     ;
135   pos--;
136   while((ch = *pos++) != 0)
137   {
138     if (ME_IsWSpace(ch))
139       return 1;
140   }
141   return 0;
142 }
143
144 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
145 {
146   int end_ofs = nVChar + nChars;
147
148   assert(nChars >= 0);
149   assert(nVChar >= 0);
150   assert(end_ofs <= s->nLen);
151
152   memmove(s->szData + nVChar, s->szData + end_ofs,
153           (s->nLen - end_ofs + 1) * sizeof(WCHAR));
154   s->nLen -= nChars;
155 }
156
157 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
158   int i;
159   for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
160     ;
161     
162   return i;
163 }
164
165 /* note: returns offset of the first trailing whitespace */
166 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
167   int i;
168   for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
169     ;
170     
171   return i;
172 }
173
174 /* note: returns offset of the first trailing nonwhitespace */
175 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
176   int i;
177   for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
178     ;
179     
180   return i;
181 }
182
183
184 static int
185 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
186 {
187   /* FIXME: Native also knows about punctuation */
188   TRACE("s==%s, start==%d, len==%d, code==%d\n",
189         debugstr_wn(s, len), start, len, code);
190   /* convert number of bytes to number of characters. */
191   len /= sizeof(WCHAR);
192   switch (code)
193   {
194     case WB_ISDELIMITER:
195       return ME_IsWSpace(s[start]);
196     case WB_LEFT:
197     case WB_MOVEWORDLEFT:
198       while (start && ME_IsWSpace(s[start - 1]))
199         start--;
200       while (start && !ME_IsWSpace(s[start - 1]))
201         start--;
202       return start;
203     case WB_RIGHT:
204     case WB_MOVEWORDRIGHT:
205       while (start < len && !ME_IsWSpace(s[start]))
206         start++;
207       while (start < len && ME_IsWSpace(s[start]))
208         start++;
209       return start;
210   }
211   return 0;
212 }
213
214
215 int
216 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
217 {
218   if (!editor->pfnWordBreak) {
219     return ME_WordBreakProc(str->szData, start, str->nLen*sizeof(WCHAR), code);
220   } else if (!editor->bEmulateVersion10) {
221     /* MSDN lied about the third parameter for EditWordBreakProc being the number
222      * of characters, it is actually the number of bytes of the string. */
223     return editor->pfnWordBreak(str->szData, start, str->nLen*sizeof(WCHAR), code);
224   } else {
225     int result;
226     int buffer_size = WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
227                                           NULL, 0, NULL, NULL);
228     char *buffer = heap_alloc(buffer_size);
229     WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
230                         buffer, buffer_size, NULL, NULL);
231     result = editor->pfnWordBreak(str->szData, start, str->nLen, code);
232     heap_free(buffer);
233     return result;
234   }
235 }
236
237 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
238 {
239   assert(psz != NULL);
240
241   if (unicode)
242     return psz;
243   else {
244     WCHAR *tmp;
245     int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
246     if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
247       MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
248     return tmp;
249   }
250 }
251
252 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
253 {
254   if (!unicode)
255     FREE_OBJ(psz);
256 }