jsstr.h 5.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2012 Jacek Caban for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19
/*
20
 * jsstr_t is a common header for all string representations. The exact layout of the string
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * representation may be:
 *
 * - inline string - string bytes directly follow string headers.
 * - heap string - a structure containing a pointer to buffer on the heap.
 * - roper string - a product of concatenation of two strings. Instead of copying whole
 *   buffers, we may store just references to concatenated strings.
 *
 * String layout may change over life time of the string. Currently possible transformation
 * is when a rope string becomes a heap stream. That happens when we need a real, linear
 * zero-terminated buffer (a flat buffer). At this point the type of the string is changed
 * and the new buffer is stored in the string, so that subsequent operations requiring
 * a flat string won't need to flatten it again.
 *
 * In the future more layouts and transformations may be added.
 */
36 37 38 39 40 41
struct _jsstr_t {
    unsigned length_flags;
    unsigned ref;
};

#define JSSTR_LENGTH_SHIFT 4
42
#define JSSTR_MAX_LENGTH ((1 << (32-JSSTR_LENGTH_SHIFT))-1)
43 44
#define JSSTR_FLAGS_MASK ((1 << JSSTR_LENGTH_SHIFT)-1)

45 46 47 48 49 50 51 52 53
#define JSSTR_FLAG_LBIT     1
#define JSSTR_FLAG_FLAT     2
#define JSSTR_FLAG_TAG_MASK 3

typedef enum {
    JSSTR_INLINE = JSSTR_FLAG_FLAT,
    JSSTR_HEAP   = JSSTR_FLAG_FLAT|JSSTR_FLAG_LBIT,
    JSSTR_ROPE   = JSSTR_FLAG_LBIT
} jsstr_tag_t;
54

55
static inline unsigned jsstr_length(jsstr_t *str)
56 57 58 59
{
    return str->length_flags >> JSSTR_LENGTH_SHIFT;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static inline jsstr_tag_t jsstr_tag(jsstr_t *str)
{
    return str->length_flags & JSSTR_FLAG_TAG_MASK;
}

static inline BOOL jsstr_is_inline(jsstr_t *str)
{
    return jsstr_tag(str) == JSSTR_INLINE;
}

static inline BOOL jsstr_is_heap(jsstr_t *str)
{
    return jsstr_tag(str) == JSSTR_HEAP;
}

static inline BOOL jsstr_is_rope(jsstr_t *str)
{
    return jsstr_tag(str) == JSSTR_ROPE;
}

80 81 82 83 84
typedef struct {
    jsstr_t str;
    WCHAR buf[1];
} jsstr_inline_t;

85 86 87 88 89 90 91 92 93 94 95 96
typedef struct {
    jsstr_t str;
    WCHAR *buf;
} jsstr_heap_t;

typedef struct {
    jsstr_t str;
    jsstr_t *left;
    jsstr_t *right;
    unsigned depth;
} jsstr_rope_t;

97
jsstr_t *jsstr_alloc_len(const WCHAR*,unsigned) DECLSPEC_HIDDEN;
98
jsstr_t *jsstr_alloc_buf(unsigned,WCHAR**) DECLSPEC_HIDDEN;
99 100 101 102 103 104

static inline jsstr_t *jsstr_alloc(const WCHAR *str)
{
    return jsstr_alloc_len(str, strlenW(str));
}

105 106
void jsstr_free(jsstr_t*) DECLSPEC_HIDDEN;

107 108
static inline void jsstr_release(jsstr_t *str)
{
109 110
    if(!--str->ref)
        jsstr_free(str);
111 112 113 114 115 116 117 118
}

static inline jsstr_t *jsstr_addref(jsstr_t *str)
{
    str->ref++;
    return str;
}

119 120 121 122 123
static inline jsstr_inline_t *jsstr_as_inline(jsstr_t *str)
{
    return CONTAINING_RECORD(str, jsstr_inline_t, str);
}

124
static inline jsstr_heap_t *jsstr_as_heap(jsstr_t *str)
125
{
126
    return CONTAINING_RECORD(str, jsstr_heap_t, str);
127 128
}

129
static inline jsstr_rope_t *jsstr_as_rope(jsstr_t *str)
130
{
131
    return CONTAINING_RECORD(str, jsstr_rope_t, str);
132 133
}

134 135 136
const WCHAR *jsstr_rope_flatten(jsstr_rope_t*) DECLSPEC_HIDDEN;

static inline const WCHAR *jsstr_flatten(jsstr_t *str)
137
{
138 139 140
    return jsstr_is_inline(str) ? jsstr_as_inline(str)->buf
        : jsstr_is_heap(str) ? jsstr_as_heap(str)->buf
        : jsstr_rope_flatten(jsstr_as_rope(str));
141 142
}

143 144
void jsstr_extract(jsstr_t*,unsigned,unsigned,WCHAR*) DECLSPEC_HIDDEN;

145 146 147
static inline unsigned jsstr_flush(jsstr_t *str, WCHAR *buf)
{
    unsigned len = jsstr_length(str);
148 149 150 151 152 153 154 155 156
    if(jsstr_is_inline(str)) {
        memcpy(buf, jsstr_as_inline(str)->buf, len*sizeof(WCHAR));
    }else if(jsstr_is_heap(str)) {
        memcpy(buf, jsstr_as_heap(str)->buf, len*sizeof(WCHAR));
    }else {
        jsstr_rope_t *rope = jsstr_as_rope(str);
        jsstr_flush(rope->left, buf);
        jsstr_flush(rope->right, buf+jsstr_length(rope->left));
    }
157 158 159
    return len;
}

160 161
static inline jsstr_t *jsstr_substr(jsstr_t *str, unsigned off, unsigned len)
{
162 163 164
    jsstr_t *ret;
    WCHAR *ptr;

165 166
    ret = jsstr_alloc_buf(len, &ptr);
    if(ret)
167 168
        jsstr_extract(str, off, len, ptr);
    return ret;
169 170
}

171
int jsstr_cmp(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
172 173 174 175 176 177

static inline BOOL jsstr_eq(jsstr_t *left, jsstr_t *right)
{
    return jsstr_length(left) == jsstr_length(right) && !jsstr_cmp(left, right);
}

178
jsstr_t *jsstr_concat(jsstr_t*,jsstr_t*) DECLSPEC_HIDDEN;
179 180 181

jsstr_t *jsstr_nan(void) DECLSPEC_HIDDEN;
jsstr_t *jsstr_empty(void) DECLSPEC_HIDDEN;
182
jsstr_t *jsstr_undefined(void) DECLSPEC_HIDDEN;
183

184 185 186
jsstr_t *jsstr_null_bstr(void) DECLSPEC_HIDDEN;
BOOL is_null_bstr(jsstr_t*) DECLSPEC_HIDDEN;

187 188 189 190
BOOL init_strings(void) DECLSPEC_HIDDEN;
void free_strings(void) DECLSPEC_HIDDEN;

const char *debugstr_jsstr(jsstr_t*) DECLSPEC_HIDDEN;