string.c 5.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include "editor.h"
22 23 24

WINE_DEFAULT_DEBUG_CHANNEL(richedit);

25
static int ME_GetOptimalBuffer(int nLen)
26
{
27 28 29
  /* FIXME: This seems wasteful for tabs and end of lines strings,
   *        since they have a small fixed length. */
  return ((sizeof(WCHAR) * nLen) + 128) & ~63;
30 31
}

32 33
/* Create a buffer (uninitialized string) of size nMaxChars */
static ME_String *ME_MakeStringB(int nMaxChars)
34 35
{
  ME_String *s = ALLOC_OBJ(ME_String);
36 37 38

  s->nLen = nMaxChars;
  s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
39
  s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
40
  s->szData[s->nLen] = 0;
41 42 43 44 45
  return s;
}

ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
{
46 47 48
  ME_String *s = ME_MakeStringB(nMaxChars);
  /* Native allows NULL chars */
  memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
49 50 51
  return s;
}

52
/* Make a string by repeating a char nMaxChars times */
53
ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
54
{
55
  int i;
56 57
  ME_String *s = ME_MakeStringB(nMaxChars);
  for (i = 0; i < nMaxChars; i++)
58 59 60 61
    s->szData[i] = cRepeat;
  return s;
}

62 63
void ME_DestroyString(ME_String *s)
{
64
  if (!s) return;
65 66 67 68
  FREE_OBJ(s->szData);
  FREE_OBJ(s);
}

69
BOOL ME_InsertString(ME_String *s, int ofs, const WCHAR *insert, int len)
70
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    DWORD new_len = s->nLen + len + 1;
    assert( ofs <= s->nLen );

    if( new_len > s->nBuffer )
    {
        s->nBuffer = ME_GetOptimalBuffer( new_len );
        s->szData = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
        if (!s->szData) return FALSE;
    }

    memmove( s->szData + ofs + len, s->szData + ofs, (s->nLen - ofs + 1) * sizeof(WCHAR) );
    memcpy( s->szData + ofs, insert, len * sizeof(WCHAR) );
    s->nLen += len;

    return TRUE;
}

BOOL ME_AppendString(ME_String *s, const WCHAR *append, int len)
{
    return ME_InsertString( s, s->nLen, append, len );
91 92 93 94 95 96 97 98 99 100 101 102
}

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);

103
  s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
104
  orig->nLen = charidx;
105
  orig->szData[charidx] = '\0';
106 107 108 109 110
  return s;
}

void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
{
111 112
  int end_ofs = nVChar + nChars;

113
  assert(nChars >= 0);
114
  assert(nVChar >= 0);
115
  assert(end_ofs <= s->nLen);
116 117 118 119

  memmove(s->szData + nVChar, s->szData + end_ofs,
          (s->nLen - end_ofs + 1) * sizeof(WCHAR));
  s->nLen -= nChars;
120 121
}

122 123 124 125 126 127
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);
128

129 130 131 132 133 134 135 136 137 138 139 140 141
  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:
142 143 144 145
      while (start < len && !ME_IsWSpace(s[start]))
        start++;
      while (start < len && ME_IsWSpace(s[start]))
        start++;
146 147 148 149 150 151 152
      return start;
  }
  return 0;
}


int
153
ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT code)
154
{
155
  if (!editor->pfnWordBreak) {
156
    return ME_WordBreakProc(str, start, len, code);
157 158 159
  } 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. */
160
    return editor->pfnWordBreak(str, start, len * sizeof(WCHAR), code);
161 162
  } else {
    int result;
163
    int buffer_size = WideCharToMultiByte(CP_ACP, 0, str, len,
164
                                          NULL, 0, NULL, NULL);
165
    char *buffer = heap_alloc(buffer_size);
166
    WideCharToMultiByte(CP_ACP, 0, str, len,
167
                        buffer, buffer_size, NULL, NULL);
168
    result = editor->pfnWordBreak((WCHAR*)buffer, start, buffer_size, code);
169 170 171
    heap_free(buffer);
    return result;
  }
172 173
}

174
LPWSTR ME_ToUnicode(LONG codepage, LPVOID psz, INT *len)
175
{
176 177
  *len = 0;
  if (!psz) return NULL;
178

179
  if (codepage == CP_UNICODE)
180 181
  {
    *len = lstrlenW(psz);
182
    return psz;
183
  }
184 185
  else {
    WCHAR *tmp;
186
    int nChars = MultiByteToWideChar(codepage, 0, psz, -1, NULL, 0);
187 188 189

    if(!nChars) return NULL;

190
    if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
191
      *len = MultiByteToWideChar(codepage, 0, psz, -1, tmp, nChars) - 1;
192 193 194 195
    return tmp;
  }
}

196
void ME_EndToUnicode(LONG codepage, LPVOID psz)
197
{
198
  if (codepage != CP_UNICODE)
199 200
    FREE_OBJ(psz);
}