string.c 6.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
ME_String *ME_StrDup(const ME_String *s)
63 64 65 66 67 68
{
  return ME_MakeStringN(s->szData, s->nLen);
}

void ME_DestroyString(ME_String *s)
{
69
  if (!s) return;
70 71 72 73
  FREE_OBJ(s->szData);
  FREE_OBJ(s);
}

74
void ME_AppendString(ME_String *s1, const ME_String *s2)
75
{
76
  if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
77
  {
78 79 80 81
    memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
    s1->nLen += s2->nLen;
    s1->szData[s1->nLen] = 0;
  } else {
82 83 84
    WCHAR *buf;
    s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);

85 86 87
    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));
88 89 90
    FREE_OBJ(s1->szData);
    s1->szData = buf;
    s1->nLen += s2->nLen;
91
    s1->szData[s1->nLen] = 0;
92 93 94 95 96 97 98 99 100 101 102 103 104
  }
}

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

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

111
int ME_IsWhitespaces(const ME_String *s)
112 113 114
{
  /* FIXME multibyte */
  WCHAR *pos = s->szData;
115
  while(ME_IsWSpace(*pos++))
116 117 118 119 120 121 122 123
    ;
  pos--;
  if (*pos)
    return 0;
  else
    return 1;
}

124
int ME_IsSplitable(const ME_String *s)
125 126 127
{
  WCHAR *pos = s->szData;
  WCHAR ch;
128
  while(ME_IsWSpace(*pos++))
129 130 131 132
    ;
  pos--;
  while((ch = *pos++) != 0)
  {
133
    if (ME_IsWSpace(ch))
134 135 136 137 138 139 140
      return 1;
  }
  return 0;
}

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

143
  assert(nChars >= 0);
144
  assert(nVChar >= 0);
145
  assert(end_ofs <= s->nLen);
146 147 148 149

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

152
int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
153
  int i;
154
  for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
155 156 157 158 159 160
    ;
    
  return i;
}

/* note: returns offset of the first trailing whitespace */
161
int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
162
  int i;
163
  for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
164 165 166 167 168 169
    ;
    
  return i;
}

/* note: returns offset of the first trailing nonwhitespace */
170
int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
171
  int i;
172
  for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
173 174 175 176 177
    ;
    
  return i;
}

178 179 180 181 182 183 184

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);
185 186
  /* convert number of bytes to number of characters. */
  len /= sizeof(WCHAR);
187 188 189 190 191 192 193 194 195 196 197 198 199
  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:
200 201 202 203
      while (start < len && !ME_IsWSpace(s[start]))
        start++;
      while (start < len && ME_IsWSpace(s[start]))
        start++;
204 205 206 207 208 209 210 211 212
      return start;
  }
  return 0;
}


int
ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
{
213 214 215 216 217 218 219 220 221 222
  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);
223
    char *buffer = heap_alloc(buffer_size);
224 225 226 227 228 229
    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;
  }
230 231
}

232
LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
233
{
234 235
  assert(psz != NULL);

236
  if (unicode)
237
    return psz;
238 239
  else {
    WCHAR *tmp;
240
    int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
241
    if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
242
      MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
243 244 245 246
    return tmp;
  }
}

247
void ME_EndToUnicode(BOOL unicode, LPVOID psz)
248
{
249
  if (!unicode)
250 251
    FREE_OBJ(psz);
}