riched20: Pass character ptrs to the whitespace finding functions.
[wine.git] / dlls / riched20 / string.c
blob0e133944ea350b4cda01a2e6f67195549ac5b499
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 static int ME_GetOptimalBuffer(int nLen)
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;
32 /* Create a buffer (uninitialized string) of size nMaxChars */
33 static ME_String *ME_MakeStringB(int nMaxChars)
35 ME_String *s = ALLOC_OBJ(ME_String);
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;
44 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
46 ME_String *s = ME_MakeStringB(nMaxChars);
47 /* Native allows NULL chars */
48 memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
49 return s;
52 /* Make a string by repeating a char nMaxChars times */
53 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
55 int i;
56 ME_String *s = ME_MakeStringB(nMaxChars);
57 for (i = 0; i < nMaxChars; i++)
58 s->szData[i] = cRepeat;
59 return s;
62 ME_String *ME_StrDup(const ME_String *s)
64 return ME_MakeStringN(s->szData, s->nLen);
67 void ME_DestroyString(ME_String *s)
69 if (!s) return;
70 FREE_OBJ(s->szData);
71 FREE_OBJ(s);
74 void ME_AppendString(ME_String *s1, const ME_String *s2)
76 if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
78 memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
79 s1->nLen += s2->nLen;
80 s1->szData[s1->nLen] = 0;
81 } else {
82 WCHAR *buf;
83 s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
85 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));
88 FREE_OBJ(s1->szData);
89 s1->szData = buf;
90 s1->nLen += s2->nLen;
91 s1->szData[s1->nLen] = 0;
95 ME_String *ME_VSplitString(ME_String *orig, int charidx)
97 ME_String *s;
99 /*if (charidx<0) charidx = 0;
100 if (charidx>orig->nLen) charidx = orig->nLen;
102 assert(charidx>=0);
103 assert(charidx<=orig->nLen);
105 s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
106 orig->nLen = charidx;
107 orig->szData[charidx] = '\0';
108 return s;
111 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
113 int end_ofs = nVChar + nChars;
115 assert(nChars >= 0);
116 assert(nVChar >= 0);
117 assert(end_ofs <= s->nLen);
119 memmove(s->szData + nVChar, s->szData + end_ofs,
120 (s->nLen - end_ofs + 1) * sizeof(WCHAR));
121 s->nLen -= nChars;
124 static int
125 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
127 /* FIXME: Native also knows about punctuation */
128 TRACE("s==%s, start==%d, len==%d, code==%d\n",
129 debugstr_wn(s, len), start, len, code);
130 /* convert number of bytes to number of characters. */
131 len /= sizeof(WCHAR);
132 switch (code)
134 case WB_ISDELIMITER:
135 return ME_IsWSpace(s[start]);
136 case WB_LEFT:
137 case WB_MOVEWORDLEFT:
138 while (start && ME_IsWSpace(s[start - 1]))
139 start--;
140 while (start && !ME_IsWSpace(s[start - 1]))
141 start--;
142 return start;
143 case WB_RIGHT:
144 case WB_MOVEWORDRIGHT:
145 while (start < len && !ME_IsWSpace(s[start]))
146 start++;
147 while (start < len && ME_IsWSpace(s[start]))
148 start++;
149 return start;
151 return 0;
156 ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT code)
158 if (!editor->pfnWordBreak) {
159 return ME_WordBreakProc(str, start, len * sizeof(WCHAR), code);
160 } else if (!editor->bEmulateVersion10) {
161 /* MSDN lied about the third parameter for EditWordBreakProc being the number
162 * of characters, it is actually the number of bytes of the string. */
163 return editor->pfnWordBreak(str, start, len * sizeof(WCHAR), code);
164 } else {
165 int result;
166 int buffer_size = WideCharToMultiByte(CP_ACP, 0, str, len,
167 NULL, 0, NULL, NULL);
168 char *buffer = heap_alloc(buffer_size);
169 WideCharToMultiByte(CP_ACP, 0, str, len,
170 buffer, buffer_size, NULL, NULL);
171 result = editor->pfnWordBreak((WCHAR*)buffer, start, buffer_size, code);
172 heap_free(buffer);
173 return result;
177 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
179 assert(psz != NULL);
181 if (unicode)
182 return psz;
183 else {
184 WCHAR *tmp;
185 int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
186 if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
187 MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
188 return tmp;
192 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
194 if (!unicode)
195 FREE_OBJ(psz);