Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include "editor.h" |
| 22 | |
| 23 | WINE_DEFAULT_DEBUG_CHANNEL(richedit); |
| 24 | |
| 25 | int ME_GetOptimalBuffer(int nLen) |
| 26 | { |
| 27 | return ((2*nLen+1)+128)&~63; |
| 28 | } |
| 29 | |
| 30 | ME_String *ME_MakeString(LPCWSTR szText) |
| 31 | { |
| 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; |
| 38 | } |
| 39 | |
| 40 | ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars) |
| 41 | { |
| 42 | ME_String *s = ALLOC_OBJ(ME_String); |
| 43 | int i; |
| 44 | for (i=0; i<nMaxChars && szText[i]; i++) |
| 45 | ; |
| 46 | s->nLen = i; |
| 47 | s->nBuffer = ME_GetOptimalBuffer(s->nLen+1); |
| 48 | s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer); |
| 49 | lstrcpynW(s->szData, szText, s->nLen+1); |
| 50 | return s; |
| 51 | } |
| 52 | |
| 53 | ME_String *ME_StrDup(ME_String *s) |
| 54 | { |
| 55 | return ME_MakeStringN(s->szData, s->nLen); |
| 56 | } |
| 57 | |
| 58 | void ME_DestroyString(ME_String *s) |
| 59 | { |
| 60 | FREE_OBJ(s->szData); |
| 61 | FREE_OBJ(s); |
| 62 | } |
| 63 | |
| 64 | void ME_AppendString(ME_String *s1, ME_String *s2) |
| 65 | { |
| 66 | if (s1->nLen+s2->nLen+1 <= s1->nBuffer) { |
| 67 | lstrcpyW(s1->szData+s1->nLen, s2->szData); |
| 68 | s1->nLen += s2->nLen; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | WCHAR *buf; |
| 73 | s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1); |
| 74 | |
| 75 | buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer); |
| 76 | lstrcpyW(buf, s1->szData); |
| 77 | lstrcpyW(buf+s1->nLen, s2->szData); |
| 78 | FREE_OBJ(s1->szData); |
| 79 | s1->szData = buf; |
| 80 | s1->nLen += s2->nLen; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ME_String *ME_ConcatString(ME_String *s1, ME_String *s2) |
| 85 | { |
| 86 | ME_String *s = ALLOC_OBJ(ME_String); |
| 87 | s->nLen = s1->nLen+s2->nLen; |
| 88 | s->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1); |
| 89 | s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer); |
| 90 | lstrcpyW(s->szData, s1->szData); |
| 91 | lstrcpyW(s->szData+s1->nLen, s2->szData); |
| 92 | return s; |
| 93 | } |
| 94 | |
| 95 | ME_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 | |
| 105 | s = ME_MakeString(orig->szData+charidx); |
| 106 | orig->nLen = charidx; |
| 107 | orig->szData[charidx] = L'\0'; |
| 108 | return s; |
| 109 | } |
| 110 | |
| 111 | int ME_IsWhitespaces(ME_String *s) |
| 112 | { |
| 113 | /* FIXME multibyte */ |
| 114 | WCHAR *pos = s->szData; |
Krzysztof Foltman | f089de1 | 2005-03-17 10:23:40 +0000 | [diff] [blame] | 115 | while(ME_IsWSpace(*pos++)) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 116 | ; |
| 117 | pos--; |
| 118 | if (*pos) |
| 119 | return 0; |
| 120 | else |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | int ME_IsSplitable(ME_String *s) |
| 125 | { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 126 | WCHAR *pos = s->szData; |
| 127 | WCHAR ch; |
Krzysztof Foltman | f089de1 | 2005-03-17 10:23:40 +0000 | [diff] [blame] | 128 | while(ME_IsWSpace(*pos++)) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 129 | ; |
| 130 | pos--; |
| 131 | while((ch = *pos++) != 0) |
| 132 | { |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 133 | if (ME_IsWSpace(ch)) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 134 | return 1; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* FIXME multibyte */ |
| 140 | /* |
| 141 | int ME_CalcSkipChars(ME_String *s) |
| 142 | { |
| 143 | int cnt = 0; |
| 144 | while(cnt < s->nLen && s->szData[s->nLen-1-cnt]==' ') |
| 145 | cnt++; |
| 146 | return cnt; |
| 147 | } |
| 148 | */ |
| 149 | |
| 150 | int ME_StrLen(ME_String *s) { |
| 151 | return s->nLen; |
| 152 | } |
| 153 | |
| 154 | int ME_StrVLen(ME_String *s) { |
| 155 | return s->nLen; |
| 156 | } |
| 157 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 158 | int ME_StrRelPos(ME_String *s, int nVChar, int *pRelChars) |
| 159 | { |
Phil Krylov | 74aa295 | 2006-01-09 17:12:34 +0100 | [diff] [blame] | 160 | int nRelChars = *pRelChars; |
| 161 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 162 | TRACE("%s,%d,&%d\n", debugstr_w(s->szData), nVChar, *pRelChars); |
| 163 | |
| 164 | assert(*pRelChars); |
Phil Krylov | 74aa295 | 2006-01-09 17:12:34 +0100 | [diff] [blame] | 165 | if (!nRelChars) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 166 | return nVChar; |
Phil Krylov | 74aa295 | 2006-01-09 17:12:34 +0100 | [diff] [blame] | 167 | |
| 168 | if (nRelChars>0) |
| 169 | nRelChars = min(*pRelChars, s->nLen - nVChar); |
| 170 | else |
| 171 | nRelChars = max(*pRelChars, -nVChar); |
| 172 | nVChar += nRelChars; |
| 173 | *pRelChars -= nRelChars; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 174 | return nVChar; |
| 175 | } |
| 176 | |
| 177 | int ME_StrRelPos2(ME_String *s, int nVChar, int nRelChars) |
| 178 | { |
| 179 | return ME_StrRelPos(s, nVChar, &nRelChars); |
| 180 | } |
| 181 | |
| 182 | int ME_VPosToPos(ME_String *s, int nVPos) |
| 183 | { |
| 184 | return nVPos; |
| 185 | /* |
| 186 | int i = 0, len = 0; |
| 187 | if (!nVPos) |
| 188 | return 0; |
| 189 | while (i < s->nLen) |
| 190 | { |
| 191 | if (i == nVPos) |
| 192 | return len; |
| 193 | if (s->szData[i]=='\\') i++; |
| 194 | i++; |
| 195 | len++; |
| 196 | } |
| 197 | return len; |
| 198 | */ |
| 199 | } |
| 200 | |
| 201 | int ME_PosToVPos(ME_String *s, int nPos) |
| 202 | { |
| 203 | if (!nPos) |
| 204 | return 0; |
| 205 | return ME_StrRelPos2(s, 0, nPos); |
| 206 | } |
| 207 | |
| 208 | void ME_StrDeleteV(ME_String *s, int nVChar, int nChars) |
| 209 | { |
| 210 | int end_ofs; |
| 211 | |
| 212 | assert(nVChar >=0 && nVChar <= s->nLen); |
| 213 | assert(nChars >= 0); |
| 214 | assert(nVChar+nChars <= s->nLen); |
| 215 | |
| 216 | end_ofs = ME_StrRelPos2(s, nVChar, nChars); |
| 217 | assert(end_ofs <= s->nLen); |
| 218 | memmove(s->szData+nVChar, s->szData+end_ofs, 2*(s->nLen+1-end_ofs)); |
| 219 | s->nLen -= (end_ofs - nVChar); |
| 220 | } |
| 221 | |
| 222 | int ME_GetCharFwd(ME_String *s, int nPos) |
| 223 | { |
| 224 | int nVPos = 0; |
| 225 | |
| 226 | assert(nPos < ME_StrLen(s)); |
| 227 | if (nPos) |
| 228 | nVPos = ME_StrRelPos2(s, nVPos, nPos); |
| 229 | |
| 230 | if (nVPos < s->nLen) |
| 231 | return s->szData[nVPos]; |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | int ME_GetCharBack(ME_String *s, int nPos) |
| 236 | { |
| 237 | int nVPos = ME_StrVLen(s); |
| 238 | |
| 239 | assert(nPos < ME_StrLen(s)); |
| 240 | if (nPos) |
| 241 | nVPos = ME_StrRelPos2(s, nVPos, -nPos); |
| 242 | |
| 243 | if (nVPos < s->nLen) |
| 244 | return s->szData[nVPos]; |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | int ME_FindNonWhitespaceV(ME_String *s, int nVChar) { |
| 249 | int i; |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 250 | for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 251 | ; |
| 252 | |
| 253 | return i; |
| 254 | } |
| 255 | |
| 256 | /* note: returns offset of the first trailing whitespace */ |
| 257 | int ME_ReverseFindNonWhitespaceV(ME_String *s, int nVChar) { |
| 258 | int i; |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 259 | for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 260 | ; |
| 261 | |
| 262 | return i; |
| 263 | } |
| 264 | |
| 265 | /* note: returns offset of the first trailing nonwhitespace */ |
| 266 | int ME_ReverseFindWhitespaceV(ME_String *s, int nVChar) { |
| 267 | int i; |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 268 | for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 269 | ; |
| 270 | |
| 271 | return i; |
| 272 | } |
| 273 | |
| 274 | LPWSTR ME_ToUnicode(HWND hWnd, LPVOID psz) |
| 275 | { |
| 276 | if (IsWindowUnicode(hWnd)) |
| 277 | return (LPWSTR)psz; |
| 278 | else { |
| 279 | WCHAR *tmp; |
| 280 | int nChars = MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, NULL, 0); |
| 281 | if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL) |
| 282 | MultiByteToWideChar(CP_ACP, 0, (char *)psz, -1, tmp, nChars); |
| 283 | return tmp; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void ME_EndToUnicode(HWND hWnd, LPVOID psz) |
| 288 | { |
| 289 | if (IsWindowUnicode(hWnd)) |
| 290 | FREE_OBJ(psz); |
| 291 | } |
| 292 | |
| 293 | LPSTR ME_ToAnsi(HWND hWnd, LPVOID psz) |
| 294 | { |
| 295 | if (!IsWindowUnicode(hWnd)) |
| 296 | return (LPSTR)psz; |
| 297 | else { |
| 298 | char *tmp; |
| 299 | int nChars = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)psz, -1, NULL, 0, NULL, NULL); |
| 300 | if((tmp = ALLOC_N_OBJ(char, nChars)) != NULL) |
| 301 | WideCharToMultiByte(CP_ACP, 0, (WCHAR *)psz, -1, tmp, nChars, NULL, NULL); |
| 302 | return tmp; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void ME_EndToAnsi(HWND hWnd, LPVOID psz) |
| 307 | { |
| 308 | if (!IsWindowUnicode(hWnd)) |
| 309 | FREE_OBJ(psz); |
| 310 | } |