ReactOS 0.4.15-dev-8131-g4988de4
string.c
Go to the documentation of this file.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21#include "editor.h"
22
24
25static int ME_GetOptimalBuffer(int nLen)
26{
27 return ((sizeof(WCHAR) * nLen) + 128) & ~63;
28}
29
30static ME_String *make_string( void (*free)(ME_String *) )
31{
32 ME_String *s = heap_alloc( sizeof(*s) );
33
34 if (s) s->free = free;
35 return s;
36}
37
38/* Create a ME_String using the const string provided.
39 * str must exist for the lifetime of the returned ME_String.
40 */
42{
44 if (!s) return NULL;
45
46 s->szData = (WCHAR *)str;
47 s->nLen = len;
48 s->nBuffer = 0;
49 return s;
50}
51
53{
54 heap_free( s->szData );
55}
56
57/* Create a buffer (uninitialized string) of size nMaxChars */
59{
61
62 if (!s) return NULL;
63 s->nLen = nMaxChars;
64 s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
65 s->szData = heap_alloc( s->nBuffer * sizeof(WCHAR) );
66 if (!s->szData)
67 {
68 heap_free( s );
69 return NULL;
70 }
71 s->szData[s->nLen] = 0;
72 return s;
73}
74
75ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
76{
77 ME_String *s = ME_MakeStringEmpty(nMaxChars);
78
79 if (!s) return NULL;
80 memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
81 return s;
82}
83
84/* Make a string by repeating a char nMaxChars times */
85ME_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 s->szData[i] = cRepeat;
93 return s;
94}
95
97{
98 if (!s) return;
99 if (s->free) s->free( s );
100 heap_free( s );
101}
102
104{
105 DWORD new_len = s->nLen + len + 1;
106 WCHAR *new;
107
108 assert( s->nBuffer ); /* Not a const string */
109 assert( ofs <= s->nLen );
110
111 if( new_len > s->nBuffer )
112 {
113 s->nBuffer = ME_GetOptimalBuffer( new_len );
114 new = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
115 if (!new) return FALSE;
116 s->szData = new;
117 }
118
119 memmove( s->szData + ofs + len, s->szData + ofs, (s->nLen - ofs + 1) * sizeof(WCHAR) );
120 memcpy( s->szData + ofs, insert, len * sizeof(WCHAR) );
121 s->nLen += len;
122
123 return TRUE;
124}
125
127{
128 return ME_InsertString( s, s->nLen, append, len );
129}
130
132{
133 ME_String *s;
134
135 assert(orig->nBuffer); /* Not a const string */
136 assert(charidx>=0);
137 assert(charidx<=orig->nLen);
138
139 s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
140 if (!s) return NULL;
141
142 orig->nLen = charidx;
143 orig->szData[charidx] = '\0';
144 return s;
145}
146
147void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
148{
149 int end_ofs = nVChar + nChars;
150
151 assert(s->nBuffer); /* Not a const string */
152 assert(nChars >= 0);
153 assert(nVChar >= 0);
154 assert(end_ofs <= s->nLen);
155
156 memmove(s->szData + nVChar, s->szData + end_ofs,
157 (s->nLen - end_ofs + 1) * sizeof(WCHAR));
158 s->nLen -= nChars;
159}
160
161static int
163{
164#ifndef __REACTOS__
165 /* FIXME: Native also knows about punctuation */
166#endif
167 TRACE("s==%s, start==%d, len==%d, code==%d\n",
169
170 switch (code)
171 {
172 case WB_ISDELIMITER:
173 return ME_IsWSpace(s[start]);
174 case WB_LEFT:
175 case WB_MOVEWORDLEFT:
176 while (start && ME_IsWSpace(s[start - 1]))
177 start--;
178#ifdef __REACTOS__
179 while (start && !ME_IsWSpace(s[start - 1]) && !iswpunct(s[start - 1]))
180 start--;
181#else
182 while (start && !ME_IsWSpace(s[start - 1]))
183 start--;
184#endif
185 return start;
186 case WB_RIGHT:
187 case WB_MOVEWORDRIGHT:
188#ifdef __REACTOS__
189 while (start < len && !ME_IsWSpace(s[start]) && !iswpunct(s[start]))
190 start++;
191#else
192 while (start < len && !ME_IsWSpace(s[start]))
193 start++;
194#endif
195 while (start < len && ME_IsWSpace(s[start]))
196 start++;
197 return start;
198 }
199 return 0;
200}
201
202
203int
205{
206 if (!editor->pfnWordBreak) {
207 return ME_WordBreakProc(str, start, len, code);
208 } else if (!editor->bEmulateVersion10) {
209 /* MSDN lied about the third parameter for EditWordBreakProc being the number
210 * of characters, it is actually the number of bytes of the string. */
211 return editor->pfnWordBreak(str, start, len * sizeof(WCHAR), code);
212 } else {
213 int result;
215 NULL, 0, NULL, NULL);
217 if (!buffer) return 0;
222 return result;
223 }
224}
225
227{
228 *len = 0;
229 if (!psz) return NULL;
230
231 if (codepage == CP_UNICODE)
232 {
233 *len = lstrlenW(psz);
234 return psz;
235 }
236 else {
237 WCHAR *tmp;
238 int nChars = MultiByteToWideChar(codepage, 0, psz, -1, NULL, 0);
239
240 if(!nChars) return NULL;
241
242 if((tmp = heap_alloc( nChars * sizeof(WCHAR) )) != NULL)
243 *len = MultiByteToWideChar(codepage, 0, psz, -1, tmp, nChars) - 1;
244 return tmp;
245 }
246}
247
249{
250 if (codepage != CP_UNICODE)
251 heap_free( psz );
252}
static void * heap_alloc(size_t len)
Definition: appwiz.h:66
static BOOL heap_free(void *mem)
Definition: appwiz.h:76
static void * heap_realloc(void *mem, size_t len)
Definition: appwiz.h:71
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define lstrlenW
Definition: compat.h:750
#define CP_UNICODE
Definition: stg_prop.c:74
BOOL ME_InsertString(ME_String *s, int ofs, const WCHAR *insert, int len)
Definition: string.c:103
void ME_DestroyString(ME_String *s)
Definition: string.c:96
ME_String * ME_MakeStringEmpty(int nMaxChars)
Definition: string.c:58
BOOL ME_AppendString(ME_String *s, const WCHAR *append, int len)
Definition: string.c:126
void ME_EndToUnicode(LONG codepage, LPVOID psz)
Definition: string.c:248
ME_String * ME_MakeStringN(LPCWSTR szText, int nMaxChars)
Definition: string.c:75
static int ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
Definition: string.c:162
static void heap_string_free(ME_String *s)
Definition: string.c:52
static int ME_GetOptimalBuffer(int nLen)
Definition: string.c:25
ME_String * ME_MakeStringConst(const WCHAR *str, int len)
Definition: string.c:41
void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
Definition: string.c:147
static ME_String * make_string(void(*free)(ME_String *))
Definition: string.c:30
ME_String * ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
Definition: string.c:85
int ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT code)
Definition: string.c:204
LPWSTR ME_ToUnicode(LONG codepage, LPVOID psz, INT *len)
Definition: string.c:226
ME_String * ME_VSplitString(ME_String *orig, int charidx)
Definition: string.c:131
#define assert(x)
Definition: debug.h:53
static int ME_IsWSpace(WCHAR ch)
Definition: editor.h:101
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint start
Definition: gl.h:1545
GLdouble s
Definition: gl.h:2039
GLuint buffer
Definition: glext.h:5915
GLenum GLsizei len
Definition: glext.h:6722
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define iswpunct(_c)
Definition: ctype.h:670
#define debugstr_wn
Definition: kernel32.h:33
static void append(struct dump_context *dc, const void *data, unsigned size)
Definition: minidump.c:397
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
long LONG
Definition: pedump.c:60
#define WB_MOVEWORDRIGHT
Definition: richedit.h:1002
#define WB_MOVEWORDLEFT
Definition: richedit.h:1000
const WCHAR * str
#define TRACE(s)
Definition: solgame.cpp:4
Definition: inflate.c:139
WCHAR * szData
Definition: editstr.h:56
int nLen
Definition: editstr.h:57
int nBuffer
Definition: editstr.h:57
EDITWORDBREAKPROCW pfnWordBreak
Definition: editstr.h:417
BOOL bEmulateVersion10
Definition: editstr.h:385
static void buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
Definition: swimpl.c:888
int32_t INT
Definition: typedefs.h:58
int codepage
Definition: win_iconv.c:156
#define WB_ISDELIMITER
Definition: winuser.h:549
#define WB_LEFT
Definition: winuser.h:550
#define WB_RIGHT
Definition: winuser.h:551
static int insert
Definition: xmllint.c:138
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185