2005-03-05 12:19:14 +01:00
|
|
|
/*
|
|
|
|
* RichEdit - string operations
|
|
|
|
*
|
|
|
|
* Copyright 2004 by Krzysztof Foltman
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-03-05 12:19:14 +01:00
|
|
|
*/
|
|
|
|
|
2009-02-07 19:20:55 +01:00
|
|
|
#include "editor.h"
|
2005-03-05 12:19:14 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(richedit);
|
|
|
|
|
2009-01-17 17:27:08 +01:00
|
|
|
static int ME_GetOptimalBuffer(int nLen)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2009-02-07 19:20:55 +01:00
|
|
|
/* FIXME: This seems wasteful for tabs and end of lines strings,
|
|
|
|
* since they have a small fixed length. */
|
|
|
|
return ((sizeof(WCHAR) * nLen) + 128) & ~63;
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
|
2009-02-07 19:20:55 +01:00
|
|
|
/* Create a buffer (uninitialized string) of size nMaxChars */
|
|
|
|
static ME_String *ME_MakeStringB(int nMaxChars)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_String *s = ALLOC_OBJ(ME_String);
|
2009-02-07 19:20:55 +01:00
|
|
|
|
|
|
|
s->nLen = nMaxChars;
|
|
|
|
s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
|
2005-03-05 12:19:14 +01:00
|
|
|
s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
|
2009-02-07 19:20:55 +01:00
|
|
|
s->szData[s->nLen] = 0;
|
2005-03-05 12:19:14 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
|
|
|
|
{
|
2009-02-07 19:20:55 +01:00
|
|
|
ME_String *s = ME_MakeStringB(nMaxChars);
|
|
|
|
/* Native allows NULL chars */
|
|
|
|
memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
|
2005-03-05 12:19:14 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-02-07 19:20:55 +01:00
|
|
|
/* Make a string by repeating a char nMaxChars times */
|
2006-08-04 21:47:44 +02:00
|
|
|
ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
|
2009-02-07 19:20:55 +01:00
|
|
|
{
|
2006-08-04 21:47:44 +02:00
|
|
|
int i;
|
2009-02-07 19:20:55 +01:00
|
|
|
ME_String *s = ME_MakeStringB(nMaxChars);
|
|
|
|
for (i = 0; i < nMaxChars; i++)
|
2006-08-04 21:47:44 +02:00
|
|
|
s->szData[i] = cRepeat;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-08-15 22:35:51 +02:00
|
|
|
ME_String *ME_StrDup(const ME_String *s)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
return ME_MakeStringN(s->szData, s->nLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ME_DestroyString(ME_String *s)
|
|
|
|
{
|
2009-01-28 07:34:56 +01:00
|
|
|
if (!s) return;
|
2005-03-05 12:19:14 +01:00
|
|
|
FREE_OBJ(s->szData);
|
|
|
|
FREE_OBJ(s);
|
|
|
|
}
|
|
|
|
|
2007-08-15 22:35:51 +02:00
|
|
|
void ME_AppendString(ME_String *s1, const ME_String *s2)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2009-02-07 19:20:46 +01:00
|
|
|
if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2009-02-07 19:20:46 +01:00
|
|
|
memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
|
|
|
|
s1->nLen += s2->nLen;
|
|
|
|
s1->szData[s1->nLen] = 0;
|
|
|
|
} else {
|
2005-03-05 12:19:14 +01:00
|
|
|
WCHAR *buf;
|
|
|
|
s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
|
|
|
|
|
2009-02-07 19:20:46 +01:00
|
|
|
buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
|
|
|
|
memcpy(buf, s1->szData, s1->nLen * sizeof(WCHAR));
|
|
|
|
memcpy(buf + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
|
2005-03-05 12:19:14 +01:00
|
|
|
FREE_OBJ(s1->szData);
|
|
|
|
s1->szData = buf;
|
|
|
|
s1->nLen += s2->nLen;
|
2009-02-07 19:20:46 +01:00
|
|
|
s1->szData[s1->nLen] = 0;
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ME_String *ME_VSplitString(ME_String *orig, int charidx)
|
|
|
|
{
|
|
|
|
ME_String *s;
|
|
|
|
|
|
|
|
/*if (charidx<0) charidx = 0;
|
|
|
|
if (charidx>orig->nLen) charidx = orig->nLen;
|
|
|
|
*/
|
|
|
|
assert(charidx>=0);
|
|
|
|
assert(charidx<=orig->nLen);
|
|
|
|
|
2008-06-25 17:33:26 +02:00
|
|
|
s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
|
2005-03-05 12:19:14 +01:00
|
|
|
orig->nLen = charidx;
|
2007-06-21 22:56:17 +02:00
|
|
|
orig->szData[charidx] = '\0';
|
2005-03-05 12:19:14 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-08-15 22:35:51 +02:00
|
|
|
int ME_IsWhitespaces(const ME_String *s)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
/* FIXME multibyte */
|
|
|
|
WCHAR *pos = s->szData;
|
2005-03-17 11:23:40 +01:00
|
|
|
while(ME_IsWSpace(*pos++))
|
2005-03-05 12:19:14 +01:00
|
|
|
;
|
|
|
|
pos--;
|
|
|
|
if (*pos)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-15 22:35:51 +02:00
|
|
|
int ME_IsSplitable(const ME_String *s)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
WCHAR *pos = s->szData;
|
|
|
|
WCHAR ch;
|
2005-03-17 11:23:40 +01:00
|
|
|
while(ME_IsWSpace(*pos++))
|
2005-03-05 12:19:14 +01:00
|
|
|
;
|
|
|
|
pos--;
|
|
|
|
while((ch = *pos++) != 0)
|
|
|
|
{
|
2005-03-19 18:06:17 +01:00
|
|
|
if (ME_IsWSpace(ch))
|
2005-03-05 12:19:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
|
|
|
|
{
|
2009-02-07 19:21:29 +01:00
|
|
|
int end_ofs = nVChar + nChars;
|
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
assert(nChars >= 0);
|
2009-02-07 19:21:29 +01:00
|
|
|
assert(nVChar >= 0);
|
2005-03-05 12:19:14 +01:00
|
|
|
assert(end_ofs <= s->nLen);
|
2009-02-07 19:21:29 +01:00
|
|
|
|
|
|
|
memmove(s->szData + nVChar, s->szData + end_ofs,
|
|
|
|
(s->nLen - end_ofs + 1) * sizeof(WCHAR));
|
|
|
|
s->nLen -= nChars;
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
|
2007-08-15 22:35:51 +02:00
|
|
|
int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
|
2005-03-05 12:19:14 +01:00
|
|
|
int i;
|
2005-03-19 18:06:17 +01:00
|
|
|
for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
|
2005-03-05 12:19:14 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: returns offset of the first trailing whitespace */
|
2007-08-15 22:35:51 +02:00
|
|
|
int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
|
2005-03-05 12:19:14 +01:00
|
|
|
int i;
|
2005-03-19 18:06:17 +01:00
|
|
|
for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
|
2005-03-05 12:19:14 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: returns offset of the first trailing nonwhitespace */
|
2007-08-15 22:35:51 +02:00
|
|
|
int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
|
2005-03-05 12:19:14 +01:00
|
|
|
int i;
|
2005-03-19 18:06:17 +01:00
|
|
|
for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
|
2005-03-05 12:19:14 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2006-01-12 11:54:57 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
|
|
|
|
{
|
|
|
|
/* FIXME: Native also knows about punctuation */
|
|
|
|
TRACE("s==%s, start==%d, len==%d, code==%d\n",
|
|
|
|
debugstr_wn(s, len), start, len, code);
|
2008-10-23 07:07:31 +02:00
|
|
|
/* convert number of bytes to number of characters. */
|
|
|
|
len /= sizeof(WCHAR);
|
2006-01-12 11:54:57 +01:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case WB_ISDELIMITER:
|
|
|
|
return ME_IsWSpace(s[start]);
|
|
|
|
case WB_LEFT:
|
|
|
|
case WB_MOVEWORDLEFT:
|
|
|
|
while (start && ME_IsWSpace(s[start - 1]))
|
|
|
|
start--;
|
|
|
|
while (start && !ME_IsWSpace(s[start - 1]))
|
|
|
|
start--;
|
|
|
|
return start;
|
|
|
|
case WB_RIGHT:
|
|
|
|
case WB_MOVEWORDRIGHT:
|
2008-06-25 17:33:19 +02:00
|
|
|
while (start < len && !ME_IsWSpace(s[start]))
|
|
|
|
start++;
|
|
|
|
while (start < len && ME_IsWSpace(s[start]))
|
|
|
|
start++;
|
2006-01-12 11:54:57 +01:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
|
|
|
|
{
|
2008-10-23 07:07:31 +02:00
|
|
|
if (!editor->pfnWordBreak) {
|
|
|
|
return ME_WordBreakProc(str->szData, start, str->nLen*sizeof(WCHAR), code);
|
|
|
|
} else if (!editor->bEmulateVersion10) {
|
|
|
|
/* MSDN lied about the third parameter for EditWordBreakProc being the number
|
|
|
|
* of characters, it is actually the number of bytes of the string. */
|
|
|
|
return editor->pfnWordBreak(str->szData, start, str->nLen*sizeof(WCHAR), code);
|
|
|
|
} else {
|
|
|
|
int result;
|
|
|
|
int buffer_size = WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
|
|
|
|
NULL, 0, NULL, NULL);
|
2008-11-03 22:36:03 +01:00
|
|
|
char *buffer = heap_alloc(buffer_size);
|
2008-10-23 07:07:31 +02:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
|
|
|
|
buffer, buffer_size, NULL, NULL);
|
|
|
|
result = editor->pfnWordBreak(str->szData, start, str->nLen, code);
|
|
|
|
heap_free(buffer);
|
|
|
|
return result;
|
|
|
|
}
|
2006-01-12 11:54:57 +01:00
|
|
|
}
|
|
|
|
|
2007-03-22 12:09:43 +01:00
|
|
|
LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2007-05-02 10:59:21 +02:00
|
|
|
assert(psz != NULL);
|
|
|
|
|
2007-03-22 12:09:43 +01:00
|
|
|
if (unicode)
|
2009-01-30 10:40:02 +01:00
|
|
|
return psz;
|
2005-03-05 12:19:14 +01:00
|
|
|
else {
|
|
|
|
WCHAR *tmp;
|
2009-01-30 10:40:02 +01:00
|
|
|
int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
|
2005-03-05 12:19:14 +01:00
|
|
|
if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
|
2009-01-30 10:40:02 +01:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
|
2005-03-05 12:19:14 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 12:09:43 +01:00
|
|
|
void ME_EndToUnicode(BOOL unicode, LPVOID psz)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2007-03-22 12:09:43 +01:00
|
|
|
if (!unicode)
|
2005-03-05 12:19:14 +01:00
|
|
|
FREE_OBJ(psz);
|
|
|
|
}
|