ReactOS 0.4.17-dev-357-ga8f14ff
jsstr.h
Go to the documentation of this file.
1/*
2 * Copyright 2012 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19/*
20 * jsstr_t is a common header for all string representations. The exact layout of the string
21 * representation may be:
22 *
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.
27 *
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.
33 *
34 * In the future more layouts and transformations may be added.
35 */
36struct _jsstr_t {
37 unsigned length_flags;
38 unsigned ref;
39};
40
41#define JSSTR_LENGTH_SHIFT 4
42#define JSSTR_MAX_LENGTH ((1 << (32-JSSTR_LENGTH_SHIFT))-1)
43#define JSSTR_FLAGS_MASK ((1 << JSSTR_LENGTH_SHIFT)-1)
44
45#define JSSTR_FLAG_LBIT 1
46#define JSSTR_FLAG_FLAT 2
47#define JSSTR_FLAG_TAG_MASK 3
48
49typedef enum {
54
55static inline unsigned jsstr_length(jsstr_t *str)
56{
57 return str->length_flags >> JSSTR_LENGTH_SHIFT;
58}
59
61{
62 return str->length_flags & JSSTR_FLAG_TAG_MASK;
63}
64
66{
67 return jsstr_tag(str) == JSSTR_INLINE;
68}
69
71{
72 return jsstr_tag(str) == JSSTR_HEAP;
73}
74
76{
77 return jsstr_tag(str) == JSSTR_ROPE;
78}
79
80typedef struct {
84
85typedef struct {
89
90typedef struct {
94 unsigned depth;
96
97jsstr_t *jsstr_alloc_len(const WCHAR*,unsigned);
98jsstr_t *jsstr_alloc_buf(unsigned,WCHAR**);
99
100static inline jsstr_t *jsstr_alloc(const WCHAR *str)
101{
103}
104
105void jsstr_free(jsstr_t*);
106
107static inline void jsstr_release(jsstr_t *str)
108{
109 if(!--str->ref)
111}
112
114{
115 str->ref++;
116 return str;
117}
118
120{
122}
123
125{
127}
128
130{
132}
133
135
136static inline const WCHAR *jsstr_flatten(jsstr_t *str)
137{
141}
142
143void jsstr_extract(jsstr_t*,unsigned,unsigned,WCHAR*);
144
145static inline unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
146{
147 unsigned len = jsstr_length(str);
148 if(jsstr_is_inline(str)) {
150 }else if(jsstr_is_heap(str)) {
151 memcpy(buf, jsstr_as_heap(str)->buf, len*sizeof(WCHAR));
152 }else {
154 jsstr_flush(rope->left, buf);
155 jsstr_flush(rope->right, buf+jsstr_length(rope->left));
156 }
157 return len;
158}
159
160static inline jsstr_t *jsstr_substr(jsstr_t *str, unsigned off, unsigned len)
161{
162 jsstr_t *ret;
163 WCHAR *ptr;
164
166 if(ret)
167 jsstr_extract(str, off, len, ptr);
168 return ret;
169}
170
172
174{
176}
177
179
180jsstr_t *jsstr_nan(void);
181jsstr_t *jsstr_empty(void);
183
186
187BOOL init_strings(void);
188void free_strings(void);
189
190const char *debugstr_jsstr(jsstr_t*);
Definition: _rope.h:1087
OLECHAR * BSTR
Definition: compat.h:2293
#define lstrlenW
Definition: compat.h:750
return ret
Definition: mutex.c:146
unsigned int BOOL
Definition: ntddk_ex.h:94
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble GLdouble right
Definition: glext.h:10859
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLint left
Definition: glext.h:7726
GLenum GLsizei len
Definition: glext.h:6722
void jsstr_free(jsstr_t *)
Definition: jsstr.c:44
BOOL init_strings(void)
Definition: jsstr.c:320
jsstr_t * jsstr_concat(jsstr_t *, jsstr_t *)
Definition: jsstr.c:214
void free_strings(void)
Definition: jsstr.c:335
static jsstr_t * jsstr_substr(jsstr_t *str, unsigned off, unsigned len)
Definition: jsstr.h:160
static jsstr_t * jsstr_addref(jsstr_t *str)
Definition: jsstr.h:113
jsstr_t * jsstr_null_bstr(void)
Definition: jsstr.c:301
jsstr_t * jsstr_alloc_len(const WCHAR *, unsigned)
Definition: jsstr.c:86
static const WCHAR * jsstr_flatten(jsstr_t *str)
Definition: jsstr.h:136
#define JSSTR_FLAG_LBIT
Definition: jsstr.h:45
HRESULT jsstr_to_bstr(jsstr_t *str, BSTR *r)
Definition: jsstr.c:306
static BOOL jsstr_eq(jsstr_t *left, jsstr_t *right)
Definition: jsstr.h:173
static void jsstr_release(jsstr_t *str)
Definition: jsstr.h:107
static jsstr_inline_t * jsstr_as_inline(jsstr_t *str)
Definition: jsstr.h:119
static unsigned jsstr_length(jsstr_t *str)
Definition: jsstr.h:55
#define JSSTR_LENGTH_SHIFT
Definition: jsstr.h:41
#define JSSTR_FLAG_FLAT
Definition: jsstr.h:46
const char * debugstr_jsstr(jsstr_t *)
Definition: jsstr.c:37
jsstr_t * jsstr_nan(void)
Definition: jsstr.c:286
#define JSSTR_FLAG_TAG_MASK
Definition: jsstr.h:47
static BOOL jsstr_is_rope(jsstr_t *str)
Definition: jsstr.h:75
const WCHAR * jsstr_rope_flatten(jsstr_rope_t *)
Definition: jsstr.c:265
static jsstr_tag_t jsstr_tag(jsstr_t *str)
Definition: jsstr.h:60
static jsstr_heap_t * jsstr_as_heap(jsstr_t *str)
Definition: jsstr.h:124
static BOOL jsstr_is_heap(jsstr_t *str)
Definition: jsstr.h:70
static unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
Definition: jsstr.h:145
static jsstr_t * jsstr_alloc(const WCHAR *str)
Definition: jsstr.h:100
static BOOL jsstr_is_inline(jsstr_t *str)
Definition: jsstr.h:65
jsstr_t * jsstr_undefined(void)
Definition: jsstr.c:296
jsstr_t * jsstr_empty(void)
Definition: jsstr.c:291
static jsstr_rope_t * jsstr_as_rope(jsstr_t *str)
Definition: jsstr.h:129
jsstr_t * jsstr_alloc_buf(unsigned, WCHAR **)
Definition: jsstr.c:69
jsstr_tag_t
Definition: jsstr.h:49
@ JSSTR_INLINE
Definition: jsstr.h:50
@ JSSTR_HEAP
Definition: jsstr.h:51
@ JSSTR_ROPE
Definition: jsstr.h:52
void jsstr_extract(jsstr_t *, unsigned, unsigned, WCHAR *)
Definition: jsstr.c:113
int jsstr_cmp(jsstr_t *, jsstr_t *)
Definition: jsstr.c:192
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static PVOID ptr
Definition: dispmode.c:27
short WCHAR
Definition: pedump.c:58
const WCHAR * str
Definition: jsstr.h:36
unsigned ref
Definition: jsstr.h:38
unsigned length_flags
Definition: jsstr.h:37
jsstr_t str
Definition: jsstr.h:86
WCHAR * buf
Definition: jsstr.h:87
jsstr_t str
Definition: jsstr.h:81
WCHAR buf[]
Definition: jsstr.h:82
jsstr_t * left
Definition: jsstr.h:92
jsstr_t str
Definition: jsstr.h:91
jsstr_t * right
Definition: jsstr.h:93
unsigned depth
Definition: jsstr.h:94
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260