ddraw/tests: Add a test for fog_start == fog_end.
[wine.git] / dlls / jscript / jsstr.h
blob2ad03f18959748f91efd522baeb839014c252a78
1 /*
2 * Copyright 2012 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * jsstr_t is a common header for all string representations. The exact layout of the string
21 * representation may be:
23 * - inline string - string bytes directly follow string headers.
24 * - heap string - a structure containing a pointer to buffer on the heap.
25 * - roper string - a product of concatenation of two strings. Instead of copying whole
26 * buffers, we may store just references to concatenated strings.
28 * String layout may change over life time of the string. Currently possible transformation
29 * is when a rope string becomes a heap stream. That happens when we need a real, linear
30 * zero-terminated buffer (a flat buffer). At this point the type of the string is changed
31 * and the new buffer is stored in the string, so that subsequent operations requiring
32 * a flat string won't need to flatten it again.
34 * In the future more layouts and transformations may be added.
36 struct _jsstr_t {
37 unsigned length_flags;
38 unsigned ref;
41 #define JSSTR_LENGTH_SHIFT 4
42 #define JSSTR_MAX_LENGTH (1 << (32-JSSTR_LENGTH_SHIFT))
43 #define JSSTR_FLAGS_MASK ((1 << JSSTR_LENGTH_SHIFT)-1)
45 #define JSSTR_FLAG_LBIT 1
46 #define JSSTR_FLAG_FLAT 2
47 #define JSSTR_FLAG_TAG_MASK 3
49 typedef enum {
50 JSSTR_INLINE = JSSTR_FLAG_FLAT,
51 JSSTR_HEAP = JSSTR_FLAG_FLAT|JSSTR_FLAG_LBIT,
52 JSSTR_ROPE = JSSTR_FLAG_LBIT
53 } jsstr_tag_t;
55 static inline unsigned jsstr_length(jsstr_t *str)
57 return str->length_flags >> JSSTR_LENGTH_SHIFT;
60 static inline jsstr_tag_t jsstr_tag(jsstr_t *str)
62 return str->length_flags & JSSTR_FLAG_TAG_MASK;
65 static inline BOOL jsstr_is_inline(jsstr_t *str)
67 return jsstr_tag(str) == JSSTR_INLINE;
70 static inline BOOL jsstr_is_heap(jsstr_t *str)
72 return jsstr_tag(str) == JSSTR_HEAP;
75 static inline BOOL jsstr_is_rope(jsstr_t *str)
77 return jsstr_tag(str) == JSSTR_ROPE;
80 typedef struct {
81 jsstr_t str;
82 WCHAR buf[1];
83 } jsstr_inline_t;
85 typedef struct {
86 jsstr_t str;
87 WCHAR *buf;
88 } jsstr_heap_t;
90 typedef struct {
91 jsstr_t str;
92 jsstr_t *left;
93 jsstr_t *right;
94 unsigned depth;
95 } jsstr_rope_t;
97 jsstr_t *jsstr_alloc_len(const WCHAR*,unsigned) DECLSPEC_HIDDEN;
98 WCHAR *jsstr_alloc_buf(unsigned,jsstr_t**) DECLSPEC_HIDDEN;
100 static inline jsstr_t *jsstr_alloc(const WCHAR *str)
102 return jsstr_alloc_len(str, strlenW(str));
105 void jsstr_free(jsstr_t*) DECLSPEC_HIDDEN;
107 static inline void jsstr_release(jsstr_t *str)
109 if(!--str->ref) {
110 if(jsstr_is_inline(str))
111 heap_free(str);
112 else
113 jsstr_free(str);
117 static inline jsstr_t *jsstr_addref(jsstr_t *str)
119 str->ref++;
120 return str;
123 static inline jsstr_inline_t *jsstr_as_inline(jsstr_t *str)
125 return CONTAINING_RECORD(str, jsstr_inline_t, str);
128 static inline jsstr_heap_t *jsstr_as_heap(jsstr_t *str)
130 return CONTAINING_RECORD(str, jsstr_heap_t, str);
133 static inline jsstr_rope_t *jsstr_as_rope(jsstr_t *str)
135 return CONTAINING_RECORD(str, jsstr_rope_t, str);
138 const WCHAR *jsstr_rope_flatten(jsstr_rope_t*) DECLSPEC_HIDDEN;
140 static inline const WCHAR *jsstr_flatten(jsstr_t *str)
142 return jsstr_is_inline(str) ? jsstr_as_inline(str)->buf
143 : jsstr_is_heap(str) ? jsstr_as_heap(str)->buf
144 : jsstr_rope_flatten(jsstr_as_rope(str));
147 void jsstr_extract(jsstr_t*,unsigned,unsigned,WCHAR*) DECLSPEC_HIDDEN;
149 static inline unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
151 unsigned len = jsstr_length(str);
152 if(jsstr_is_inline(str)) {
153 memcpy(buf, jsstr_as_inline(str)->buf, len*sizeof(WCHAR));
154 }else if(jsstr_is_heap(str)) {
155 memcpy(buf, jsstr_as_heap(str)->buf, len*sizeof(WCHAR));
156 }else {
157 jsstr_rope_t *rope = jsstr_as_rope(str);
158 jsstr_flush(rope->left, buf);
159 jsstr_flush(rope->right, buf+jsstr_length(rope->left));
161 return len;
164 static inline jsstr_t *jsstr_substr(jsstr_t *str, unsigned off, unsigned len)
166 jsstr_t *ret;
167 WCHAR *ptr;
169 ptr = jsstr_alloc_buf(len, &ret);
170 if(ptr)
171 jsstr_extract(str, off, len, ptr);
172 return ret;
175 int jsstr_cmp(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
177 static inline BOOL jsstr_eq(jsstr_t *left, jsstr_t *right)
179 return jsstr_length(left) == jsstr_length(right) && !jsstr_cmp(left, right);
182 jsstr_t *jsstr_concat(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
184 jsstr_t *jsstr_nan(void) DECLSPEC_HIDDEN;
185 jsstr_t *jsstr_empty(void) DECLSPEC_HIDDEN;
186 jsstr_t *jsstr_undefined(void) DECLSPEC_HIDDEN;
188 jsstr_t *jsstr_null_bstr(void) DECLSPEC_HIDDEN;
189 BOOL is_null_bstr(jsstr_t*) DECLSPEC_HIDDEN;
191 BOOL init_strings(void) DECLSPEC_HIDDEN;
192 void free_strings(void) DECLSPEC_HIDDEN;
194 const char *debugstr_jsstr(jsstr_t*) DECLSPEC_HIDDEN;