string.c 5.99 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
  return ((sizeof(WCHAR) * nLen) + 128) & ~63;
28 29
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static ME_String *make_string( void (*free)(ME_String *) )
{
  ME_String *s = heap_alloc( sizeof(*s) );

  if (s) s->free = free;
  return s;
}

/* Create a ME_String using the const string provided.
 * str must exist for the lifetime of the returned ME_String.
 */
ME_String *ME_MakeStringConst(const WCHAR *str, int len)
{
  ME_String *s = make_string( NULL );
  if (!s) return NULL;

  s->szData = (WCHAR *)str;
  s->nLen = len;
  s->nBuffer = 0;
  return s;
}

static void heap_string_free(ME_String *s)
{
  heap_free( s->szData );
}

57
/* Create a buffer (uninitialized string) of size nMaxChars */
58
ME_String *ME_MakeStringEmpty(int nMaxChars)
59
{
60
  ME_String *s = make_string( heap_string_free );
61

62
  if (!s) return NULL;
63 64
  s->nLen = nMaxChars;
  s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
65 66 67 68 69 70
  s->szData = heap_alloc( s->nBuffer * sizeof(WCHAR) );
  if (!s->szData)
  {
    heap_free( s );
    return NULL;
  }
71
  s->szData[s->nLen] = 0;
72 73 74 75 76
  return s;
}

ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
{
77
  ME_String *s = ME_MakeStringEmpty(nMaxChars);
78 79

  if (!s) return NULL;
80
  memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
81 82 83
  return s;
}

84
/* Make a string by repeating a char nMaxChars times */
85
ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
86
{
87
  int i;
88
  ME_String *s = ME_MakeStringEmpty(nMaxChars);
89 90

  if (!s) return NULL;
91
  for (i = 0; i < nMaxChars; i++)
92 93 94 95
    s->szData[i] = cRepeat;
  return s;
}

96 97
void ME_DestroyString(ME_String *s)
{
98
  if (!s) return;
99
  if (s->free) s->free( s );
100
  heap_free( s );
101 102
}

103
BOOL ME_InsertString(ME_String *s, int ofs, const WCHAR *insert, int len)
104
{
105
    DWORD new_len = s->nLen + len + 1;
106 107
    WCHAR *new;

108
    assert( s->nBuffer ); /* Not a const string */
109 110 111 112 113
    assert( ofs <= s->nLen );

    if( new_len > s->nBuffer )
    {
        s->nBuffer = ME_GetOptimalBuffer( new_len );
114 115 116
        new = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
        if (!new) return FALSE;
        s->szData = new;
117 118 119 120 121 122 123 124 125 126 127 128
    }

    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 );
129 130 131 132 133 134
}

ME_String *ME_VSplitString(ME_String *orig, int charidx)
{
  ME_String *s;

135
  assert(orig->nBuffer); /* Not a const string */
136 137 138
  assert(charidx>=0);
  assert(charidx<=orig->nLen);

139
  s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
140 141
  if (!s) return NULL;

142
  orig->nLen = charidx;
143
  orig->szData[charidx] = '\0';
144 145 146 147 148
  return s;
}

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

151
  assert(s->nBuffer); /* Not a const string */
152
  assert(nChars >= 0);
153
  assert(nVChar >= 0);
154
  assert(end_ofs <= s->nLen);
155 156 157 158

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

161 162 163 164 165 166
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);
167

168 169 170 171 172 173 174 175 176 177 178 179 180
  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:
181 182 183 184
      while (start < len && !ME_IsWSpace(s[start]))
        start++;
      while (start < len && ME_IsWSpace(s[start]))
        start++;
185 186 187 188 189 190 191
      return start;
  }
  return 0;
}


int
192
ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT code)
193
{
194
  if (!editor->pfnWordBreak) {
195
    return ME_WordBreakProc(str, start, len, code);
196 197 198
  } 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. */
199
    return editor->pfnWordBreak(str, start, len * sizeof(WCHAR), code);
200 201
  } else {
    int result;
202
    int buffer_size = WideCharToMultiByte(CP_ACP, 0, str, len,
203
                                          NULL, 0, NULL, NULL);
204
    char *buffer = heap_alloc(buffer_size);
205
    if (!buffer) return 0;
206
    WideCharToMultiByte(CP_ACP, 0, str, len,
207
                        buffer, buffer_size, NULL, NULL);
208
    result = editor->pfnWordBreak((WCHAR*)buffer, start, buffer_size, code);
209 210 211
    heap_free(buffer);
    return result;
  }
212 213
}

214
LPWSTR ME_ToUnicode(LONG codepage, LPVOID psz, INT *len)
215
{
216 217
  *len = 0;
  if (!psz) return NULL;
218

219
  if (codepage == CP_UNICODE)
220 221
  {
    *len = lstrlenW(psz);
222
    return psz;
223
  }
224 225
  else {
    WCHAR *tmp;
226
    int nChars = MultiByteToWideChar(codepage, 0, psz, -1, NULL, 0);
227 228 229

    if(!nChars) return NULL;

230
    if((tmp = heap_alloc( nChars * sizeof(WCHAR) )) != NULL)
231
      *len = MultiByteToWideChar(codepage, 0, psz, -1, tmp, nChars) - 1;
232 233 234 235
    return tmp;
  }
}

236
void ME_EndToUnicode(LONG codepage, LPVOID psz)
237
{
238
  if (codepage != CP_UNICODE)
239
    heap_free( psz );
240
}