ReactOS 0.4.15-dev-7670-g69b08be
jsstr.c File Reference
#include <assert.h>
#include "jscript.h"
#include "wine/debug.h"
Include dependency graph for jsstr.c:

Go to the source code of this file.

Macros

#define JSSTR_SHORT_STRING_LENGTH   8
 
#define JSSTR_MAX_ROPE_DEPTH   100
 
#define TMP_BUF_SIZE   256
 

Functions

const chardebugstr_jsstr (jsstr_t *str)
 
void jsstr_free (jsstr_t *str)
 
static void jsstr_init (jsstr_t *str, unsigned len, jsstr_tag_t tag)
 
jsstr_tjsstr_alloc_buf (unsigned len, WCHAR **buf)
 
jsstr_tjsstr_alloc_len (const WCHAR *buf, unsigned len)
 
static void jsstr_rope_extract (jsstr_rope_t *str, unsigned off, unsigned len, WCHAR *buf)
 
void jsstr_extract (jsstr_t *str, unsigned off, unsigned len, WCHAR *buf)
 
static int jsstr_cmp_str (jsstr_t *jsstr, const WCHAR *str, unsigned len)
 
static int ropes_cmp (jsstr_rope_t *left, jsstr_rope_t *right)
 
static const WCHARjsstr_try_flat (jsstr_t *str)
 
int jsstr_cmp (jsstr_t *str1, jsstr_t *str2)
 
jsstr_tjsstr_concat (jsstr_t *str1, jsstr_t *str2)
 
 C_ASSERT (sizeof(jsstr_heap_t)<=sizeof(jsstr_rope_t))
 
const WCHARjsstr_rope_flatten (jsstr_rope_t *str)
 
jsstr_tjsstr_nan (void)
 
jsstr_tjsstr_empty (void)
 
jsstr_tjsstr_undefined (void)
 
jsstr_tjsstr_null_bstr (void)
 
BOOL is_null_bstr (jsstr_t *str)
 
BOOL init_strings (void)
 
void free_strings (void)
 

Variables

static jsstr_tempty_str
 
static jsstr_tnan_str
 
static jsstr_tundefined_str
 
static jsstr_tnull_bstr_str
 

Macro Definition Documentation

◆ JSSTR_MAX_ROPE_DEPTH

#define JSSTR_MAX_ROPE_DEPTH   100

Definition at line 35 of file jsstr.c.

◆ JSSTR_SHORT_STRING_LENGTH

#define JSSTR_SHORT_STRING_LENGTH   8

Definition at line 30 of file jsstr.c.

◆ TMP_BUF_SIZE

#define TMP_BUF_SIZE   256

Definition at line 154 of file jsstr.c.

Function Documentation

◆ C_ASSERT()

◆ debugstr_jsstr()

const char * debugstr_jsstr ( jsstr_t str)

Definition at line 37 of file jsstr.c.

38{
42}
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:296
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLint left
Definition: glext.h:7726
const char * debugstr_jsstr(jsstr_t *str)
Definition: jsstr.c:37
static jsstr_inline_t * jsstr_as_inline(jsstr_t *str)
Definition: jsstr.h:122
static unsigned jsstr_length(jsstr_t *str)
Definition: jsstr.h:58
static jsstr_heap_t * jsstr_as_heap(jsstr_t *str)
Definition: jsstr.h:127
static BOOL jsstr_is_heap(jsstr_t *str)
Definition: jsstr.h:73
static BOOL jsstr_is_inline(jsstr_t *str)
Definition: jsstr.h:68
static jsstr_rope_t * jsstr_as_rope(jsstr_t *str)
Definition: jsstr.h:132
#define debugstr_wn
Definition: kernel32.h:33
const WCHAR * str

Referenced by array_join(), debugstr_jsstr(), debugstr_jsval(), dump_instr_arg(), interp_obj_prop(), interp_regexp(), interp_str(), interp_throw_type(), JSGlobal_decodeURI(), JSGlobal_encodeURI(), String_idx_get(), and String_replace().

◆ free_strings()

void free_strings ( void  )

Definition at line 325 of file jsstr.c.

326{
327 if(empty_str)
329 if(nan_str)
331 if(undefined_str)
333 if(null_bstr_str)
335}
static jsstr_t * undefined_str
Definition: jsstr.c:281
static jsstr_t * null_bstr_str
Definition: jsstr.c:281
static jsstr_t * empty_str
Definition: jsstr.c:281
static jsstr_t * nan_str
Definition: jsstr.c:281
static void jsstr_release(jsstr_t *str)
Definition: jsstr.h:110

Referenced by DllMain().

◆ init_strings()

BOOL init_strings ( void  )

Definition at line 308 of file jsstr.c.

309{
310 static const WCHAR NaNW[] = { 'N','a','N',0 };
311 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
312 WCHAR *ptr;
313
314 if(!(empty_str = jsstr_alloc_buf(0, &ptr)))
315 return FALSE;
316 if(!(nan_str = jsstr_alloc(NaNW)))
317 return FALSE;
319 return FALSE;
320 if(!(null_bstr_str = jsstr_alloc_buf(0, &ptr)))
321 return FALSE;
322 return TRUE;
323}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static const WCHAR undefinedW[]
Definition: engine.c:39
static const WCHAR NaNW[]
Definition: global.c:34
jsstr_t * jsstr_alloc_buf(unsigned len, WCHAR **buf)
Definition: jsstr.c:69
static jsstr_t * jsstr_alloc(const WCHAR *str)
Definition: jsstr.h:103
static PVOID ptr
Definition: dispmode.c:27
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by DllMain().

◆ is_null_bstr()

BOOL is_null_bstr ( jsstr_t str)

Definition at line 303 of file jsstr.c.

304{
305 return str == null_bstr_str;
306}

Referenced by jsval_to_variant(), and variant_change_type().

◆ jsstr_alloc_buf()

jsstr_t * jsstr_alloc_buf ( unsigned  len,
WCHAR **  buf 
)

◆ jsstr_alloc_len()

jsstr_t * jsstr_alloc_len ( const WCHAR buf,
unsigned  len 
)

◆ jsstr_cmp()

int jsstr_cmp ( jsstr_t str1,
jsstr_t str2 
)

Definition at line 189 of file jsstr.c.

190{
191 unsigned len1 = jsstr_length(str1);
192 unsigned len2 = jsstr_length(str2);
193 const WCHAR *str;
194 int ret;
195
196 str = jsstr_try_flat(str2);
197 if(str) {
198 ret = jsstr_cmp_str(str1, str, min(len1, len2));
199 return ret || len1 == len2 ? ret : -1;
200 }
201
202 str = jsstr_try_flat(str1);
203 if(str) {
204 ret = jsstr_cmp_str(str2, str, min(len1, len2));
205 return ret || len1 == len2 ? -ret : 1;
206 }
207
208 return ropes_cmp(jsstr_as_rope(str1), jsstr_as_rope(str2));
209}
static int ropes_cmp(jsstr_rope_t *left, jsstr_rope_t *right)
Definition: jsstr.c:156
static int jsstr_cmp_str(jsstr_t *jsstr, const WCHAR *str, unsigned len)
Definition: jsstr.c:128
static const WCHAR * jsstr_try_flat(jsstr_t *str)
Definition: jsstr.c:182
#define min(a, b)
Definition: monoChain.cc:55

Referenced by jsstr_eq(), less_eval(), and sort_cmp().

◆ jsstr_cmp_str()

static int jsstr_cmp_str ( jsstr_t jsstr,
const WCHAR str,
unsigned  len 
)
static

Definition at line 128 of file jsstr.c.

129{
130 int ret;
131
132 switch(jsstr_tag(jsstr)) {
133 case JSSTR_INLINE:
134 ret = memcmp(jsstr_as_inline(jsstr)->buf, str, len*sizeof(WCHAR));
135 return ret || jsstr_length(jsstr) == len ? ret : 1;
136 case JSSTR_HEAP:
137 ret = memcmp(jsstr_as_heap(jsstr)->buf, str, len*sizeof(WCHAR));
138 return ret || jsstr_length(jsstr) == len ? ret : 1;
139 case JSSTR_ROPE: {
141 unsigned left_len = jsstr_length(rope->left);
142
143 ret = jsstr_cmp_str(rope->left, str, min(len, left_len));
144 if(ret || len <= left_len)
145 return ret;
146 return jsstr_cmp_str(rope->right, str+left_len, len-left_len);
147 }
148 }
149
150 assert(0);
151 return 0;
152}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
Definition: _rope.h:1087
#define assert(x)
Definition: debug.h:53
static jsstr_tag_t jsstr_tag(jsstr_t *str)
Definition: jsstr.h:63
@ JSSTR_HEAP
Definition: jsstr.h:54
@ JSSTR_ROPE
Definition: jsstr.h:55

Referenced by jsstr_cmp(), and jsstr_cmp_str().

◆ jsstr_concat()

jsstr_t * jsstr_concat ( jsstr_t str1,
jsstr_t str2 
)

Definition at line 211 of file jsstr.c.

212{
213 unsigned len1, len2;
214 jsstr_t *ret;
215 WCHAR *ptr;
216
217 len1 = jsstr_length(str1);
218 if(!len1)
219 return jsstr_addref(str2);
220
221 len2 = jsstr_length(str2);
222 if(!len2)
223 return jsstr_addref(str1);
224
225 if(len1 + len2 >= JSSTR_SHORT_STRING_LENGTH) {
226 unsigned depth, depth2;
228
229 depth = jsstr_is_rope(str1) ? jsstr_as_rope(str1)->depth : 0;
230 depth2 = jsstr_is_rope(str2) ? jsstr_as_rope(str2)->depth : 0;
231 if(depth2 > depth)
232 depth = depth2;
233
235 if(len1+len2 > JSSTR_MAX_LENGTH)
236 return NULL;
237
238 rope = heap_alloc(sizeof(*rope));
239 if(!rope)
240 return NULL;
241
242 jsstr_init(&rope->str, len1+len2, JSSTR_ROPE);
243 rope->left = jsstr_addref(str1);
244 rope->right = jsstr_addref(str2);
245 rope->depth = depth;
246 return &rope->str;
247 }
248 }
249
250 ret = jsstr_alloc_buf(len1+len2, &ptr);
251 if(!ret)
252 return NULL;
253
254 jsstr_flush(str1, ptr);
255 jsstr_flush(str2, ptr+len1);
256 return ret;
257
258}
GLint GLint GLsizei GLsizei GLsizei depth
Definition: gl.h:1546
#define JSSTR_SHORT_STRING_LENGTH
Definition: jsstr.c:30
#define JSSTR_MAX_ROPE_DEPTH
Definition: jsstr.c:35
static jsstr_t * jsstr_addref(jsstr_t *str)
Definition: jsstr.h:116
static BOOL jsstr_is_rope(jsstr_t *str)
Definition: jsstr.h:78
static unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
Definition: jsstr.h:148
unsigned depth
Definition: jsstr.h:97

Referenced by add_eval(), and String_concat().

◆ jsstr_empty()

◆ jsstr_extract()

void jsstr_extract ( jsstr_t str,
unsigned  off,
unsigned  len,
WCHAR buf 
)

Definition at line 113 of file jsstr.c.

114{
115 switch(jsstr_tag(str)) {
116 case JSSTR_INLINE:
117 memcpy(buf, jsstr_as_inline(str)->buf+off, len*sizeof(WCHAR));
118 return;
119 case JSSTR_HEAP:
120 memcpy(buf, jsstr_as_heap(str)->buf+off, len*sizeof(WCHAR));
121 return;
122 case JSSTR_ROPE:
124 return;
125 }
126}
static void jsstr_rope_extract(jsstr_rope_t *str, unsigned off, unsigned len, WCHAR *buf)
Definition: jsstr.c:98

Referenced by JSON_stringify(), jsstr_rope_extract(), jsstr_substr(), and String_charCodeAt().

◆ jsstr_free()

void jsstr_free ( jsstr_t str)

Definition at line 44 of file jsstr.c.

45{
46 switch(jsstr_tag(str)) {
47 case JSSTR_HEAP:
49 break;
50 case JSSTR_ROPE: {
52 jsstr_release(rope->left);
53 jsstr_release(rope->right);
54 break;
55 }
56 case JSSTR_INLINE:
57 break;
58 }
59
61}
static BOOL heap_free(void *mem)
Definition: appwiz.h:76

Referenced by jsstr_release().

◆ jsstr_init()

static void jsstr_init ( jsstr_t str,
unsigned  len,
jsstr_tag_t  tag 
)
inlinestatic

Definition at line 63 of file jsstr.c.

64{
65 str->length_flags = len << JSSTR_LENGTH_SHIFT | tag;
66 str->ref = 1;
67}
#define JSSTR_LENGTH_SHIFT
Definition: jsstr.h:44
Definition: ecma_167.h:138

Referenced by jsstr_alloc_buf(), and jsstr_concat().

◆ jsstr_nan()

◆ jsstr_null_bstr()

jsstr_t * jsstr_null_bstr ( void  )

Definition at line 298 of file jsstr.c.

299{
301}

Referenced by variant_to_jsval().

◆ jsstr_rope_extract()

static void jsstr_rope_extract ( jsstr_rope_t str,
unsigned  off,
unsigned  len,
WCHAR buf 
)
static

Definition at line 98 of file jsstr.c.

99{
100 unsigned left_len = jsstr_length(str->left);
101
102 if(left_len <= off) {
103 jsstr_extract(str->right, off-left_len, len, buf);
104 }else if(left_len >= len+off) {
105 jsstr_extract(str->left, off, len, buf);
106 }else {
107 left_len -= off;
108 jsstr_extract(str->left, off, left_len, buf);
109 jsstr_extract(str->right, 0, len-left_len, buf+left_len);
110 }
111}
void jsstr_extract(jsstr_t *str, unsigned off, unsigned len, WCHAR *buf)
Definition: jsstr.c:113

Referenced by jsstr_extract(), and ropes_cmp().

◆ jsstr_rope_flatten()

const WCHAR * jsstr_rope_flatten ( jsstr_rope_t str)

Definition at line 262 of file jsstr.c.

263{
264 WCHAR *buf;
265
266 buf = heap_alloc((jsstr_length(&str->str)+1) * sizeof(WCHAR));
267 if(!buf)
268 return NULL;
269
270 jsstr_flush(str->left, buf);
271 jsstr_flush(str->right, buf+jsstr_length(str->left));
272 buf[jsstr_length(&str->str)] = 0;
273
274 /* Trasform to heap string */
275 jsstr_release(str->left);
276 jsstr_release(str->right);
277 str->str.length_flags |= JSSTR_FLAG_FLAT;
278 return jsstr_as_heap(&str->str)->buf = buf;
279}
#define JSSTR_FLAG_FLAT
Definition: jsstr.h:49
WCHAR * buf
Definition: jsstr.h:90

Referenced by jsstr_flatten().

◆ jsstr_try_flat()

static const WCHAR * jsstr_try_flat ( jsstr_t str)
inlinestatic

Definition at line 182 of file jsstr.c.

183{
186 : NULL;
187}
WCHAR buf[1]
Definition: jsstr.h:85

Referenced by jsstr_cmp().

◆ jsstr_undefined()

◆ ropes_cmp()

static int ropes_cmp ( jsstr_rope_t left,
jsstr_rope_t right 
)
static

Definition at line 156 of file jsstr.c.

157{
158 WCHAR left_buf[TMP_BUF_SIZE], right_buf[TMP_BUF_SIZE];
159 unsigned left_len = jsstr_length(&left->str);
160 unsigned right_len = jsstr_length(&right->str);
161 unsigned cmp_off = 0, cmp_size;
162 int ret;
163
164 /* FIXME: We can avoid temporary buffers here. */
165 while(cmp_off < min(left_len, right_len)) {
166 cmp_size = min(left_len, right_len) - cmp_off;
167 if(cmp_size > TMP_BUF_SIZE)
168 cmp_size = TMP_BUF_SIZE;
169
170 jsstr_rope_extract(left, cmp_off, cmp_size, left_buf);
171 jsstr_rope_extract(right, cmp_off, cmp_size, right_buf);
172 ret = memcmp(left_buf, right_buf, cmp_size);
173 if(ret)
174 return ret;
175
176 cmp_off += cmp_size;
177 }
178
179 return left_len - right_len;
180}
GLdouble GLdouble right
Definition: glext.h:10859
#define TMP_BUF_SIZE
Definition: jsstr.c:154

Referenced by jsstr_cmp().

Variable Documentation

◆ empty_str

jsstr_t* empty_str
static

Definition at line 281 of file jsstr.c.

Referenced by free_strings(), init_strings(), and jsstr_empty().

◆ nan_str

jsstr_t * nan_str
static

Definition at line 281 of file jsstr.c.

Referenced by free_strings(), init_strings(), and jsstr_nan().

◆ null_bstr_str

jsstr_t * null_bstr_str
static

Definition at line 281 of file jsstr.c.

Referenced by free_strings(), init_strings(), is_null_bstr(), and jsstr_null_bstr().

◆ undefined_str

jsstr_t * undefined_str
static

Definition at line 281 of file jsstr.c.

Referenced by free_strings(), init_strings(), and jsstr_undefined().